-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabook.module
258 lines (225 loc) · 7.12 KB
/
abook.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
<?php
/**
* @file
* This module is meant to be an example for AJAX coding using the
* built in ajax features of the drupal Forms API.
*/
/**
* Implements hook_menu().
*/
function abook_menu() {
$items = array();
$items['abook'] = array(
'title' => 'Address book',
'page callback' => 'drupal_get_form',
'page arguments' => array('abook_form'),
'access callback' => TRUE,
);
$items['abook/autocomplete'] = array(
'title' => 'Autocomplete for perssons',
'page callback' => 'abook_autocomplete',
'access callback' => TRUE,
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Builds the form.
*/
function abook_form($form, &$form_state) {
$form['search'] = array(
'#type' => 'fieldset',
'#title' => t('Search'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
);
$form['search']['keyword'] = array(
'#type' => 'textfield',
'#title' => t('Address'),
'#size' => 60,
'#maxlength' => 128,
'#description' => t('Enter id, first and/or lastname to find an address.'),
'#autocomplete_path' => 'abook/autocomplete',
);
$form['search']['submit'] = array(
'#type' => 'submit',
'#value' => t('Load'),
'#submit' => array('abook_load'), // this call back is called to do the real processing
'#ajax' => array(
'callback' => 'abook_ajax_callback', // here we just tell drupal what to return
'wrapper' => 'address-replace',
),
);
$form['address'] = array(
'#type' => 'fieldset',
'#title' => t('Adress'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#prefix' => '<div id="address-replace">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
$form['address']['pid'] = array(
'#type' => 'textfield',
'#title' => t('Id'),
'#size' => 4,
'#maxlength' => 4,
'#attributes' => array('readonly' => 'readonly')
);
$form['address']['firstname'] = array(
'#type' => 'textfield',
'#title' => t('Firstname'),
'#size' => 60,
'#maxlength' => 128,
);
$form['address']['lastname'] = array(
'#type' => 'textfield',
'#title' => t('Lastname'),
'#size' => 60,
'#maxlength' => 128,
);
$form['address']['male'] = array(
'#type' => 'checkbox',
'#title' => t('Male'),
);
$form['address']['dayofbirth'] = array(
'#type' => 'date',
'#title' => t('Day of Birth'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('abook_submit'), // this call back is called to do the real processing
'#ajax' => array(
'callback' => 'abook_ajax_callback', // here we just tell drupal what to return
'wrapper' => 'address-replace',
),
);
$form['new'] = array(
'#type' => 'submit',
'#value' => t('New'),
'#submit' => array('abook_new'), // this call back is called to do the real processing
'#ajax' => array(
'callback' => 'abook_ajax_callback', // here we just tell drupal what to return
'wrapper' => 'address-replace',
),
);
/*
* I know this looks ugly. Actually we're telling the Form API that the user
* has entered this values. The nice thing is, that Form API is filling in
* everything for us once it rebuilds the form.
*/
if (isset($form_state['person'])) {
list($year, $month, $day) = explode('-', $form_state['person']->dayofbirth);
$form_state['input']['address']['pid'] = (string)$form_state['person']->pid;
$form_state['input']['address']['firstname'] = (string)$form_state['person']->firstname;
$form_state['input']['address']['lastname'] = (string)$form_state['person']->lastname;
$form_state['input']['address']['male'] = (string)$form_state['person']->male;
$form_state['input']['address']['dayofbirth']['year'] = $year;
$form_state['input']['address']['dayofbirth']['month'] = _abook_strip_zero($month);
$form_state['input']['address']['dayofbirth']['day'] = _abook_strip_zero($day);
}
return $form;
}
/**
* Save or update the person. Then make sure all form fields ar populated.
*/
function abook_submit($form, &$form_state) {
$person = _abook_save_or_update($form, $form_state);
$form_state['person'] = (object) $person;
$form_state['rebuild'] = TRUE;
}
/**
* Just clear all form fields. This could be done with pur JS as well, but
* this module is written to demonstrate AJAX :-).
*/
function abook_new($form, &$form_state) {
unset($form_state['person']);
unset($form_state['input']['address']);
$form_state['rebuild'] = TRUE;
}
/**
* Load a person according to the value of the search field.
* Then make sure all form fields ar populated.
*/
function abook_load($form, &$form_state) {
$person = null;
if (isset($form_state['values']['search']['keyword'])) {
list($first, $last) = explode(', ', $form_state['values']['search']['keyword']);
$query = db_select('person', 'p')
->condition('firstname', db_like($first), 'LIKE')
->fields('p');
if (isset($last)) {
$query->condition('lastname', db_like($last), 'LIKE');
}
$s = $query->__toString();
$person = $query->execute()->fetch();
}
if (isset($person)) {
$form_state['person'] = $person;
$form_state['rebuild'] = TRUE;
}
}
function abook_ajax_callback($form, &$form_state) {
return $form['address'];
}
/**
* Tries to complete the search key for a person. We are looking
* for id, firstname or lastname.
*
* @param $key The (partial) key entered by the user.
*/
function abook_autocomplete($key) {
$matches = array();
$key = check_plain($key);
if (is_numeric($key)) {
$query = db_select('person', 'p')
->condition('pid', $key)
->fields('p');
$s = $query->__toString();
$person = $query->execute()->fetch();
$matches[_abook_person_tostring_names($person)] = _abook_person_tostring_full($person);
}
else {
$query = db_select('person', 'p')
->condition(db_or()->condition('firstname', db_like($key).'%', 'LIKE')->condition('lastname', "$key%", 'LIKE'))
->fields('p');
$s = $query->__toString();
$result = $query->execute();
foreach($result as $person) {
$matches[_abook_person_tostring_names($person)] = _abook_person_tostring_full($person);
}
}
drupal_json_output($matches);
}
/**
* Persist the form data.
*/
function _abook_save_or_update($form, &$form_state) {
$address = NULL;
if (isset($form_state['values']['address']['firstname']) && strlen($form_state['values']['address']['firstname'])>0) {
foreach ($form_state['values']['address'] as $key => $value) {
if (is_array($value)) {
$address[strtolower($key)] = $value['year'].'-'.$value['month'].'-'.$value['day'];
}
else {
if (strlen($value)>0)
$address[$key] = $value;
}
}
$empty = is_null($address['pid']);
drupal_write_record('person', $address, $empty ? array() : 'pid');
}
return $address;
}
function _abook_person_tostring_full($person) {
return $person->pid . ', ' . $person->firstname . ', ' . $person->lastname;
}
function _abook_person_tostring_names($person) {
return $person->firstname . ', ' . $person->lastname;
}
function _abook_strip_zero($string) {
$val = (integer)$string;
return (string)$val;
}