forked from wojtha/ctools_automodal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathctools_automodal.module
executable file
·692 lines (600 loc) · 20.4 KB
/
ctools_automodal.module
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
<?php
/**
* Implements hook_module_implements_alter().
*/
function ctools_automodal_module_implements_alter(&$implementations, $hook) {
if ($hook == 'menu_alter') {
// Try to ensure that ctools_automodal_menu_alter() gets invoked last.
$group = $implementations['ctools_automodal'];
unset($implementations['ctools_automodal']);
$implementations['ctools_automodal'] = $group;
}
}
/**
* Implements hook_menu_alter().
*/
function ctools_automodal_menu_alter(&$items) {
$modal_paths = ctools_automodal_get_modal_paths();
$modal_regexp_pieces = array();
// Create unique node edit links for each content type
// @todo: necessary (taken from node_menu()) node_type_cache_reset();
foreach (node_type_get_types() as $type) {
$type_url_str = str_replace('_', '-', $type->type);
if (empty($items['node/%node/edit/' . $type_url_str])) {
$items['node/%node/edit/' . $type_url_str] = $items['node/%node/edit'];
$items['node/%node/edit/' . $type_url_str]['type'] = MENU_CALLBACK;
}
}
//$convert_to_regexp = function ($value) { return preg_replace('/%[^\/]*/', '[^/]+', $value); };
foreach ($modal_paths as $path) {
if (empty($items[$path])) {
continue;
}
$item = $items[$path];
if ($item['page callback'] == 'drupal_get_form') {
$items["$path/%ctools_js"] = $item;
$items["$path/%ctools_js"]['page callback'] = 'ctools_automodal_get_form';
$items["$path/%ctools_js"]['page arguments'][] = substr_count($path, '/') + 1;
$items["$path/%ctools_js"]['type'] = MENU_CALLBACK;
}
elseif ($item['page callback'] == 'node_add') {
$items["$path/%ctools_js"] = $item;
$items["$path/%ctools_js"]['page callback'] = 'ctools_automodal_node_add';
$items["$path/%ctools_js"]['page arguments'][] = substr_count($path, '/') + 1;
$items["$path/%ctools_js"]['type'] = MENU_CALLBACK;
}
elseif ($item['page callback'] == 'node_page_edit') {
$items["$path/%ctools_js"] = $item;
$items["$path/%ctools_js"]['page callback'] = 'ctools_automodal_node_edit';
$items["$path/%ctools_js"]['page arguments'][] = substr_count($path, '/') + 1;
$items["$path/%ctools_js"]['type'] = MENU_CALLBACK;
}
else {
$items["$path/%ctools_js"] = $item + array('page arguments' => array());
$items["$path/%ctools_js"]['page callback'] = 'ctools_automodal_get_page';
array_unshift($items["$path/%ctools_js"]['page arguments'], $item['page callback']);
$items["$path/%ctools_js"]['page arguments'][] = substr_count($path, '/') + 1;
$items["$path/%ctools_js"]['type'] = MENU_CALLBACK;
}
$modal_regexp_pieces[] = _ctools_automodal_menu_alter_regex($path);
$modal_paths[] = $path;
}
if (!empty($modal_paths)) {
$all_paths = array_keys($items);
$modal_regexp = '@^' . implode('|^', $modal_regexp_pieces) . '@';
$not_modal_regexp = '@^' . implode('|^', array_map('_ctools_automodal_menu_alter_regex', array_diff(preg_grep($modal_regexp, $all_paths), $modal_paths))) . '@';
}
else {
$modal_regexp = $not_modal_regexp = '';
}
variable_set('ctools_automodal_regexp', $modal_regexp);
variable_set('ctools_automodal_not_regexp', $not_modal_regexp);
}
/**
* A helper function that matches a path in ctools_automodal_menu_alter().
*/
function _ctools_automodal_menu_alter_regex($value) {
return preg_replace('/%[^\/]*/', '[^/]+', $value);
}
/**
* Implements hook_page_delivery_callback_alter()
*/
function ctools_automodal_page_delivery_callback_alter(&$callback) {
// jQuery sets a HTTP_X_REQUESTED_WITH header of 'XMLHttpRequest'.
// If a page would normally be delivered as an html page, and it is called
// from jQuery, deliver it instead as an Ajax response.
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && $callback == 'drupal_deliver_html_page') {
$path = ctools_automodal_get_system_uri();
if (ctools_automodal_is_path_modal($path)) {
$page_callback_result = ctools_automodal_get_page_callback_error($path);
if (is_int($page_callback_result)) {
ctools_automodal_deliver_error($path, $page_callback_result);
}
}
}
}
/**
* Returns URI of the request with stripped "nojs" or "ajax".
*
* @return string
*/
function ctools_automodal_get_system_uri() {
$path = drupal_parse_url(trim(request_uri(), '/'));
$path_parts = explode('/', $path['path']);
if (in_array(end($path_parts), array('nojs', 'ajax'))) {
array_pop($path_parts);
$path = implode('/', $path_parts);
}
return $path;
}
/**
* Check the menu path for access denied and not found errors.
*
* Returns MENU_NOT_FOUND, MENU_ACCESS_DENIED or NULL if no error was found.
*
* @param $path
* @return int|null
*/
function ctools_automodal_get_page_callback_error($path) {
$page_callback_result = NULL;
if ($router_item = menu_get_item($path)) {
if ($router_item['access'] == FALSE) {
$page_callback_result = MENU_ACCESS_DENIED;
}
}
else {
$page_callback_result = MENU_NOT_FOUND;
}
return $page_callback_result;
}
/**
* Package and send the result of a page callback to the browser as HTML.
*
* @param $page_callback_result
* The result of a page callback. Can be one of:
* - NULL: to indicate no content.
* - An integer menu status constant: to indicate an error condition.
* - A string of HTML content.
* - A renderable array of content.
*
* @see drupal_deliver_page()
* @see drupal_deliver_html_page()
* @see ajax_prepare_response()
*/
function ctools_automodal_deliver_error($path, $page_callback_result) {
// Menu status constants are integers; page content is a string or array.
if (is_int($page_callback_result)) {
$output = '';
switch ($page_callback_result) {
case MENU_NOT_FOUND:
drupal_set_title(t('Page not found'));
$output = t('The requested page "@path" could not be found.', array('@path' => request_uri()));
break;
case MENU_ACCESS_DENIED:
drupal_set_title(t('Access denied'));
$output = t('You are not authorized to access this page.');
break;
case MENU_SITE_OFFLINE:
drupal_set_title(t('Site maintenance'));
$output = theme('maintenance_page', array('content' => filter_xss_admin(variable_get('maintenance_mode_message',
t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))))));
break;
}
// If there is any error output render it and exit.
if ($output) {
ctools_include('modal');
ctools_include('ajax');
ctools_add_js('ajax-responder');
$commands = ctools_automodal_page_render($output);
drupal_alter('modal_error', $commands, $path, $page_callback_result);
print ajax_render($commands);
exit();
}
}
}
/**
* Check if an internal Drupal path should be converted to a modal link.
*/
function ctools_automodal_is_path_modal($path) {
$modal_regexp = variable_get('ctools_automodal_regexp');
$not_modal_regexp = variable_get('ctools_automodal_not_regexp');
if (!$modal_regexp || !$not_modal_regexp) {
return FALSE;
}
return preg_match($modal_regexp, $path) && !preg_match($not_modal_regexp, $path);
}
/**
* Implements hook_preprocess_link().
*/
function ctools_automodal_preprocess_link(&$variables) {
static $ctools_modal_included = FALSE;
if (ctools_automodal_is_path_modal($variables['path'])) {
// Only process the modal includes once per request.
if (!$ctools_modal_included) {
ctools_include('modal');
ctools_modal_add_js();
$ctools_modal_included = TRUE;
}
$options = ctools_automodal_get_modal_options($variables['path']);
if (!empty($options['style'])) {
ctools_automodal_include_style($options['style']);
$variables['options']['attributes']['class'][] = 'ctools-modal-' . $options['style'];
}
$variables['options']['attributes']['class'][] = 'ctools-use-modal';
if (strpos($variables['path'], 'nojs') === FALSE) {
$variables['path'] .= '/nojs';
}
}
}
/**
* Implements hook_preprocess_menu_local_action().
*/
function ctools_automodal_preprocess_menu_local_action(&$variables) {
// Prepare the link array in the way that the hook_preprocess_link() expects.
$link = array(
'path' => &$variables['element']['#link']['href'],
'options' => &$variables['element']['#link']['localized_options'],
'text' => &$variables['element']['#link']['title'],
);
ctools_automodal_preprocess_link($link);
}
/**
* Implemenation of hook_theme().
*/
function ctools_automodal_theme($existing, $type, $theme, $path) {
return array(
'ctools_automodal_confirmation' => array(
'variables' => array(
'messages' => NULL,
'confirmation' => NULL,
'actions' => NULL,
),
),
);
}
/**
* Return modal configuration for given path.
*
* @param string $path
* @return array|NULL
*/
function ctools_automodal_get_modal_options($path) {
$modal_options = NULL;
$ctools_modal_paths = ctools_automodal_get_all_modal_options();
// @todo: Do this with a regex?
//$router_item = menu_get_item($path);
//$path = $router_item['path'];
foreach ($ctools_modal_paths as $key => $modal_options) {
$pattern = _ctools_automodal_menu_alter_regex($key);
$pattern = str_replace(array('/'), array('\/'), $pattern);
$pattern = '/^' . $pattern . '$/';
if (preg_match($pattern, $path)) {
$modal_options += array(
'style' => '',
'confirm' => FALSE,
'close' => FALSE,
'confirm' => FALSE,
'reload' => FALSE,
'redirect' => '',
);
if ($modal_options['confirm']) {
$modal_options['confirm'] += array(
'title' => t('Confirmation'),
'text' => t('The form was submitted.'),
'callback' => 'ctools_automodal_confirmation_callback',
);
}
return $modal_options;
}
}
}
/**
* Return keyed array of modal paths and options returned by hook_modal_paths() implementations.
*
* @return array
*/
function ctools_automodal_get_all_modal_options() {
static $ctools_modal_paths = NULL;
if (is_null($ctools_modal_paths)) {
$ctools_modal_paths = module_invoke_all('modal_paths');
drupal_alter('modal_paths', $ctools_modal_paths);
}
return $ctools_modal_paths;
}
/**
* Return array of modal paths returned by hook_modal_paths() implementations.
*
* @return array
*/
function ctools_automodal_get_modal_paths() {
return array_keys(ctools_automodal_get_all_modal_options());
}
/**
* Returns CTools modal style definition.
*
* http://drupal.org/node/872072#comment-3391816
*
* @param string $style_id
* @return array|NULL
*/
function ctools_automodal_get_style($style_id) {
static $ctools_modal_styles = NULL;
if (is_null($ctools_modal_styles)) {
$ctools_modal_styles = module_invoke_all('modal_styles');
drupal_alter('modal_styles', $ctools_modal_styles);
}
return isset($ctools_modal_styles[$style_id]) ? $ctools_modal_styles[$style_id] : NULL;
}
/**
* Includes CTools modal style in the page.
*
* @param string $style_id
* @return boolean
*/
function ctools_automodal_include_style($style_id) {
static $ctools_modal_styles = array();
if (!isset($ctools_modal_styles[$style_id])) {
$style = ctools_automodal_get_style($style_id);
if ($style) {
drupal_add_js(array($style_id => $style), 'setting');
$ctools_modal_styles[$style_id] = TRUE;
}
else {
$ctools_modal_styles[$style_id] = FALSE;
}
}
return $ctools_modal_styles[$style_id];
}
/**
* Dirty, dirty function to fix the 'current path' global on modal pages.
*/
function _ctools_automodal_fix_get_q() {
$path = current_path();
// Pop off the /js or /nojs suffix to the path.
$path = substr($path, 0, strrpos($path, '/'));
// @todo Shower multiple times after modifing global variables.
$_GET['q'] = $path;
}
/**
* Returns redirect path if found.
*
* Search for the redirect path in the following order:
* 1. destination query string,
* 2. modal redirect option
* 3. form state redirect property
*
* @param $modal_options
* @param $form_state
*
* @return array
*
* @see ctools_automodal_get_form()
*/
function _ctools_automodal_get_form_redirect_path($modal_options, $form_state) {
$path = NULL;
$options = array();
if ($modal_options['redirect'] !== FALSE && ($modal_options['redirect'] || $form_state['redirect'] || !empty($_GET['destination']))) {
if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
$options = drupal_parse_url($_GET['destination']);
$path = $options['path'];
}
elseif (is_array($modal_options['redirect'])) {
list($path, $options) = $modal_options['redirect'] + array(NULL, array());
}
elseif (is_string($modal_options['redirect'])) {
$path = $modal_options['redirect'];
}
elseif (is_array($form_state['redirect'])) {
list($path, $options) = $form_state['redirect'] + array(NULL, array());
}
elseif (is_string($form_state['redirect'])) {
$path = $form_state['redirect'];
}
}
return array($path, $options);
}
/**
* Renders form confirm page.
*
* @param $modal_options
* @return array
*
* @see ctools_automodal_get_form()
*/
function _ctools_automodal_get_form_confirm_page($modal_options) {
$confirmation = call_user_func($modal_options['confirm']['callback'], $modal_options['confirm']);
$rendered_confirmation = theme('ctools_automodal_confirmation', $confirmation);
return array($modal_options['confirm']['title'], $rendered_confirmation);
}
/**
* A wrapper to make node_add pages work as a form.
*/
function ctools_automodal_node_add() {
$args = func_get_args();
global $user;
$type = $args[0];
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
'language' => LANGUAGE_NONE,
);
$output = ctools_automodal_get_form($type . '_node_form', $node, $args[1]);
return $output;
}
/**
* A wrapper to make node_edit pages work as a form.
*/
function ctools_automodal_node_edit() {
$args = func_get_args();
$node = $args[0];
$output = ctools_automodal_get_form($node->type . '_node_form', $node, $args[1]);
return $output;
}
/**
* Display a Drupal form using CTools modal or normal page display.
*/
function ctools_automodal_get_form() {
$args = func_get_args();
$form_id = array_shift($args);
$ajax = array_pop($args);
_ctools_automodal_fix_get_q();
// Load modal options.
$modal_options = ctools_automodal_get_modal_options(current_path());
if ($ajax) {
ctools_include('modal');
ctools_include('ajax');
ctools_add_js('ajax-responder');
$form_state = array(
'ajax' => $ajax,
'build_info' => array('args' => $args),
);
$commands = ctools_modal_form_wrapper($form_id, $form_state);
// Handle submitted form.
if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
// Overwrite the form wrapper output.
$commands = array();
// Add custom commands
if ($modal_options['commands_callback']) {
$commands = call_user_func_array($modal_options['commands_callback'], array($form_state));
print ajax_render($commands);
exit();
}
$commands[] = ctools_modal_command_loading();
// Close the form or display the confirmation page.
if ($modal_options['close']) {
$commands[] = ctools_modal_command_dismiss();
}
elseif ($modal_options['confirm']) {
list($title, $confirm_page) = _ctools_automodal_get_form_confirm_page($modal_options);
$commands[] = ctools_modal_command_display($title, $confirm_page);
}
// Redirect or reload current page.
list($path, $options) = _ctools_automodal_get_form_redirect_path($modal_options, $form_state);
if (!empty($path) && $modal_options['redirect'] !== FALSE) {
if ($modal_options['redirect'] === 'modal') {
// Open the redirect path in a modal
$commands = _ctools_automodal_redirect($path, $options);
}
else {
// Issue a generic page redirect
$commands[] = ctools_ajax_command_redirect($path, 0, $options);
}
}
elseif ($modal_options['reload']) {
$commands[] = ctools_ajax_command_reload();
}
}
print ajax_render($commands);
exit();
}
else {
array_unshift($args, $form_id);
return call_user_func_array('drupal_get_form', $args);
}
}
/**
* Hijack a redirect so it stays in the modal.
* A lot of code copied from menu_execute_active_handler().
* @param [type] $path [description]
* @param [type] $options [description]
* @return [type] [description]
*/
function _ctools_automodal_redirect($path, $options) {
if ($router_item = menu_get_item($path)) {
if ($router_item['access']) {
if ($router_item['include_file']) {
require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
}
// Alter response ala the hook_menu_item call above.
if ($router_item['page_callback'] == 'node_page_edit') {
$args = $router_item['page_arguments'];
$router_item['page_callback'] = 'drupal_get_form';
$router_item['page_arguments'] = array($args[0]->type . '_node_form', $args[0], $args[1]);
}
if ($router_item['page_callback'] == 'drupal_get_form') {
// @todo
// Some modules (like multistep) depend on vars passed via the querystring
// This logic taken from ctools_automodal_get_form() above
$args = $router_item['page_arguments'];
$form_id = array_shift($args);
$form_state = array(
'ajax' => TRUE,
'build_info' => array('args' => $args),
);
// @todo? $form = drupal_rebuild_form($form_id, $form_state);
$commands = ctools_modal_form_wrapper($form_id, $form_state);
}
// TODO:
//elseif ($item['page callback'] == 'node_add') {
// $router_item['page callback'] = 'ctools_automodal_node_add';
//}
else {
$output = call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
$commands = ctools_automodal_page_render($output);
}
}
else {
// TODO:
$page_callback_result = MENU_ACCESS_DENIED;
}
}
else {
// TODO:
$page_callback_result = MENU_NOT_FOUND;
}
return $commands;
}
/**
* Display a normal Drupal page using CTools modal.
*/
function ctools_automodal_get_page() {
$args = func_get_args();
$callback = array_shift($args);
$ajax = array_pop($args);
_ctools_automodal_fix_get_q();
if (function_exists($callback)) {
$output = call_user_func_array($callback, $args);
if ($ajax) {
ctools_include('modal');
ctools_include('ajax');
$commands = ctools_automodal_page_render($output);
if (empty($commands)) {
$commands[] = ctools_modal_command_loading();
if (!empty($_GET['destination'])) {
$commands[] = ctools_ajax_command_redirect($_GET['destination']);
}
}
print ajax_render($commands);
exit();
}
else {
return $output;
}
}
else {
return MENU_NOT_FOUND;
}
}
/**
* Render a page into an AJAX display.
*/
function ctools_automodal_page_render($output) {
if (is_array($output)) {
$output = drupal_render($output);
}
// If there are messages for the form, render them.
if ($messages = theme('status_messages')) {
$output = '<div class="messages">' . $messages . '</div>' . $output;
}
$commands = array();
$title = drupal_get_title();
$commands[] = ctools_modal_command_display($title, $output);
return $commands;
}
/**
* Default confirmation callback.
*
* @param array $options
*/
function ctools_automodal_confirmation_callback($options) {
$confirmation = array();
$confirmation['messages']['#markup'] = theme('status_messages');
$confirmation['confirmation']['#markup'] = $options['text'];
$confirmation['actions']['#markup'] = '<a class="button dismiss" onclick="Drupal.CTools.Modal.modal_dismiss();" href="#">' . t('Dismiss') . '</a>';
return $confirmation;
}
/**
* Theme modal confirmation page.
*/
function theme_ctools_automodal_confirmation($variables) {
$output = '';
$output .= '<div class="ctools-automodal-confirmation-wrapper">';
$output .= render($variables['messages']);
$output .= '<div class="confirmation">';
$output .= render($variables['confirmation']);
$output .= '</div>';
$output .= render($variables['actions']);
$output .= '</div>';
return $output;
}