Mercurial > defr > drupal > ad
diff image/ad_image.module @ 1:948362c2a207 ad
update advertisement
author | pierre |
---|---|
date | Thu, 02 Apr 2009 15:28:21 +0000 |
parents | d8a3998dac8e |
children | e5584a19768b |
line wrap: on
line diff
--- a/image/ad_image.module Fri Feb 20 14:04:09 2009 +0000 +++ b/image/ad_image.module Thu Apr 02 15:28:21 2009 +0000 @@ -1,5 +1,5 @@ <?php -// $Id: ad_image.module,v 1.2.2.13.2.40.2.11 2009/02/17 18:56:26 jeremy Exp $ +// $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.5 2009/03/11 16:12:07 jeremy Exp $ /** * @file @@ -25,14 +25,14 @@ * A string containing the ad markup. */ function theme_ad_image_ad($ad) { - if (isset($ad->aid) && isset($ad->filepath)) { + if (isset($ad->aid) && (isset($ad->filepath) || isset($ad->remote_image))) { $output = '<div class="image-advertisement" id="ad-'. $ad->aid .'">'; if (isset($ad->url) && !empty($ad->url)) { - $image = theme('ad_image_image', $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip)); + $image = theme('ad_image_image', !empty($ad->remote_image) ? $ad->remote_image : $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip)); $output .= l($image, $ad->redirect .'/@HOSTID___', array('attributes' => ad_link_attributes(), 'absolute' => TRUE, 'html' => TRUE)); } else { - $output .= theme('ad_image_image', $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip)); + $output .= theme('ad_image_image', !empty($ad->remote_image) ? $ad->remote_image : $ad->filepath, check_plain($ad->tooltip), check_plain($ad->tooltip)); } $output .= '</div>'; return $output; @@ -59,10 +59,18 @@ * A string containing the image tag. */ function theme_ad_image_image($path, $alt = '', $tooltip = '', $attributes = NULL, $getsize = TRUE) { - if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) { - $attributes = drupal_attributes($attributes); - $url = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($path)); - return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($tooltip) .'" '. $image_attributes . $attributes .' />'; + if ($getsize) { + list($width, $height, $type, $image_attributes) = @getimagesize($path); + if (isset($width) && isset($height)) { + $attributes = drupal_attributes($attributes); + if (is_file($path)) { + $url = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($path)); + } + else { + $url = $path; + } + return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($tooltip) .'" '. $image_attributes . $attributes .' />'; + } } } @@ -116,6 +124,18 @@ function ad_image_global_settings($edit = array()) { $form = array(); + $form['general'] = array( + '#type' => 'fieldset', + '#title' => t('General settings'), + '#collapsible' => TRUE, + ); + $form['general']['remote_images'] = array( + '#type' => 'checkbox', + '#title' => t('Allow remote hosted images'), + '#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.'), + '#default_value' => variable_get('ad_image_remote_images', FALSE), + ); + $groups = module_invoke('ad', 'groups_list', TRUE); foreach ($groups as $tid => $group) { $form["group-$tid"] = array( @@ -171,6 +191,8 @@ '#value' => t('Save'), ); + $form['#submit'] = array('ad_image_global_settings_submit'); + return $form; } @@ -178,6 +200,7 @@ * Save min and max image width and height values for ad groups. */ function ad_image_global_settings_submit($form, &$form_state) { + variable_set('ad_image_remote_images', $form_state['values']['remote_images']); $groups = module_invoke('ad', 'groups_list', TRUE); foreach ($groups as $group) { // TODO: Update the database schema, convert gid to tid. @@ -201,23 +224,34 @@ case 'load': $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'])); - $return['ad'] = '<img src="'. file_create_url($return['filepath']) .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />'; + if (isset($return['remote_image']) && !empty($return['remote_image'])) { + $path = $return['remote_image']; + } + else { + $path = file_create_url($return['filepath']); + } + $return['ad'] = '<img src="'. $path .'" width="'. $return['width'] .'" height="'. $return['height'] .'" alt="'. check_plain($return['tooltip']) .'" />'; return $return; case 'insert': case 'update': - $image = ad_image_load_image($node); - $fid = (int)ad_image_active_file($node->files); + $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0; + if ($fid) { + $image = ad_image_load_image($node); + } + else if (isset($node->remote_image)) { + list($image->width, $image->height) = getimagesize($node->remote_image); + } // This is ugly, but as "a" comes before "u" we don't seem to be able // to modify the upload module's form. Instead, we check after the fact // if someone is editing images when they're not allowed, and if so we // prevent the ad from being saved. - if ($op == 'update' && !ad_adaccess($node, 'manage active ad')) { + if ($op == 'update' && !ad_permission($node->nid, 'manage active image')) { // See if fid is changing -- it's okay if new images are uploaded, it's // just not okay if the active fid is changed. if ($fid != $image->fid) { drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error'); - // This causes upload_save() to simply return without making any + // This causes upload_save() to simply return without making any // changes to the files attached to this node. unset($node->files); } @@ -228,8 +262,8 @@ $width = isset($image->width) ? $image->width : 0; $height = isset($image->height) ? $image->height : 0; $fid = isset($image->fid) ? $image->fid : 0; - if ($image !== FALSE && $width != 0 && $height != 0 && $fid != 0) { - $node->fid = $image->fid; + if ($image !== FALSE && $width != 0 && $height != 0 && ($fid != 0 || $node->remote_image)) { + $node->fid = isset($image->fid) ? $image->fid : 0; $node->width = $image->width; $node->height = $image->height; } @@ -238,33 +272,40 @@ } } if ($op == 'insert') { - 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); + 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); } else { - 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); + 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); } - // No valid image has been uploaded, don't allow ad to be 'active'. - if ($image === FALSE || !ad_image_active_file(($node->files))) { - db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); - if (db_affected_rows()) { - 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'); + if (!isset($node->remote_image) || empty($node->remote_image)) { + // No valid image has been uploaded, don't allow ad to be 'active'. + if ($image === FALSE || !ad_image_active_file(($node->files))) { + db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); + if (db_affected_rows()) { + 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'); + } } - } - else if (!$fid) { - db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); - if (db_affected_rows()) { - 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'); + else if (!$fid) { + db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); + if (db_affected_rows()) { + 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'); + } } } break; case 'validate': + if (isset($node->remote_image) && !empty($node->remote_image)) { + if (variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) { + drupal_set_message('You must specify a valid path for your remote advertisement.', 'error'); + } + } + else if (!isset($node->files) || !ad_image_active_file($node->files)) { + form_set_error('upload', t('It is required that you upload an image for your image advertisement.')); + } if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) { form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL')))); } - if (!isset($node->files) || !ad_image_active_file($node->files)) { - form_set_error('upload', t('It is required that you upload an image for your image advertisement.')); - } break; case 'delete': @@ -291,7 +332,7 @@ ); case 'permissions': if (!isset($node->adtype) || $node->adtype == 'image') { - return array('manage active ad'); + return array('manage active image' => TRUE); } case 'check_install': @@ -400,7 +441,7 @@ * Returns image object from given ad node. */ function ad_image_load_image($node) { - if (is_array($node->files)) { + if (isset($node->files) && is_array($node->files)) { foreach ($node->files as $file) { if (is_array($file)) { if ($file['list'] && file_exists($file['filepath'])) { @@ -439,7 +480,11 @@ '#collapsible' => TRUE, ); - if (isset($node->files)) { + if (!empty($node->remote_image)) { + list($image->width, $image->height) = getimagesize($node->remote_image); + $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> '; + } + else if (isset($node->files)) { $files = $node->files; } else { @@ -448,11 +493,11 @@ } $files = module_invoke('upload', 'load', $node); } - $num = sizeof($files); + $num = isset($files) ? sizeof($files) : 0; - $path = NULL; - $active = 0; if ($num) { + $path = NULL; + $active = 0; foreach ($files as $file) { if ($file->list && file_exists($file->filepath)) { $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> '; @@ -473,15 +518,16 @@ } } } - if ($path == NULL) { + if (!isset($path) || $path == NULL) { $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />'); // Only set error if node has been previewed or submitted. if (isset($_POST['edit'])) { form_set_error('upload', t('It is required that you upload an image for your image advertisement.')); } } - - $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.'); + else if ($num) { + $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.'); + } $form['ad_image']['image'] = array( '#type' => 'markup', @@ -506,6 +552,16 @@ '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'), ); + if (variable_get('ad_image_remote_images', FALSE)) { + $form['ad_image']['remote_image'] = array( + '#type' => 'textfield', + '#title' => t('Remote image path'), + '#required' => FALSE, + '#default_value' => isset($node->remote_image) ? $node->remote_image : '', + '#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'))), + ); + } + return $form; }