Mercurial > defr > drupal > ad
comparison image/ad_image.module @ 2:e5584a19768b ad
maj module ad
| author | sly |
|---|---|
| date | Wed, 15 Apr 2009 07:58:32 +0000 |
| parents | 948362c2a207 |
| children | 6aeff3329e01 |
comparison
equal
deleted
inserted
replaced
| 1:948362c2a207 | 2:e5584a19768b |
|---|---|
| 1 <?php | 1 <?php |
| 2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.5 2009/03/11 16:12:07 jeremy Exp $ | 2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.6 2009/04/02 15:48:29 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 * |
| 150 '#suffix' => '</div>', | 150 '#suffix' => '</div>', |
| 151 '#value' => theme_placeholder("$group->description"), | 151 '#value' => theme_placeholder("$group->description"), |
| 152 ); | 152 ); |
| 153 | 153 |
| 154 $format = db_fetch_object(db_query('SELECT * FROM {ad_image_format} WHERE gid = %d', $tid)); | 154 $format = db_fetch_object(db_query('SELECT * FROM {ad_image_format} WHERE gid = %d', $tid)); |
| 155 $form["group-$tid"]["max-size-$tid"] = array( | |
| 156 '#type' => 'textfield', | |
| 157 '#title' => t('Maximum filesize'), | |
| 158 '#size' => 5, | |
| 159 '#maxlength' => 15, | |
| 160 '#default_value' => isset($format->max_size) ? $format->max_size : 0, | |
| 161 '#description' => t('Optionally specify a maximum filesize in bytes for images in this group. To specify no maximum filesize, enter <em>0</em>.'), | |
| 162 ); | |
| 155 $form["group-$tid"]["min-height-$tid"] = array( | 163 $form["group-$tid"]["min-height-$tid"] = array( |
| 156 '#type' => 'textfield', | 164 '#type' => 'textfield', |
| 157 '#title' => t('Minimum height'), | 165 '#title' => t('Minimum height'), |
| 158 '#size' => 5, | 166 '#size' => 5, |
| 159 '#maxlength' => 5, | 167 '#maxlength' => 5, |
| 204 $groups = module_invoke('ad', 'groups_list', TRUE); | 212 $groups = module_invoke('ad', 'groups_list', TRUE); |
| 205 foreach ($groups as $group) { | 213 foreach ($groups as $group) { |
| 206 // TODO: Update the database schema, convert gid to tid. | 214 // TODO: Update the database schema, convert gid to tid. |
| 207 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid)); | 215 $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid)); |
| 208 if (is_numeric($gid)) { | 216 if (is_numeric($gid)) { |
| 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); | 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); |
| 210 } | 218 } |
| 211 else { | 219 else { |
| 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"]); | 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"]); |
| 213 } | 221 } |
| 214 } | 222 } |
| 215 drupal_set_message('Image ad global settings updated.'); | 223 drupal_set_message('Image ad global settings updated.'); |
| 216 } | 224 } |
| 217 | 225 |
| 234 return $return; | 242 return $return; |
| 235 | 243 |
| 236 case 'insert': | 244 case 'insert': |
| 237 case 'update': | 245 case 'update': |
| 238 $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0; | 246 $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0; |
| 239 if ($fid) { | 247 $image = ad_image_load_image($node); |
| 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 } | |
| 245 // This is ugly, but as "a" comes before "u" we don't seem to be able | 248 // This is ugly, but as "a" comes before "u" we don't seem to be able |
| 246 // to modify the upload module's form. Instead, we check after the fact | 249 // to modify the upload module's form. Instead, we check after the fact |
| 247 // if someone is editing images when they're not allowed, and if so we | 250 // if someone is editing images when they're not allowed, and if so we |
| 248 // prevent the ad from being saved. | 251 // prevent the ad from being saved. |
| 249 if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) { | 252 if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) { |
| 270 else { | 273 else { |
| 271 $image = FALSE; | 274 $image = FALSE; |
| 272 } | 275 } |
| 273 } | 276 } |
| 274 if ($op == 'insert') { | 277 if ($op == 'insert') { |
| 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); | 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); |
| 276 } | 279 } |
| 277 else { | 280 else { |
| 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); | 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); |
| 279 } | 282 } |
| 280 if (!isset($node->remote_image) || empty($node->remote_image)) { | 283 // No valid image has been uploaded, don't allow ad to be 'active'. |
| 281 // No valid image has been uploaded, don't allow ad to be 'active'. | 284 if ($image === FALSE || !ad_image_active_file(($node->files))) { |
| 282 if ($image === FALSE || !ad_image_active_file(($node->files))) { | 285 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); |
| 283 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); | 286 if (db_affected_rows()) { |
| 284 if (db_affected_rows()) { | 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'); |
| 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'); | 288 } |
| 286 } | 289 } |
| 287 } | 290 else if (!$node->remote_image && !$fid) { |
| 288 else if (!$fid) { | 291 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); |
| 289 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); | 292 if (db_affected_rows()) { |
| 290 if (db_affected_rows()) { | 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'); |
| 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 } | |
| 293 } | 294 } |
| 294 } | 295 } |
| 295 break; | 296 break; |
| 296 | 297 |
| 297 case 'validate': | 298 case 'validate': |
| 380 function ad_image_format_load($gid) { | 381 function ad_image_format_load($gid) { |
| 381 static $format; | 382 static $format; |
| 382 if (isset($format[$gid])) { | 383 if (isset($format[$gid])) { |
| 383 return $format[$gid]; | 384 return $format[$gid]; |
| 384 } | 385 } |
| 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)); | 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)); |
| 386 return $format[$gid]; | 387 return $format[$gid]; |
| 387 } | 388 } |
| 388 | 389 |
| 389 /** | 390 /** |
| 390 * Validate that the size of the uploaded image is within the defined limits. | 391 * Validate that the size of the uploaded image is within the defined limits. |
| 403 // default image size. | 404 // default image size. |
| 404 $terms[] = NULL; | 405 $terms[] = NULL; |
| 405 } | 406 } |
| 406 foreach ($terms as $tid => $term) { | 407 foreach ($terms as $tid => $term) { |
| 407 list($size->width, $size->height) = getimagesize($file->filepath); | 408 list($size->width, $size->height) = getimagesize($file->filepath); |
| 409 $size->bytes = strlen(join('', file($file->filepath))); | |
| 408 if ($format = ad_image_format_load($tid)) { | 410 if ($format = ad_image_format_load($tid)) { |
| 409 if ($size->width < $format->min_width) { | 411 if ($size->width < $format->min_width) { |
| 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'); | 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'); |
| 411 $error = TRUE; | 413 $error = TRUE; |
| 412 } | 414 } |
| 413 else if ($format->max_width && ($size->width > $format->max_width)) { | 415 else if ($format->max_width && ($size->width > $format->max_width)) { |
| 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'); | 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'); |
| 415 $error = TRUE; | 417 $error = TRUE; |
| 416 } | 418 } |
| 417 if ($size->height < $format->min_height) { | 419 if ($size->height < $format->min_height) { |
| 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'); | 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'); |
| 419 $error = TRUE; | 421 $error = TRUE; |
| 420 } | 422 } |
| 421 else if ($format->max_height && $size->height > $format->max_height) { | 423 else if ($format->max_height && $size->height > $format->max_height) { |
| 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'); | 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'); |
| 425 $error = TRUE; | |
| 426 } | |
| 427 if ($format->max_size && $size->bytes > $format->max_size) { | |
| 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'); | |
| 423 $error = TRUE; | 429 $error = TRUE; |
| 424 } | 430 } |
| 425 } | 431 } |
| 426 } | 432 } |
| 427 } | 433 } |
| 439 | 445 |
| 440 /** | 446 /** |
| 441 * Returns image object from given ad node. | 447 * Returns image object from given ad node. |
| 442 */ | 448 */ |
| 443 function ad_image_load_image($node) { | 449 function ad_image_load_image($node) { |
| 444 if (isset($node->files) && is_array($node->files)) { | 450 if (isset($node->remote_image) && !empty($node->remote_image)) { |
| 451 $file->filename = $node->remote_image; | |
| 452 $file->filepath = $node->remote_image; | |
| 453 $image = ad_image_validate_size($file, $node->nid); | |
| 454 if ($image !== FALSE) { | |
| 455 return $image; | |
| 456 } | |
| 457 } | |
| 458 else if (isset($node->files) && is_array($node->files)) { | |
| 445 foreach ($node->files as $file) { | 459 foreach ($node->files as $file) { |
| 446 if (is_array($file)) { | 460 if (is_array($file)) { |
| 447 if ($file['list'] && file_exists($file['filepath'])) { | 461 if ($file['list'] && file_exists($file['filepath'])) { |
| 448 $image = ad_image_validate_size((object)$file, $node->nid); | 462 $image = ad_image_validate_size((object)$file, $node->nid); |
| 449 if ($image !== FALSE) { | 463 if ($image !== FALSE) { |
| 479 '#title' => t('Image'), | 493 '#title' => t('Image'), |
| 480 '#collapsible' => TRUE, | 494 '#collapsible' => TRUE, |
| 481 ); | 495 ); |
| 482 | 496 |
| 483 if (!empty($node->remote_image)) { | 497 if (!empty($node->remote_image)) { |
| 484 list($image->width, $image->height) = getimagesize($node->remote_image); | 498 $file->filename = $node->remote_image; |
| 499 $file->filepath = $node->remote_image; | |
| 500 $image = ad_image_validate_size($file, $node->nid); | |
| 485 $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> '; | 501 $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> '; |
| 502 if ($image === FALSE) { | |
| 503 $path .= t('(invalid image)'). '<br />'; | |
| 504 } | |
| 486 } | 505 } |
| 487 else if (isset($node->files)) { | 506 else if (isset($node->files)) { |
| 488 $files = $node->files; | 507 $files = $node->files; |
| 489 } | 508 } |
| 490 else { | 509 else { |
