annotate image/ad_image.module @ 0:d8a3998dac8e ad

ajout module ad
author pierre
date Fri, 20 Feb 2009 14:04:09 +0000
parents
children 948362c2a207
rev   line source
pierre@0 1 <?php
pierre@0 2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11 2009/02/17 18:56:26 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@0 28 if (isset($ad->aid) && isset($ad->filepath)) {
pierre@0 29 $output = '<div class="image-advertisement" id="ad-'. $ad->aid .'">';
pierre@0 30 if (isset($ad->url) && !empty($ad->url)) {
pierre@0 31 $image = theme('ad_image_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@0 35 $output .= theme('ad_image_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@0 62 if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) {
pierre@0 63 $attributes = drupal_attributes($attributes);
pierre@0 64 $url = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($path));
pierre@0 65 return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($tooltip) .'" '. $image_attributes . $attributes .' />';
pierre@0 66 }
pierre@0 67 }
pierre@0 68
pierre@0 69 /**
pierre@0 70 * Implementation of hook_theme().
pierre@0 71 */
pierre@0 72 function ad_image_theme() {
pierre@0 73 return array(
pierre@0 74 'ad_image_ad' => array(
pierre@0 75 'file' => 'ad_image.module',
pierre@0 76 'arguments' => array(
pierre@0 77 'ad' => NULL,
pierre@0 78 ),
pierre@0 79 ),
pierre@0 80 'ad_image_image' => array(
pierre@0 81 'file' => 'ad_image.module',
pierre@0 82 'arguments' => array(
pierre@0 83 'path' => NULL,
pierre@0 84 'alt' => '',
pierre@0 85 'tooltip' => '',
pierre@0 86 'attributes' => NULL,
pierre@0 87 'getsize' => TRUE,
pierre@0 88 ),
pierre@0 89 ),
pierre@0 90 );
pierre@0 91 }
pierre@0 92
pierre@0 93 /**
pierre@0 94 * Implementation of hook_help().
pierre@0 95 */
pierre@0 96 function ad_image_help($path, $arg) {
pierre@0 97 $output = '';
pierre@0 98 switch ($path) {
pierre@0 99 case 'node/add/ad#image':
pierre@0 100 $output = t('An image or banner advertisement.');
pierre@0 101 break;
pierre@0 102 }
pierre@0 103 return $output;
pierre@0 104 }
pierre@0 105
pierre@0 106 /**
pierre@0 107 * Implementation of hook_access().
pierre@0 108 */
pierre@0 109 function ad_image_access($op, $node, $account) {
pierre@0 110 return ad_access($op, $node, $account);
pierre@0 111 }
pierre@0 112
pierre@0 113 /**
pierre@0 114 * Image ad settings form.
pierre@0 115 */
pierre@0 116 function ad_image_global_settings($edit = array()) {
pierre@0 117 $form = array();
pierre@0 118
pierre@0 119 $groups = module_invoke('ad', 'groups_list', TRUE);
pierre@0 120 foreach ($groups as $tid => $group) {
pierre@0 121 $form["group-$tid"] = array(
pierre@0 122 '#type' => 'fieldset',
pierre@0 123 '#title' => $group->name,
pierre@0 124 '#collapsible' => TRUE,
pierre@0 125 );
pierre@0 126
pierre@0 127 $form["group-$tid"]["description-$tid"] = array(
pierre@0 128 '#type' => 'markup',
pierre@0 129 '#prefix' => '<div>',
pierre@0 130 '#suffix' => '</div>',
pierre@0 131 '#value' => theme_placeholder("$group->description"),
pierre@0 132 );
pierre@0 133
pierre@0 134 $format = db_fetch_object(db_query('SELECT * FROM {ad_image_format} WHERE gid = %d', $tid));
pierre@0 135 $form["group-$tid"]["min-height-$tid"] = array(
pierre@0 136 '#type' => 'textfield',
pierre@0 137 '#title' => t('Minimum height'),
pierre@0 138 '#size' => 5,
pierre@0 139 '#maxlength' => 5,
pierre@0 140 '#default_value' => isset($format->min_height) ? $format->min_height : 0,
pierre@0 141 '#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 142 );
pierre@0 143 $form["group-$tid"]["min-width-$tid"] = array(
pierre@0 144 '#type' => 'textfield',
pierre@0 145 '#title' => t('Minimum width'),
pierre@0 146 '#size' => 5,
pierre@0 147 '#maxlength' => 5,
pierre@0 148 '#default_value' => isset($format->min_width) ? $format->min_width : 0,
pierre@0 149 '#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 150 );
pierre@0 151 $form["group-$tid"]["max-height-$tid"] = array(
pierre@0 152 '#type' => 'textfield',
pierre@0 153 '#title' => t('Maximum height'),
pierre@0 154 '#size' => 5,
pierre@0 155 '#maxlength' => 5,
pierre@0 156 '#default_value' => isset($format->max_height) ? $format->max_height : 0,
pierre@0 157 '#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 158 );
pierre@0 159 $form["group-$tid"]["max-width-$tid"] = array(
pierre@0 160 '#type' => 'textfield',
pierre@0 161 '#title' => t('Maximum width'),
pierre@0 162 '#size' => 5,
pierre@0 163 '#maxlength' => 5,
pierre@0 164 '#default_value' => isset($format->max_width) ? $format->max_width : 0,
pierre@0 165 '#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 166 );
pierre@0 167 }
pierre@0 168
pierre@0 169 $form['save'] = array(
pierre@0 170 '#type' => 'submit',
pierre@0 171 '#value' => t('Save'),
pierre@0 172 );
pierre@0 173
pierre@0 174 return $form;
pierre@0 175 }
pierre@0 176
pierre@0 177 /**
pierre@0 178 * Save min and max image width and height values for ad groups.
pierre@0 179 */
pierre@0 180 function ad_image_global_settings_submit($form, &$form_state) {
pierre@0 181 $groups = module_invoke('ad', 'groups_list', TRUE);
pierre@0 182 foreach ($groups as $group) {
pierre@0 183 // TODO: Update the database schema, convert gid to tid.
pierre@0 184 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid));
pierre@0 185 if (is_numeric($gid)) {
pierre@0 186 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 187 }
pierre@0 188 else {
pierre@0 189 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 190 }
pierre@0 191 }
pierre@0 192 drupal_set_message('Image ad global settings updated.');
pierre@0 193 }
pierre@0 194
pierre@0 195 /**
pierre@0 196 * Implementation of hook_adapi().
pierre@0 197 */
pierre@0 198 function ad_image_adapi($op, &$node) {
pierre@0 199 $output = NULL;
pierre@0 200 switch ($op) {
pierre@0 201
pierre@0 202 case 'load':
pierre@0 203 $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@0 204 $return['ad'] = '<img src="'. file_create_url($return['filepath']) .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />';
pierre@0 205 return $return;
pierre@0 206
pierre@0 207 case 'insert':
pierre@0 208 case 'update':
pierre@0 209 $image = ad_image_load_image($node);
pierre@0 210 $fid = (int)ad_image_active_file($node->files);
pierre@0 211 // This is ugly, but as "a" comes before "u" we don't seem to be able
pierre@0 212 // to modify the upload module's form. Instead, we check after the fact
pierre@0 213 // if someone is editing images when they're not allowed, and if so we
pierre@0 214 // prevent the ad from being saved.
pierre@0 215 if ($op == 'update' && !ad_adaccess($node, 'manage active ad')) {
pierre@0 216 // See if fid is changing -- it's okay if new images are uploaded, it's
pierre@0 217 // just not okay if the active fid is changed.
pierre@0 218 if ($fid != $image->fid) {
pierre@0 219 drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error');
pierre@0 220 // This causes upload_save() to simply return without making any
pierre@0 221 // changes to the files attached to this node.
pierre@0 222 unset($node->files);
pierre@0 223 }
pierre@0 224 }
pierre@0 225 else {
pierre@0 226 // Check that all values are valid -- this is a kludge to work around
pierre@0 227 // bug #146147 until the problem is better understood.
pierre@0 228 $width = isset($image->width) ? $image->width : 0;
pierre@0 229 $height = isset($image->height) ? $image->height : 0;
pierre@0 230 $fid = isset($image->fid) ? $image->fid : 0;
pierre@0 231 if ($image !== FALSE && $width != 0 && $height != 0 && $fid != 0) {
pierre@0 232 $node->fid = $image->fid;
pierre@0 233 $node->width = $image->width;
pierre@0 234 $node->height = $image->height;
pierre@0 235 }
pierre@0 236 else {
pierre@0 237 $image = FALSE;
pierre@0 238 }
pierre@0 239 }
pierre@0 240 if ($op == 'insert') {
pierre@0 241 db_query("INSERT INTO {ad_image} (aid, fid, url, tooltip, width, height) VALUES(%d, %d, '%s', '%s', %d, %d)", $node->nid, $node->fid, $node->url, $node->tooltip, $node->width, $node->height);
pierre@0 242 }
pierre@0 243 else {
pierre@0 244 db_query("UPDATE {ad_image} SET fid = %d, url = '%s', tooltip = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->tooltip, $node->width, $node->height, $node->nid);
pierre@0 245 }
pierre@0 246 // No valid image has been uploaded, don't allow ad to be 'active'.
pierre@0 247 if ($image === FALSE || !ad_image_active_file(($node->files))) {
pierre@0 248 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
pierre@0 249 if (db_affected_rows()) {
pierre@0 250 drupal_set_message(t('Image validation failed, unable to mark ad as %active. Setting ad as %pending. If you do not see any more errors, you should now be able to set your ad as %active.', array('%active' => t('active'), '%pending' => t('pending'))), 'error');
pierre@0 251 }
pierre@0 252 }
pierre@0 253 else if (!$fid) {
pierre@0 254 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
pierre@0 255 if (db_affected_rows()) {
pierre@0 256 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@0 257 }
pierre@0 258 }
pierre@0 259 break;
pierre@0 260
pierre@0 261 case 'validate':
pierre@0 262 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
pierre@0 263 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL'))));
pierre@0 264 }
pierre@0 265 if (!isset($node->files) || !ad_image_active_file($node->files)) {
pierre@0 266 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
pierre@0 267 }
pierre@0 268 break;
pierre@0 269
pierre@0 270 case 'delete':
pierre@0 271 db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid);
pierre@0 272 break;
pierre@0 273
pierre@0 274 case 'form':
pierre@0 275 return ad_image_node_form($node);
pierre@0 276
pierre@0 277 case 'view':
pierre@0 278 return ad_image_node_view($node);
pierre@0 279
pierre@0 280 case 'redirect':
pierre@0 281 return db_result(db_query('SELECT url FROM {ad_image} WHERE aid = %d', $node->nid));
pierre@0 282
pierre@0 283 case 'type':
pierre@0 284 return array(
pierre@0 285 'image' => array(
pierre@0 286 'name' => t('Image ad'),
pierre@0 287 'module' => 'ad_image',
pierre@0 288 'description' => t('An image or banner advertisement.'),
pierre@0 289 'help' => t('An image or banner advertisement.'),
pierre@0 290 ),
pierre@0 291 );
pierre@0 292 case 'permissions':
pierre@0 293 if (!isset($node->adtype) || $node->adtype == 'image') {
pierre@0 294 return array('manage active ad');
pierre@0 295 }
pierre@0 296
pierre@0 297 case 'check_install':
pierre@0 298 if (!module_exists('upload')) {
pierre@0 299 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 300 }
pierre@0 301 if (is_object($node) && !variable_get("upload_$node->type", TRUE)) {
pierre@0 302 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 303 }
pierre@0 304 if (empty($node)) {
pierre@0 305 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
pierre@0 306 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 307 }
pierre@0 308 }
pierre@0 309
pierre@0 310 break;
pierre@0 311
pierre@0 312 }
pierre@0 313
pierre@0 314 return $output;
pierre@0 315 }
pierre@0 316
pierre@0 317 /**
pierre@0 318 * Determine the currently active ad.
pierre@0 319 */
pierre@0 320 function ad_image_active_file($files = array()) {
pierre@0 321 if (is_array($files)) {
pierre@0 322 foreach ($files as $fid => $data) {
pierre@0 323 if (is_array($data)) {
pierre@0 324 if ($data['list'] && !$data['remove']) {
pierre@0 325 return $fid;
pierre@0 326 }
pierre@0 327 }
pierre@0 328 else if ($data->list && !isset($data->remove)) {
pierre@0 329 return $fid;
pierre@0 330 }
pierre@0 331 }
pierre@0 332 }
pierre@0 333 return 0;
pierre@0 334 }
pierre@0 335
pierre@0 336 /**
pierre@0 337 * Loads image format object from DB.
pierre@0 338 */
pierre@0 339 function ad_image_format_load($gid) {
pierre@0 340 static $format;
pierre@0 341 if (isset($format[$gid])) {
pierre@0 342 return $format[$gid];
pierre@0 343 }
pierre@0 344 $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 345 return $format[$gid];
pierre@0 346 }
pierre@0 347
pierre@0 348 /**
pierre@0 349 * Validate that the size of the uploaded image is within the defined limits.
pierre@0 350 */
pierre@0 351 function ad_image_validate_size($file, $nid) {
pierre@0 352 $size = NULL;
pierre@0 353 $error = FALSE;
pierre@0 354 $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
pierre@0 355 if (is_object($file)) {
pierre@0 356 // TODO: Detect if new terms have been set, and if so validate against
pierre@0 357 // them, not the old ones. See what's in $edit['taxonomy'].
pierre@0 358 $node = node_load($nid);
pierre@0 359 $terms = module_invoke('taxonomy', 'node_get_terms', $node);
pierre@0 360 if (count($terms) == 0) {
pierre@0 361 // We need at least a single (NULL) term to be ensure we still get the
pierre@0 362 // default image size.
pierre@0 363 $terms[] = NULL;
pierre@0 364 }
pierre@0 365 foreach ($terms as $tid => $term) {
pierre@0 366 list($size->width, $size->height) = getimagesize($file->filepath);
pierre@0 367 if ($format = ad_image_format_load($tid)) {
pierre@0 368 if ($size->width < $format->min_width) {
pierre@0 369 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 370 $error = TRUE;
pierre@0 371 }
pierre@0 372 else if ($format->max_width && ($size->width > $format->max_width)) {
pierre@0 373 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 374 $error = TRUE;
pierre@0 375 }
pierre@0 376 if ($size->height < $format->min_height) {
pierre@0 377 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 378 $error = TRUE;
pierre@0 379 }
pierre@0 380 else if ($format->max_height && $size->height > $format->max_height) {
pierre@0 381 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 382 $error = TRUE;
pierre@0 383 }
pierre@0 384 }
pierre@0 385 }
pierre@0 386 }
pierre@0 387 else {
pierre@0 388 $error = TRUE;
pierre@0 389 drupal_set_message('Please report error: $file is not an object, bug #146147.');
pierre@0 390 }
pierre@0 391 if ($error) {
pierre@0 392 return FALSE;
pierre@0 393 }
pierre@0 394 else {
pierre@0 395 return $size;
pierre@0 396 }
pierre@0 397 }
pierre@0 398
pierre@0 399 /**
pierre@0 400 * Returns image object from given ad node.
pierre@0 401 */
pierre@0 402 function ad_image_load_image($node) {
pierre@0 403 if (is_array($node->files)) {
pierre@0 404 foreach ($node->files as $file) {
pierre@0 405 if (is_array($file)) {
pierre@0 406 if ($file['list'] && file_exists($file['filepath'])) {
pierre@0 407 $image = ad_image_validate_size((object)$file, $node->nid);
pierre@0 408 if ($image !== FALSE) {
pierre@0 409 $image->fid = $file['fid'];
pierre@0 410 return $image;
pierre@0 411 }
pierre@0 412 }
pierre@0 413 }
pierre@0 414 else {
pierre@0 415 if ($file->list && file_exists($file->filepath)) {
pierre@0 416 $image = ad_image_validate_size($file, $node->nid);
pierre@0 417 if ($image !== FALSE) {
pierre@0 418 $image->fid = $file->fid;
pierre@0 419 return $image;
pierre@0 420 }
pierre@0 421 }
pierre@0 422 }
pierre@0 423 }
pierre@0 424 }
pierre@0 425 return FALSE;
pierre@0 426 }
pierre@0 427
pierre@0 428 /**
pierre@0 429 * Adapi helper function for displaying a node form.
pierre@0 430 */
pierre@0 431 function ad_image_node_form(&$node) {
pierre@0 432 $form = array();
pierre@0 433
pierre@0 434 ad_image_adapi('check_install', $node);
pierre@0 435
pierre@0 436 $form['ad_image'] = array(
pierre@0 437 '#type' => 'fieldset',
pierre@0 438 '#title' => t('Image'),
pierre@0 439 '#collapsible' => TRUE,
pierre@0 440 );
pierre@0 441
pierre@0 442 if (isset($node->files)) {
pierre@0 443 $files = $node->files;
pierre@0 444 }
pierre@0 445 else {
pierre@0 446 if (!isset($node->vid)) {
pierre@0 447 $node->vid = '';
pierre@0 448 }
pierre@0 449 $files = module_invoke('upload', 'load', $node);
pierre@0 450 }
pierre@0 451 $num = sizeof($files);
pierre@0 452
pierre@0 453 $path = NULL;
pierre@0 454 $active = 0;
pierre@0 455 if ($num) {
pierre@0 456 foreach ($files as $file) {
pierre@0 457 if ($file->list && file_exists($file->filepath)) {
pierre@0 458 $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> ';
pierre@0 459 $image = ad_image_validate_size($file, $node->nid);
pierre@0 460 if ($image === FALSE) {
pierre@0 461 $path .= t('(invalid image)'). '<br />';
pierre@0 462 }
pierre@0 463 else if (!$active++) {
pierre@0 464 $path .= t('(active)'). '<br />';
pierre@0 465 }
pierre@0 466 else {
pierre@0 467 $path .= t('(inactive)'). '<br />';
pierre@0 468 }
pierre@0 469 }
pierre@0 470 else if (!file_exists($file->filepath)) {
pierre@0 471 drupal_set_message(t('Unable to locate image %image.', array('%image' => "$file->filepath")));
pierre@0 472 $path .= t('Unable to locate the uploaded image.');
pierre@0 473 }
pierre@0 474 }
pierre@0 475 }
pierre@0 476 if ($path == NULL) {
pierre@0 477 $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />');
pierre@0 478 // Only set error if node has been previewed or submitted.
pierre@0 479 if (isset($_POST['edit'])) {
pierre@0 480 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
pierre@0 481 }
pierre@0 482 }
pierre@0 483
pierre@0 484 $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@0 485
pierre@0 486 $form['ad_image']['image'] = array(
pierre@0 487 '#type' => 'markup',
pierre@0 488 '#value' => $path,
pierre@0 489 '#prefix' => '<div class="container-inline">',
pierre@0 490 '#suffix' => '</div>',
pierre@0 491 );
pierre@0 492
pierre@0 493 $form['ad_image']['url'] = array(
pierre@0 494 '#type' => 'textfield',
pierre@0 495 '#title' => t('Destination URL'),
pierre@0 496 '#required' => FALSE,
pierre@0 497 '#default_value' => isset($node->url) ? $node->url : '',
pierre@0 498 '#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 499 );
pierre@0 500
pierre@0 501 $form['ad_image']['tooltip'] = array(
pierre@0 502 '#type' => 'textfield',
pierre@0 503 '#title' => t('Mouseover'),
pierre@0 504 '#required' => FALSE,
pierre@0 505 '#default_value' => isset($node->tooltip) ? $node->tooltip : '',
pierre@0 506 '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'),
pierre@0 507 );
pierre@0 508
pierre@0 509 return $form;
pierre@0 510 }
pierre@0 511
pierre@0 512 /**
pierre@0 513 * Adapi helper function for displaying ad itself.
pierre@0 514 */
pierre@0 515 function ad_image_node_view(&$node) {
pierre@0 516 $node->content['ad'] = array(
pierre@0 517 '#value' => preg_replace('&@HOSTID___&', '0', ad_image_display_ad($node)),
pierre@0 518 '#weight' => -1,
pierre@0 519 );
pierre@0 520 if (!empty($node->url)) {
pierre@0 521 $link = t('Links to !url.', array('!url' => $node->url));
pierre@0 522 $link = check_plain($link, $node->format, FALSE);
pierre@0 523 $node->content['ad-link'] = array(
pierre@0 524 '#value' => "<div class=\"links-to\">$link</div>",
pierre@0 525 '#weight' => 0,
pierre@0 526 );
pierre@0 527 }
pierre@0 528 }