comparison popups.module @ 1:4215c43e74eb

Popups: Mise à jour en version alpha6
author Franck Deroche <franck@defr.org>
date Fri, 31 Dec 2010 13:44:00 +0100
parents 76f9b43738f2
children 5efa741592f9
comparison
equal deleted inserted replaced
0:76f9b43738f2 1:4215c43e74eb
1 <?php 1 <?php
2 // $Id: popups.module,v 1.11.8.10 2009/03/21 00:57:15 starbow Exp $ 2 // $Id: popups.module,v 1.11.8.14 2010/12/10 19:58:10 drewish Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * This module provides a hook_popups for links to be openned in an Ajax Popup Modal Dialog. 6 * This module provides a hook_popups for links to be openned in an Ajax Popup Modal Dialog.
7 */ 7 */
8 8
9 9
10 // ************************************************************************** 10 // **************************************************************************
11 // CORE HOOK FUNCTIONS **************************************************** 11 // CORE HOOK FUNCTIONS ****************************************************
14 /** 14 /**
15 * Implementation of hook_menu(). 15 * Implementation of hook_menu().
16 * 16 *
17 * @return array of new menu items. 17 * @return array of new menu items.
18 */ 18 */
19 function popups_menu() { 19 function popups_menu() {
20 20
21 // Admin Settings. 21 // Admin Settings.
22 $items['admin/settings/popups'] = array( 22 $items['admin/settings/popups'] = array(
23 'page callback' => 'drupal_get_form', 23 'page callback' => 'drupal_get_form',
24 'page arguments' => array('popups_admin_settings'), 24 'page arguments' => array('popups_admin_settings'),
25 'title' => 'Popups', 25 'title' => 'Popups',
26 'access arguments' => array('administer site configuration'), 26 'access arguments' => array('administer site configuration'),
27 'description' => 'Configure the page-in-a-dialog behavior.', 27 'description' => 'Configure the page-in-a-dialog behavior.',
28 ); 28 );
29 29
30 return $items; 30 return $items;
31 } 31 }
32 32
33 /** 33 /**
34 * Implementation of hook_init(). 34 * Implementation of hook_init().
35 * 35 *
36 * Look at the page path and see if popup behavior has been requested for any links in this page. 36 * Look at the page path and see if popup behavior has been requested for any links in this page.
37 */ 37 */
38 function popups_init() { 38 function popups_init() {
39 $popups = popups_get_popups(); 39 $popups = popups_get_popups();
40 40
41 if (variable_get('popups_always_scan', 0)) { 41 if (variable_get('popups_always_scan', 0)) {
42 popups_add_popups(); 42 popups_add_popups();
43 } 43 }
47 popups_add_popups($popup_config); 47 popups_add_popups($popup_config);
48 } 48 }
49 elseif (strpos($path, '*') !== FALSE && drupal_match_path($_GET['q'], $path)) { 49 elseif (strpos($path, '*') !== FALSE && drupal_match_path($_GET['q'], $path)) {
50 popups_add_popups($popup_config); 50 popups_add_popups($popup_config);
51 } 51 }
52 } 52 }
53 53
54 $render_mode = ''; 54 $render_mode = '';
55 if (isset($_SERVER['HTTP_X_DRUPAL_RENDER_MODE'])) { 55 if (isset($_SERVER['HTTP_X_DRUPAL_RENDER_MODE'])) {
56 $render_mode = $_SERVER['HTTP_X_DRUPAL_RENDER_MODE']; 56 $render_mode = $_SERVER['HTTP_X_DRUPAL_RENDER_MODE'];
57 } 57 }
58 58
59 // Check and see if the page_override param is in the URL. 59 // Check and see if the page_override param is in the URL.
60 // Note - the magic happens here. 60 // Note - the magic happens here.
61 // Need to cache the page_override flag in the session, so it will effect 61 // Need to cache the page_override flag in the session, so it will effect
62 // the results page that follows a form submission. 62 // the results page that follows a form submission.
63 if ($render_mode == 'json/popups') { 63 if ($render_mode == 'json/popups') {
66 66
67 // Move the page_override flag back out of the session. 67 // Move the page_override flag back out of the session.
68 if (isset($_SESSION['page_override'])) { 68 if (isset($_SESSION['page_override'])) {
69 // This call will not return on form submission. 69 // This call will not return on form submission.
70 $content = menu_execute_active_handler(); 70 $content = menu_execute_active_handler();
71 71
72 // The call did return, so it wasn't a form request, 72 // The call did return, so it wasn't a form request,
73 // so we are returning a result, so clear the session flag. 73 // so we are returning a result, so clear the session flag.
74 $override = $_SESSION['page_override']; 74 $override = $_SESSION['page_override'];
75 unset($_SESSION['page_override']); 75 unset($_SESSION['page_override']);
76 76
77 // Menu status constants are integers; page content is a string. 77 // Menu status constants are integers; page content is a string.
78 if (isset($content) && !is_int($content) && isset($override)) { 78 if (isset($content) && !is_int($content) && isset($override)) {
79 print popups_render_as_json($content); 79 print popups_render_as_json($content);
80 exit; // Do not continue processing request in index.html. 80 exit; // Do not continue processing request in index.html.
81 } 81 }
82 } 82 }
83 83
84 } 84 }
85 85
86 /** 86 /**
87 * Implementation of hook_form_alter(). 87 * Implementation of hook_form_alter().
88 * 88 *
89 * Look at the form_id and see if popup behavior has been requested for any links in this form. 89 * Look at the form_id and see if popup behavior has been requested for any links in this form.
90 * 90 *
91 * @param form_array $form 91 * @param form_array $form
92 * @param array $form_state 92 * @param array $form_state
93 * @param str $form_id: 93 * @param str $form_id:
94 */ 94 */
95 function popups_form_alter(&$form, $form_state, $form_id) { 95 function popups_form_alter(&$form, $form_state, $form_id) {
96 // Add popup behavior to the form if requested. 96 // Add popup behavior to the form if requested.
97 // dsm($form_id);
98 $popups = popups_get_popups(); 97 $popups = popups_get_popups();
99 if (isset($popups[$form_id])) { 98 if (isset($popups[$form_id])) {
100 popups_add_popups($popups[$form_id]); 99 popups_add_popups($popups[$form_id]);
101 } 100 }
102 101
103 // Alter the theme configuration pages, to add a per-theme-content selector. 102 // Alter the theme configuration pages, to add a per-theme-content selector.
104 $theme = arg(4); 103 $theme = arg(4);
105 if ($form_id == 'system_theme_settings' && $theme) { 104 if ($form_id == 'system_theme_settings' && $theme) {
106 $form['popups'] = array( 105 $form['popups'] = array(
107 '#type' => 'fieldset', 106 '#type' => 'fieldset',
108 '#title' => t('Popup Settings'), 107 '#title' => t('Popup Settings'),
111 $form['popups']['popups_content_selector'] = array( 110 $form['popups']['popups_content_selector'] = array(
112 '#type' => 'textfield', 111 '#type' => 'textfield',
113 '#title' => t('Content Selector'), 112 '#title' => t('Content Selector'),
114 '#default_value' => variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()), 113 '#default_value' => variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()),
115 '#description' => t("jQuery selector to define the page's content area on this theme."), 114 '#description' => t("jQuery selector to define the page's content area on this theme."),
116 ); 115 );
117 $form['popups']['popups_theme'] = array( 116 $form['popups']['popups_theme'] = array(
118 '#type' => 'hidden', 117 '#type' => 'hidden',
119 '#value' => $theme, 118 '#value' => $theme,
120 ); 119 );
121 $form['#submit'][] = 'popups_theme_settings_form_submit'; 120 $form['#submit'][] = 'popups_theme_settings_form_submit';
122 } 121 }
123 } 122 }
124 123
125 // ************************************************************************** 124 // **************************************************************************
130 * Render the page contents in a custom json wrapper. 129 * Render the page contents in a custom json wrapper.
131 * 130 *
132 * @param $content: themed html. 131 * @param $content: themed html.
133 * @return $content in a json wrapper with metadata. 132 * @return $content in a json wrapper with metadata.
134 */ 133 */
135 function popups_render_as_json($content) { 134 function popups_render_as_json($content) {
135 // Call theme_page so modules like jquery_update can do their thing. We don't
136 // really care about the mark up though.
137 $ignore = theme('page', $content);
138
136 $path = $_GET['q']; // Get current path from params. 139 $path = $_GET['q']; // Get current path from params.
137 return drupal_json(array( 140 return drupal_json(array(
138 'title' => drupal_get_title(), 141 'title' => drupal_get_title(),
139 'messages' => theme('status_messages'), 142 'messages' => theme('status_messages'),
140 'path' => $path, 143 'path' => $path,
141 'content' => $content, 144 'content' => $content,
142 'js' => popups_get_js(), 145 'js' => popups_get_js(),
143 'css' => popups_get_css(), 146 'css' => popups_get_css(),
144 )); 147 ));
145 } 148 }
146 149
147 /** 150 /**
148 * Get the added JS in a format that is readable by popups.js. 151 * Get the added JS in a format that is readable by popups.js.
149 */ 152 */
150 function popups_get_js() { 153 function popups_get_js() {
151 $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer')); 154 $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer'));
152 $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1); 155 $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
153 156
154 $popup_js = array(); 157 $popup_js = array();
155 158
156 foreach ($js as $type => $data) { 159 foreach ($js as $type => $data) {
157 if (!$data) continue; 160 if (!$data) continue;
158 switch ($type) { 161 switch ($type) {
171 } 174 }
172 break; 175 break;
173 } 176 }
174 } 177 }
175 178
176 // A special exception, never include the popups settings in the JS array. 179 unset($popup_js['core']['misc/jquery.js']);
177 // ??? 180 unset($popup_js['core']['misc/drupal.js']);
178 // unset($popup_js['setting']['popups']); 181
182 if (module_exists('jquery_update')) {
183 foreach (jquery_update_get_replacements() as $type => $replacements) {
184 foreach ($replacements as $find => $replace) {
185 if (isset($popup_js[$type][$find])) {
186 // Create a new entry for the replacement file, and unset the original one.
187 $replace = JQUERY_UPDATE_REPLACE_PATH .'/'. $replace;
188 //$popup_js[$type][$replace] = str_replace($find, $replace, $popup_js[$type][$find]);
189 unset($popup_js[$type][$find]);
190 }
191 }
192 }
193 }
179 194
180 return $popup_js; 195 return $popup_js;
181 } 196 }
182 197
183 /** 198 /**
231 246
232 247
233 /** 248 /**
234 * Define hook_popups(). 249 * Define hook_popups().
235 * Build the list of popup rules from all modules that implement hook_popups. 250 * Build the list of popup rules from all modules that implement hook_popups.
236 * 251 *
237 * Retrieves the list of popup rules from all modules that implement hook_popups. 252 * Retrieves the list of popup rules from all modules that implement hook_popups.
238 * 253 *
239 * @param $reset 254 * @param $reset
240 * (optional) If set to TRUE, forces the popup rule cache to reset. 255 * (optional) If set to TRUE, forces the popup rule cache to reset.
241 * 256 *
242 */ 257 */
243 function popups_get_popups($reset = FALSE) { 258 function popups_get_popups($reset = FALSE) {
244 static $popups = NULL; 259 static $popups = NULL;
245 if (!isset($popups) || $reset) { 260 if (!isset($popups) || $reset) {
246 // Get popups hash out of cache. 261 // Get popups hash out of cache.
247 if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) { 262 if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) {
248 $popups = $cache->data; 263 $popups = $cache->data;
249 } 264 }
250 else { 265 else {
251 // Call all hook_popups and cache results. 266 // Call all hook_popups and cache results.
252 $popups = module_invoke_all('popups'); 267 $popups = module_invoke_all('popups');
253 268
254 // Invoke hook_popups_alter() to allow altering the default popups registry. 269 // Invoke hook_popups_alter() to allow altering the default popups registry.
255 drupal_alter('popups', $popups); 270 drupal_alter('popups', $popups);
256 271
257 // Save the popup registry in the cache. 272 // Save the popup registry in the cache.
258 cache_set('popups:popups', $popups); 273 cache_set('popups:popups', $popups);
259 } 274 }
260 } 275 }
261 return $popups; 276 return $popups;
262 } 277 }
263 278
264 /** 279 /**
265 * Attach the popup behavior to the page. 280 * Attach the popup behavior to the page.
266 * 281 *
267 * The default behavoir of a popup is to open a form that will modify the original page. 282 * The default behavoir of a popup is to open a form that will modify the original page.
268 * The popup submits the form and reloads the original page with the resulting new content. 283 * The popup submits the form and reloads the original page with the resulting new content.
269 * The popup then replaces the original page's content area with the new copy of that content area. 284 * The popup then replaces the original page's content area with the new copy of that content area.
270 * 285 *
271 * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector. 286 * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector.
272 * See README.txt for a listing of the options, and popups_admin.module for examples. 287 * See README.txt for a listing of the options, and popups_admin.module for examples.
273 */ 288 */
274 function popups_add_popups($rules=NULL) { 289 function popups_add_popups($rules=NULL) {
275 static $added = FALSE; 290 static $added = FALSE;
276 $settings = array('popups' => array()); 291 $settings = array('popups' => array());
277 292
278 if (is_array($rules)) { 293 if (is_array($rules)) {
279 $settings['popups']['links'] = array(); 294 $settings['popups']['links'] = array();
280 foreach ($rules as $popup_selector => $options) { 295 foreach ($rules as $popup_selector => $options) {
281 if (is_array($options)) { 296 if (is_array($options)) {
282 $settings['popups']['links'][$popup_selector] = $options; 297 $settings['popups']['links'][$popup_selector] = $options;
283 } 298 }
284 else { 299 else {
285 $settings['popups']['links'][$options] = array(); 300 $settings['popups']['links'][$options] = array();
294 global $custom_theme; 309 global $custom_theme;
295 $theme = $custom_theme; 310 $theme = $custom_theme;
296 if (!$theme) { 311 if (!$theme) {
297 $theme = variable_get('theme_default','none'); 312 $theme = variable_get('theme_default','none');
298 } 313 }
299 314
300 drupal_add_js('misc/jquery.form.js'); 315 drupal_add_js('misc/jquery.form.js');
301 drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css'); 316 drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css');
302 drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js'); 317 drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js');
303 318
304 // Allow skinning of the popup. 319 // Allow skinning of the popup.
310 } 325 }
311 else { // Get css and js from selected skin. 326 else { // Get css and js from selected skin.
312 drupal_add_css($skins[$skin]['css']); 327 drupal_add_css($skins[$skin]['css']);
313 if (isset($skins[$skin]['js'])) { 328 if (isset($skins[$skin]['js'])) {
314 drupal_add_js($skins[$skin]['js']); 329 drupal_add_js($skins[$skin]['js']);
315 } 330 }
316 } 331 }
317 332
318 $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()); 333 $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector());
319 334
320 $settings['popups']['originalPath'] = $_GET['q']; 335 $settings['popups']['originalPath'] = $_GET['q'];
321 $settings['popups']['defaultTargetSelector'] = $default_target_selector; 336 $settings['popups']['defaultTargetSelector'] = $default_target_selector;
322 $settings['popups']['modulePath'] = drupal_get_path('module', 'popups'); 337 $settings['popups']['modulePath'] = drupal_get_path('module', 'popups');
323 // $settings['popups']['popupFinalMessage'] = variable_get('popups_popup_final_message', 1);
324 $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1); 338 $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1);
325 drupal_add_js( $settings, 'setting' ); 339 drupal_add_js( $settings, 'setting' );
326 $added = TRUE; 340 $added = TRUE;
327 } 341 }
328 } 342 }
350 return $skins; 364 return $skins;
351 } 365 }
352 366
353 /** 367 /**
354 * Implementation of hook_popups_skins. 368 * Implementation of hook_popups_skins.
355 * 369 *
356 * This hook allows other modules to create additional custom skins for the 370 * This hook allows other modules to create additional custom skins for the
357 * popups module. 371 * popups module.
358 * 372 *
359 * @return array 373 * @return array
360 * An array of key => value pairs suitable for inclusion as the #options in a 374 * An array of key => value pairs suitable for inclusion as the #options in a
361 * select or radios form element. Each key must be the location of at least a 375 * select or radios form element. Each key must be the location of at least a
362 * css file for a popups skin. Optionally can have a js index as well. Each 376 * css file for a popups skin. Optionally can have a js index as well. Each
363 * value should be the name of the skin. 377 * value should be the name of the skin.
364 */ 378 */
365 function popups_popups_skins() { 379 function popups_popups_skins() {
366 $skins = array(); 380 $skins = array();
367 $skins_directory = drupal_get_path('module', 'popups') .'/skins'; 381 $skins_directory = drupal_get_path('module', 'popups') .'/skins';
379 } 393 }
380 394
381 395
382 /** 396 /**
383 * Returns the default jQuery content selector as a string. 397 * Returns the default jQuery content selector as a string.
384 * Currently uses the selector for Garland. 398 * Currently uses the selector for Garland.
385 * Sometime in the future I will change this to '#content' or '#content-content'. 399 * Sometime in the future I will change this to '#content' or '#content-content'.
386 */ 400 */
387 function _popups_default_content_selector() { 401 function _popups_default_content_selector() {
388 return 'div.left-corner > div.clear-block:last'; // Garland in Drupal 6. 402 return 'div.left-corner > div.clear-block:last'; // Garland in Drupal 6.
389 } 403 }
396 * Form for the Popups Settings page. 410 * Form for the Popups Settings page.
397 * 411 *
398 */ 412 */
399 function popups_admin_settings() { 413 function popups_admin_settings() {
400 popups_add_popups(); 414 popups_add_popups();
401 // drupal_add_css(drupal_get_path('module', 'popups'). '/skins/blue/blue.css'); // temp
402 drupal_set_title("Popups Settings"); 415 drupal_set_title("Popups Settings");
403 $form = array(); 416 $form = array();
404 417
405 $form['popups_always_scan'] = array( 418 $form['popups_always_scan'] = array(
406 '#type' => 'checkbox', 419 '#type' => 'checkbox',
414 ); 427 );
415 428
416 // Retrieve all available skins, forcing the registry to refresh. 429 // Retrieve all available skins, forcing the registry to refresh.
417 $skins['Unskinned'] = array(); 430 $skins['Unskinned'] = array();
418 $skins += popups_skins(TRUE); 431 $skins += popups_skins(TRUE);
419 432
420 $skin_options = drupal_map_assoc(array_keys($skins)); 433 $skin_options = drupal_map_assoc(array_keys($skins));
421 $form['popups_skins'] = array( 434 $form['popups_skins'] = array(
422 '#type' => 'fieldset', 435 '#type' => 'fieldset',
423 '#title' => t('Skins'), 436 '#title' => t('Skins'),
424 '#description' => t('Choose a skin from the list. After you save, click !here to test it out.', array('!here' => l('here', 'user', array('attributes' => array('class' => 'popups'))))), 437 '#description' => t('Choose a skin from the list. After you save, click !here to test it out.', array('!here' => l('here', 'user', array('attributes' => array('class' => 'popups'))))),
430 '#title' => t('Available skins'), 443 '#title' => t('Available skins'),
431 '#default_value' => variable_get('popups_skin', 'Basic'), 444 '#default_value' => variable_get('popups_skin', 'Basic'),
432 '#options' => $skin_options, 445 '#options' => $skin_options,
433 ); 446 );
434 447
435 448
436 return system_settings_form($form); 449 return system_settings_form($form);
437 } 450 }
438 451
439 /** 452 /**
440 * popups_form_alter adds this submit handler to the theme pages. 453 * popups_form_alter adds this submit handler to the theme pages.
441 * Set a per-theme jQuery content selector that gets passed into the js settings. 454 * Set a per-theme jQuery content selector that gets passed into the js settings.
442 * 455 *
443 * @param $form 456 * @param $form
444 * @param $form_state 457 * @param $form_state
445 */ 458 */
446 function popups_theme_settings_form_submit($form, &$form_state) { 459 function popups_theme_settings_form_submit($form, &$form_state) {
447 $theme = $form_state['values']['popups_theme']; 460 $theme = $form_state['values']['popups_theme'];
448 $content_selector = $form_state['values']['popups_content_selector']; 461 $content_selector = $form_state['values']['popups_content_selector'];
449 variable_set('popups_'. $theme .'_content_selector', $content_selector); 462 variable_set('popups_'. $theme .'_content_selector', $content_selector);
450 } 463 }
451 464
465 /**
466 * Implementation of hook_preprocess_hook().
467 *
468 * When CSS or JS aggregation is enabled make a list of the CSS/JS incorporated
469 * in it so we don't re-add it when loading the popup content.
470 */
471 function popups_preprocess_page() {
472 $base_path = base_path();
473
474 if (variable_get('preprocess_css', 0)) {
475 $css_on_page = array();
476 foreach (popups_get_css() as $type => $files) {
477 foreach ($files as $path => $html) {
478 $css_on_page[$base_path . $path] = 1;
479 }
480 }
481 $settings['popups']['originalCSS'] = $css_on_page;
482 }
483
484 if (variable_get('preprocess_js', 0)) {
485 $js_on_page = array();
486 $js = popups_get_js();
487 // We don't care about settings or inline css.
488 unset($js['inline'], $js['setting']);
489 foreach ($js as $type => $files) {
490 foreach ($files as $path => $html) {
491 $js_on_page[$base_path . $path] = 1;
492 }
493 }
494 $settings['popups']['originalJS'] = $js_on_page;
495 }
496
497 if (isset($settings)) {
498 drupal_add_js($settings, 'setting');
499 }
500 }