forked from Islandora/php_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAhah.inc
123 lines (111 loc) · 3.23 KB
/
Ahah.inc
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
<?php
/**
* @file
*
*/
class Ahah {
/**
* Is this a valid request.
*
* Ensure that a form_build_id is specified.
*
* @return boolean
*/
public static function validRequest() {
return isset($_REQUEST['form_build_id']);
}
/**
* Cancel processing an AHAH callback.
*/
public static function cancelResponse() {
header("HTTP/1.0 200 OK", FALSE, 200);
exit();
}
/**
* Ge the form_id, form_build_id, form, and form_state.
*
* @param Post $post
* @return array
*/
public static function getFormInfo() {
if (!self::validRequest()) {
self::cancelResponse();
}
$form_build_id = $_REQUEST['form_build_id'];
$form_state = array('storage' => NULL, 'submitted' => FALSE, 'post' => $_POST);
if (!$form = form_get_cache($form_build_id, $form_state)) {
if ($form_id = $form_state['post']['form_id']) {
$form = drupal_retrieve_form($form_id, $form_state);
}
}
if (!$form) {
self::cancelResponse();
}
module_load_include('inc', 'node', 'node.pages'); // Need for using AHAH in conjunction with Node API.
if(!$form_id) {
$form_id = array_shift($form['#parameters']);
}
return array($form_id, $form_build_id, &$form, &$form_state);
}
/**
*
* @param type $form_id
* @param array $form
* @param array $form_state
*/
public static function &processForm($form_id, array &$form, array &$form_state) {
$form['#post'] = $_POST;
$form['#redirect'] = FALSE;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
$form_state['values'] = array();
$form = form_builder($form_id, $form, $form_state);
return $form;
}
/**
*
* @param type $form_id
* @param type $form_build_id
* @param array $form
* @param array $form_state
* @return type
*/
public static function &rebuildForm($form_id, $form_build_id, array &$form, array &$form_state) {
$args = $form['#parameters'];
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
return $form;
}
/**
* Send a response to the AHAH request.
*
* @param array $form
*/
public static function respond(array &$form = NULL) {
$data = theme('status_messages');
if (isset($form)) {
unset($form['#prefix'], $form['#suffix']);
$data .= drupal_render($form);
}
$javascript = drupal_add_js(NULL, NULL, 'header');
$settings = call_user_func_array('array_merge_recursive', $javascript['setting']);
unset($settings['ahah']['']);
// We forego the usuage of drupal_json as this sets a header which causes
// IE to prompt for downloads. Instead we use the header set in
// _drupal_bootstrup_full which sets the Content Type of 'text/html'.
// Relevant threads: http://drupal.org/node/952220,
// http://drupal.org/node/1086098
$response = array(
'status' => TRUE,
'data' => $data,
'settings' => $settings,
);
echo drupal_to_js($response);
}
/**
* Loads the needed ahah.js file so that Drupal.settings
* are updated when AHAH elements with AHAH callbacks are created
*/
public static function get_ahah_js() {
drupal_add_js(drupal_get_path('module', 'php_lib') . '/js/ahah/ahah.js', 'footer');
}
}