annotate image/ad_image.module @ 1:948362c2a207 ad

update advertisement
author pierre
date Thu, 02 Apr 2009 15:28:21 +0000
parents d8a3998dac8e
children e5584a19768b
rev   line source
pierre@0 1 <?php
pierre@1 2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.5 2009/03/11 16:12:07 jeremy Exp $
pierre@0 3
pierre@0 4 /**
pierre@0 5 * @file
pierre@0 6 * Enhances the ad module to support banner ads.
pierre@0 7 *
pierre@0 8 * Copyright (c) 2005-2009.
pierre@0 9 * Jeremy Andrews <jeremy@tag1consulting.com>.
pierre@0 10 */
pierre@0 11
pierre@0 12 /**
pierre@0 13 * Function used to display the selected ad.
pierre@0 14 */
pierre@0 15 function ad_image_display_ad($ad) {
pierre@0 16 return theme('ad_image_ad', $ad);
pierre@0 17 }
pierre@0 18
pierre@0 19 /**
pierre@0 20 * Return a themed ad of type ad_image.
pierre@0 21 *
pierre@0 22 * @param @ad
pierre@0 23 * The ad object.
pierre@0 24 * @return
pierre@0 25 * A string containing the ad markup.
pierre@0 26 */
pierre@0 27 function theme_ad_image_ad($ad) {
pierre@1 28 if (isset($ad->aid) && (isset($ad->filepath) || isset($ad->remote_image))) {
pierre@0 29 $output = '<div class="image-advertisement" id="ad-'. $ad->aid .'">';
pierre@0 30 if (isset($ad->url) && !empty($ad->url)) {
pierre@1 31 $image = theme('ad_image_image', !empty($ad->remote_image) ? $ad->remote_image : $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip));
pierre@0 32 $output .= l($image, $ad->redirect .'/@HOSTID___', array('attributes' => ad_link_attributes(), 'absolute' => TRUE, 'html' => TRUE));
pierre@0 33 }
pierre@0 34 else {
pierre@1 35 $output .= theme('ad_image_image', !empty($ad->remote_image) ? $ad->remote_image : $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip));
pierre@0 36 }
pierre@0 37 $output .= '</div>';
pierre@0 38 return $output;
pierre@0 39 }
pierre@0 40 }
pierre@0 41
pierre@0 42 /**
pierre@0 43 * Return a themed ad image.
pierre@0 44 *
pierre@0 45 * @param $path
pierre@0 46 * Either the path of the ad image file (relative to base_path()) or a full
pierre@0 47 * URL.
pierre@0 48 * @param $alt
pierre@0 49 * The alternative text for text-based browsers.
pierre@0 50 * @param $tooltip
pierre@0 51 * The tooltip text is displayed when the image is hovered in some popular
pierre@0 52 * browsers.
pierre@0 53 * @param $attributes
pierre@0 54 * Associative array of attributes to be placed in the img tag.
pierre@0 55 * @param $getsize
pierre@0 56 * If set to TRUE, the image's dimension are fetched and added as width/height
pierre@0 57 * attributes.
pierre@0 58 * @return
pierre@0 59 * A string containing the image tag.
pierre@0 60 */
pierre@0 61 function theme_ad_image_image($path, $alt = '', $tooltip = '', $attributes = NULL, $getsize = TRUE) {
pierre@1 62 if ($getsize) {
pierre@1 63 list($width, $height, $type, $image_attributes) = @getimagesize($path);
pierre@1 64 if (isset($width) && isset($height)) {
pierre@1 65 $attributes = drupal_attributes($attributes);
pierre@1 66 if (is_file($path)) {
pierre@1 67 $url = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($path));
pierre@1 68 }
pierre@1 69 else {
pierre@1 70 $url = $path;
pierre@1 71 }
pierre@1 72 return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($tooltip) .'" '. $image_attributes . $attributes .' />';
pierre@1 73 }
pierre@0 74 }
pierre@0 75 }
pierre@0 76
pierre@0 77 /**
pierre@0 78 * Implementation of hook_theme().
pierre@0 79 */
pierre@0 80 function ad_image_theme() {
pierre@0 81 return array(
pierre@0 82 'ad_image_ad' => array(
pierre@0 83 'file' => 'ad_image.module',
pierre@0 84 'arguments' => array(
pierre@0 85 'ad' => NULL,
pierre@0 86 ),
pierre@0 87 ),
pierre@0 88 'ad_image_image' => array(
pierre@0 89 'file' => 'ad_image.module',
pierre@0 90 'arguments' => array(
pierre@0 91 'path' => NULL,
pierre@0 92 'alt' => '',
pierre@0 93 'tooltip' => '',
pierre@0 94 'attributes' => NULL,
pierre@0 95 'getsize' => TRUE,
pierre@0 96 ),
pierre@0 97 ),
pierre@0 98 );
pierre@0 99 }
pierre@0 100
pierre@0 101 /**
pierre@0 102 * Implementation of hook_help().
pierre@0 103 */
pierre@0 104 function ad_image_help($path, $arg) {
pierre@0 105 $output = '';
pierre@0 106 switch ($path) {
pierre@0 107 case 'node/add/ad#image':
pierre@0 108 $output = t('An image or banner advertisement.');
pierre@0 109 break;
pierre@0 110 }
pierre@0 111 return $output;
pierre@0 112 }
pierre@0 113
pierre@0 114 /**
pierre@0 115 * Implementation of hook_access().
pierre@0 116 */
pierre@0 117 function ad_image_access($op, $node, $account) {
pierre@0 118 return ad_access($op, $node, $account);
pierre@0 119 }
pierre@0 120
pierre@0 121 /**
pierre@0 122 * Image ad settings form.
pierre@0 123 */
pierre@0 124 function ad_image_global_settings($edit = array()) {
pierre@0 125 $form = array();
pierre@0 126
pierre@1 127 $form['general'] = array(
pierre@1 128 '#type' => 'fieldset',
pierre@1 129 '#title' => t('General settings'),
pierre@1 130 '#collapsible' => TRUE,
pierre@1 131 );
pierre@1 132 $form['general']['remote_images'] = array(
pierre@1 133 '#type' => 'checkbox',
pierre@1 134 '#title' => t('Allow remote hosted images'),
pierre@1 135 '#description' => t('Check this box to add a new field when creating image advertisements allowing you to specify a path to a remotely hosted image rather than locally uploading an image. This option is disabled by default as it is a confusing field to someone not using it.'),
pierre@1 136 '#default_value' => variable_get('ad_image_remote_images', FALSE),
pierre@1 137 );
pierre@1 138
pierre@0 139 $groups = module_invoke('ad', 'groups_list', TRUE);
pierre@0 140 foreach ($groups as $tid => $group) {
pierre@0 141 $form["group-$tid"] = array(
pierre@0 142 '#type' => 'fieldset',
pierre@0 143 '#title' => $group->name,
pierre@0 144 '#collapsible' => TRUE,
pierre@0 145 );
pierre@0 146
pierre@0 147 $form["group-$tid"]["description-$tid"] = array(
pierre@0 148 '#type' => 'markup',
pierre@0 149 '#prefix' => '<div>',
pierre@0 150 '#suffix' => '</div>',
pierre@0 151 '#value' => theme_placeholder("$group->description"),
pierre@0 152 );
pierre@0 153
pierre@0 154 $format = db_fetch_object(db_query('SELECT * FROM {ad_image_format} WHERE gid = %d', $tid));
pierre@0 155 $form["group-$tid"]["min-height-$tid"] = array(
pierre@0 156 '#type' => 'textfield',
pierre@0 157 '#title' => t('Minimum height'),
pierre@0 158 '#size' => 5,
pierre@0 159 '#maxlength' => 5,
pierre@0 160 '#default_value' => isset($format->min_height) ? $format->min_height : 0,
pierre@0 161 '#description' => t('Optionally specify a minimum height in pixels for images in this group. To specify no minimum height, enter <em>0</em>.'),
pierre@0 162 );
pierre@0 163 $form["group-$tid"]["min-width-$tid"] = array(
pierre@0 164 '#type' => 'textfield',
pierre@0 165 '#title' => t('Minimum width'),
pierre@0 166 '#size' => 5,
pierre@0 167 '#maxlength' => 5,
pierre@0 168 '#default_value' => isset($format->min_width) ? $format->min_width : 0,
pierre@0 169 '#description' => t('Optionally specify a minimum width in pixels for images in this group. To specify no minimum width, enter <em>0</em>.'),
pierre@0 170 );
pierre@0 171 $form["group-$tid"]["max-height-$tid"] = array(
pierre@0 172 '#type' => 'textfield',
pierre@0 173 '#title' => t('Maximum height'),
pierre@0 174 '#size' => 5,
pierre@0 175 '#maxlength' => 5,
pierre@0 176 '#default_value' => isset($format->max_height) ? $format->max_height : 0,
pierre@0 177 '#description' => t('Optionally specify a maximum height in pixels for images in this group. To specify no maximum height, enter <em>0</em>.'),
pierre@0 178 );
pierre@0 179 $form["group-$tid"]["max-width-$tid"] = array(
pierre@0 180 '#type' => 'textfield',
pierre@0 181 '#title' => t('Maximum width'),
pierre@0 182 '#size' => 5,
pierre@0 183 '#maxlength' => 5,
pierre@0 184 '#default_value' => isset($format->max_width) ? $format->max_width : 0,
pierre@0 185 '#description' => t('Optionally specify a maximum width in pixels for images in this group. To specify no maximum width, enter <em>0</em>.'),
pierre@0 186 );
pierre@0 187 }
pierre@0 188
pierre@0 189 $form['save'] = array(
pierre@0 190 '#type' => 'submit',
pierre@0 191 '#value' => t('Save'),
pierre@0 192 );
pierre@0 193
pierre@1 194 $form['#submit'] = array('ad_image_global_settings_submit');
pierre@1 195
pierre@0 196 return $form;
pierre@0 197 }
pierre@0 198
pierre@0 199 /**
pierre@0 200 * Save min and max image width and height values for ad groups.
pierre@0 201 */
pierre@0 202 function ad_image_global_settings_submit($form, &$form_state) {
pierre@1 203 variable_set('ad_image_remote_images', $form_state['values']['remote_images']);
pierre@0 204 $groups = module_invoke('ad', 'groups_list', TRUE);
pierre@0 205 foreach ($groups as $group) {
pierre@0 206 // TODO: Update the database schema, convert gid to tid.
pierre@0 207 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid));
pierre@0 208 if (is_numeric($gid)) {
pierre@0 209 db_query("UPDATE {ad_image_format} SET min_width = %d, max_width = %d, min_height = %d, max_height = %d WHERE gid = %d", $form_state['values']["min-width-$group->tid"], $form_state['values']["max-width-$group->tid"], $form_state['values']["min-height-$group->tid"], $form_state['values']["max-height-$group->tid"], $group->tid);
pierre@0 210 }
pierre@0 211 else {
pierre@0 212 db_query("INSERT INTO {ad_image_format} (gid, min_width, max_width, min_height, max_height) VALUES (%d, %d, %d, %d, %d)", $group->tid, $form_state['values']["min-width-$group->tid"], $form_state['values']["max-width-$group->tid"], $form_state['values']["min-height-$group->tid"], $form_state['values']["max-height-$group->tid"]);
pierre@0 213 }
pierre@0 214 }
pierre@0 215 drupal_set_message('Image ad global settings updated.');
pierre@0 216 }
pierre@0 217
pierre@0 218 /**
pierre@0 219 * Implementation of hook_adapi().
pierre@0 220 */
pierre@0 221 function ad_image_adapi($op, &$node) {
pierre@0 222 $output = NULL;
pierre@0 223 switch ($op) {
pierre@0 224
pierre@0 225 case 'load':
pierre@0 226 $return = db_fetch_array(db_query("SELECT a.*, f.filepath FROM {ad_image} a LEFT JOIN {files} f ON a.fid = f.fid WHERE aid = %d", $node['aid']));
pierre@1 227 if (isset($return['remote_image']) && !empty($return['remote_image'])) {
pierre@1 228 $path = $return['remote_image'];
pierre@1 229 }
pierre@1 230 else {
pierre@1 231 $path = file_create_url($return['filepath']);
pierre@1 232 }
pierre@1 233 $return['ad'] = '<img src="'. $path .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />';
pierre@0 234 return $return;
pierre@0 235
pierre@0 236 case 'insert':
pierre@0 237 case 'update':
pierre@1 238 $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0;
pierre@1 239 if ($fid) {
pierre@1 240 $image = ad_image_load_image($node);
pierre@1 241 }
pierre@1 242 else if (isset($node->remote_image)) {
pierre@1 243 list($image->width, $image->height) = getimagesize($node->remote_image);
pierre@1 244 }
pierre@0 245 // This is ugly, but as "a" comes before "u" we don't seem to be able
pierre@0 246 // to modify the upload module's form. Instead, we check after the fact
pierre@0 247 // if someone is editing images when they're not allowed, and if so we
pierre@0 248 // prevent the ad from being saved.
pierre@1 249 if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) {
pierre@0 250 // See if fid is changing -- it's okay if new images are uploaded, it's
pierre@0 251 // just not okay if the active fid is changed.
pierre@0 252 if ($fid != $image->fid) {
pierre@0 253 drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error');
pierre@1 254 // This causes upload_save() to simply return without making any
pierre@0 255 // changes to the files attached to this node.
pierre@0 256 unset($node->files);
pierre@0 257 }
pierre@0 258 }
pierre@0 259 else {
pierre@0 260 // Check that all values are valid -- this is a kludge to work around
pierre@0 261 // bug #146147 until the problem is better understood.
pierre@0 262 $width = isset($image->width) ? $image->width : 0;
pierre@0 263 $height = isset($image->height) ? $image->height : 0;
pierre@0 264 $fid = isset($image->fid) ? $image->fid : 0;
pierre@1 265 if ($image !== FALSE && $width != 0 && $height != 0 && ($fid != 0 || $node->remote_image)) {
pierre@1 266 $node->fid = isset($image->fid) ? $image->fid : 0;
pierre@0 267 $node->width = $image->width;
pierre@0 268 $node->height = $image->height;
pierre@0 269 }
pierre@0 270 else {
pierre@0 271 $image = FALSE;
pierre@0 272 }
pierre@0 273 }
pierre@0 274 if ($op == 'insert') {
pierre@1 275 db_query("INSERT INTO {ad_image} (aid, fid, url, tooltip, remote_image, width, height) VALUES(%d, %d, '%s', '%s', '%s', %d, %d)", $node->nid, $node->fid, $node->url, $node->tooltip, $node->remote_image, $node->width, $node->height);
pierre@0 276 }
pierre@0 277 else {
pierre@1 278 db_query("UPDATE {ad_image} SET fid = %d, url = '%s', tooltip = '%s', remote_image = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->tooltip, $node->remote_image, $node->width, $node->height, $node->nid);
pierre@0 279 }
pierre@1 280 if (!isset($node->remote_image) || empty($node->remote_image)) {
pierre@1 281 // No valid image has been uploaded, don't allow ad to be 'active'.
pierre@1 282 if ($image === FALSE || !ad_image_active_file(($node->files))) {
pierre@1 283 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
pierre@1 284 if (db_affected_rows()) {
pierre@1 285 drupal_set_message(t('Image validation failed, unable to mark ad as %active. Setting ad as %pending.', array('%active' => t('active'), '%pending' => t('pending'))), 'error');
pierre@1 286 }
pierre@0 287 }
pierre@1 288 else if (!$fid) {
pierre@1 289 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
pierre@1 290 if (db_affected_rows()) {
pierre@1 291 drupal_set_message(t('Unable to mark ad as <em>active</em> until uploaded image is validated. If you do not see any more errors, you should now be able to set your ad as <em>active</em>.'), 'error');
pierre@1 292 }
pierre@0 293 }
pierre@0 294 }
pierre@0 295 break;
pierre@0 296
pierre@0 297 case 'validate':
pierre@1 298 if (isset($node->remote_image) && !empty($node->remote_image)) {
pierre@1 299 if (variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
pierre@1 300 drupal_set_message('You must specify a valid path for your remote advertisement.', 'error');
pierre@1 301 }
pierre@1 302 }
pierre@1 303 else if (!isset($node->files) || !ad_image_active_file($node->files)) {
pierre@1 304 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
pierre@1 305 }
pierre@0 306 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
pierre@0 307 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL'))));
pierre@0 308 }
pierre@0 309 break;
pierre@0 310
pierre@0 311 case 'delete':
pierre@0 312 db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid);
pierre@0 313 break;
pierre@0 314
pierre@0 315 case 'form':
pierre@0 316 return ad_image_node_form($node);
pierre@0 317
pierre@0 318 case 'view':
pierre@0 319 return ad_image_node_view($node);
pierre@0 320
pierre@0 321 case 'redirect':
pierre@0 322 return db_result(db_query('SELECT url FROM {ad_image} WHERE aid = %d', $node->nid));
pierre@0 323
pierre@0 324 case 'type':
pierre@0 325 return array(
pierre@0 326 'image' => array(
pierre@0 327 'name' => t('Image ad'),
pierre@0 328 'module' => 'ad_image',
pierre@0 329 'description' => t('An image or banner advertisement.'),
pierre@0 330 'help' => t('An image or banner advertisement.'),
pierre@0 331 ),
pierre@0 332 );
pierre@0 333 case 'permissions':
pierre@0 334 if (!isset($node->adtype) || $node->adtype == 'image') {
pierre@1 335 return array('manage active image' => TRUE);
pierre@0 336 }
pierre@0 337
pierre@0 338 case 'check_install':
pierre@0 339 if (!module_exists('upload')) {
pierre@0 340 drupal_set_message(t("The required <em>upload module</em> is not enabled, you will not be able to upload image ads. Please %enable the upload module, or %disable the ad_image module.", array('%enable' => l('enable', 'admin/modules'), '%disable' => l('disable', 'admin/modules'))), 'error');
pierre@0 341 }
pierre@0 342 if (is_object($node) && !variable_get("upload_$node->type", TRUE)) {
pierre@0 343 drupal_set_message(t('You will not be able to upload image ads until you !enable for the advertisement content type.', array('!enable' => l(t('enable attachments'), 'admin/content/types/ad'))), 'error');
pierre@0 344 }
pierre@0 345 if (empty($node)) {
pierre@0 346 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
pierre@0 347 drupal_set_message(t('Your website is configured to use Drupal\'s private !method. You have to enable "!view" permissions in addition to the ad module\'s standard "!show" permissions for all roles that you wish to view image advertisements.', array('!method' => l(t('download method'), 'admin/settings/file-system'), '!view' => l(t('view uploaded files'), 'admin/user/access'), '!show' => l(t('show advertisements'), 'admin/user/access'))));
pierre@0 348 }
pierre@0 349 }
pierre@0 350
pierre@0 351 break;
pierre@0 352
pierre@0 353 }
pierre@0 354
pierre@0 355 return $output;
pierre@0 356 }
pierre@0 357
pierre@0 358 /**
pierre@0 359 * Determine the currently active ad.
pierre@0 360 */
pierre@0 361 function ad_image_active_file($files = array()) {
pierre@0 362 if (is_array($files)) {
pierre@0 363 foreach ($files as $fid => $data) {
pierre@0 364 if (is_array($data)) {
pierre@0 365 if ($data['list'] && !$data['remove']) {
pierre@0 366 return $fid;
pierre@0 367 }
pierre@0 368 }
pierre@0 369 else if ($data->list && !isset($data->remove)) {
pierre@0 370 return $fid;
pierre@0 371 }
pierre@0 372 }
pierre@0 373 }
pierre@0 374 return 0;
pierre@0 375 }
pierre@0 376
pierre@0 377 /**
pierre@0 378 * Loads image format object from DB.
pierre@0 379 */
pierre@0 380 function ad_image_format_load($gid) {
pierre@0 381 static $format;
pierre@0 382 if (isset($format[$gid])) {
pierre@0 383 return $format[$gid];
pierre@0 384 }
pierre@0 385 $format[$gid] = db_fetch_object(db_query('SELECT min_width, max_width, min_height, max_height FROM {ad_image_format} WHERE gid = %d', $gid));
pierre@0 386 return $format[$gid];
pierre@0 387 }
pierre@0 388
pierre@0 389 /**
pierre@0 390 * Validate that the size of the uploaded image is within the defined limits.
pierre@0 391 */
pierre@0 392 function ad_image_validate_size($file, $nid) {
pierre@0 393 $size = NULL;
pierre@0 394 $error = FALSE;
pierre@0 395 $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
pierre@0 396 if (is_object($file)) {
pierre@0 397 // TODO: Detect if new terms have been set, and if so validate against
pierre@0 398 // them, not the old ones. See what's in $edit['taxonomy'].
pierre@0 399 $node = node_load($nid);
pierre@0 400 $terms = module_invoke('taxonomy', 'node_get_terms', $node);
pierre@0 401 if (count($terms) == 0) {
pierre@0 402 // We need at least a single (NULL) term to be ensure we still get the
pierre@0 403 // default image size.
pierre@0 404 $terms[] = NULL;
pierre@0 405 }
pierre@0 406 foreach ($terms as $tid => $term) {
pierre@0 407 list($size->width, $size->height) = getimagesize($file->filepath);
pierre@0 408 if ($format = ad_image_format_load($tid)) {
pierre@0 409 if ($size->width < $format->min_width) {
pierre@0 410 drupal_set_message(t('The image <em>%name</em> is only %current pixels wide, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->width, '%minimum' => $format->min_width, '%group' => theme_placeholder($term->name))), 'error');
pierre@0 411 $error = TRUE;
pierre@0 412 }
pierre@0 413 else if ($format->max_width && ($size->width > $format->max_width)) {
pierre@0 414 drupal_set_message(t('The image <em>%name</em> is %current pixels wide, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->width, '%maximum' => $format->max_width, '%group' => theme_placeholder($term->name))), 'error');
pierre@0 415 $error = TRUE;
pierre@0 416 }
pierre@0 417 if ($size->height < $format->min_height) {
pierre@0 418 drupal_set_message(t('The image <em>%name</em> is only %current pixels high, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->height, '%minimum' => $format->min_height, '%group' => theme_placeholder($term->name))), 'error');
pierre@0 419 $error = TRUE;
pierre@0 420 }
pierre@0 421 else if ($format->max_height && $size->height > $format->max_height) {
pierre@0 422 drupal_set_message(t('The image <em>%name</em> is %current pixels high, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->height, '%maximum' => $format->max_height, '%group' => theme_placeholder($term->name))), 'error');
pierre@0 423 $error = TRUE;
pierre@0 424 }
pierre@0 425 }
pierre@0 426 }
pierre@0 427 }
pierre@0 428 else {
pierre@0 429 $error = TRUE;
pierre@0 430 drupal_set_message('Please report error: $file is not an object, bug #146147.');
pierre@0 431 }
pierre@0 432 if ($error) {
pierre@0 433 return FALSE;
pierre@0 434 }
pierre@0 435 else {
pierre@0 436 return $size;
pierre@0 437 }
pierre@0 438 }
pierre@0 439
pierre@0 440 /**
pierre@0 441 * Returns image object from given ad node.
pierre@0 442 */
pierre@0 443 function ad_image_load_image($node) {
pierre@1 444 if (isset($node->files) && is_array($node->files)) {
pierre@0 445 foreach ($node->files as $file) {
pierre@0 446 if (is_array($file)) {
pierre@0 447 if ($file['list'] && file_exists($file['filepath'])) {
pierre@0 448 $image = ad_image_validate_size((object)$file, $node->nid);
pierre@0 449 if ($image !== FALSE) {
pierre@0 450 $image->fid = $file['fid'];
pierre@0 451 return $image;
pierre@0 452 }
pierre@0 453 }
pierre@0 454 }
pierre@0 455 else {
pierre@0 456 if ($file->list && file_exists($file->filepath)) {
pierre@0 457 $image = ad_image_validate_size($file, $node->nid);
pierre@0 458 if ($image !== FALSE) {
pierre@0 459 $image->fid = $file->fid;
pierre@0 460 return $image;
pierre@0 461 }
pierre@0 462 }
pierre@0 463 }
pierre@0 464 }
pierre@0 465 }
pierre@0 466 return FALSE;
pierre@0 467 }
pierre@0 468
pierre@0 469 /**
pierre@0 470 * Adapi helper function for displaying a node form.
pierre@0 471 */
pierre@0 472 function ad_image_node_form(&$node) {
pierre@0 473 $form = array();
pierre@0 474
pierre@0 475 ad_image_adapi('check_install', $node);
pierre@0 476
pierre@0 477 $form['ad_image'] = array(
pierre@0 478 '#type' => 'fieldset',
pierre@0 479 '#title' => t('Image'),
pierre@0 480 '#collapsible' => TRUE,
pierre@0 481 );
pierre@0 482
pierre@1 483 if (!empty($node->remote_image)) {
pierre@1 484 list($image->width, $image->height) = getimagesize($node->remote_image);
pierre@1 485 $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> ';
pierre@1 486 }
pierre@1 487 else if (isset($node->files)) {
pierre@0 488 $files = $node->files;
pierre@0 489 }
pierre@0 490 else {
pierre@0 491 if (!isset($node->vid)) {
pierre@0 492 $node->vid = '';
pierre@0 493 }
pierre@0 494 $files = module_invoke('upload', 'load', $node);
pierre@0 495 }
pierre@1 496 $num = isset($files) ? sizeof($files) : 0;
pierre@0 497
pierre@0 498 if ($num) {
pierre@1 499 $path = NULL;
pierre@1 500 $active = 0;
pierre@0 501 foreach ($files as $file) {
pierre@0 502 if ($file->list && file_exists($file->filepath)) {
pierre@0 503 $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> ';
pierre@0 504 $image = ad_image_validate_size($file, $node->nid);
pierre@0 505 if ($image === FALSE) {
pierre@0 506 $path .= t('(invalid image)'). '<br />';
pierre@0 507 }
pierre@0 508 else if (!$active++) {
pierre@0 509 $path .= t('(active)'). '<br />';
pierre@0 510 }
pierre@0 511 else {
pierre@0 512 $path .= t('(inactive)'). '<br />';
pierre@0 513 }
pierre@0 514 }
pierre@0 515 else if (!file_exists($file->filepath)) {
pierre@0 516 drupal_set_message(t('Unable to locate image %image.', array('%image' => "$file->filepath")));
pierre@0 517 $path .= t('Unable to locate the uploaded image.');
pierre@0 518 }
pierre@0 519 }
pierre@0 520 }
pierre@1 521 if (!isset($path) || $path == NULL) {
pierre@0 522 $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />');
pierre@0 523 // Only set error if node has been previewed or submitted.
pierre@0 524 if (isset($_POST['edit'])) {
pierre@0 525 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
pierre@0 526 }
pierre@0 527 }
pierre@1 528 else if ($num) {
pierre@1 529 $path .= t('<br />Only the first uploaded image that has <em>List</em> checked in the <em>File attachments</em> form section below will be displayed as an advertisement. The image that will be displayed is marked as <em>active</em> above.');
pierre@1 530 }
pierre@0 531
pierre@0 532 $form['ad_image']['image'] = array(
pierre@0 533 '#type' => 'markup',
pierre@0 534 '#value' => $path,
pierre@0 535 '#prefix' => '<div class="container-inline">',
pierre@0 536 '#suffix' => '</div>',
pierre@0 537 );
pierre@0 538
pierre@0 539 $form['ad_image']['url'] = array(
pierre@0 540 '#type' => 'textfield',
pierre@0 541 '#title' => t('Destination URL'),
pierre@0 542 '#required' => FALSE,
pierre@0 543 '#default_value' => isset($node->url) ? $node->url : '',
pierre@0 544 '#description' => t('Enter the complete URL where you want people to be redirected when they click on this advertisement. The URL must be valid and begin with http:// or https://, for example %url, unless you !disable. If you do not enter a URL, the advertisement will not be clickable.', array('%url' => t('http://www.sample.org/'), '!disable' => l(t('disable URL validation'), 'admin/content/ad/configure', array('fragment' => 'edit-ad-validate-url-wrapper')))),
pierre@0 545 );
pierre@0 546
pierre@0 547 $form['ad_image']['tooltip'] = array(
pierre@0 548 '#type' => 'textfield',
pierre@0 549 '#title' => t('Mouseover'),
pierre@0 550 '#required' => FALSE,
pierre@0 551 '#default_value' => isset($node->tooltip) ? $node->tooltip : '',
pierre@0 552 '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'),
pierre@0 553 );
pierre@0 554
pierre@1 555 if (variable_get('ad_image_remote_images', FALSE)) {
pierre@1 556 $form['ad_image']['remote_image'] = array(
pierre@1 557 '#type' => 'textfield',
pierre@1 558 '#title' => t('Remote image path'),
pierre@1 559 '#required' => FALSE,
pierre@1 560 '#default_value' => isset($node->remote_image) ? $node->remote_image : '',
pierre@1 561 '#description' => t('Instead of uploading an image, you may optionally specify a complete URL to a remotely hosted image. For example, %example. If you do not specify a remotely hosted image, you must attach an image to this advertisement in the %attachment section below.', array('%example' => 'http://sample.com/images/ad.png', '%attachment' => t('File attachements'))),
pierre@1 562 );
pierre@1 563 }
pierre@1 564
pierre@0 565 return $form;
pierre@0 566 }
pierre@0 567
pierre@0 568 /**
pierre@0 569 * Adapi helper function for displaying ad itself.
pierre@0 570 */
pierre@0 571 function ad_image_node_view(&$node) {
pierre@0 572 $node->content['ad'] = array(
pierre@0 573 '#value' => preg_replace('&@HOSTID___&', '0', ad_image_display_ad($node)),
pierre@0 574 '#weight' => -1,
pierre@0 575 );
pierre@0 576 if (!empty($node->url)) {
pierre@0 577 $link = t('Links to !url.', array('!url' => $node->url));
pierre@0 578 $link = check_plain($link, $node->format, FALSE);
pierre@0 579 $node->content['ad-link'] = array(
pierre@0 580 '#value' => "<div class=\"links-to\">$link</div>",
pierre@0 581 '#weight' => 0,
pierre@0 582 );
pierre@0 583 }
pierre@0 584 }