pierre@0
|
1 <?php |
piotre@7
|
2 // $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.7 2009/05/15 13:27:23 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'. |
piotre@7
|
284 if (($node->remote_image && $image === FALSE) || |
piotre@7
|
285 (!$node->remote_image && |
piotre@7
|
286 ($image === FALSE || !ad_image_active_file(($node->files))) |
piotre@7
|
287 )) { |
sly@2
|
288 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); |
sly@2
|
289 if (db_affected_rows()) { |
sly@2
|
290 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
|
291 } |
sly@2
|
292 } |
sly@2
|
293 else if (!$node->remote_image && !$fid) { |
sly@2
|
294 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); |
sly@2
|
295 if (db_affected_rows()) { |
sly@2
|
296 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
|
297 } |
pierre@0
|
298 } |
pierre@0
|
299 break; |
pierre@0
|
300 |
pierre@0
|
301 case 'validate': |
pierre@1
|
302 if (isset($node->remote_image) && !empty($node->remote_image)) { |
pierre@1
|
303 if (variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) { |
pierre@1
|
304 drupal_set_message('You must specify a valid path for your remote advertisement.', 'error'); |
pierre@1
|
305 } |
pierre@1
|
306 } |
pierre@1
|
307 else if (!isset($node->files) || !ad_image_active_file($node->files)) { |
pierre@1
|
308 form_set_error('upload', t('It is required that you upload an image for your image advertisement.')); |
pierre@1
|
309 } |
pierre@0
|
310 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) { |
pierre@0
|
311 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL')))); |
pierre@0
|
312 } |
pierre@0
|
313 break; |
pierre@0
|
314 |
pierre@0
|
315 case 'delete': |
pierre@0
|
316 db_query('DELETE FROM {ad_image} WHERE aid = %d', $node->nid); |
pierre@0
|
317 break; |
pierre@0
|
318 |
pierre@0
|
319 case 'form': |
pierre@0
|
320 return ad_image_node_form($node); |
pierre@0
|
321 |
pierre@0
|
322 case 'view': |
pierre@0
|
323 return ad_image_node_view($node); |
pierre@0
|
324 |
pierre@0
|
325 case 'redirect': |
pierre@0
|
326 return db_result(db_query('SELECT url FROM {ad_image} WHERE aid = %d', $node->nid)); |
pierre@0
|
327 |
pierre@0
|
328 case 'type': |
pierre@0
|
329 return array( |
pierre@0
|
330 'image' => array( |
pierre@0
|
331 'name' => t('Image ad'), |
pierre@0
|
332 'module' => 'ad_image', |
pierre@0
|
333 'description' => t('An image or banner advertisement.'), |
pierre@0
|
334 'help' => t('An image or banner advertisement.'), |
pierre@0
|
335 ), |
pierre@0
|
336 ); |
pierre@0
|
337 case 'permissions': |
pierre@0
|
338 if (!isset($node->adtype) || $node->adtype == 'image') { |
pierre@1
|
339 return array('manage active image' => TRUE); |
pierre@0
|
340 } |
pierre@0
|
341 |
pierre@0
|
342 case 'check_install': |
pierre@0
|
343 if (!module_exists('upload')) { |
pierre@0
|
344 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
|
345 } |
pierre@0
|
346 if (is_object($node) && !variable_get("upload_$node->type", TRUE)) { |
pierre@0
|
347 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
|
348 } |
pierre@0
|
349 if (empty($node)) { |
pierre@0
|
350 if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) { |
pierre@0
|
351 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
|
352 } |
pierre@0
|
353 } |
pierre@0
|
354 |
pierre@0
|
355 break; |
pierre@0
|
356 |
pierre@0
|
357 } |
pierre@0
|
358 |
pierre@0
|
359 return $output; |
pierre@0
|
360 } |
pierre@0
|
361 |
pierre@0
|
362 /** |
pierre@0
|
363 * Determine the currently active ad. |
pierre@0
|
364 */ |
pierre@0
|
365 function ad_image_active_file($files = array()) { |
pierre@0
|
366 if (is_array($files)) { |
pierre@0
|
367 foreach ($files as $fid => $data) { |
pierre@0
|
368 if (is_array($data)) { |
pierre@0
|
369 if ($data['list'] && !$data['remove']) { |
pierre@0
|
370 return $fid; |
pierre@0
|
371 } |
pierre@0
|
372 } |
pierre@0
|
373 else if ($data->list && !isset($data->remove)) { |
pierre@0
|
374 return $fid; |
pierre@0
|
375 } |
pierre@0
|
376 } |
pierre@0
|
377 } |
pierre@0
|
378 return 0; |
pierre@0
|
379 } |
pierre@0
|
380 |
pierre@0
|
381 /** |
pierre@0
|
382 * Loads image format object from DB. |
pierre@0
|
383 */ |
pierre@0
|
384 function ad_image_format_load($gid) { |
pierre@0
|
385 static $format; |
pierre@0
|
386 if (isset($format[$gid])) { |
pierre@0
|
387 return $format[$gid]; |
pierre@0
|
388 } |
sly@2
|
389 $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
|
390 return $format[$gid]; |
pierre@0
|
391 } |
pierre@0
|
392 |
pierre@0
|
393 /** |
pierre@0
|
394 * Validate that the size of the uploaded image is within the defined limits. |
pierre@0
|
395 */ |
pierre@0
|
396 function ad_image_validate_size($file, $nid) { |
pierre@0
|
397 $size = NULL; |
pierre@0
|
398 $error = FALSE; |
pierre@0
|
399 $edit = isset($_POST['edit']) ? $_POST['edit'] : array(); |
pierre@0
|
400 if (is_object($file)) { |
pierre@0
|
401 // TODO: Detect if new terms have been set, and if so validate against |
pierre@0
|
402 // them, not the old ones. See what's in $edit['taxonomy']. |
pierre@0
|
403 $node = node_load($nid); |
pierre@0
|
404 $terms = module_invoke('taxonomy', 'node_get_terms', $node); |
pierre@0
|
405 if (count($terms) == 0) { |
pierre@0
|
406 // We need at least a single (NULL) term to be ensure we still get the |
pierre@0
|
407 // default image size. |
pierre@0
|
408 $terms[] = NULL; |
pierre@0
|
409 } |
pierre@0
|
410 foreach ($terms as $tid => $term) { |
pierre@0
|
411 list($size->width, $size->height) = getimagesize($file->filepath); |
sly@2
|
412 $size->bytes = strlen(join('', file($file->filepath))); |
pierre@0
|
413 if ($format = ad_image_format_load($tid)) { |
pierre@0
|
414 if ($size->width < $format->min_width) { |
sly@2
|
415 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
|
416 $error = TRUE; |
pierre@0
|
417 } |
pierre@0
|
418 else if ($format->max_width && ($size->width > $format->max_width)) { |
sly@2
|
419 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
|
420 $error = TRUE; |
pierre@0
|
421 } |
pierre@0
|
422 if ($size->height < $format->min_height) { |
sly@2
|
423 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
|
424 $error = TRUE; |
pierre@0
|
425 } |
pierre@0
|
426 else if ($format->max_height && $size->height > $format->max_height) { |
sly@2
|
427 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
|
428 $error = TRUE; |
sly@2
|
429 } |
sly@2
|
430 if ($format->max_size && $size->bytes > $format->max_size) { |
sly@2
|
431 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
|
432 $error = TRUE; |
pierre@0
|
433 } |
pierre@0
|
434 } |
pierre@0
|
435 } |
pierre@0
|
436 } |
pierre@0
|
437 else { |
pierre@0
|
438 $error = TRUE; |
pierre@0
|
439 drupal_set_message('Please report error: $file is not an object, bug #146147.'); |
pierre@0
|
440 } |
pierre@0
|
441 if ($error) { |
pierre@0
|
442 return FALSE; |
pierre@0
|
443 } |
pierre@0
|
444 else { |
pierre@0
|
445 return $size; |
pierre@0
|
446 } |
pierre@0
|
447 } |
pierre@0
|
448 |
pierre@0
|
449 /** |
pierre@0
|
450 * Returns image object from given ad node. |
pierre@0
|
451 */ |
pierre@0
|
452 function ad_image_load_image($node) { |
sly@2
|
453 if (isset($node->remote_image) && !empty($node->remote_image)) { |
sly@2
|
454 $file->filename = $node->remote_image; |
sly@2
|
455 $file->filepath = $node->remote_image; |
sly@2
|
456 $image = ad_image_validate_size($file, $node->nid); |
sly@2
|
457 if ($image !== FALSE) { |
sly@2
|
458 return $image; |
sly@2
|
459 } |
sly@2
|
460 } |
sly@2
|
461 else if (isset($node->files) && is_array($node->files)) { |
pierre@0
|
462 foreach ($node->files as $file) { |
pierre@0
|
463 if (is_array($file)) { |
pierre@0
|
464 if ($file['list'] && file_exists($file['filepath'])) { |
pierre@0
|
465 $image = ad_image_validate_size((object)$file, $node->nid); |
pierre@0
|
466 if ($image !== FALSE) { |
pierre@0
|
467 $image->fid = $file['fid']; |
pierre@0
|
468 return $image; |
pierre@0
|
469 } |
pierre@0
|
470 } |
pierre@0
|
471 } |
pierre@0
|
472 else { |
pierre@0
|
473 if ($file->list && file_exists($file->filepath)) { |
pierre@0
|
474 $image = ad_image_validate_size($file, $node->nid); |
pierre@0
|
475 if ($image !== FALSE) { |
pierre@0
|
476 $image->fid = $file->fid; |
pierre@0
|
477 return $image; |
pierre@0
|
478 } |
pierre@0
|
479 } |
pierre@0
|
480 } |
pierre@0
|
481 } |
pierre@0
|
482 } |
pierre@0
|
483 return FALSE; |
pierre@0
|
484 } |
pierre@0
|
485 |
pierre@0
|
486 /** |
pierre@0
|
487 * Adapi helper function for displaying a node form. |
pierre@0
|
488 */ |
pierre@0
|
489 function ad_image_node_form(&$node) { |
pierre@0
|
490 $form = array(); |
pierre@0
|
491 |
pierre@0
|
492 ad_image_adapi('check_install', $node); |
pierre@0
|
493 |
pierre@0
|
494 $form['ad_image'] = array( |
pierre@0
|
495 '#type' => 'fieldset', |
pierre@0
|
496 '#title' => t('Image'), |
pierre@0
|
497 '#collapsible' => TRUE, |
pierre@0
|
498 ); |
pierre@0
|
499 |
pierre@1
|
500 if (!empty($node->remote_image)) { |
sly@2
|
501 $file->filename = $node->remote_image; |
sly@2
|
502 $file->filepath = $node->remote_image; |
sly@2
|
503 $image = ad_image_validate_size($file, $node->nid); |
pierre@1
|
504 $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> '; |
sly@2
|
505 if ($image === FALSE) { |
sly@2
|
506 $path .= t('(invalid image)'). '<br />'; |
sly@2
|
507 } |
pierre@1
|
508 } |
pierre@1
|
509 else if (isset($node->files)) { |
pierre@0
|
510 $files = $node->files; |
pierre@0
|
511 } |
pierre@0
|
512 else { |
pierre@0
|
513 if (!isset($node->vid)) { |
pierre@0
|
514 $node->vid = ''; |
pierre@0
|
515 } |
pierre@0
|
516 $files = module_invoke('upload', 'load', $node); |
pierre@0
|
517 } |
pierre@1
|
518 $num = isset($files) ? sizeof($files) : 0; |
pierre@0
|
519 |
pierre@0
|
520 if ($num) { |
pierre@1
|
521 $path = NULL; |
pierre@1
|
522 $active = 0; |
pierre@0
|
523 foreach ($files as $file) { |
pierre@0
|
524 if ($file->list && file_exists($file->filepath)) { |
pierre@0
|
525 $path .= '<img src="'. file_create_url($file->filepath) .'" alt="'. check_plain($file->filename) .'" /> '; |
pierre@0
|
526 $image = ad_image_validate_size($file, $node->nid); |
pierre@0
|
527 if ($image === FALSE) { |
pierre@0
|
528 $path .= t('(invalid image)'). '<br />'; |
pierre@0
|
529 } |
pierre@0
|
530 else if (!$active++) { |
pierre@0
|
531 $path .= t('(active)'). '<br />'; |
pierre@0
|
532 } |
pierre@0
|
533 else { |
pierre@0
|
534 $path .= t('(inactive)'). '<br />'; |
pierre@0
|
535 } |
pierre@0
|
536 } |
pierre@0
|
537 else if (!file_exists($file->filepath)) { |
pierre@0
|
538 drupal_set_message(t('Unable to locate image %image.', array('%image' => "$file->filepath"))); |
pierre@0
|
539 $path .= t('Unable to locate the uploaded image.'); |
pierre@0
|
540 } |
pierre@0
|
541 } |
pierre@0
|
542 } |
pierre@1
|
543 if (!isset($path) || $path == NULL) { |
pierre@0
|
544 $path = t('No images have been uploaded. Please upload an image via the <em>File attachments</em> form section below.<br />'); |
pierre@0
|
545 // Only set error if node has been previewed or submitted. |
pierre@0
|
546 if (isset($_POST['edit'])) { |
pierre@0
|
547 form_set_error('upload', t('It is required that you upload an image for your image advertisement.')); |
pierre@0
|
548 } |
pierre@0
|
549 } |
pierre@1
|
550 else if ($num) { |
pierre@1
|
551 $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
|
552 } |
pierre@0
|
553 |
pierre@0
|
554 $form['ad_image']['image'] = array( |
pierre@0
|
555 '#type' => 'markup', |
pierre@0
|
556 '#value' => $path, |
pierre@0
|
557 '#prefix' => '<div class="container-inline">', |
pierre@0
|
558 '#suffix' => '</div>', |
pierre@0
|
559 ); |
pierre@0
|
560 |
pierre@0
|
561 $form['ad_image']['url'] = array( |
pierre@0
|
562 '#type' => 'textfield', |
pierre@0
|
563 '#title' => t('Destination URL'), |
pierre@0
|
564 '#required' => FALSE, |
pierre@0
|
565 '#default_value' => isset($node->url) ? $node->url : '', |
pierre@0
|
566 '#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
|
567 ); |
pierre@0
|
568 |
pierre@0
|
569 $form['ad_image']['tooltip'] = array( |
pierre@0
|
570 '#type' => 'textfield', |
pierre@0
|
571 '#title' => t('Mouseover'), |
pierre@0
|
572 '#required' => FALSE, |
pierre@0
|
573 '#default_value' => isset($node->tooltip) ? $node->tooltip : '', |
pierre@0
|
574 '#description' => t('Optionally enter text to appear when a mouse pointer hovers over the ad image.'), |
pierre@0
|
575 ); |
pierre@0
|
576 |
pierre@1
|
577 if (variable_get('ad_image_remote_images', FALSE)) { |
pierre@1
|
578 $form['ad_image']['remote_image'] = array( |
pierre@1
|
579 '#type' => 'textfield', |
pierre@1
|
580 '#title' => t('Remote image path'), |
pierre@1
|
581 '#required' => FALSE, |
pierre@1
|
582 '#default_value' => isset($node->remote_image) ? $node->remote_image : '', |
pierre@1
|
583 '#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
|
584 ); |
pierre@1
|
585 } |
pierre@1
|
586 |
pierre@0
|
587 return $form; |
pierre@0
|
588 } |
pierre@0
|
589 |
pierre@0
|
590 /** |
pierre@0
|
591 * Adapi helper function for displaying ad itself. |
pierre@0
|
592 */ |
pierre@0
|
593 function ad_image_node_view(&$node) { |
pierre@0
|
594 $node->content['ad'] = array( |
pierre@0
|
595 '#value' => preg_replace('&@HOSTID___&', '0', ad_image_display_ad($node)), |
pierre@0
|
596 '#weight' => -1, |
pierre@0
|
597 ); |
pierre@0
|
598 if (!empty($node->url)) { |
pierre@0
|
599 $link = t('Links to !url.', array('!url' => $node->url)); |
pierre@0
|
600 $link = check_plain($link, $node->format, FALSE); |
pierre@0
|
601 $node->content['ad-link'] = array( |
pierre@0
|
602 '#value' => "<div class=\"links-to\">$link</div>", |
pierre@0
|
603 '#weight' => 0, |
pierre@0
|
604 ); |
pierre@0
|
605 } |
pierre@0
|
606 } |