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

Contents of /contributions/modules/pageear/pageear.module

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


Revision 1.20 - (show annotations) (download) (as text)
Mon Nov 2 14:06:19 2009 UTC (3 weeks, 6 days ago) by manfer
Branch: MAIN
Branch point for: DRUPAL-7--2
Changes since 1.19: +44 -44 lines
File MIME type: text/x-php
Added by manfer: Added waitURL default value on update.
Changed by manfer: prepairing module for drupal 7.
1 <?php
2 // $Id: pageear.module,v 1.19 2009/10/30 13:28:39 manfer Exp $
3
4 /**
5 * @file
6 * Shows configurable peel away ad (magic corner, peelad) to your website.
7 *
8 * Acknowledgement: Some of the code and way of doing things were borrowed from corner and block modules.
9 */
10
11 define('PAGEEAR_PATH_IMAGES', 'pageturn');
12
13 /**
14 * Implementation of hook_help().
15 */
16 function pageear_help($path, $arg) {
17 switch ($path) {
18 case 'admin/modules#description':
19 return t('Add a peel away ad (magic corner, peelad) to your website.');
20 case 'admin/build/pageear':
21 $params = array('!add_new' => l(t('add pageear page'), 'admin/build/pageear/add'), '%edit' => t('edit'));
22 return t('Pageears are peelads which are shown at the top of pages - in the left or right \'corner\'. Create new pageears on the !add_new or edit existing pageears by clicking the %edit link next to each one.', $params);
23 }
24 }
25
26
27 /**
28 * Implementation of hook_init().
29 *
30 * We can't move this into pageear_footer(), because PHP-only based themes
31 * like chameleon load and output scripts and stylesheets in front of
32 * theme_closure(), so we ensure the style(s) are loaded on all pages.
33 */
34 function pageear_init() {
35
36 $num_active = variable_get('numEnabledPageears', 0);
37 variable_set('current_topleft_pageear', FALSE);
38 variable_set('current_topright_pageear', FALSE);
39
40 // Add the JS only if we have any active pageear.
41 // Test if pageear must be displayed in this node
42 // and prepare it for hook_footer.
43 if ($num_active) {
44
45 // Fetch all active pageears.
46 // TODO: cache?
47 $pageears = array();
48 $result = db_query("SELECT * FROM {pageears} WHERE status = 1 ORDER BY weight ASC, peid ASC");
49 while ($row = db_fetch_object($result)) {
50 $pageears[] = $row;
51 }
52
53 global $user;
54 global $language;
55 $rids = array_keys($user->roles);
56 $lang_name = $language->language;
57
58 $output = '';
59
60 // Pageear for the current path/role/language. If more than one are configured for this path/role/language, only first would be presented.
61 foreach ($pageears as $pageear) {
62
63 if ($pageear->peelPosition == 'topright' && variable_get('current_topright_pageear', FALSE)) {
64 continue;
65 }
66
67 if ($pageear->peelPosition == 'topleft' && variable_get('current_topleft_pageear', FALSE)) {
68 continue;
69 }
70
71 // Get the settings
72 $vis_languages = explode(',', $pageear->languages);
73 $vis_roles = explode(',', $pageear->roles);
74 $vis_vis = $pageear->visibility;
75 $vis_pages = $pageear->pages;
76
77 // Match languages
78 if (module_exists('locale') && count(array_filter($vis_languages))) {
79 $lang_enabled = in_array($lang_name, $vis_languages);
80 }
81 else {
82 $lang_enabled = TRUE;
83 }
84
85 // Match roles
86 if (count(array_filter($vis_roles))) {
87 $role_enabled = count(array_intersect($vis_roles, $rids)) ? TRUE : FALSE;
88 }
89 else {
90 $role_enabled = TRUE;
91 }
92
93 // Match path if necessary
94 if ($vis_pages) {
95 if ($vis_vis < 2) {
96 $path = drupal_get_path_alias($_GET['q']);
97 // Compare with the internal and path alias (if any).
98 $page_match = drupal_match_path($path, $vis_pages);
99 if ($path != $_GET['q']) {
100 $page_match = $page_match || drupal_match_path($_GET['q'], $vis_pages);
101 }
102 // When $vis_vis has a value of 0, the pageear is displayed on
103 // all pages except those listed in $vis_pages. When set to 1, it
104 // is displayed only on those pages listed in $vis_pages.
105 $page_match = !($vis_vis xor $page_match);
106 }
107 else {
108 $page_match = drupal_eval($vis_pages);
109 }
110 }
111 else {
112 $page_match = TRUE;
113 }
114
115 // Generate pageears if enabled for current path, role and language
116 if ($lang_enabled === TRUE && $role_enabled === TRUE && $page_match) {
117
118 // Prepare the pageear object to adjust it to pageTurn javascript variable needs
119 // and store it on persistent variable currentPageear
120 if ($pageear->peelPosition == 'topright') {
121 variable_set('current_topright_pageear', pageear_prepare($pageear));
122 }
123 else {
124 variable_set('current_topleft_pageear', pageear_prepare($pageear));
125 }
126
127 if (variable_get('current_topleft_pageear', FALSE) && variable_get('current_topright_pageear', FALSE)) {
128 // Include on page javascript files needed to make pageear work
129 $path = drupal_get_path('module', 'pageear');
130 drupal_add_js($path .'/pageturn/swfobject.js', 'module', 'header', FALSE, FALSE, TRUE);
131 drupal_add_js($path .'/pageturn/pageTurn.js', 'module', 'header', FALSE, FALSE, TRUE);
132 drupal_add_css($path .'/pageturn/pageTurn.css', 'module', 'all', TRUE);
133 return;
134 }
135
136 }
137 }
138
139 if (variable_get('current_topleft_pageear', FALSE) || variable_get('current_topright_pageear', FALSE)) {
140 // Include on page javascript files needed to make pageear work
141 $path = drupal_get_path('module', 'pageear');
142 drupal_add_js($path .'/pageturn/swfobject.js', 'module', 'header', FALSE, FALSE, TRUE);
143 drupal_add_js($path .'/pageturn/pageTurn.js', 'module', 'header', FALSE, FALSE, TRUE);
144 drupal_add_css($path .'/pageturn/pageTurn.css', 'module', 'all', TRUE);
145 }
146
147 }
148
149 }
150
151 /**
152 * Implementation of hook_perm().
153 */
154 function pageear_perm() {
155 return array('administer pageears', 'use PHP for pageear visibility');
156 }
157
158 /**
159 * Implementation of hook_menu().
160 */
161 function pageear_menu() {
162 $items = array();
163 $items['admin/build/pageear'] = array(
164 'title' => 'Pageears',
165 'description' => 'Edit pageears, how they look and where they appear on the site.',
166
167 //'page callback' => 'pageear_admin_display',
168 'page callback' => 'drupal_get_form',
169 'page arguments' => array('pageear_list_form'),
170
171 'access arguments' => array('administer pageears'),
172 'file' => 'pageear.admin.inc',
173 );
174 $items['admin/build/pageear/list'] = array(
175 'title' => 'List',
176 'type' => MENU_DEFAULT_LOCAL_TASK,
177 'weight' => -10,
178 );
179 $items['admin/build/pageear/add'] = array(
180 'title' => 'Add pageear',
181 'page callback' => 'drupal_get_form',
182 'page arguments' => array('pageear_admin_edit', NULL, 'add'),
183 'access arguments' => array('administer pageears'),
184 'type' => MENU_LOCAL_TASK,
185 'file' => 'pageear.admin.inc',
186 );
187 $items['admin/build/pageear/%pageear/edit'] = array(
188 'title' => 'Edit pageear',
189 'page callback' => 'drupal_get_form',
190 'page arguments' => array('pageear_admin_edit', 3),
191 'access arguments' => array('administer pageears'),
192 'type' => MENU_CALLBACK,
193 'file' => 'pageear.admin.inc',
194 );
195 $items['admin/build/pageear/%pageear/clone'] = array(
196 'title' => 'Clone pageear',
197 'page callback' => 'drupal_get_form',
198 'page arguments' => array('pageear_admin_edit', 3, 'clone'),
199 'access arguments' => array('administer pageears'),
200 'type' => MENU_CALLBACK,
201 'file' => 'pageear.admin.inc',
202 );
203 $items['admin/build/pageear/%pageear/delete'] = array(
204 'title' => 'Delete pageear',
205 'page callback' => 'drupal_get_form',
206 'page arguments' => array('pageear_admin_delete', 3),
207 'access arguments' => array('administer pageears'),
208 'type' => MENU_CALLBACK,
209 'file' => 'pageear.admin.inc',
210 );
211 $items['admin/build/pageear/%pageear/disable'] = array(
212 'title' => 'Disables pageear',
213 'page callback' => 'pageear_admin_disable',
214 'page arguments' => array(3),
215 'access arguments' => array('administer pageears'),
216 'type' => MENU_CALLBACK,
217 'file' => 'pageear.admin.inc',
218 );
219 $items['admin/build/pageear/%pageear/enable'] = array(
220 'title' => 'Enables pageear',
221 'page callback' => 'pageear_admin_enable',
222 'page arguments' => array(3),
223 'access arguments' => array('administer pageears'),
224 'type' => MENU_CALLBACK,
225 'file' => 'pageear.admin.inc',
226 );
227
228 return $items;
229 }
230
231 /**
232 * Implementation of hook_footer().
233 */
234 function pageear_footer($main = 0) {
235
236 $current_topleft_pageear = variable_get('current_topleft_pageear', FALSE);
237 $current_topright_pageear = variable_get('current_topright_pageear', FALSE);
238
239 if ($current_topleft_pageear || $current_topright_pageear) {
240
241 // Output pageears if there is one for current node
242 $js = "<script type=\"text/javascript\">\n";
243 $js .= "flagSwf = '". $base_url . base_path() . drupal_get_path('module', 'pageear') ."/pageturn/flag.swf';\n";
244 $js .= "peelSwf = '". $base_url . base_path() . drupal_get_path('module', 'pageear') ."/pageturn/turn.swf';\n";
245
246 if ($current_topleft_pageear) {
247 $js .= "topleft_pageearvars = {\n";
248 foreach ($current_topleft_pageear as $key => $value) {
249 $js .= $key .": escape(". drupal_to_js($value) ."),\n";
250 }
251 $js .= "};\n";
252
253 $js .= "topleft_pageear = new pageear(topleft_pageearvars);\n";
254 $js .= "topleft_pageear.write();\n";
255 }
256
257 if ($current_topright_pageear) {
258 $js .= "topright_pageearvars = {\n";
259 foreach ($current_topright_pageear as $key => $value) {
260 $js .= $key .": escape(". drupal_to_js($value) ."),\n";
261 }
262 $js .= "};\n";
263
264 $js .= "topright_pageear = new pageear(topright_pageearvars);\n";
265 $js .= "topright_pageear.write();\n";
266 }
267 $js .= "</script>\n";
268
269 return $js;
270
271 }
272
273 }
274
275 /**
276 * Prepares a pageear object prior to being use in JS.
277 *
278 * @param $pageear A pageear object
279 * @return A prepared pageear object
280 */
281 function pageear_prepare($pageear) {
282
283 // No need to carry over these values to JS
284 unset($pageear->peid);
285 unset($pageear->name);
286 unset($pageear->status);
287 unset($pageear->roles);
288 unset($pageear->visibility);
289 unset($pageear->pages);
290 unset($pageear->languages);
291 unset($pageear->weight);
292
293 // Construct the image and sound paths
294 $pageear->waitURL = base_path() . $pageear->waitURL;
295 $pageear->smallURL = base_path() . $pageear->smallURL;
296 $pageear->bigURL = base_path() . $pageear->bigURL;
297 $pageear->loadSoundURL = ($pageear->loadSoundURL == '') ? '' : base_path() . $pageear->loadSoundURL;
298 $pageear->openSoundURL = ($pageear->openSoundURL == '') ? '' : base_path() . $pageear->openSoundURL;
299 $pageear->closeSoundURL = ($pageear->closeSoundURL == '') ? '' : base_path() . $pageear->closeSoundURL;
300
301 // Convert automaticOpen, automaticClose from seconds to milliseconds
302 $pageear->automaticOpen = $pageear->automaticOpen * 1000;
303 $pageear->automaticClose = $pageear->automaticClose * 1000;
304
305 return $pageear;
306
307 }
308
309 /**
310 * Get a pageear by its id or a combination of other fields.
311 *
312 * @param $array An associative array of attributes to search for in selecting the pageear,
313 * such as pageear id (peid) or name (name).
314 * @return A pageear array if found, otherwise false.
315 */
316 //function pageear_load($array = array()) {
317 // // Dynamically compose a SQL query (similar to user.module -> user_load):
318 // $query = array();
319 // $params = array();
320 //
321 // if (is_numeric($array)) {
322 // $array = array('peid' => $array);
323 // }
324 // elseif (!is_array($array)) {
325 // return FALSE;
326 // }
327 //
328 // foreach ($array as $key => $value) {
329 // if ($key == 'peid' || $key == 'status' || $key == 'visibility' || $key == 'mirror' || $key == 'transitionDuration' || $key == 'redValue' || $key == 'greenValue' || $key == 'blueValue' || $key == 'flagSpeed' || $key == 'peelSpeed' || $key == 'automaticOpen' || $key == 'automaticClose' || $key == 'close_button_enable' || $key == 'close_redValue' || $key == 'close_greenValue' || $key == 'close_blueValue' || $key == 'flagWidth' || $key == 'flagHeight' || $key == 'peelWidth' || $key == 'peelHeight' || $key == 'linkEnabled') {
330 // $query[] = "$key = %d";
331 // $params[] = $value;
332 // }
333 // else {
334 // $query[]= "LOWER($key) = LOWER('%s')";
335 // $params[] = $value;
336 // }
337 // }
338 // // Only return first hit
339 // $pageear = db_fetch_object(db_query('SELECT * FROM {pageears} WHERE '. implode(' AND ', $query), $params));
340 // return $pageear;
341 //}
342
343 /**
344 * Loading one, more or all pageears.
345 */
346 function pageear_load($peid = NULL) {
347 static $pageears;
348
349 if (!is_array($pageears)) {
350
351 if (is_numeric($peid)) {
352 $pageear = db_fetch_object(db_query("SELECT * FROM {pageears} WHERE peid = %d", array(':peid' => $peid)));
353 return $pageear;
354
355 }
356 else {
357 $result = db_query("SELECT * FROM {pageears} ORDER BY weight ASC");
358 $pageears = array();
359
360 while ($pageear = db_fetch_object($result)) {
361 $pageears[$pageear->peid] = $pageear;
362 }
363 }
364 }
365
366 if (is_array($pageears)) {
367
368 if (is_numeric($peid)) {
369 return $pageears[$peid];
370 }
371 elseif (is_array($peid)) {
372 return array_intersect_key($pageears, array_flip($peid));
373 }
374 else {
375 return $pageears;
376 }
377 }
378 }
379
380 /**
381 * Get the default values of a new pageear
382 */
383 function pageear_get_default() {
384
385 $default = new stdClass();
386
387 $default->peid = 0;
388 $default->weight = 0;
389 $default->status = 1;
390 $default->name = '';
391 $default->flagStyle = 'style1';
392 $default->peelStyle = 'style1';
393 $default->peelPosition = 'topright';
394 $default->peelPositionModel = 'absolute';
395 $default->flagWidth = 100;
396 $default->flagHeight = 100;
397 $default->peelWidth = 500;
398 $default->peelHeight = 500;
399 $default->waitEnable = 0;
400 $default->waitURL = drupal_get_path('module', 'pageear') .'/pageturn/wait.gif';
401 $default->waitWidth = 42;
402 $default->waitHeight = 42;
403 $default->smallURL = drupal_get_path('module', 'pageear') .'/pageturn/small.jpg';
404 $default->bigURL = drupal_get_path('module', 'pageear') .'/pageturn/big.jpg';
405 $default->mirror = 1;
406 $default->inTransition = 'none';
407 $default->transitionDuration = 4;
408 $default->peelColor = 'custom';
409 $default->peelColorStyle = 'gradient';
410 $default->redValue = 255;
411 $default->greenValue = 255;
412 $default->blueValue = 255;
413 $default->linkEnabled = 1;
414 $default->linkTarget = '_blank';
415 $default->link = 'http://www.drupal.org/';
416 $default->loadSoundURL = '';
417 $default->openSoundURL = '';
418 $default->closeSoundURL = '';
419 $default->flagSpeed = 4;
420 $default->peelSpeed = 4;
421 $default->automaticOpen = 0;
422 $default->automaticClose = 0;
423 $default->close_button_enable = 0;
424 $default->text_on_close_button = 'close';
425 $default->close_redValue = 255;
426 $default->close_greenValue = 255;
427 $default->close_blueValue = 255;
428 $default->languages = '';
429 $default->roles = '';
430 $default->visibility = 0;
431 $default->pages = '';
432
433 return (object) $default;
434
435 }
436
437 /**
438 * Returns either an array of select options or, if a key is specified, the value for the specific key in the given array.
439 *
440 * @param $type
441 * @param $key A key corresponding to a specific entry in one of the options arrays
442 * @return mixed
443 */
444 function pageear_get_options($type, $key = '') {
445 switch ($type) {
446 case 'flagStyle':
447 $options = array(
448 'style1' => t('Style') .' 1',
449 'style2' => t('Style') .' 2',
450 'style3' => t('Style') .' 3',
451 );
452 break;
453 case 'peelStyle':
454 $options = array(
455 'style1' => t('Style') .' 1',
456 'style2' => t('Style') .' 2',
457 'style3' => t('Style') .' 3',
458 );
459 break;
460 case 'peelPosition':
461 $options = array(
462 'topleft' => t('Top left'),
463 'topright' => t('Top right')
464 );
465 break;
466 case 'peelPositionModel':
467 $options = array(
468 'absolute' => t('absolute'),
469 'fixed' => t('fixed')
470 );
471 break;
472 case 'inTransition':
473 $options = array(
474 'none' => t('(disabled)'),
475 'Blinds' => t('Blinds'),
476 'Fade' => t('Fade'),
477 'Fly' => t('Fly'),
478 'Iris' => t('Iris'),
479 'Photo' => t('Photo'),
480 'Rotate' => t('Rotate'),
481 'Squeeze' => t('Squeeze'),
482 'Wipe' => t('Wipe'),
483 'PixelDissolve' => t('PixelDissolve'),
484 'Zoom' => t('Zoom')
485 );
486 break;
487 case 'peelColor':
488 $options = array(
489 'golden' => t('Golden'),
490 'silver' => t('Silver'),
491 'custom' => t('Custom')
492 );
493 break;
494 case 'peelColorStyle':
495 $options = array(
496 'flat' => t('Flat'),
497 'gradient' => t('Gradient')
498 );
499 break;
500 case 'linkTarget':
501 $options = array(
502 '_self' => t('Same window'),
503 '_blank' => t('New window')
504 );
505 break;
506 default:
507 $options = array();
508 }
509
510 if ($key == '') {
511 return $options;
512 }
513 else {
514 return $options[$key];
515 }
516 }
517
518 /**
519 * Implementation of hook_theme().
520 */
521 function pageear_theme() {
522 return array(
523 'pageear_list_form' => array('arguments' => array('form' => NULL)),
524 );
525 }

  ViewVC Help
Powered by ViewVC 1.1.2