/[drupal]/contributions/modules/addresses/addresses.module
ViewVC logotype

Contents of /contributions/modules/addresses/addresses.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.62 - (show annotations) (download) (as text)
Wed Feb 11 07:24:29 2009 UTC (9 months, 2 weeks ago) by brmassa
Branch: MAIN
CVS Tags: HEAD
Changes since 1.61: +3 -2 lines
File MIME type: text/x-php
Bugs fixed:
* #337541 by deviantintegral: validation of required fields fails
1 <?php
2 // $Id: addresses.module,v 1.61 2009/01/20 20:31:42 brmassa Exp $
3 /**
4 * @author Bruno Massa
5 * @file
6 * You can associate a geographic address with content and users.
7 */
8
9 /**
10 * Each address field can be: displayed, required, hidden or not
11 * used at all
12 */
13 define('ADDRESSES_FIELD_NONE', 0);
14 define('ADDRESSES_FIELD_SHOW', 1);
15 define('ADDRESSES_FIELD_REQUIRED', 2);
16 define('ADDRESSES_FIELD_HIDDEN', 3);
17
18 /**
19 * Implementation of hook_addressfieldapi().
20 *
21 * Its a specific Addresses module hook function, to
22 * allow other modules to add more fields to the addresses
23 */
24 function addresses_addressesfieldapi($op, $fields = array(), $values = array()) {
25 if ($op == 'fields') {
26 return array(
27 'aname' => array(
28 'type' => 'varchar',
29 'length' => 75,
30 'description' => t('The nickname of this address, like "Home", "Office", "Anna\'s appartment"'),
31 'display' => ADDRESSES_FIELD_NONE,
32 'title' => t('Address Name'),
33 'theme' => array(
34 'aname' => ''
35 ),
36 'token' => 'addresses_general',
37 ),
38 'street' => array(
39 'type' => 'varchar',
40 'length' => 255,
41 'description' => t('Street, number...'),
42 'display' => ADDRESSES_FIELD_SHOW,
43 'title' => t('Street'),
44 'theme' => array(
45 'street' => t('Street and number.'),
46 ),
47 'token' => 'addresses_adr',
48 ),
49 'additional' => array(
50 'type' => 'varchar',
51 'length' => 255,
52 'description' => t('More info like appartment block, number or address reference'),
53 'display' => ADDRESSES_FIELD_SHOW,
54 'title' => t('Additional'),
55 'theme' => array(
56 'additional' => ''
57 ),
58 'token' => 'addresses_adr',
59 ),
60 'city' => array(
61 'type' => 'varchar',
62 'length' => 255,
63 'description' => t('Name of the city'),
64 'display' => ADDRESSES_FIELD_SHOW,
65 'title' => t('City'),
66 'theme' => array(
67 'city' => t('City name'),
68 ),
69 'token' => 'addresses_adr',
70 ),
71 'province' => array(
72 'type' => 'varchar',
73 'length' => 16,
74 'description' => t('Name of the state/province/county/territory'),
75 'display' => ADDRESSES_FIELD_SHOW,
76 'title' => t('Province'),
77 'theme' => array(
78 'province_name' => t('State/Province name.'),
79 'province_code' => t('State/Province code.'),
80 ),
81 'token' => 'addresses_adr',
82 ),
83 'country' => array(
84 'type' => 'varchar',
85 'length' => 2,
86 'description' => t('The ISO alpha 3 code of each country (its a 2-digit code)'),
87 'display' => ADDRESSES_FIELD_SHOW,
88 'title' => t('Country'),
89 'theme' => array(
90 'country_name' => t('Country name.'),
91 'country_code2' => t('Country 2-digits code.'),
92 'country_code3' => t('Country 3-digits code.'),
93 ),
94 'token' => 'addresses_adr',
95 ),
96 'postal_code' => array(
97 'type' => 'varchar',
98 'length' => 16,
99 'description' => t('The address postal code (ZIP code for US people)'),
100 'display' => ADDRESSES_FIELD_SHOW,
101 'title' => t('Postal code'),
102 'theme' => array(
103 'postal_code' => t('Postal code.'),
104 ),
105 'token' => 'addresses_adr',
106 ),
107 'is_primary' => array(
108 'type' => 'int',
109 'size' => 'tiny',
110 'default' => 0,
111 'description' => t('Mark it as the primary address or not (default is not)'),
112 'display' => ADDRESSES_FIELD_NONE,
113 'title' => t('Primary Address Checkbox'),
114 'token' => 'addresses_general',
115 )
116 );
117 }
118 elseif ($op == 'form') {
119 module_load_include('settings.inc', 'addresses');
120 return _addresses_addressesfieldapi_form($fields, $values);
121 }
122 }
123
124 /**
125 * Implementation of FAPI hook_elements().
126 *
127 * Any FAPI callbacks needed for individual widgets can be declared here,
128 * and the element will be passed to those callbacks for processing.
129 *
130 * Drupal will automatically theme the element using a theme with
131 * the same name as the hook_elements key.
132 *
133 * Autocomplete_path is not used by text_widget but other widgets can use it
134 * (see nodereference and userreference).
135 */
136 function addresses_elements() {
137 // Get a list of all address-related fields
138 $ftypes = module_invoke_all('addressesfieldapi', 'fields');
139 foreach (array_keys($ftypes) as $ftype) {
140 $fields[] = $ftype;
141 }
142
143 return array(
144 'addresses_elements' => array(
145 '#columns' => $fields,
146 '#delta' => 0,
147 '#input' => TRUE,
148 '#process' => array('addresses_elements_process'),
149 )
150 );
151 }
152
153 /**
154 * Generate the address "field", creating all form fields
155 *
156 * @return
157 * Array. Form fields
158 */
159 function addresses_elements_process($element, $edit, $form_state, $form) {
160 $fields = $element['#fields'];
161 $values = $element['#default_value'];
162
163 // Reset the required fields based on the general required value
164 if (empty($element['#required'])) {
165 foreach ($fields as $field => $required) {
166 if ($required == ADDRESSES_FIELD_REQUIRED
167 and $form['#id'] == 'content-field-edit-form') {
168 $fields[$field] = ADDRESSES_FIELD_SHOW;
169 }
170 }
171 }
172
173 // Reset unwanted attributes
174 $element['#title'] = '';
175 $element['#description'] = '';
176
177 // If its a existing address, save the Address ID
178 // for further processing
179 if (!empty($values['aid'])) {
180 $element['aid'] = array(
181 '#type' => 'hidden',
182 '#value' => $values['aid']
183 );
184 }
185
186 // Include the main module file
187 module_load_include('inc', 'addresses');
188
189 // Get other fields from hook_addressesfieldsapi
190 $element = array_merge(
191 $element,
192 module_invoke_all('addressesfieldapi', 'form', $fields, $values)
193 );
194
195 return $element;
196 }
197
198 /**
199 * Implementation of hook_menu().
200 */
201 function addresses_menu() {
202 $items['admin/settings/address'] = array(
203 'access arguments' => array('access administration pages'),
204 'description' => 'Settings for Address module',
205 'file' => 'addresses.settings.inc',
206 'page callback' => 'drupal_get_form',
207 'page arguments' => array('_addresses_settings'),
208 'title' => 'Addresses',
209 );
210 $items['admin/settings/address/format'] = array(
211 'access arguments' => array('access administration pages'),
212 'description' => 'Format addresses',
213 'file' => 'addresses.settings.inc',
214 'page callback' => 'drupal_get_form',
215 'page arguments' => array('_addresses_settings_format', 4),
216 'title' => 'Addresses Format',
217 'type' => MENU_CALLBACK
218 );
219 $items['admin/settings/address/autocomplete'] = array(
220 'access callback' => TRUE,
221 'file' => 'addresses.settings.inc',
222 'page callback' => '_addresses_autocomplete',
223 'type' => MENU_CALLBACK
224 );
225
226 return $items;
227 }
228
229 /**
230 * Implementation of hook_theme().
231 */
232 function addresses_theme() {
233 return array(
234 // Shows addresses normally
235 'addresses' => array(
236 'arguments' => array('address', 'hide'),
237 'file' => 'addresses.inc',
238 ),
239 // Shows address form elements
240 'addresses_elements' => array(
241 'arguments' => array('form'),
242 'file' => 'addresses.settings.inc',
243 ),
244 // Shows address weight on settings page
245 'addresses_field_weight' => array(
246 'arguments' => array('form'),
247 'file' => 'addresses.settings.inc',
248 ),
249 // Shows address fields
250 'addresses_field_primary' => array(
251 'arguments' => array('form'),
252 'file' => 'addresses.inc',
253 ),
254 'addresses_field_aname' => array(
255 'arguments' => array('form'),
256 'file' => 'addresses.inc',
257 ),
258 'addresses_field_city' => array(
259 'arguments' => array('form'),
260 'file' => 'addresses.inc',
261 ),
262 'addresses_field_street' => array(
263 'arguments' => array('form'),
264 'file' => 'addresses.inc',
265 ),
266 'addresses_field_additional' => array(
267 'arguments' => array('form'),
268 'file' => 'addresses.inc',
269 ),
270 'addresses_field_country_name' => array(
271 'arguments' => array('form'),
272 'file' => 'addresses.inc',
273 ),
274 'addresses_field_country_code2' => array(
275 'arguments' => array('form'),
276 'file' => 'addresses.inc',
277 ),
278 'addresses_field_country_code3' => array(
279 'arguments' => array('form'),
280 'file' => 'addresses.inc',
281 ),
282 'addresses_field_postal_code' => array(
283 'arguments' => array('form'),
284 'file' => 'addresses.inc',
285 ),
286 'addresses_field_province_name' => array(
287 'arguments' => array('form'),
288 'file' => 'addresses.inc',
289 ),
290 'addresses_field_province_code' => array(
291 'arguments' => array('form'),
292 'file' => 'addresses.inc',
293 ),
294 // Shows addresses using a single line
295 'addresses_address_singleline' => array(
296 'arguments' => array('addresses'),
297 'file' => 'addresses.inc',
298 ),
299 // Choose the addresses fields
300 'addresses_settings_fields' => array(
301 'arguments' => array('form'),
302 'file' => 'addresses.settings.inc',
303 ),
304 );
305 }
306
307 /**
308 * Implementation of hook_token_list().
309 */
310 function addresses_token_list($type = 'all') {
311 if ($type == 'addresses_general') {
312 $category = t('Addresses: General');
313 }
314 elseif ($type == 'addresses_adr') {
315 $category = t('Addresses: Address');
316 }
317 else {
318 return;
319 }
320
321 $ftypes = module_invoke_all('addressesfieldapi', 'fields');
322 foreach ($ftypes as $ftype => $field) {
323 if (empty($field['theme'])) {
324 continue;
325 }
326 foreach ($field['theme'] as $theme => $theme_name) {
327 if (is_numeric($theme)) {
328 $tokens[$category][$theme_name] = $field['title'];
329 }
330 else {
331 $tokens[$category][$theme] = $theme_name;
332 }
333 }
334 }
335 return $tokens;
336 }
337
338 /**
339 * Implementation of hook_token_values().
340 */
341 function addresses_token_values($type, $object = NULL) {
342 // Grant that all values are plain text (Avoiding code injection)
343 if (is_array($object)) {
344 foreach ($object as $index => $value) {
345 $object[$index] = check_plain($value);
346 }
347
348 $fields = module_invoke_all('addressesfieldapi', 'fields');
349
350 if ($type == 'addresses_general' or $type == 'addresses_adr') {
351 foreach ($fields as $field => $field_data) {
352 if ($field_data['token'] != $type or empty($field_data['theme'])) {
353 continue;
354 }
355 foreach (array_keys($field_data['theme']) as $theme_function) {
356 $values[$theme_function] = empty($object[$field]) ? '' : theme('addresses_field_'. $theme_function, $object);
357 }
358 }
359 return $values;
360 }
361 }
362 }

  ViewVC Help
Powered by ViewVC 1.1.2