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

update advertisement
author pierre
date Thu, 02 Apr 2009 15:28:21 +0000
parents d8a3998dac8e
children e5584a19768b
comparison
equal deleted inserted replaced
0:d8a3998dac8e 1:948362c2a207
1 <?php 1 <?php
2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11 2009/02/17 18:56:26 jeremy Exp $ 2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.5 2009/03/11 16:12:07 jeremy Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * Enhances the ad module to support banner ads. 6 * Enhances the ad module to support banner ads.
7 * 7 *
23 * The ad object. 23 * The ad object.
24 * @return 24 * @return
25 * A string containing the ad markup. 25 * A string containing the ad markup.
26 */ 26 */
27 function theme_ad_image_ad($ad) { 27 function theme_ad_image_ad($ad) {
28 if (isset($ad->aid) && isset($ad->filepath)) { 28 if (isset($ad->aid) && (isset($ad->filepath) || isset($ad->remote_image))) {
29 $output = '<div class="image-advertisement" id="ad-'. $ad->aid .'">'; 29 $output = '<div class="image-advertisement" id="ad-'. $ad->aid .'">';
30 if (isset($ad->url) && !empty($ad->url)) { 30 if (isset($ad->url) && !empty($ad->url)) {
31 $image = theme('ad_image_image', $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip)); 31 $image = theme('ad_image_image', !empty($ad->remote_image) ? $ad->remote_image : $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip));
32 $output .= l($image, $ad->redirect .'/@HOSTID___', array('attributes' => ad_link_attributes(), 'absolute' => TRUE, 'html' => TRUE)); 32 $output .= l($image, $ad->redirect .'/@HOSTID___', array('attributes' => ad_link_attributes(), 'absolute' => TRUE, 'html' => TRUE));
33 } 33 }
34 else { 34 else {
35 $output .= theme('ad_image_image', $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip)); 35 $output .= theme('ad_image_image', !empty($ad->remote_image) ? $ad->remote_image : $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip));
36 } 36 }
37 $output .= '</div>'; 37 $output .= '</div>';
38 return $output; 38 return $output;
39 } 39 }
40 } 40 }
57 * attributes. 57 * attributes.
58 * @return 58 * @return
59 * A string containing the image tag. 59 * A string containing the image tag.
60 */ 60 */
61 function theme_ad_image_image($path, $alt = '', $tooltip = '', $attributes = NULL, $getsize = TRUE) { 61 function theme_ad_image_image($path, $alt = '', $tooltip = '', $attributes = NULL, $getsize = TRUE) {
62 if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) { 62 if ($getsize) {
63 $attributes = drupal_attributes($attributes); 63 list($width, $height, $type, $image_attributes) = @getimagesize($path);
64 $url = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($path)); 64 if (isset($width) && isset($height)) {
65 return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($tooltip) .'" '. $image_attributes . $attributes .' />'; 65 $attributes = drupal_attributes($attributes);
66 if (is_file($path)) {
67 $url = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($path));
68 }
69 else {
70 $url = $path;
71 }
72 return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($tooltip) .'" '. $image_attributes . $attributes .' />';
73 }
66 } 74 }
67 } 75 }
68 76
69 /** 77 /**
70 * Implementation of hook_theme(). 78 * Implementation of hook_theme().
114 * Image ad settings form. 122 * Image ad settings form.
115 */ 123 */
116 function ad_image_global_settings($edit = array()) { 124 function ad_image_global_settings($edit = array()) {
117 $form = array(); 125 $form = array();
118 126
127 $form['general'] = array(
128 '#type' => 'fieldset',
129 '#title' => t('General settings'),
130 '#collapsible' => TRUE,
131 );
132 $form['general']['remote_images'] = array(
133 '#type' => 'checkbox',
134 '#title' => t('Allow remote hosted images'),
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.'),
136 '#default_value' => variable_get('ad_image_remote_images', FALSE),
137 );
138
119 $groups = module_invoke('ad', 'groups_list', TRUE); 139 $groups = module_invoke('ad', 'groups_list', TRUE);
120 foreach ($groups as $tid => $group) { 140 foreach ($groups as $tid => $group) {
121 $form["group-$tid"] = array( 141 $form["group-$tid"] = array(
122 '#type' => 'fieldset', 142 '#type' => 'fieldset',
123 '#title' => $group->name, 143 '#title' => $group->name,
169 $form['save'] = array( 189 $form['save'] = array(
170 '#type' => 'submit', 190 '#type' => 'submit',
171 '#value' => t('Save'), 191 '#value' => t('Save'),
172 ); 192 );
173 193
194 $form['#submit'] = array('ad_image_global_settings_submit');
195
174 return $form; 196 return $form;
175 } 197 }
176 198
177 /** 199 /**
178 * Save min and max image width and height values for ad groups. 200 * Save min and max image width and height values for ad groups.
179 */ 201 */
180 function ad_image_global_settings_submit($form, &$form_state) { 202 function ad_image_global_settings_submit($form, &$form_state) {
203 variable_set('ad_image_remote_images', $form_state['values']['remote_images']);
181 $groups = module_invoke('ad', 'groups_list', TRUE); 204 $groups = module_invoke('ad', 'groups_list', TRUE);
182 foreach ($groups as $group) { 205 foreach ($groups as $group) {
183 // TODO: Update the database schema, convert gid to tid. 206 // TODO: Update the database schema, convert gid to tid.
184 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid)); 207 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid));
185 if (is_numeric($gid)) { 208 if (is_numeric($gid)) {
199 $output = NULL; 222 $output = NULL;
200 switch ($op) { 223 switch ($op) {
201 224
202 case 'load': 225 case 'load':
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'])); 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']));
204 $return['ad'] = '<img src="'. file_create_url($return['filepath']) .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />'; 227 if (isset($return['remote_image']) && !empty($return['remote_image'])) {
228 $path = $return['remote_image'];
229 }
230 else {
231 $path = file_create_url($return['filepath']);
232 }
233 $return['ad'] = '<img src="'. $path .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />';
205 return $return; 234 return $return;
206 235
207 case 'insert': 236 case 'insert':
208 case 'update': 237 case 'update':
209 $image = ad_image_load_image($node); 238 $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0;
210 $fid = (int)ad_image_active_file($node->files); 239 if ($fid) {
240 $image = ad_image_load_image($node);
241 }
242 else if (isset($node->remote_image)) {
243 list($image->width, $image->height) = getimagesize($node->remote_image);
244 }
211 // This is ugly, but as "a" comes before "u" we don't seem to be able 245 // This is ugly, but as "a" comes before "u" we don't seem to be able
212 // to modify the upload module's form. Instead, we check after the fact 246 // to modify the upload module's form. Instead, we check after the fact
213 // if someone is editing images when they're not allowed, and if so we 247 // if someone is editing images when they're not allowed, and if so we
214 // prevent the ad from being saved. 248 // prevent the ad from being saved.
215 if ($op == 'update' && !ad_adaccess($node, 'manage active ad')) { 249 if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) {
216 // See if fid is changing -- it's okay if new images are uploaded, it's 250 // See if fid is changing -- it's okay if new images are uploaded, it's
217 // just not okay if the active fid is changed. 251 // just not okay if the active fid is changed.
218 if ($fid != $image->fid) { 252 if ($fid != $image->fid) {
219 drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error'); 253 drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error');
220 // This causes upload_save() to simply return without making any 254 // This causes upload_save() to simply return without making any
221 // changes to the files attached to this node. 255 // changes to the files attached to this node.
222 unset($node->files); 256 unset($node->files);
223 } 257 }
224 } 258 }
225 else { 259 else {
226 // Check that all values are valid -- this is a kludge to work around 260 // Check that all values are valid -- this is a kludge to work around
227 // bug #146147 until the problem is better understood. 261 // bug #146147 until the problem is better understood.
228 $width = isset($image->width) ? $image->width : 0; 262 $width = isset($image->width) ? $image->width : 0;
229 $height = isset($image->height) ? $image->height : 0; 263 $height = isset($image->height) ? $image->height : 0;
230 $fid = isset($image->fid) ? $image->fid : 0; 264 $fid = isset($image->fid) ? $image->fid : 0;
231 if ($image !== FALSE && $width != 0 && $height != 0 && $fid != 0) { 265 if ($image !== FALSE && $width != 0 && $height != 0 && ($fid != 0 || $node->remote_image)) {
232 $node->fid = $image->fid; 266 $node->fid = isset($image->fid) ? $image->fid : 0;
233 $node->width = $image->width; 267 $node->width = $image->width;
234 $node->height = $image->height; 268 $node->height = $image->height;
235 } 269 }
236 else { 270 else {
237 $image = FALSE; 271 $image = FALSE;
238 } 272 }
239 } 273 }
240 if ($op == 'insert') { 274 if ($op == 'insert') {
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); 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);
242 } 276 }
243 else { 277 else {
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); 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);
245 } 279 }
246 // No valid image has been uploaded, don't allow ad to be 'active'. 280 if (!isset($node->remote_image) || empty($node->remote_image)) {
247 if ($image === FALSE || !ad_image_active_file(($node->files))) { 281 // No valid image has been uploaded, don't allow ad to be 'active'.
248 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); 282 if ($image === FALSE || !ad_image_active_file(($node->files))) {
249 if (db_affected_rows()) { 283 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
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'); 284 if (db_affected_rows()) {
251 } 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');
252 } 286 }
253 else if (!$fid) { 287 }
254 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); 288 else if (!$fid) {
255 if (db_affected_rows()) { 289 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
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'); 290 if (db_affected_rows()) {
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');
292 }
257 } 293 }
258 } 294 }
259 break; 295 break;
260 296
261 case 'validate': 297 case 'validate':
298 if (isset($node->remote_image) && !empty($node->remote_image)) {
299 if (variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
300 drupal_set_message('You must specify a valid path for your remote advertisement.', 'error');
301 }
302 }
303 else if (!isset($node->files) || !ad_image_active_file($node->files)) {
304 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
305 }
262 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) { 306 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) {
263 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL')))); 307 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL'))));
264 }
265 if (!isset($node->files) || !ad_image_active_file($node->files)) {
266 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
267 } 308 }
268 break; 309 break;
269 310
270 case 'delete': 311 case 'delete':
271 db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid); 312 db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid);
289 'help' => t('An image or banner advertisement.'), 330 'help' => t('An image or banner advertisement.'),
290 ), 331 ),
291 ); 332 );
292 case 'permissions': 333 case 'permissions':
293 if (!isset($node->adtype) || $node->adtype == 'image') { 334 if (!isset($node->adtype) || $node->adtype == 'image') {
294 return array('manage active ad'); 335 return array('manage active image' => TRUE);
295 } 336 }
296 337
297 case 'check_install': 338 case 'check_install':
298 if (!module_exists('upload')) { 339 if (!module_exists('upload')) {
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'); 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');
398 439
399 /** 440 /**
400 * Returns image object from given ad node. 441 * Returns image object from given ad node.
401 */ 442 */
402 function ad_image_load_image($node) { 443 function ad_image_load_image($node) {
403 if (is_array($node->files)) { 444 if (isset($node->files) && is_array($node->files)) {
404 foreach ($node->files as $file) { 445 foreach ($node->files as $file) {
405 if (is_array($file)) { 446 if (is_array($file)) {
406 if ($file['list'] && file_exists($file['filepath'])) { 447 if ($file['list'] && file_exists($file['filepath'])) {
407 $image = ad_image_validate_size((object)$file, $node->nid); 448 $image = ad_image_validate_size((object)$file, $node->nid);
408 if ($image !== FALSE) { 449 if ($image !== FALSE) {
437 '#type' => 'fieldset', 478 '#type' => 'fieldset',
438 '#title' => t('Image'), 479 '#title' => t('Image'),
439 '#collapsible' => TRUE, 480 '#collapsible' => TRUE,
440 ); 481 );
441 482
442 if (isset($node->files)) { 483 if (!empty($node->remote_image)) {
484 list($image->width, $image->height) = getimagesize($node->remote_image);
485 $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> ';
486 }
487 else if (isset($node->files)) {
443 $files = $node->files; 488 $files = $node->files;
444 } 489 }
445 else { 490 else {
446 if (!isset($node->vid)) { 491 if (!isset($node->vid)) {
447 $node->vid = ''; 492 $node->vid = '';
448 } 493 }
449 $files = module_invoke('upload', 'load', $node); 494 $files = module_invoke('upload', 'load', $node);
450 } 495 }
451 $num = sizeof($files); 496 $num = isset($files) ? sizeof($files) : 0;
452 497
453 $path = NULL;
454 $active = 0;
455 if ($num) { 498 if ($num) {
499 $path = NULL;
500 $active = 0;
456 foreach ($files as $file) { 501 foreach ($files as $file) {
457 if ($file->list && file_exists($file->filepath)) { 502 if ($file->list && file_exists($file->filepath)) {
458 $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> '; 503 $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> ';
459 $image = ad_image_validate_size($file, $node->nid); 504 $image = ad_image_validate_size($file, $node->nid);
460 if ($image === FALSE) { 505 if ($image === FALSE) {
471 drupal_set_message(t('Unable to locate image %image.', array('%image' => "$file->filepath"))); 516 drupal_set_message(t('Unable to locate image %image.', array('%image' => "$file->filepath")));
472 $path .= t('Unable to locate the uploaded image.'); 517 $path .= t('Unable to locate the uploaded image.');
473 } 518 }
474 } 519 }
475 } 520 }
476 if ($path == NULL) { 521 if (!isset($path) || $path == NULL) {
477 $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />'); 522 $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />');
478 // Only set error if node has been previewed or submitted. 523 // Only set error if node has been previewed or submitted.
479 if (isset($_POST['edit'])) { 524 if (isset($_POST['edit'])) {
480 form_set_error('upload', t('It is required that you upload an image for your image advertisement.')); 525 form_set_error('upload', t('It is required that you upload an image for your image advertisement.'));
481 } 526 }
482 } 527 }
483 528 else if ($num) {
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.'); 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.');
530 }
485 531
486 $form['ad_image']['image'] = array( 532 $form['ad_image']['image'] = array(
487 '#type' => 'markup', 533 '#type' => 'markup',
488 '#value' => $path, 534 '#value' => $path,
489 '#prefix' => '<div class="container-inline">', 535 '#prefix' => '<div class="container-inline">',
504 '#required' => FALSE, 550 '#required' => FALSE,
505 '#default_value' => isset($node->tooltip) ? $node->tooltip : '', 551 '#default_value' => isset($node->tooltip) ? $node->tooltip : '',
506 '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'), 552 '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'),
507 ); 553 );
508 554
555 if (variable_get('ad_image_remote_images', FALSE)) {
556 $form['ad_image']['remote_image'] = array(
557 '#type' => 'textfield',
558 '#title' => t('Remote image path'),
559 '#required' => FALSE,
560 '#default_value' => isset($node->remote_image) ? $node->remote_image : '',
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'))),
562 );
563 }
564
509 return $form; 565 return $form;
510 } 566 }
511 567
512 /** 568 /**
513 * Adapi helper function for displaying ad itself. 569 * Adapi helper function for displaying ad itself.