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