annotate image/ad_image.module @ 3:416ea999ed76 ad

maj ad version rc1
author sly
date Mon, 20 Apr 2009 09:49:37 +0000
parents e5584a19768b
children 6aeff3329e01
rev   line source
pierre@0 1 <?php
sly@2 2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.6 2009/04/02 15:48:29 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));
sly@2 155 $form["group-$tid"]["max-size-$tid"] = array(
sly@2 156 '#type' => 'textfield',
sly@2 157 '#title' => t('Maximum filesize'),
sly@2 158 '#size' => 5,
sly@2 159 '#maxlength' => 15,
sly@2 160 '#default_value' => isset($format->max_size) ? $format->max_size : 0,
sly@2 161 '#description' => t('Optionally specify a maximum filesize in bytes for images in this group. To specify no maximum filesize, enter <em>0</em>.'),
sly@2 162 );
pierre@0 163 $form["group-$tid"]["min-height-$tid"] = array(
pierre@0 164 '#type' => 'textfield',
pierre@0 165 '#title' => t('Minimum height'),
pierre@0 166 '#size' => 5,
pierre@0 167 '#maxlength' => 5,
pierre@0 168 '#default_value' => isset($format->min_height) ? $format->min_height : 0,
pierre@0 169 '#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 170 );
pierre@0 171 $form["group-$tid"]["min-width-$tid"] = array(
pierre@0 172 '#type' => 'textfield',
pierre@0 173 '#title' => t('Minimum width'),
pierre@0 174 '#size' => 5,
pierre@0 175 '#maxlength' => 5,
pierre@0 176 '#default_value' => isset($format->min_width) ? $format->min_width : 0,
pierre@0 177 '#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 178 );
pierre@0 179 $form["group-$tid"]["max-height-$tid"] = array(
pierre@0 180 '#type' => 'textfield',
pierre@0 181 '#title' => t('Maximum height'),
pierre@0 182 '#size' => 5,
pierre@0 183 '#maxlength' => 5,
pierre@0 184 '#default_value' => isset($format->max_height) ? $format->max_height : 0,
pierre@0 185 '#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 186 );
pierre@0 187 $form["group-$tid"]["max-width-$tid"] = array(
pierre@0 188 '#type' => 'textfield',
pierre@0 189 '#title' => t('Maximum width'),
pierre@0 190 '#size' => 5,
pierre@0 191 '#maxlength' => 5,
pierre@0 192 '#default_value' => isset($format->max_width) ? $format->max_width : 0,
pierre@0 193 '#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 194 );
pierre@0 195 }
pierre@0 196
pierre@0 197 $form['save'] = array(
pierre@0 198 '#type' => 'submit',
pierre@0 199 '#value' => t('Save'),
pierre@0 200 );
pierre@0 201
pierre@1 202 $form['#submit'] = array('ad_image_global_settings_submit');
pierre@1 203
pierre@0 204 return $form;
pierre@0 205 }
pierre@0 206
pierre@0 207 /**
pierre@0 208 * Save min and max image width and height values for ad groups.
pierre@0 209 */
pierre@0 210 function ad_image_global_settings_submit($form, &$form_state) {
pierre@1 211 variable_set('ad_image_remote_images', $form_state['values']['remote_images']);
pierre@0 212 $groups = module_invoke('ad', 'groups_list', TRUE);
pierre@0 213 foreach ($groups as $group) {
pierre@0 214 // TODO: Update the database schema, convert gid to tid.
pierre@0 215 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid));
pierre@0 216 if (is_numeric($gid)) {
sly@2 217 db_query("UPDATE {ad_image_format} SET min_width = %d, max_width = %d, min_height = %d, max_height = %d, max_size = %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"], $form_state['values']["max-size-$group->tid"], $group->tid);
pierre@0 218 }
pierre@0 219 else {
sly@2 220 db_query("INSERT INTO {ad_image_format} (gid, min_width, max_width, min_height, max_height, max_size) VALUES (%d, %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"], $form_state['values']["max-size-$group->tid"]);
pierre@0 221 }
pierre@0 222 }
pierre@0 223 drupal_set_message('Image ad global settings updated.');
pierre@0 224 }
pierre@0 225
pierre@0 226 /**
pierre@0 227 * Implementation of hook_adapi().
pierre@0 228 */
pierre@0 229 function ad_image_adapi($op, &$node) {
pierre@0 230 $output = NULL;
pierre@0 231 switch ($op) {
pierre@0 232
pierre@0 233 case 'load':
pierre@0 234 $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 235 if (isset($return['remote_image']) && !empty($return['remote_image'])) {
pierre@1 236 $path = $return['remote_image'];
pierre@1 237 }
pierre@1 238 else {
pierre@1 239 $path = file_create_url($return['filepath']);
pierre@1 240 }
pierre@1 241 $return['ad'] = '<img src="'. $path .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />';
pierre@0 242 return $return;
pierre@0 243
pierre@0 244 case 'insert':
pierre@0 245 case 'update':
pierre@1 246 $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0;
sly@2 247 $image = ad_image_load_image($node);
pierre@0 248 // This is ugly, but as "a" comes before "u" we don't seem to be able
pierre@0 249 // to modify the upload module's form. Instead, we check after the fact
pierre@0 250 // if someone is editing images when they're not allowed, and if so we
pierre@0 251 // prevent the ad from being saved.
pierre@1 252 if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) {
pierre@0 253 // See if fid is changing -- it's okay if new images are uploaded, it's
pierre@0 254 // just not okay if the active fid is changed.
pierre@0 255 if ($fid != $image->fid) {
pierre@0 256 drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error');
pierre@1 257 // This causes upload_save() to simply return without making any
pierre@0 258 // changes to the files attached to this node.
pierre@0 259 unset($node->files);
pierre@0 260 }
pierre@0 261 }
pierre@0 262 else {
pierre@0 263 // Check that all values are valid -- this is a kludge to work around
pierre@0 264 // bug #146147 until the problem is better understood.
pierre@0 265 $width = isset($image->width) ? $image->width : 0;
pierre@0 266 $height = isset($image->height) ? $image->height : 0;
pierre@0 267 $fid = isset($image->fid) ? $image->fid : 0;
pierre@1 268 if ($image !== FALSE && $width != 0 && $height != 0 && ($fid != 0 || $node->remote_image)) {
pierre@1 269 $node->fid = isset($image->fid) ? $image->fid : 0;
pierre@0 270 $node->width = $image->width;
pierre@0 271 $node->height = $image->height;
pierre@0 272 }
pierre@0 273 else {
pierre@0 274 $image = FALSE;
pierre@0 275 }
pierre@0 276 }
pierre@0 277 if ($op == 'insert') {
sly@2 278 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, isset($node->width) ? $node->width : 0, isset($node->height) ? $node->height : 0);
pierre@0 279 }
pierre@0 280 else {
sly@2 281 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, isset($node->width) ? $node->width : 0, isset($node->height) ? $node->height : 0, $node->nid);
pierre@0 282 }
sly@2 283 // No valid image has been uploaded, don't allow ad to be 'active'.
sly@2 284 if ($image === FALSE || !ad_image_active_file(($node->files))) {
sly@2 285 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
sly@2 286 if (db_affected_rows()) {
sly@2 287 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@0 288 }
sly@2 289 }
sly@2 290 else if (!$node->remote_image && !$fid) {
sly@2 291 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
sly@2 292 if (db_affected_rows()) {
sly@2 293 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 294 }
pierre@0 295 }
pierre@0 296 break;
pierre@0 297
pierre@0 298 case 'validate':
pierre@1 299 if (isset($node->remote_image) && !empty($node->remote_image)) {
pierre@1 300 if (variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
pierre@1 301 drupal_set_message('You must specify a valid path for your remote advertisement.', 'error');
pierre@1 302 }
pierre@1 303 }
pierre@1 304 else if (!isset($node->files) || !ad_image_active_file($node->files)) {
pierre@1 305 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
pierre@1 306 }
pierre@0 307 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
pierre@0 308 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL'))));
pierre@0 309 }
pierre@0 310 break;
pierre@0 311
pierre@0 312 case 'delete':
pierre@0 313 db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid);
pierre@0 314 break;
pierre@0 315
pierre@0 316 case 'form':
pierre@0 317 return ad_image_node_form($node);
pierre@0 318
pierre@0 319 case 'view':
pierre@0 320 return ad_image_node_view($node);
pierre@0 321
pierre@0 322 case 'redirect':
pierre@0 323 return db_result(db_query('SELECT url FROM {ad_image} WHERE aid = %d', $node->nid));
pierre@0 324
pierre@0 325 case 'type':
pierre@0 326 return array(
pierre@0 327 'image' => array(
pierre@0 328 'name' => t('Image ad'),
pierre@0 329 'module' => 'ad_image',
pierre@0 330 'description' => t('An image or banner advertisement.'),
pierre@0 331 'help' => t('An image or banner advertisement.'),
pierre@0 332 ),
pierre@0 333 );
pierre@0 334 case 'permissions':
pierre@0 335 if (!isset($node->adtype) || $node->adtype == 'image') {
pierre@1 336 return array('manage active image' => TRUE);
pierre@0 337 }
pierre@0 338
pierre@0 339 case 'check_install':
pierre@0 340 if (!module_exists('upload')) {
pierre@0 341 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 342 }
pierre@0 343 if (is_object($node) && !variable_get("upload_$node->type", TRUE)) {
pierre@0 344 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 345 }
pierre@0 346 if (empty($node)) {
pierre@0 347 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
pierre@0 348 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 349 }
pierre@0 350 }
pierre@0 351
pierre@0 352 break;
pierre@0 353
pierre@0 354 }
pierre@0 355
pierre@0 356 return $output;
pierre@0 357 }
pierre@0 358
pierre@0 359 /**
pierre@0 360 * Determine the currently active ad.
pierre@0 361 */
pierre@0 362 function ad_image_active_file($files = array()) {
pierre@0 363 if (is_array($files)) {
pierre@0 364 foreach ($files as $fid => $data) {
pierre@0 365 if (is_array($data)) {
pierre@0 366 if ($data['list'] && !$data['remove']) {
pierre@0 367 return $fid;
pierre@0 368 }
pierre@0 369 }
pierre@0 370 else if ($data->list && !isset($data->remove)) {
pierre@0 371 return $fid;
pierre@0 372 }
pierre@0 373 }
pierre@0 374 }
pierre@0 375 return 0;
pierre@0 376 }
pierre@0 377
pierre@0 378 /**
pierre@0 379 * Loads image format object from DB.
pierre@0 380 */
pierre@0 381 function ad_image_format_load($gid) {
pierre@0 382 static $format;
pierre@0 383 if (isset($format[$gid])) {
pierre@0 384 return $format[$gid];
pierre@0 385 }
sly@2 386 $format[$gid] = db_fetch_object(db_query('SELECT min_width, max_width, min_height, max_height, max_size FROM {ad_image_format} WHERE gid = %d', $gid));
pierre@0 387 return $format[$gid];
pierre@0 388 }
pierre@0 389
pierre@0 390 /**
pierre@0 391 * Validate that the size of the uploaded image is within the defined limits.
pierre@0 392 */
pierre@0 393 function ad_image_validate_size($file, $nid) {
pierre@0 394 $size = NULL;
pierre@0 395 $error = FALSE;
pierre@0 396 $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
pierre@0 397 if (is_object($file)) {
pierre@0 398 // TODO: Detect if new terms have been set, and if so validate against
pierre@0 399 // them, not the old ones. See what's in $edit['taxonomy'].
pierre@0 400 $node = node_load($nid);
pierre@0 401 $terms = module_invoke('taxonomy', 'node_get_terms', $node);
pierre@0 402 if (count($terms) == 0) {
pierre@0 403 // We need at least a single (NULL) term to be ensure we still get the
pierre@0 404 // default image size.
pierre@0 405 $terms[] = NULL;
pierre@0 406 }
pierre@0 407 foreach ($terms as $tid => $term) {
pierre@0 408 list($size->width, $size->height) = getimagesize($file->filepath);
sly@2 409 $size->bytes = strlen(join('', file($file->filepath)));
pierre@0 410 if ($format = ad_image_format_load($tid)) {
pierre@0 411 if ($size->width < $format->min_width) {
sly@2 412 drupal_set_message(t('The image %name 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' => isset($term->name) ? $term->name : t('default'))), 'error');
pierre@0 413 $error = TRUE;
pierre@0 414 }
pierre@0 415 else if ($format->max_width && ($size->width > $format->max_width)) {
sly@2 416 drupal_set_message(t('The image %name 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' => isset($term->name) ? $term->name : t('default'))), 'error');
pierre@0 417 $error = TRUE;
pierre@0 418 }
pierre@0 419 if ($size->height < $format->min_height) {
sly@2 420 drupal_set_message(t('The image %name 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' => isset($term->name) ? $term->name : t('default'))), 'error');
pierre@0 421 $error = TRUE;
pierre@0 422 }
pierre@0 423 else if ($format->max_height && $size->height > $format->max_height) {
sly@2 424 drupal_set_message(t('The image %name 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' => isset($term->name) ? $term->name : t('default'))), 'error');
sly@2 425 $error = TRUE;
sly@2 426 }
sly@2 427 if ($format->max_size && $size->bytes > $format->max_size) {
sly@2 428 drupal_set_message(t('The image %name is %current bytes in size, which is more than the maximum of %maximum bytes allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->bytes, '%maximum' => $format->max_size, '%group' => isset($term->name) ? $term->name : t('default'))), 'error');
pierre@0 429 $error = TRUE;
pierre@0 430 }
pierre@0 431 }
pierre@0 432 }
pierre@0 433 }
pierre@0 434 else {
pierre@0 435 $error = TRUE;
pierre@0 436 drupal_set_message('Please report error: $file is not an object, bug #146147.');
pierre@0 437 }
pierre@0 438 if ($error) {
pierre@0 439 return FALSE;
pierre@0 440 }
pierre@0 441 else {
pierre@0 442 return $size;
pierre@0 443 }
pierre@0 444 }
pierre@0 445
pierre@0 446 /**
pierre@0 447 * Returns image object from given ad node.
pierre@0 448 */
pierre@0 449 function ad_image_load_image($node) {
sly@2 450 if (isset($node->remote_image) && !empty($node->remote_image)) {
sly@2 451 $file->filename = $node->remote_image;
sly@2 452 $file->filepath = $node->remote_image;
sly@2 453 $image = ad_image_validate_size($file, $node->nid);
sly@2 454 if ($image !== FALSE) {
sly@2 455 return $image;
sly@2 456 }
sly@2 457 }
sly@2 458 else if (isset($node->files) && is_array($node->files)) {
pierre@0 459 foreach ($node->files as $file) {
pierre@0 460 if (is_array($file)) {
pierre@0 461 if ($file['list'] && file_exists($file['filepath'])) {
pierre@0 462 $image = ad_image_validate_size((object)$file, $node->nid);
pierre@0 463 if ($image !== FALSE) {
pierre@0 464 $image->fid = $file['fid'];
pierre@0 465 return $image;
pierre@0 466 }
pierre@0 467 }
pierre@0 468 }
pierre@0 469 else {
pierre@0 470 if ($file->list && file_exists($file->filepath)) {
pierre@0 471 $image = ad_image_validate_size($file, $node->nid);
pierre@0 472 if ($image !== FALSE) {
pierre@0 473 $image->fid = $file->fid;
pierre@0 474 return $image;
pierre@0 475 }
pierre@0 476 }
pierre@0 477 }
pierre@0 478 }
pierre@0 479 }
pierre@0 480 return FALSE;
pierre@0 481 }
pierre@0 482
pierre@0 483 /**
pierre@0 484 * Adapi helper function for displaying a node form.
pierre@0 485 */
pierre@0 486 function ad_image_node_form(&$node) {
pierre@0 487 $form = array();
pierre@0 488
pierre@0 489 ad_image_adapi('check_install', $node);
pierre@0 490
pierre@0 491 $form['ad_image'] = array(
pierre@0 492 '#type' => 'fieldset',
pierre@0 493 '#title' => t('Image'),
pierre@0 494 '#collapsible' => TRUE,
pierre@0 495 );
pierre@0 496
pierre@1 497 if (!empty($node->remote_image)) {
sly@2 498 $file->filename = $node->remote_image;
sly@2 499 $file->filepath = $node->remote_image;
sly@2 500 $image = ad_image_validate_size($file, $node->nid);
pierre@1 501 $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> ';
sly@2 502 if ($image === FALSE) {
sly@2 503 $path .= t('(invalid image)'). '<br />';
sly@2 504 }
pierre@1 505 }
pierre@1 506 else if (isset($node->files)) {
pierre@0 507 $files = $node->files;
pierre@0 508 }
pierre@0 509 else {
pierre@0 510 if (!isset($node->vid)) {
pierre@0 511 $node->vid = '';
pierre@0 512 }
pierre@0 513 $files = module_invoke('upload', 'load', $node);
pierre@0 514 }
pierre@1 515 $num = isset($files) ? sizeof($files) : 0;
pierre@0 516
pierre@0 517 if ($num) {
pierre@1 518 $path = NULL;
pierre@1 519 $active = 0;
pierre@0 520 foreach ($files as $file) {
pierre@0 521 if ($file->list && file_exists($file->filepath)) {
pierre@0 522 $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> ';
pierre@0 523 $image = ad_image_validate_size($file, $node->nid);
pierre@0 524 if ($image === FALSE) {
pierre@0 525 $path .= t('(invalid image)'). '<br />';
pierre@0 526 }
pierre@0 527 else if (!$active++) {
pierre@0 528 $path .= t('(active)'). '<br />';
pierre@0 529 }
pierre@0 530 else {
pierre@0 531 $path .= t('(inactive)'). '<br />';
pierre@0 532 }
pierre@0 533 }
pierre@0 534 else if (!file_exists($file->filepath)) {
pierre@0 535 drupal_set_message(t('Unable to locate image %image.', array('%image' => "$file->filepath")));
pierre@0 536 $path .= t('Unable to locate the uploaded image.');
pierre@0 537 }
pierre@0 538 }
pierre@0 539 }
pierre@1 540 if (!isset($path) || $path == NULL) {
pierre@0 541 $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />');
pierre@0 542 // Only set error if node has been previewed or submitted.
pierre@0 543 if (isset($_POST['edit'])) {
pierre@0 544 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
pierre@0 545 }
pierre@0 546 }
pierre@1 547 else if ($num) {
pierre@1 548 $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 549 }
pierre@0 550
pierre@0 551 $form['ad_image']['image'] = array(
pierre@0 552 '#type' => 'markup',
pierre@0 553 '#value' => $path,
pierre@0 554 '#prefix' => '<div class="container-inline">',
pierre@0 555 '#suffix' => '</div>',
pierre@0 556 );
pierre@0 557
pierre@0 558 $form['ad_image']['url'] = array(
pierre@0 559 '#type' => 'textfield',
pierre@0 560 '#title' => t('Destination URL'),
pierre@0 561 '#required' => FALSE,
pierre@0 562 '#default_value' => isset($node->url) ? $node->url : '',
pierre@0 563 '#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 564 );
pierre@0 565
pierre@0 566 $form['ad_image']['tooltip'] = array(
pierre@0 567 '#type' => 'textfield',
pierre@0 568 '#title' => t('Mouseover'),
pierre@0 569 '#required' => FALSE,
pierre@0 570 '#default_value' => isset($node->tooltip) ? $node->tooltip : '',
pierre@0 571 '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'),
pierre@0 572 );
pierre@0 573
pierre@1 574 if (variable_get('ad_image_remote_images', FALSE)) {
pierre@1 575 $form['ad_image']['remote_image'] = array(
pierre@1 576 '#type' => 'textfield',
pierre@1 577 '#title' => t('Remote image path'),
pierre@1 578 '#required' => FALSE,
pierre@1 579 '#default_value' => isset($node->remote_image) ? $node->remote_image : '',
pierre@1 580 '#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 581 );
pierre@1 582 }
pierre@1 583
pierre@0 584 return $form;
pierre@0 585 }
pierre@0 586
pierre@0 587 /**
pierre@0 588 * Adapi helper function for displaying ad itself.
pierre@0 589 */
pierre@0 590 function ad_image_node_view(&$node) {
pierre@0 591 $node->content['ad'] = array(
pierre@0 592 '#value' => preg_replace('&@HOSTID___&', '0', ad_image_display_ad($node)),
pierre@0 593 '#weight' => -1,
pierre@0 594 );
pierre@0 595 if (!empty($node->url)) {
pierre@0 596 $link = t('Links to !url.', array('!url' => $node->url));
pierre@0 597 $link = check_plain($link, $node->format, FALSE);
pierre@0 598 $node->content['ad-link'] = array(
pierre@0 599 '#value' => "<div class=\"links-to\">$link</div>",
pierre@0 600 '#weight' => 0,
pierre@0 601 );
pierre@0 602 }
pierre@0 603 }