annotate popups.module @ 2:c076d54409cb

Popus: Version RF
author Franck Deroche <franck@defr.org>
date Fri, 31 Dec 2010 13:46:12 +0100
parents 76f9b43738f2
children
rev   line source
franck@0 1 <?php
franck@0 2 // $Id: popups.module,v 1.11.8.10 2009/03/21 00:57:15 starbow Exp $
franck@0 3
franck@0 4 /**
franck@0 5 * @file
franck@0 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@0 19 function popups_menu() {
franck@0 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@0 28 );
franck@0 29
franck@0 30 return $items;
franck@0 31 }
franck@0 32
franck@0 33 /**
franck@0 34 * Implementation of hook_init().
franck@0 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@0 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@0 52 }
franck@0 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@0 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@0 71
franck@0 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@0 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@0 79 print popups_render_as_json($content);
franck@0 80 exit; // Do not continue processing request in index.html.
franck@0 81 }
franck@0 82 }
franck@0 83
franck@0 84 }
franck@0 85
franck@0 86 /**
franck@0 87 * Implementation of hook_form_alter().
franck@0 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@0 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 // dsm($form_id);
franck@0 98 $popups = popups_get_popups();
franck@0 99 if (isset($popups[$form_id])) {
franck@0 100 popups_add_popups($popups[$form_id]);
franck@0 101 }
franck@0 102
franck@0 103 // Alter the theme configuration pages, to add a per-theme-content selector.
franck@0 104 $theme = arg(4);
franck@0 105 if ($form_id == 'system_theme_settings' && $theme) {
franck@0 106 $form['popups'] = array(
franck@0 107 '#type' => 'fieldset',
franck@0 108 '#title' => t('Popup Settings'),
franck@0 109 '#weight' => -2,
franck@0 110 );
franck@0 111 $form['popups']['popups_content_selector'] = array(
franck@0 112 '#type' => 'textfield',
franck@0 113 '#title' => t('Content Selector'),
franck@0 114 '#default_value' => variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector()),
franck@0 115 '#description' => t("jQuery selector to define the page's content area on this theme."),
franck@0 116 );
franck@0 117 $form['popups']['popups_theme'] = array(
franck@0 118 '#type' => 'hidden',
franck@0 119 '#value' => $theme,
franck@0 120 );
franck@0 121 $form['#submit'][] = 'popups_theme_settings_form_submit';
franck@0 122 }
franck@0 123 }
franck@0 124
franck@0 125 // **************************************************************************
franck@0 126 // UTILITY FUNCTIONS ******************************************************
franck@0 127 // **************************************************************************
franck@0 128
franck@0 129 /**
franck@0 130 * Render the page contents in a custom json wrapper.
franck@0 131 *
franck@0 132 * @param $content: themed html.
franck@0 133 * @return $content in a json wrapper with metadata.
franck@0 134 */
franck@0 135 function popups_render_as_json($content) {
franck@0 136 $path = $_GET['q']; // Get current path from params.
franck@0 137 return drupal_json(array(
franck@0 138 'title' => drupal_get_title(),
franck@0 139 'messages' => theme('status_messages'),
franck@0 140 'path' => $path,
franck@0 141 'content' => $content,
franck@0 142 'js' => popups_get_js(),
franck@0 143 'css' => popups_get_css(),
franck@0 144 ));
franck@0 145 }
franck@0 146
franck@0 147 /**
franck@0 148 * Get the added JS in a format that is readable by popups.js.
franck@0 149 */
franck@0 150 function popups_get_js() {
franck@0 151 $js = array_merge_recursive(drupal_add_js(), drupal_add_js(NULL, NULL, 'footer'));
franck@0 152 $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
franck@0 153
franck@0 154 $popup_js = array();
franck@0 155
franck@2 156 // Allow modules to specifically alter the JS used in a popup.
franck@2 157 drupal_alter('popups_js', $js);
franck@2 158 if (module_exists('jquery_update')) {
franck@2 159 $replacements = jquery_update_get_replacements();
franck@2 160 $path = drupal_get_path('module', 'jquery_update') .'/replace/';
franck@2 161 foreach($replacements as $type => $data) {
franck@2 162 foreach($data as $src => $dst) {
franck@2 163 $replacements[$type][$src] = $path . $dst;
franck@2 164 }
franck@2 165 }
franck@2 166 // The behavior got called, so we're pretty sure that jQuery is already
franck@2 167 // loaded.
franck@2 168 unset($js['core']['misc/jquery.js']);
franck@2 169 unset($js['core']['misc/drupal.js']);
franck@2 170 }
franck@2 171 else {
franck@2 172 $replacements = array();
franck@2 173 }
franck@2 174
franck@0 175 foreach ($js as $type => $data) {
franck@0 176 if (!$data) continue;
franck@0 177 switch ($type) {
franck@0 178 case 'setting':
franck@0 179 // Why not just array_merge_recursive($data);
franck@0 180 $popup_js['setting'] = call_user_func_array('array_merge_recursive', $data);
franck@0 181 break;
franck@0 182 case 'inline':
franck@0 183 foreach ($data as $info) {
franck@0 184 $popup_js['inline'][] = '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . '>' . $info['code'] . "</script>\n";
franck@0 185 }
franck@0 186 break;
franck@0 187 default:
franck@0 188 foreach ($data as $path => $info) {
franck@2 189 $path = isset($replacements[$type][$path]) ? $replacements[$type][$path] : $path;
franck@0 190 $popup_js[$type][$path] = '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. base_path() . $path . $query_string ."\"></script>\n";
franck@0 191 }
franck@0 192 break;
franck@0 193 }
franck@0 194 }
franck@0 195
franck@0 196 // A special exception, never include the popups settings in the JS array.
franck@0 197 // ???
franck@0 198 // unset($popup_js['setting']['popups']);
franck@0 199
franck@0 200 return $popup_js;
franck@0 201 }
franck@0 202
franck@0 203 /**
franck@0 204 * Get the added CSSS in a format that is readable by popups.js.
franck@0 205 */
franck@0 206 function popups_get_css() {
franck@0 207 $css = drupal_add_css();
franck@0 208 $popup_css = array();
franck@0 209
franck@0 210 $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);
franck@0 211
franck@0 212 // Only process styles added to "all".
franck@0 213 $media = 'all';
franck@0 214 foreach ($css[$media] as $type => $files) {
franck@0 215 if ($type == 'module') {
franck@0 216 // Setup theme overrides for module styles.
franck@0 217 $theme_styles = array();
franck@0 218 foreach (array_keys($css[$media]['theme']) as $theme_style) {
franck@0 219 $theme_styles[] = basename($theme_style);
franck@0 220 }
franck@0 221 }
franck@0 222 foreach($css[$media][$type] as $file => $preprocess) {
franck@0 223 // If the theme supplies its own style using the name of the module style, skip its inclusion.
franck@0 224 // This includes any RTL styles associated with its main LTR counterpart.
franck@0 225 if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
franck@0 226 // Unset the file to prevent its inclusion when CSS aggregation is enabled.
franck@0 227 unset($css[$media][$type][$file]);
franck@0 228 continue;
franck@0 229 }
franck@0 230 // Only include the stylesheet if it exists.
franck@0 231 if (file_exists($file)) {
franck@0 232 // 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 233 // regardless of whether preprocessing is on or off.
franck@0 234 if ($type == 'module') {
franck@0 235 $popup_css['module'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
franck@0 236 }
franck@0 237 // 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 238 // regardless of whether preprocessing is on or off.
franck@0 239 else if ($type == 'theme') {
franck@0 240 $popup_css['theme'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
franck@0 241 }
franck@0 242 else {
franck@0 243 $popup_css['unknown'][$file] = '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
franck@0 244 }
franck@0 245 }
franck@0 246 }
franck@0 247 }
franck@0 248
franck@0 249 return $popup_css;
franck@0 250 }
franck@0 251
franck@0 252
franck@0 253 /**
franck@0 254 * Define hook_popups().
franck@0 255 * Build the list of popup rules from all modules that implement hook_popups.
franck@0 256 *
franck@0 257 * Retrieves the list of popup rules from all modules that implement hook_popups.
franck@0 258 *
franck@0 259 * @param $reset
franck@0 260 * (optional) If set to TRUE, forces the popup rule cache to reset.
franck@0 261 *
franck@0 262 */
franck@0 263 function popups_get_popups($reset = FALSE) {
franck@0 264 static $popups = NULL;
franck@0 265 if (!isset($popups) || $reset) {
franck@0 266 // Get popups hash out of cache.
franck@0 267 if (!$reset && ($cache = cache_get('popups:popups')) && !empty($cache->data)) {
franck@0 268 $popups = $cache->data;
franck@0 269 }
franck@0 270 else {
franck@0 271 // Call all hook_popups and cache results.
franck@0 272 $popups = module_invoke_all('popups');
franck@0 273
franck@0 274 // Invoke hook_popups_alter() to allow altering the default popups registry.
franck@0 275 drupal_alter('popups', $popups);
franck@0 276
franck@0 277 // Save the popup registry in the cache.
franck@0 278 cache_set('popups:popups', $popups);
franck@0 279 }
franck@0 280 }
franck@0 281 return $popups;
franck@0 282 }
franck@0 283
franck@0 284 /**
franck@0 285 * Attach the popup behavior to the page.
franck@0 286 *
franck@0 287 * The default behavoir of a popup is to open a form that will modify the original page.
franck@0 288 * The popup submits the form and reloads the original page with the resulting new content.
franck@0 289 * The popup then replaces the original page's content area with the new copy of that content area.
franck@0 290 *
franck@0 291 * @param array $rules: Array of rules to apply to the page or form, keyed by jQuery link selector.
franck@0 292 * See README.txt for a listing of the options, and popups_admin.module for examples.
franck@0 293 */
franck@0 294 function popups_add_popups($rules=NULL) {
franck@0 295 static $added = FALSE;
franck@2 296 static $shouldAdd = NULL;
franck@2 297 if (is_null($shouldAdd)) {
franck@2 298 $shouldAdd = !isset($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' || $_SESSION['page_override'];
franck@2 299 }
franck@0 300 $settings = array('popups' => array());
franck@0 301
franck@0 302 if (is_array($rules)) {
franck@0 303 $settings['popups']['links'] = array();
franck@0 304 foreach ($rules as $popup_selector => $options) {
franck@0 305 if (is_array($options)) {
franck@0 306 $settings['popups']['links'][$popup_selector] = $options;
franck@0 307 }
franck@0 308 else {
franck@0 309 $settings['popups']['links'][$options] = array();
franck@0 310 }
franck@0 311 }
franck@0 312 if($added) {
franck@0 313 drupal_add_js( $settings, 'setting' );
franck@0 314 }
franck@0 315 }
franck@2 316 if (!$added && $shouldAdd) {
franck@0 317 // Determing if we are showing the default theme or a custom theme.
franck@0 318 global $custom_theme;
franck@0 319 $theme = $custom_theme;
franck@0 320 if (!$theme) {
franck@0 321 $theme = variable_get('theme_default','none');
franck@0 322 }
franck@0 323
franck@0 324 drupal_add_js('misc/jquery.form.js');
franck@0 325 drupal_add_css(drupal_get_path('module', 'popups') .'/popups.css');
franck@0 326 drupal_add_js(drupal_get_path('module', 'popups') .'/popups.js');
franck@0 327
franck@0 328 // Allow skinning of the popup.
franck@0 329 $skin = variable_get('popups_skin', 'Basic');
franck@0 330 $skins = popups_skins();
franck@0 331 if (!$skins[$skin]['css']) { // $skin == 'Unskinned'
franck@0 332 // Look in the current theme for popups-skin.js
franck@0 333 drupal_add_js(drupal_get_path('theme', $theme) . '/popups-skin.js');
franck@0 334 }
franck@0 335 else { // Get css and js from selected skin.
franck@0 336 drupal_add_css($skins[$skin]['css']);
franck@0 337 if (isset($skins[$skin]['js'])) {
franck@0 338 drupal_add_js($skins[$skin]['js']);
franck@0 339 }
franck@0 340 }
franck@0 341
franck@0 342 $default_target_selector = variable_get('popups_'. $theme .'_content_selector', _popups_default_content_selector());
franck@0 343
franck@0 344 $settings['popups']['originalPath'] = $_GET['q'];
franck@0 345 $settings['popups']['defaultTargetSelector'] = $default_target_selector;
franck@0 346 $settings['popups']['modulePath'] = drupal_get_path('module', 'popups');
franck@0 347 // $settings['popups']['popupFinalMessage'] = variable_get('popups_popup_final_message', 1);
franck@0 348 $settings['popups']['autoCloseFinalMessage'] = variable_get('popups_autoclose_final_message', 1);
franck@0 349 drupal_add_js( $settings, 'setting' );
franck@0 350 $added = TRUE;
franck@0 351 }
franck@0 352 }
franck@0 353
franck@0 354 /**
franck@0 355 * Retrieve all information from the popup skin registry.
franck@0 356 *
franck@0 357 * @param $reset
franck@0 358 * (optional) If TRUE, will force the the skin registry to reset.
franck@0 359 * @see popups_popups_skins
franck@0 360 */
franck@0 361 function popups_skins($reset = FALSE) {
franck@0 362 static $skins = array();
franck@0 363 if (empty($skins) || $reset) {
franck@0 364 if (!$reset && ($cache = cache_get('popups:skins')) && !empty($cache->data)) {
franck@0 365 $skins = $cache->data;
franck@0 366 }
franck@0 367 else {
franck@0 368 // Create the popup skin registry (hook_popups_skins) and cache it.
franck@0 369 $skins = module_invoke_all('popups_skins');
franck@0 370 ksort($skins); // Sort them alphabetically
franck@0 371 cache_set('popups:skins', $skins, 'cache', CACHE_PERMANENT);
franck@0 372 }
franck@0 373 }
franck@0 374 return $skins;
franck@0 375 }
franck@0 376
franck@0 377 /**
franck@0 378 * Implementation of hook_popups_skins.
franck@0 379 *
franck@0 380 * This hook allows other modules to create additional custom skins for the
franck@0 381 * popups module.
franck@0 382 *
franck@0 383 * @return array
franck@0 384 * An array of key => value pairs suitable for inclusion as the #options in a
franck@0 385 * select or radios form element. Each key must be the location of at least a
franck@0 386 * css file for a popups skin. Optionally can have a js index as well. Each
franck@0 387 * value should be the name of the skin.
franck@0 388 */
franck@0 389 function popups_popups_skins() {
franck@0 390 $skins = array();
franck@0 391 $skins_directory = drupal_get_path('module', 'popups') .'/skins';
franck@0 392 $files = file_scan_directory($skins_directory, '\.css$');
franck@0 393
franck@0 394 foreach ($files as $file) {
franck@0 395 $name = drupal_ucfirst($file->name);
franck@0 396 $skins[$name]['css'] = $file->filename;
franck@0 397 $js = substr_replace($file->filename, '.js', -4);
franck@0 398 if (file_exists($js)) {
franck@0 399 $skins[$name]['js'] = $js;
franck@0 400 }
franck@0 401 }
franck@0 402 return $skins;
franck@0 403 }
franck@0 404
franck@0 405
franck@0 406 /**
franck@0 407 * Returns the default jQuery content selector as a string.
franck@0 408 * Currently uses the selector for Garland.
franck@0 409 * Sometime in the future I will change this to '#content' or '#content-content'.
franck@0 410 */
franck@0 411 function _popups_default_content_selector() {
franck@0 412 return 'div.left-corner > div.clear-block:last'; // Garland in Drupal 6.
franck@0 413 }
franck@0 414
franck@0 415 // **************************************************************************
franck@0 416 // ADMIN SETTINGS *********************************************************
franck@0 417 // **************************************************************************
franck@0 418
franck@0 419 /**
franck@0 420 * Form for the Popups Settings page.
franck@0 421 *
franck@0 422 */
franck@0 423 function popups_admin_settings() {
franck@0 424 popups_add_popups();
franck@0 425 // drupal_add_css(drupal_get_path('module', 'popups'). '/skins/blue/blue.css'); // temp
franck@0 426 drupal_set_title("Popups Settings");
franck@0 427 $form = array();
franck@0 428
franck@0 429 $form['popups_always_scan'] = array(
franck@0 430 '#type' => 'checkbox',
franck@0 431 '#title' => t('Scan all pages for popup links.'),
franck@0 432 '#default_value' => variable_get('popups_always_scan', 0),
franck@0 433 );
franck@0 434 $form['popups_autoclose_final_message'] = array(
franck@0 435 '#type' => 'checkbox',
franck@0 436 '#title' => t('Automatically close final confirmation messages.'),
franck@0 437 '#default_value' => variable_get('popups_autoclose_final_message', 1),
franck@0 438 );
franck@0 439
franck@0 440 // Retrieve all available skins, forcing the registry to refresh.
franck@0 441 $skins['Unskinned'] = array();
franck@0 442 $skins += popups_skins(TRUE);
franck@0 443
franck@0 444 $skin_options = drupal_map_assoc(array_keys($skins));
franck@0 445 $form['popups_skins'] = array(
franck@0 446 '#type' => 'fieldset',
franck@0 447 '#title' => t('Skins'),
franck@0 448 '#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 449 '#collapsible' => TRUE,
franck@0 450 '#collapsed' => FALSE,
franck@0 451 );
franck@0 452 $form['popups_skins']['popups_skin'] = array(
franck@0 453 '#type' => 'radios',
franck@0 454 '#title' => t('Available skins'),
franck@0 455 '#default_value' => variable_get('popups_skin', 'Basic'),
franck@0 456 '#options' => $skin_options,
franck@0 457 );
franck@0 458
franck@0 459
franck@0 460 return system_settings_form($form);
franck@0 461 }
franck@0 462
franck@0 463 /**
franck@0 464 * popups_form_alter adds this submit handler to the theme pages.
franck@0 465 * Set a per-theme jQuery content selector that gets passed into the js settings.
franck@0 466 *
franck@0 467 * @param $form
franck@0 468 * @param $form_state
franck@0 469 */
franck@0 470 function popups_theme_settings_form_submit($form, &$form_state) {
franck@0 471 $theme = $form_state['values']['popups_theme'];
franck@0 472 $content_selector = $form_state['values']['popups_content_selector'];
franck@0 473 variable_set('popups_'. $theme .'_content_selector', $content_selector);
franck@0 474 }
franck@0 475