/[drupal]/contributions/modules/ad_flash/ad_flash.module
ViewVC logotype

Contents of /contributions/modules/ad_flash/ad_flash.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Dec 4 11:03:23 2008 UTC (11 months, 3 weeks ago) by chriscohen
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
Initial commit of ad_flash module from ad_flash code originally written by mixman and developed by the Drupal community.
1 <?php
2 // $Id: $
3
4 /**
5 * @file
6 * Enhances the ad module to support flash ads.
7 *
8 *
9 * Based on the ad_image.module by Jeremy Andrews
10 *
11 * Copyright (c) 2007
12 * Fabio Varesano <fvaresano at yahoo dot it> All rights reserved.
13 *
14 * Copyright (c) 2005-2007.
15 * Jeremy Andrews <jeremy@kerneltrap.org>. All rights reserved.
16 */
17
18 /**
19 * Function used to display the selected ad.
20 */
21 function ad_flash_display_ad($ad, $hostid = NULL) {
22 $flash = db_fetch_object(db_query('SELECT a.fid, a.url, a.width, a.height, f.filepath FROM {ad_flash} a INNER JOIN {files} f ON a.fid = f.fid WHERE a.aid = %d AND f.nid = %d', $ad->aid, $ad->aid));
23 // Strip module path from flash path if serving flash files from adserve.php.
24 $flash->path = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($flash->filepath));
25
26 return theme('ad_flash_render', $ad, $flash);
27 }
28
29
30 /**
31 * Drupal _init hook. Include ad_flash_views.inc if the views.module is
32 * enabled.
33
34 function ad_flash_init() {
35
36 } */
37
38 /**
39 * Implementation of hook_help().
40 */
41 function ad_flash_help($path) {
42 switch ($path) {
43 case 'node/add/ad#flash':
44 $output = t('A flash based banner advertisement.');
45 break;
46 }
47 return $output;
48 }
49
50 /**
51 * Implementation of hook_menu().
52 */
53 function ad_flash_menu($may_cache) {
54 drupal_add_js(drupal_get_path('module', 'ad_flash') . '/scripts/AC_RunActiveContent.js');
55
56 $items = array();
57
58 if ($may_cache) {
59 $items[] = array(
60 'path' => 'admin/content/ad/configure/ad_flash',
61 'title' => t('Flash ads'),
62 'callback' => 'drupal_get_form',
63 'callback arguments' => array('ad_flash_group_settings'),
64 'type' => MENU_LOCAL_TASK,
65 'weight' => 3,
66 );
67 }
68 else {
69 if (function_exists('drupal_set_content')) {
70 if (module_exists('views')) {
71 include drupal_get_path('module', 'ad_flash'). '/ad_flash_views.inc';
72 }
73 }
74 }
75
76 return $items;
77 }
78
79 function ad_flash_group_settings($edit = array()) {
80 $form = array();
81
82 $groups = module_invoke('ad', 'groups_list', TRUE);
83 foreach ($groups as $tid => $group) {
84 $form["group-$tid"] = array(
85 '#type' => 'fieldset',
86 '#title' => $group->name,
87 '#collapsible' => TRUE,
88 );
89
90 $form["group-$tid"]["description-$tid"] = array(
91 '#type' => 'markup',
92 '#prefix' => '<div>',
93 '#suffix' => '</div>',
94 '#value' => theme_placeholder("$group->description"),
95 );
96
97 $format = db_fetch_object(db_query('SELECT * FROM {ad_flash_format} WHERE gid = %d', $tid));
98 $form["group-$tid"]["min-height-$tid"] = array(
99 '#type' => 'textfield',
100 '#title' => t('Minimum height'),
101 '#size' => 5,
102 '#maxlength' => 5,
103 '#default_value' => $format->min_height ? $format->min_height : 0,
104 '#description' => t('Optionally specify a minimum height in pixels for flash files in this group. To specify no minimum height, enter <em>0</em>.'),
105 );
106 $form["group-$tid"]["min-width-$tid"] = array(
107 '#type' => 'textfield',
108 '#title' => t('Minimum width'),
109 '#size' => 5,
110 '#maxlength' => 5,
111 '#default_value' => $format->min_width ? $format->min_width : 0,
112 '#description' => t('Optionally specify a minimum width in pixels for flash files in this group. To specify no minimum width, enter <em>0</em>.'),
113 );
114 $form["group-$tid"]["max-height-$tid"] = array(
115 '#type' => 'textfield',
116 '#title' => t('Maximum height'),
117 '#size' => 5,
118 '#maxlength' => 5,
119 '#default_value' => $format->max_height ? $format->max_height : 0,
120 '#description' => t('Optionally specify a maximum height in pixels for flash files in this group. To specify no maximum height, enter <em>0</em>.'),
121 );
122 $form["group-$tid"]["max-width-$tid"] = array(
123 '#type' => 'textfield',
124 '#title' => t('Maximum width'),
125 '#size' => 5,
126 '#maxlength' => 5,
127 '#default_value' => $format->max_width ? $format->max_width : 0,
128 '#description' => t('Optionally specify a maximum width in pixels for flash files in this group. To specify no maximum width, enter <em>0</em>.'),
129 );
130 }
131
132 $form['save'] = array(
133 '#type' => 'submit',
134 '#value' => t('Save'),
135 );
136
137 return $form;
138 }
139
140 /**
141 * Save min and max flash width and height values for ad groups.
142 */
143 function ad_flash_group_settings_submit($form_id, $form_values) {
144 $groups = module_invoke('ad', 'groups_list', TRUE);
145 foreach ($groups as $group) {
146 // TODO: Update the database schema, convert gid to tid.
147 $gid = db_result(db_query('SELECT gid FROM {ad_flash_format} WHERE gid = %d', $group->tid));
148 if (is_numeric($gid)) {
149 db_query("UPDATE {ad_flash_format} SET min_width = %d, max_width = %d, min_height = %d, max_height = %d WHERE gid = %d", $form_values["min-width-$group->tid"], $form_values["max-width-$group->tid"], $form_values["min-height-$group->tid"], $form_values["max-height-$group->tid"], $group->tid);
150 }
151 else {
152 db_query("INSERT INTO {ad_flash_format} (gid, min_width, max_width, min_height, max_height) VALUES (%d, %d, %d, %d, %d)", $group->tid, $form_values["min-width-$group->tid"], $form_values["max-width-$group->tid"], $form_values["min-height-$group->tid"], $form_values["max-height-$group->tid"]);
153 }
154 }
155 drupal_set_message('Flash ad global settings updated.');
156 }
157
158 /**
159 * Adapi implementation.
160 */
161 function ad_flash_adapi($op, &$node) {
162 $output = NULL;
163 switch ($op) {
164
165 case 'load':
166 return db_fetch_array(db_query("SELECT * FROM {ad_flash} WHERE aid = %d", $node['aid']));
167
168 case 'validate':
169 if (!valid_url($node->url, TRUE)) {
170 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL'))));
171 }
172 break;
173
174 case 'submit':
175
176 break;
177
178 case 'insert':
179 case 'update':
180 $flash = ad_flash_load_flash($node);
181 $file = (object) ad_flash_active_file($node->files);
182 $fid = (int) $file->fid;
183
184 // This is ugly, but as "a" comes before "u" we don't seem to be able
185 // to modify the upload module's form. Instead, we check after the fact
186 // if someone is editing flash files when they're not allowed, and if so we
187 // prevent the ad from being saved.
188 if ($op == 'update' && !ad_adaccess($node->nid, 'manage active ad')) {
189 // See if fid is changing -- it's okay if new flash files are uploaded, it's
190 // just not okay if the active fid is changed.
191 if ($fid != $flash->fid) {
192 drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error');
193 // This causes upload_save() to simply return without making any
194 // changes to the files attached to this node.
195 unset($node->files);
196 }
197 }
198 else {
199 // Check that all values are valid -- this is a kludge to work around bug #146147 until
200 // the problem is better understood.
201 if ($flash !== FALSE && $flash->type == 'swf' && (int)$flash->width != 0 && (int)$flash->height != 0 && (int)$flash->fid != 0) {
202 $node->fid = $flash->fid;
203 $node->width = $flash->width;
204 $node->height = $flash->height;
205 } else if ($flash !== FALSE && $flash->type == 'flv') {
206 $node->fid = $flash->fid;
207 $node->width = 0;
208 $node->height = 0;
209 } else {
210 $flash = FALSE;
211 }
212 }
213
214 if ($op == 'insert') {
215 db_query("INSERT INTO {ad_flash} (aid, fid, url, width, height) VALUES(%d, %d, '%s', %d, %d)", $node->nid, $fid, $node->url, $node->width, $node->height);
216 }
217 else {
218 db_query("UPDATE {ad_flash} SET fid = %d, url = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->width, $node->height, $node->nid);
219 }
220
221 // No valid flash has been uploaded, don't allow ad to be 'active'.
222 if ($flash === FALSE || !ad_flash_active_file(($node->files))) {
223 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
224 if (db_affected_rows()) {
225 drupal_set_message(t('Unable to mark ad as %active as you have not uploaded any valid flash files. Setting ad as %pending.', array('%active' => t('active'), '%pending' => t('pending'))), 'error');
226 }
227 }
228 else if (!$fid) {
229 db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
230 if (db_affected_rows()) {
231 drupal_set_message(t('Unable to mark ad as <em>active</em> until uploaded flash is validated. If you do not see any more errors, you should now be able to set your ad as <em>active</em>.'), 'error');
232 }
233 }
234 break;
235
236 case 'delete':
237 db_query('DELETE FROM {ad_flash} WHERE aid = %d', $node->nid);
238 break;
239
240 case 'form':
241 return ad_flash_node_form($node);
242
243 case 'view':
244 return ad_flash_node_view($node);
245
246 case 'redirect':
247 return db_result(db_query('SELECT url FROM {ad_flash} WHERE aid = %d', $node->nid));
248
249 case 'type':
250 return 'flash';
251
252 case 'permissions':
253 if (!isset($node->adtype) || $node->adtype == 'flash') {
254 return array(t('manage active ad'));
255 }
256
257 case 'check_install':
258 if (!module_exists('upload')) {
259 drupal_set_message(t("The required <em>upload module</em> is not enabled, you will not be able to upload flash ads. Please %enable the upload module, or %disable the ad_flash module.", array('%enable' => l('enable', 'admin/modules'), '%disable' => l('disable', 'admin/modules'))), 'error');
260 }
261 break;
262
263 }
264
265 return $output;
266 }
267
268 /**
269 * Determine the currently active ad.
270 */
271 function ad_flash_active_file($files = array()) {
272 foreach ($files as $fid => $data) {
273 if (is_array($data)) {
274 if ($data['list'] && !$data['remove']) {
275 return $data;
276 }
277 }
278 else if ($data->list && !$data->remove) {
279 return $data;
280 }
281 }
282 }
283
284 function ad_flash_format_load($gid) {
285 static $format;
286 if (!is_null($format[$gid])) {
287 return $format[$gid];
288 }
289 $format[$gid] = db_fetch_object(db_query('SELECT * FROM {ad_flash_format} WHERE gid = %d', $gid));
290 return $format[$gid];
291 }
292
293 /**
294 * Validate that the size of the uploaded flash is within the defined limits.
295 */
296 function ad_flash_validate_size($file, $gid) {
297 $size = NULL;
298 $error = FALSE;
299 if (is_object($file)) {
300 if (strstr(strtolower($file->filename), '.flv')) return (object) array('width' => 0, 'height' => 0);
301
302 $format = ad_flash_format_load($gid);
303 list($size->width, $size->height) = getimagesize($file->filepath);
304 if ($size->width < $format->min_width) {
305 drupal_set_message(t('The flash %name is only %current pixels wide, which is less than the minimum of %minimum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->width, '%minimum' => $format->min_width)), 'error');
306 $error = TRUE;
307 }
308 else if ($format->max_width && ($size->width > $format->max_width)) {
309 drupal_set_message(t('The flash %name is %current pixels wide, which is more than the maximum of %maximum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->width, '%maximum' => $format->max_width)), 'error');
310 $error = TRUE;
311 }
312 if ($size->height < $format->min_height) {
313 drupal_set_message(t('The flash %name is only %current pixels high, which is less than the minimum of %minimum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->height, '%minimum' => $format->min_height)), 'error');
314 $error = TRUE;
315 }
316 else if ($format->max_height && $size->height > $format->max_height) {
317 drupal_set_message(t('The flash %name is %current pixels high, which is more than the maximum of %maximum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->height, '%maximum' => $format->max_height)), 'error');
318 $error = TRUE;
319 }
320 }
321 if ($error) {
322 return FALSE;
323 }
324 else {
325 return $size;
326 }
327 }
328
329 /**
330 *
331 */
332 function ad_flash_update_node(&$data) {
333 }
334
335 /**
336 *
337 */
338 function ad_flash_load_flash($node) {
339 if (is_array($node->files)) {
340 foreach ($node->files as $file) {
341 if (is_array($file)) {
342 if ($file['list'] && file_exists($file['filepath'])) {
343 $flash = ad_flash_validate_size((object)$file, $node->gid);
344 if ($flash !== FALSE) {
345 $flash->fid = $file['fid'];
346 $flash->type = strtolower(pathinfo($file['filepath'], PATHINFO_EXTENSION));
347 return $flash;
348 }
349 }
350 }
351 else {
352 if ($file->list && file_exists($file->filepath)) {
353 $flash = ad_flash_validate_size($file, $node->gid);
354 if ($flash !== FALSE) {
355 $flash->fid = $file->fid;
356 $flash->type = strtolower(pathinfo($file->filepath, PATHINFO_EXTENSION));
357 return $flash;
358 }
359 }
360 }
361 }
362 }
363 return FALSE;
364 }
365
366 /**
367 * Adapi helper function for displaying a node form.
368 */
369 function ad_flash_node_form(&$node) {
370 $form = array();
371
372 ad_flash_adapi('check_install', $node);
373
374 $form['ad_flash'] = array(
375 '#type' => 'fieldset',
376 '#title' => t('Flash File'),
377 '#collapsible' => TRUE,
378 );
379
380 if (is_array($node->files)) {
381 $files = $node->files;
382 }
383 else {
384 $files = module_invoke('upload', 'load', $node);
385 }
386 $num = sizeof($files);
387
388 $path = NULL;
389 $active = 0;
390 if ($num) {
391 foreach ($files as $file) {
392 if ($file->list && file_exists($file->filepath)) {
393 list($flash->width, $flash->height) = getimagesize($file->filepath);
394 $flash->path = file_create_url($file->filepath);
395 //$path .= theme('ad_flash_render', null, $flash);
396 $flash = ad_flash_validate_size($file, $node->gid);
397 if ($flash === FALSE) {
398 $path .= t('(invalid flash file)'). '<br />';
399 }
400 else if (!$active++) {
401 $path .= t('(active)'). '<br />';
402 }
403 else {
404 $path .= t('(inactive)'). '<br />';
405 }
406 }
407 else if (!file_exists($file->filepath)) {
408 drupal_set_message(t('Unable to locate flash %flash.', array('%flash' => "$file->filepath")));
409 $path .= t('Unable to locate the uploaded flash.');
410 }
411 }
412 }
413 if ($path == NULL) {
414 $path = t('No flash files have been uploaded. Please upload an flash file via the <em>File attachments</em> form section below.<br />');
415 // Only set error if node has been previewed or submitted.
416 if (isset($_POST['edit'])) {
417 form_set_error('upload', t('It is required that you upload an flash for your flash advertisement.'));
418 }
419 }
420
421 $path .= t('<br />Only the first uploaded flash file that has <em>List</em> checked in the <em>File attachments</em> form section below will be displayed as an advertisement. The flash that will be displayed is marked as <em>active</em> above.');
422
423 $form['ad_flash']['flash'] = array(
424 '#type' => 'markup',
425 '#value' => $path,
426 '#prefix' => '<div class="container-inline">',
427 '#suffix' => '</div>',
428 );
429
430 $form['ad_flash']['flash'] = array(
431 '#type' => 'markup',
432 '#value' => $path,
433 '#prefix' => '<div class="container-inline">',
434 '#suffix' => '</div>',
435 );
436
437 $form['ad_flash']['url'] = array(
438 '#type' => 'textfield',
439 '#title' => t('Destination URL'),
440 '#required' => TRUE,
441 '#default_value' => $node->url,
442 '#description' => t('Enter the complete URL where you want people to be redirected when they click on this advertisement. The URL must begin with http:// or https://. For example, %url.', array('%url' => t('http://www.sample.org/'))),
443 );
444
445 return $form;
446 }
447
448 function ad_flash_node_view(&$node) {
449 $ad = db_fetch_object(db_query('SELECT aid, redirect FROM {ads} WHERE aid = %d', $node->nid));
450 $node->content['ad'] = array(
451 '#value' => ad_flash_display_ad($ad)
452 .'<br />'. t('Links to %url.', array('%url' => $node->url)),
453 '#weight' => -1,
454 );
455 }
456
457 function ad_flash_player_conf($settings) {
458 if (empty($settings) && !is_array($settings)) return '';
459
460 $conf = '';
461
462 foreach ($settings as $option => $value) {
463 if (!empty($conf)) $conf .= ', ';
464 $conf .= (string)$option . ': ';
465 if (is_numeric($value)) $conf .= $value;
466 else if (is_bool($value)) $conf .= $value ? 'true' : 'false';
467 else $conf .= "'{$value}'";
468 }
469
470 return $conf;
471 }
472
473 /* Themeable functions */
474
475 /**
476 * Displays a flash advertisement
477 */
478 function theme_ad_flash_render($ad, $flash) {
479 // Get the groups (terms) this ad belongs to and use the first one
480 $terms = taxonomy_node_get_terms($ad->aid);
481 $term = array_pop($terms);
482 $term = strtolower($term->name);
483
484 $flash->type = strtolower(pathinfo($flash->path, PATHINFO_EXTENSION));
485
486 //jrm addition
487 $flash_path = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($flash->filepath));
488 $target = variable_get('ad_link_target', '_self');
489
490 $output = "<div class=\"flash-advertisement flash-advertisement-{$flash->type} ad-group-{$term}\" id=\"ad-$ad->aid\">";
491
492 $output .= theme('ad_flash_' . $flash->type . '_render', $ad, $flash);
493
494 $output .= "</div>";
495
496 return $output;
497 }
498
499 function theme_ad_flash_swf_render($ad, $flash) {
500 // The .swf extensions isn't needed, because we use Adobe scripts
501 $url = str_replace('.swf', '', $flash->path);
502
503 $output = "<script type='text/javascript'>AC_FL_RunContent('codebase','http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','src','" . $url . "','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','" . $url ."','width','" . $flash->width . "','height','" . $flash->height . "','menu','false','wmode','transparent','flashvars','clickTag=" . $ad->redirect . "');</script>";
504 return $output;
505 }
506
507 function theme_ad_flash_player_settings($ad, $flash) {
508 return array(
509 'autoBuffering' => true,
510 'bufferLength' => 1,
511 'startingBufferLength' => 1,
512 'hideControls' => false,
513 'showMenu' => false,
514 'initialScale' => 'fit',
515 'hideControls' => false,
516 'linkUrl' => $flash->url
517 );
518 }
519
520 function theme_ad_flash_flv_render($ad, $flash) {
521 $player = '/' . drupal_get_path('module', 'ad_flash') . '/player/FlowPlayerWhite.swf';
522 $url = $flash->path;
523
524 if ($flash->url) {
525 $output = '<div class="info">' . l(!empty($ad->title) ? $ad->title : $flash->url, $flash->url, array('target' => '_blank')) . '</div>';
526 }
527
528 $settings = theme('ad_flash_player_settings', $ad, $flash);
529 $settings = array_merge(array('videoFile' => $url), $settings);
530 $conf = ad_flash_player_conf($settings);
531
532 $output .= "<object type=\"application/x-shockwave-flash\" data=\"{$player}\" width=\"520\" height=\"440\" id=\"FlowPlayer\"><param name=\"allowScriptAccess\" value=\"sameDomain\" /><param name=\"movie\" value=\"{$player}\" /><param name=\"quality\" value=\"high\" /><param name=\"scale\" value=\"noScale\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"flashvars\" value=\"clickTag=" . $ad->redirect . "&config={" . $conf . "}\" /></object>";
533
534 return $output;
535 }

  ViewVC Help
Powered by ViewVC 1.1.2