annotate popups.module @ 7:85e281a44a62 tip

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