annotate 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
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@1 182 if (module_exists('jquery_update')) {
franck@1 183 foreach (jquery_update_get_replacements() as $type => $replacements) {
franck@1 184 foreach ($replacements as $find => $replace) {
franck@1 185 if (isset($popup_js[$type][$find])) {
franck@1 186 // Create a new entry for the replacement file, and unset the original one.
franck@1 187 $replace = JQUERY_UPDATE_REPLACE_PATH .'/'. $replace;
franck@1 188 //$popup_js[$type][$replace] = str_replace($find, $replace, $popup_js[$type][$find]);
franck@1 189 unset($popup_js[$type][$find]);
franck@1 190 }
franck@1 191 }
franck@1 192 }
franck@1 193 }
franck@0 194
franck@0 195 return $popup_js;
franck@0 196 }
franck@0 197
franck@0 198 /**
franck@0 199 * Get the added CSSS in a format that is readable by popups.js.
franck@0 200 */
franck@0 201 function popups_get_css() {
franck@0 202 $css = drupal_add_css();
franck@0 203 $popup_css = array();
franck@0 204
franck@0 205 $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
franck@0 206
franck@0 207 // Only process styles added to "all".
franck@0 208 $media = 'all';
franck@0 209 foreach ($css[$media] as $type => $files) {
franck@0 210 if ($type == 'module') {
franck@0 211 // Setup theme overrides for module styles.
franck@0 212 $theme_styles = array();
franck@0 213 foreach (array_keys($css[$media]['theme']) as $theme_style) {
franck@0 214 $theme_styles[] = basename($theme_style);
franck@0 215 }
franck@0 216 }
franck@0 217 foreach($css[$media][$type] as $file => $preprocess) {
franck@0 218 // If the theme supplies its own style using the name of the module style, skip its inclusion.
franck@0 219 // This includes any RTL styles associated with its main LTR counterpart.
franck@0 220 if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
franck@0 221 // Unset the file to prevent its inclusion when CSS aggregation is enabled.
franck@0 222 unset($css[$media][$type][$file]);
franck@0 223 continue;
franck@0 224 }
franck@0 225 // Only include the stylesheet if it exists.
franck@0 226 if (file_exists($file)) {
franck@0 227 // 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 228 // regardless of whether preprocessing is on or off.
franck@0 229 if ($type == 'module') {
franck@0 230 $popup_css['module'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
franck@0 231 }
franck@0 232 // 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 233 // regardless of whether preprocessing is on or off.
franck@0 234 else if ($type == 'theme') {
franck@0 235 $popup_css['theme'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
franck@0 236 }
franck@0 237 else {
franck@0 238 $popup_css['unknown'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
franck@0 239 }
franck@0 240 }
franck@0 241 }
franck@0 242 }
franck@0 243
franck@0 244 return $popup_css;
franck@0 245 }
franck@0 246
franck@0 247
franck@0 248 /**
franck@0 249 * Define hook_popups().
franck@0 250 * Build the list of popup rules from all modules that implement hook_popups.
franck@1 251 *
franck@0 252 * Retrieves the list of popup rules from all modules that implement hook_popups.
franck@0 253 *
franck@0 254 * @param $reset
franck@0 255 * (optional) If set to TRUE, forces the popup rule cache to reset.
franck@1 256 *
franck@0 257 */
franck@0 258 function popups_get_popups($reset = FALSE) {
franck@0 259 static $popups = NULL;
franck@0 260 if (!isset($popups) || $reset) {
franck@0 261 // Get popups hash out of cache.
franck@0 262 if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) {
franck@0 263 $popups = $cache->data;
franck@0 264 }
franck@1 265 else {
franck@0 266 // Call all hook_popups and cache results.
franck@0 267 $popups = module_invoke_all('popups');
franck@1 268
franck@0 269 // Invoke hook_popups_alter() to allow altering the default popups registry.
franck@0 270 drupal_alter('popups', $popups);
franck@0 271
franck@1 272 // Save the popup registry in the cache.
franck@0 273 cache_set('popups:popups', $popups);
franck@1 274 }
franck@0 275 }
franck@0 276 return $popups;
franck@0 277 }
franck@0 278
franck@0 279 /**
franck@0 280 * Attach the popup behavior to the page.
franck@1 281 *
franck@1 282 * The default behavoir of a popup is to open a form that will modify the original page.
franck@1 283 * The popup submits the form and reloads the original page with the resulting new content.
franck@0 284 * The popup then replaces the original page's content area with the new copy of that content area.
franck@0 285 *
franck@0 286 * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector.
franck@1 287 * See README.txt for a listing of the options, and popups_admin.module for examples.
franck@0 288 */
franck@1 289 function popups_add_popups($rules=NULL) {
franck@0 290 static $added = FALSE;
franck@0 291 $settings = array('popups' => array());
franck@1 292
franck@0 293 if (is_array($rules)) {
franck@0 294 $settings['popups']['links'] = array();
franck@1 295 foreach ($rules as $popup_selector => $options) {
franck@0 296 if (is_array($options)) {
franck@0 297 $settings['popups']['links'][$popup_selector] = $options;
franck@0 298 }
franck@0 299 else {
franck@0 300 $settings['popups']['links'][$options] = array();
franck@0 301 }
franck@0 302 }
franck@0 303 if($added) {
franck@0 304 drupal_add_js( $settings, 'setting' );
franck@0 305 }
franck@0 306 }
franck@0 307 if (!$added) {
franck@0 308 // Determing if we are showing the default theme or a custom theme.
franck@0 309 global $custom_theme;
franck@0 310 $theme = $custom_theme;
franck@0 311 if (!$theme) {
franck@0 312 $theme = variable_get('theme_default','none');
franck@0 313 }
franck@1 314
franck@0 315 drupal_add_js('misc/jquery.form.js');
franck@0 316 drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css');
franck@0 317 drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js');
franck@0 318
franck@0 319 // Allow skinning of the popup.
franck@0 320 $skin = variable_get('popups_skin', 'Basic');
franck@0 321 $skins = popups_skins();
franck@0 322 if (!$skins[$skin]['css']) { // $skin == 'Unskinned'
franck@0 323 // Look in the current theme for popups-skin.js
franck@0 324 drupal_add_js(drupal_get_path('theme', $theme) . '/popups-skin.js');
franck@0 325 }
franck@0 326 else { // Get css and js from selected skin.
franck@0 327 drupal_add_css($skins[$skin]['css']);
franck@0 328 if (isset($skins[$skin]['js'])) {
franck@0 329 drupal_add_js($skins[$skin]['js']);
franck@1 330 }
franck@0 331 }
franck@1 332
franck@0 333 $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector());
franck@1 334
franck@0 335 $settings['popups']['originalPath'] = $_GET['q'];
franck@0 336 $settings['popups']['defaultTargetSelector'] = $default_target_selector;
franck@0 337 $settings['popups']['modulePath'] = drupal_get_path('module', 'popups');
franck@0 338 $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1);
franck@0 339 drupal_add_js( $settings, 'setting' );
franck@0 340 $added = TRUE;
franck@0 341 }
franck@0 342 }
franck@0 343
franck@0 344 /**
franck@0 345 * Retrieve all information from the popup skin registry.
franck@0 346 *
franck@0 347 * @param $reset
franck@0 348 * (optional) If TRUE, will force the the skin registry to reset.
franck@0 349 * @see popups_popups_skins
franck@0 350 */
franck@0 351 function popups_skins($reset = FALSE) {
franck@0 352 static $skins = array();
franck@0 353 if (empty($skins) || $reset) {
franck@0 354 if (!$reset && ($cache = cache_get('popups:skins')) && !empty($cache->data)) {
franck@0 355 $skins = $cache->data;
franck@0 356 }
franck@0 357 else {
franck@0 358 // Create the popup skin registry (hook_popups_skins) and cache it.
franck@0 359 $skins = module_invoke_all('popups_skins');
franck@0 360 ksort($skins); // Sort them alphabetically
franck@0 361 cache_set('popups:skins', $skins, 'cache', CACHE_PERMANENT);
franck@0 362 }
franck@0 363 }
franck@0 364 return $skins;
franck@0 365 }
franck@0 366
franck@0 367 /**
franck@0 368 * Implementation of hook_popups_skins.
franck@1 369 *
franck@0 370 * This hook allows other modules to create additional custom skins for the
franck@0 371 * popups module.
franck@1 372 *
franck@0 373 * @return array
franck@0 374 * An array of key => value pairs suitable for inclusion as the #options in a
franck@1 375 * select or radios form element. Each key must be the location of at least a
franck@1 376 * css file for a popups skin. Optionally can have a js index as well. Each
franck@0 377 * value should be the name of the skin.
franck@0 378 */
franck@0 379 function popups_popups_skins() {
franck@0 380 $skins = array();
franck@0 381 $skins_directory = drupal_get_path('module', 'popups') .'/skins';
franck@0 382 $files = file_scan_directory($skins_directory, '\.css$');
franck@0 383
franck@0 384 foreach ($files as $file) {
franck@0 385 $name = drupal_ucfirst($file->name);
franck@0 386 $skins[$name]['css'] = $file->filename;
franck@0 387 $js = substr_replace($file->filename, '.js', -4);
franck@0 388 if (file_exists($js)) {
franck@0 389 $skins[$name]['js'] = $js;
franck@0 390 }
franck@0 391 }
franck@0 392 return $skins;
franck@0 393 }
franck@0 394
franck@0 395
franck@0 396 /**
franck@0 397 * Returns the default jQuery content selector as a string.
franck@1 398 * Currently uses the selector for Garland.
franck@0 399 * Sometime in the future I will change this to '#content' or '#content-content'.
franck@0 400 */
franck@0 401 function _popups_default_content_selector() {
franck@0 402 return 'div.left-corner > div.clear-block:last'; // Garland in Drupal 6.
franck@0 403 }
franck@0 404
franck@0 405 // **************************************************************************
franck@0 406 // ADMIN SETTINGS *********************************************************
franck@0 407 // **************************************************************************
franck@0 408
franck@0 409 /**
franck@0 410 * Form for the Popups Settings page.
franck@0 411 *
franck@0 412 */
franck@0 413 function popups_admin_settings() {
franck@0 414 popups_add_popups();
franck@0 415 drupal_set_title("Popups Settings");
franck@0 416 $form = array();
franck@0 417
franck@0 418 $form['popups_always_scan'] = array(
franck@0 419 '#type' => 'checkbox',
franck@0 420 '#title' => t('Scan all pages for popup links.'),
franck@0 421 '#default_value' => variable_get('popups_always_scan', 0),
franck@0 422 );
franck@0 423 $form['popups_autoclose_final_message'] = array(
franck@0 424 '#type' => 'checkbox',
franck@0 425 '#title' => t('Automatically close final confirmation messages.'),
franck@0 426 '#default_value' => variable_get('popups_autoclose_final_message', 1),
franck@0 427 );
franck@0 428
franck@0 429 // Retrieve all available skins, forcing the registry to refresh.
franck@0 430 $skins['Unskinned'] = array();
franck@0 431 $skins += popups_skins(TRUE);
franck@1 432
franck@0 433 $skin_options = drupal_map_assoc(array_keys($skins));
franck@0 434 $form['popups_skins'] = array(
franck@0 435 '#type' => 'fieldset',
franck@0 436 '#title' => t('Skins'),
franck@0 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'))))),
franck@0 438 '#collapsible' => TRUE,
franck@0 439 '#collapsed' => FALSE,
franck@0 440 );
franck@0 441 $form['popups_skins']['popups_skin'] = array(
franck@0 442 '#type' => 'radios',
franck@0 443 '#title' => t('Available skins'),
franck@0 444 '#default_value' => variable_get('popups_skin', 'Basic'),
franck@0 445 '#options' => $skin_options,
franck@0 446 );
franck@0 447
franck@1 448
franck@0 449 return system_settings_form($form);
franck@0 450 }
franck@0 451
franck@0 452 /**
franck@0 453 * popups_form_alter adds this submit handler to the theme pages.
franck@1 454 * Set a per-theme jQuery content selector that gets passed into the js settings.
franck@0 455 *
franck@0 456 * @param $form
franck@0 457 * @param $form_state
franck@0 458 */
franck@0 459 function popups_theme_settings_form_submit($form, &$form_state) {
franck@0 460 $theme = $form_state['values']['popups_theme'];
franck@0 461 $content_selector = $form_state['values']['popups_content_selector'];
franck@0 462 variable_set('popups_'. $theme .'_content_selector', $content_selector);
franck@0 463 }
franck@0 464
franck@1 465 /**
franck@1 466 * Implementation of hook_preprocess_hook().
franck@1 467 *
franck@1 468 * When CSS or JS aggregation is enabled make a list of the CSS/JS incorporated
franck@1 469 * in it so we don't re-add it when loading the popup content.
franck@1 470 */
franck@1 471 function popups_preprocess_page() {
franck@1 472 $base_path = base_path();
franck@1 473
franck@1 474 if (variable_get('preprocess_css', 0)) {
franck@1 475 $css_on_page = array();
franck@1 476 foreach (popups_get_css() as $type => $files) {
franck@1 477 foreach ($files as $path => $html) {
franck@1 478 $css_on_page[$base_path . $path] = 1;
franck@1 479 }
franck@1 480 }
franck@1 481 $settings['popups']['originalCSS'] = $css_on_page;
franck@1 482 }
franck@1 483
franck@1 484 if (variable_get('preprocess_js', 0)) {
franck@1 485 $js_on_page = array();
franck@1 486 $js = popups_get_js();
franck@1 487 // We don't care about settings or inline css.
franck@1 488 unset($js['inline'], $js['setting']);
franck@1 489 foreach ($js as $type => $files) {
franck@1 490 foreach ($files as $path => $html) {
franck@1 491 $js_on_page[$base_path . $path] = 1;
franck@1 492 }
franck@1 493 }
franck@1 494 $settings['popups']['originalJS'] = $js_on_page;
franck@1 495 }
franck@1 496
franck@1 497 if (isset($settings)) {
franck@1 498 drupal_add_js($settings, 'setting');
franck@1 499 }
franck@1 500 }