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