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

  ViewVC Help
Powered by ViewVC 1.1.2