forked from brendon-codes/drupal-ajax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.module
executable file
·560 lines (535 loc) · 12.5 KB
/
ajax.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
<?php
/**
* Automatic AJAX forms validation, preview, and submission
*
* @see http://drupal.org/project/ajax
* @see irc://freenode.net/#drupy
* @depends Drupal 6
* @author brendoncrawford
* @note This file uses a 79 character width limit.
* @note Available hooks are:
* hook_ajax_validate_fail
* hook_ajax_validate_cleanup
* hook_ajax_alter
*
*/
/**
* Implementation of hook_preprocess_hook().
*
* @param variables Assoc
* @return Bool
*
*/
function ajax_preprocess_page(&$variables) {
$p = drupal_get_path('module', 'ajax');
drupal_add_js($p . '/jquery/jquery.a_form.packed.js', 'module');
drupal_add_js($p . '/ajax.js', 'module');
$scripts = drupal_add_js();
$variables['scripts'] = drupal_get_js('header', $scripts);
return TRUE;
}
/**
* hook_form_alter
*
* @param $form Assoc
* @param $form_state Assoc
* @param $form_id String
* @return Bool
*/
function ajax_form_alter(&$form, $form_state, $form_id) {
ajax_form_configure($form, $form_state, $form_id);
if (ajax_is_enabled($form) && !$form['#programmed']) {
$found = array('submitter' => FALSE);
ajax_invoke_alter($form, $form_state, $form_id);
ajax_validator_set($form);
ajax_submitter_find($form, $found);
ajax_submitter_set($form, $found);
}
return TRUE;
}
/**
* Adds warnings to various configuration forms
*
* @param $form
* @param $form_state
* @param $form_id
* @return unknown_type
*/
function ajax_form_configure(&$form, $form_state, $form_id) {
if ($form_id === 'node_configure') {
$form['node_preview']['#description'] .=
" " .
"<span style='color:#F00;'>" .
t(
'Setting this option to "Required" will conflict with Ajax ' .
'form submissions. '
) .
"</span>";
}
return TRUE;
}
/**
* Checks if form is ajax-enabled
*
* @param $form Assoc
* @return Bool
*/
function ajax_is_enabled($form) {
return (
array_key_exists('#ajax', $form) &&
is_array($form['#ajax']) &&
array_key_exists('enabled', $form['#ajax']) &&
$form['#ajax']['enabled'] === TRUE
);
}
/**
* Gets internal path from actual url path
*
* @param $val String
* @return String
*/
function ajax_drupal_path($val) {
$b = base_path();
$bs = sizeof($b);
if (substr($val, 0, $bs) === $b) {
return substr($val, $bs);
}
else {
return $val;
}
}
/**
* Collects path info
*
* @param $val String
* @return Assoc
*/
function ajax_path_info($val) {
$out = array(
'path' => NULL,
'query' => array(),
'fragment' => NULL
);
$u = parse_url($val);
if (array_key_exists('query', $u)) {
parse_str($u, $out['query']);
}
if (array_key_exists('path', $u)) {
$out['path'] = $u['path'];
}
if (array_key_exists('fragment', $u)) {
$out['fragment'] = $u['fragment'];
}
return $out;
}
/**
* Sets the validator
*
* @param $form Assoc
* @return Bool
*/
function ajax_validator_set(&$form) {
$form['#validate'][] = 'ajax_validator';
if (empty($form['#attributes']['class'])) {
$form['#attributes']['class'] = 'ajax-form';
}
else {
$form['#attributes']['class'] .= ' ajax-form';
}
return TRUE;
}
/**
* Sets the submitter
*
* @param $form Assoc
* @param $found Assoc
* @return Bool
*/
function ajax_submitter_set(&$form, $found) {
if (!$found['submitter']) {
$form['#submit'][] = 'ajax_submitter';
if (empty($form['#attributes']['class'])) {
$form['#attributes']['class'] = 'ajax-form';
}
else {
$form['#attributes']['class'] .= ' ajax-form';
}
}
return TRUE;
}
/**
* Finds the submitter
*
* @param $form Assoc
* @param $found Assoc
* @return Bool
*/
function ajax_submitter_find(&$form, &$found) {
foreach ($form as $form_key => $form_val) {
if (is_array($form[$form_key])) {
//submit or preview button
if (ajax_is_submitter($form, $form_key)) {
$form[$form_key]['#attributes']['class'] = 'ajax-trigger';
if (array_key_exists('#submit', $form[$form_key]) &&
!empty($form[$form_key]['#submit'])) {
$form[$form_key]['#submit'][] = 'ajax_submitter';
$found['submitter'] = TRUE;
}
}
//nested
else {
ajax_submitter_find($form[$form_key], $found);
}
}
}
return TRUE;
}
/**
* Determines if item is a submitter
*
* @param $form Assoc
* @param $form_key
* @return Bool
*
*/
function ajax_is_submitter($form, $form_key) {
$is = FALSE;
if (array_key_exists('#type', $form[$form_key])) {
if (
$form[$form_key]['#type'] === 'submit' ||
$form[$form_key]['#type'] === 'button'
) {
if ($form_key === 'submit' || $form_key === 'preview') {
$is = TRUE;
}
elseif (array_key_exists('#ajax', $form[$form_key])) {
if (array_key_exists('submitter', $form[$form_key]['#ajax'])) {
if ($form[$form_key]['#ajax']['submitter'] === TRUE) {
$is = TRUE;
}
}
}
}
}
return $is;
}
/**
* Gets redirect
* Sometimes the redirect can be an array in the form of
* 0 => path
* 1 => query
* 2 => fragment
*
* @param $redirect String|Array
* @return String
*/
function ajax_get_redirect($redirect) {
//watchdog('AJAX Forms', 'Redirect: !redirect',
// array('!redirect'=>gettype($redirect[1])));
$args = array();
$args['absolute'] = TRUE;
if (is_array($redirect)) {
if ($redirect[1] !== NULL) {
$args['query'] = $redirect[1];
}
if ($redirect[2] !== NULL) {
$args['fragment'] = $redirect[2];
}
$path = $redirect[0];
}
else {
$path = $redirect;
}
$n = url($path, $args);
$u = ajax_get_url($n);
$u['query'][mt_rand()] = 1;
$u['fragment'] = NULL;
$out = ajax_build_url($u);
return $out;
}
/**
* Builds a URL
*
* @param Assoc
* @return String
*/
function ajax_build_url($u) {
$out = '';
if (!empty($u['scheme'])) {
$out .= $u['scheme'];
}
else {
$out .= 'http';
}
$out .= '://';
if (!empty($u['user'])) {
$out .= $u['user'];
if (!empty($u['pass'])) {
$out .= ':';
$out .= $u['pass'];
}
$out .= '@';
}
$out .= $u['host'];
$out .= $u['path'];
if (!empty($u['query'])) {
$out .= '?';
$out .= drupal_query_string_encode($u['query']);
}
if (!empty($u['fragment'])) {
$out .= '#';
$out .= $u['fragment'];
}
return $out;
}
/**
* Gets url parts
*
* @param $url String
* @return Asoc
*/
function ajax_get_url($url) {
$p = parse_url($url);
if (!empty($p['query'])) {
parse_str($p['query'], $q);
$p['query'] = $q;
}
else {
$p['query'] = array();
}
return $p;
}
/**
* Submission handler callback
*
* @param $form Assoc
* @param $form_state Assoc
* @return Bool
*/
function ajax_submitter(&$form, &$form_state) {
$messages = drupal_get_messages(NULL, TRUE);
$data = array(
'form_id' => $form_state['values']['form_id'],
'options' => $form['#ajax']
);
// Node Preview
if (array_key_exists('node_preview', $form_state) &&
!empty($form_state['node_preview'])) {
$data['preview'] = $form_state['node_preview'];
}
// form_state:redirect
if (array_key_exists('redirect', $form_state) &&
!empty($form_state['redirect'])) {
$data['redirect'] = $form_state['redirect'];
}
// form:redirect
if (array_key_exists('redirect', $form) &&
!empty($form_state['redirect'])) {
$data['redirect'] = $form['redirect'];
}
// Messages: Status
if (array_key_exists('status', $messages)) {
$data['messages_status'] = $messages['status'];
}
// Messages warning
if (array_key_exists('warning', $messages)) {
$data['messages_warning'] = $messages['warning'];
}
$out = ajax_build($data);
ajax_out($out);
return TRUE;
}
/**
* Invokes hooks: ajax_validate_pass
*
* @param $form Assoc
* @param $form_state Assoc
* @param $data Assoc
* @param $pass Bool
* @return Bool
*/
function ajax_invoke_validate_pass(&$form, &$form_state, &$data, &$pass) {
$hook = 'ajax_validate_pass';
foreach (module_implements($hook) as $name) {
$function = $name . '_' . $hook;
$result = $function($form, $form_state, $data, $pass);
}
return TRUE;
}
/**
* Invokes hooks: ajax_validate_fail
*
* @param $form Assoc
* @param $form_state Assoc
* @param $data Assoc
* @param $pass Bool
* @return Bool
*
*/
function ajax_invoke_validate_fail(&$form, &$form_state, &$data) {
$hook = 'ajax_validate_fail';
foreach (module_implements($hook) as $name) {
$function = $name . '_' . $hook;
$result = $function($form, $form_state, $data);
}
return TRUE;
}
/**
* Invokes hooks: ajax_alter
*
* @param $form Assoc
* @param $form_state Assoc
* @param $form_id String
* @return Bool
*
*/
function ajax_invoke_alter(&$form, &$form_state, $form_id) {
$hook = 'ajax_alter';
foreach (module_implements($hook) as $name) {
$function = $name . '_' . $hook;
$result = $function($form, $form_state, $form_id);
}
return TRUE;
}
/**
* Validation handler callback
*
* @param $form Assoc
* @param $form_state Assoc
* @return Bool
*/
function ajax_validator(&$form, &$form_state) {
if (array_key_exists('drupal_ajax', $_REQUEST)) {
drupal_get_messages(NULL, TRUE);
$data = ajax_build(array(
'messages_error' => form_get_errors(),
'form_id' => $form_state['values']['form_id'],
'options' => $form['#ajax']
));
// FAIL
if (!$data['status']) {
ajax_invoke_validate_fail($form, $form_state, $data);
ajax_out($data);
}
// PASS
else {
$pass = TRUE;
ajax_invoke_validate_pass($form, $form_state, $data, $pass);
if (!$pass) {
ajax_out($data);
}
}
}
return TRUE;
}
/**
* Outputs data
*
* @param $data String
* @return Exit
*/
function ajax_out($data) {
$buffer_len = ob_get_length();
if ($buffer_len !== FALSE && $buffer_len > 0) {
ob_clean();
}
header('HTTP/1.1 200 OK', TRUE);
if (!array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
$_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest') {
drupal_set_header('Content-Type: text/html; Charset=UTF-8');
print "<textarea>";
print drupal_to_js($data);
print "</textarea>\n";
}
else {
drupal_set_header('Content-Type: text/javascript; Charset=UTF-8');
print drupal_to_js($data);
}
exit;
}
/**
* Gets a clean element id
* taken from form_clean_id
*
* @param $field_id String
* @return String
*/
function ajax_clean_id($field_id) {
return str_replace(array('][', '_', ' '), '-', $field_id);
}
/**
* Builds the output object
*
* @param $data Assoc
* @return Assoc
*/
function ajax_build($data) {
$out = array(
'status' => true,
'updaters' => array(),
'debug' => array(),
'messages_error' => array(),
'messages_status' => array(),
'messages_warning' => array(),
'redirect' => NULL,
'preview' => NULL,
'form_id' => NULL,
'options' => array()
);
// MESSAGE:ERROR
if (array_key_exists('messages_error', $data) &&
$data['messages_error'] !== NULL) {
$out['status'] = FALSE;
foreach ($data['messages_error'] as $k => $v) {
$out['messages_error'][] = array(
'id' => ajax_clean_id("edit-" . $k),
'value' => $v
);
}
}
// MESSAGE:STATUS
if (array_key_exists('messages_status', $data) &&
$data['messages_status'] !== NULL) {
foreach ($data['messages_status'] as $k => $v) {
$out['messages_status'][] = array(
'id' => (int)$k,
'value' => $v
);
}
}
// MESSAGE:WARNING
if (array_key_exists('messages_warning', $data) &&
$data['messages_warning'] !== null) {
foreach ($data['messages_warning'] as $k => $v) {
$out['messages_warning'][] = array(
'id' => (int)$k,
'value' => $v
);
}
}
// Redirect: Destination Parameter
if (array_key_exists('destination', $_GET)) {
$out['redirect'] = ajax_get_redirect($_GET['destination']);
}
// Redirect: Explicit
elseif (array_key_exists('redirect', $data) && $data['redirect'] !== NULL) {
$out['redirect'] = ajax_get_redirect($data['redirect']);
}
// Preview
if (array_key_exists('preview', $data) && $data['preview'] !== NULL) {
$out['preview'] = rawurlencode($data['preview']);
}
// Debug
if (array_key_exists('debug', $data) && $data['debug'] !== NULL) {
$out['debug'] = $data['debug'];
}
// Form ID
if (array_key_exists('form_id', $data) && $data['form_id'] !== NULL) {
$out['form_id'] = $data['form_id'];
}
// Misc Admin Options to Send to Client
if (array_key_exists('options', $data) && $data['options'] !== NULL) {
$out['options'] = $data['options'];
}
return $out;
}