Issue #1419028 by Liam Morland: Integrate node_import module code.
[project/zipcode.git] / zipcode.module
1 <?php
2
3 // Copyright 2008 Thierry GUEGAN http://www.arvoriad.com
4
5 /**
6 * @file
7 * Defines zipcodes fields for CCK.
8 * Provide some verifications on the zipcodes
9 */
10
11 /**
12 * Implements hook_field_info().
13 *
14 * Here we indicate that the content module will use its default
15 * handling for the view of this field.
16 *
17 * Callbacks can be omitted if default handing is used.
18 * They're included here just so this module can be used
19 * as an example for custom modules that might do things
20 * differently.
21 *
22 * If your module will provide its own Views tables or arguments,
23 * change CONTENT_CALLBACK_DEFAULT to CONTENT_CALLBACK_CUSTOM.
24 *
25 * IMPORTANT! - field and widget names will be truncated to 32 characters in
26 * the database and in internal arrays, like content_fields().
27 */
28 function zipcode_field_info() {
29 return array(
30 'fr_zipcode' => array('label' => t('Zipcode - France')),
31 'ca_zipcode' => array('label' => t('Zipcode - Canada')),
32 'us_zipcode' => array('label' => t('Zipcode - US')),
33 'gp_zipcode' => array('label' => t('Zipcode - Guadeloupe')),
34 'cl_zipcode' => array('label' => t('Zipcode - Chile')),
35 'ar_zipcode' => array('label' => t('Zipcode - Argentina')),
36 'br_zipcode' => array('label' => t('Zipcode - Brazil')),
37 'uk_zipcode' => array('label' => t('Zipcode - United Kingdom')),
38 'be_zipcode' => array('label' => t('Zipcode - Belgium')),
39 'nl_zipcode' => array('label' => t('Zipcode - Netherlanc')),
40 'de_zipcode' => array('label' => t('Zipcode - Germany')),
41 'ch_zipcode' => array('label' => t('Zipcode - Switzerland')),
42 'it_zipcode' => array('label' => t('Zipcode - Italy')),
43 'ad_zipcode' => array('label' => t('Zipcode - Andorra')),
44 'hu_zipcode' => array('label' => t('Zipcode - Hungary')),
45 'ee_zipcode' => array('label' => t('Zipcode - Estonia')),
46 'hr_zipcode' => array('label' => t('Zipcode - Croatia')),
47 'by_zipcode' => array('label' => t('Zipcode - Belarussia')),
48 'ru_zipcode' => array('label' => t('Zipcode - Russia')),
49 'cn_zipcode' => array('label' => t('Zipcode - China')),
50 'jp_zipcode' => array('label' => t('Zipcode - Japan')),
51 'au_zipcode' => array('label' => t('Zipcode - Australia')),
52 'nz_zipcode' => array('label' => t('Zipcode - New Zealand')),
53 'dk_zipcode' => array('label' => t('Zipcode - Daenmark')),
54 'se_zipcode' => array('label' => t('Zipcode - Sweden')),
55 'pt_zipcode' => array('label' => t('Zipcode - Portugal')),
56 'cy_zipcode' => array('label' => t('Zipcode - Cyprus')),
57 'dz_zipcode' => array('label' => t('Zipcode - Algeria')),
58 'gw_zipcode' => array('label' => t('Zipcode - Guinea Bissau')),
59 'eg_zipcode' => array('label' => t('Zipcode - Egypt')),
60 'bn_zipcode' => array('label' => t('Zipcode - Brunei')),
61 'bd_zipcode' => array('label' => t('Zipcode - Bangladesh')),
62 'in_zipcode' => array('label' => t('Zipcode - India')),
63 );
64 }
65
66 /**
67 * Implements hook_theme().
68 */
69 function zipcode_theme() {
70 return array(
71 'zipcode_textfield' => array(
72 'arguments' => array('element' => NULL),
73 ),
74 'zipcode_formatter_default' => array(
75 'arguments' => array('element' => NULL),
76 ),
77 );
78 }
79
80 /**
81 * Implements hook_field_settings().
82 *
83 * Handle the settings for a field.
84 *
85 * @param $op
86 * The operation to be performed. Possible values:
87 * - "form": Display the field settings form.
88 * - "validate": Check the field settings form for errors.
89 * - "save": Declare which fields to save back to the database.
90 * - "database columns": Declare the columns that content.module should create
91 * and manage on behalf of the field. If the field module wishes to handle
92 * its own database storage, this should be omitted.
93 * - "filters": Declare the Views filters available for the field.
94 * (this is used in CCK's default Views tables definition)
95 * They always apply to the first column listed in the "database columns"
96 * array.
97 * @param $field
98 * The field on which the operation is to be performed.
99 * @return
100 * This varies depending on the operation.
101 * - "form": an array of form elements to add to
102 * the settings page.
103 * - "validate": no return value. Use form_set_error().
104 * - "save": an array of names of form elements to
105 * be saved in the database.
106 * - "database columns": an array keyed by column name, with arrays of column
107 * information as values. This column information must include "type", the
108 * MySQL data type of the column, and may also include a "sortable" parameter
109 * to indicate to views.module that the column contains ordered information.
110 * TODO: Details of other information that can be passed to the database layer can
111 * be found in the API for the Schema API.
112 * - "filters": an array of 'filters' definitions as expected by views.module
113 * (see Views Documentation).
114 * When providing several filters, it is recommended to use the 'name'
115 * attribute in order to let the user distinguish between them. If no 'name'
116 * is specified for a filter, the key of the filter will be used instead.
117 */
118 function zipcode_field_settings($op, $field) {
119 switch ($op) {
120 case 'database columns':
121 if ($field['type'] == 'fr_zipcode') {
122 $columns = array(
123 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
124 );
125 }
126 if ($field['type'] == 'ca_zipcode') {
127 $columns = array(
128 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
129 );
130 }
131 if ($field['type'] == 'us_zipcode') {
132 $columns = array(
133 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
134 );
135 }
136 if ($field['type'] == 'gp_zipcode') {
137 $columns = array(
138 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
139 );
140 }
141 if ($field['type'] == 'cl_zipcode') {
142 $columns = array(
143 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
144 );
145 }
146 if ($field['type'] == 'ar_zipcode') {
147 $columns = array(
148 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
149 );
150 }
151 if ($field['type'] == 'br_zipcode') {
152 $columns = array(
153 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
154 );
155 }
156 if ($field['type'] == 'uk_zipcode') {
157 $columns = array(
158 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
159 );
160 }
161 if ($field['type'] == 'be_zipcode') {
162 $columns = array(
163 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
164 );
165 }
166 if ($field['type'] == 'nl_zipcode') {
167 $columns = array(
168 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
169 );
170 }
171 if ($field['type'] == 'de_zipcode') {
172 $columns = array(
173 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
174 );
175 }
176 if ($field['type'] == 'ch_zipcode') {
177 $columns = array(
178 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
179 );
180 }
181 if ($field['type'] == 'it_zipcode') {
182 $columns = array(
183 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
184 );
185 }
186 if ($field['type'] == 'ad_zipcode') {
187 $columns = array(
188 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
189 );
190 }
191 if ($field['type'] == 'hu_zipcode') {
192 $columns = array(
193 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
194 );
195 }
196 if ($field['type'] == 'ee_zipcode') {
197 $columns = array(
198 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
199 );
200 }
201 if ($field['type'] == 'hr_zipcode') {
202 $columns = array(
203 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
204 );
205 }
206 if ($field['type'] == 'by_zipcode') {
207 $columns = array(
208 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
209 );
210 }
211 if ($field['type'] == 'ru_zipcode') {
212 $columns = array(
213 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
214 );
215 }
216 if ($field['type'] == 'cn_zipcode') {
217 $columns = array(
218 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
219 );
220 }
221 if ($field['type'] == 'jp_zipcode') {
222 $columns = array(
223 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
224 );
225 }
226 if ($field['type'] == 'au_zipcode') {
227 $columns = array(
228 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
229 );
230 }
231 if ($field['type'] == 'nz_zipcode') {
232 $columns = array(
233 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
234 );
235 }
236 if ($field['type'] == 'dk_zipcode') {
237 $columns = array(
238 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
239 );
240 }
241 if ($field['type'] == 'se_zipcode') {
242 $columns = array(
243 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
244 );
245 }
246 if ($field['type'] == 'pt_zipcode') {
247 $columns = array(
248 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
249 );
250 }
251 if ($field['type'] == 'cy_zipcode') {
252 $columns = array(
253 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
254 );
255 }
256 if ($field['type'] == 'dz_zipcode') {
257 $columns = array(
258 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
259 );
260 }
261 if ($field['type'] == 'gw_zipcode') {
262 $columns = array(
263 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
264 );
265 }
266 if ($field['type'] == 'eg_zipcode') {
267 $columns = array(
268 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
269 );
270 }
271 if ($field['type'] == 'bn_zipcode') {
272 $columns = array(
273 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
274 );
275 }
276 if ($field['type'] == 'bd_zipcode') {
277 $columns = array(
278 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
279 );
280 }
281 if ($field['type'] == 'in_zipcode') {
282 $columns = array(
283 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
284 );
285 }
286 return $columns;
287 }
288 }
289
290 /**
291 * Implements hook_field().
292 *
293 * Define the behavior of a field type.
294 *
295 * @param $op
296 * What kind of action is being performed. Possible values:
297 * - "load": The node is about to be loaded from the database. This hook
298 * should be used to load the field.
299 * - "validate": The user has just finished editing the node and is
300 * trying to preview or submit it. This hook can be used to check or
301 * even modify the node. Errors should be set with form_set_error().
302 * - "presave": The user has just finished editing the node and the node has
303 * passed validation. This hook can be used to modify the node.
304 * - "insert": The node is being created (inserted in the database).
305 * - "update": The node is being updated.
306 * - "delete": The node is being deleted.
307 * @param &$node
308 * The node the action is being performed on. This argument is passed by
309 * reference for performance only; do not modify it.
310 * @param $field
311 * The field the action is being performed on.
312 * @param &$node_field
313 * The contents of the field in this node. Changes to this variable will
314 * be saved back to the node object.
315 * @return
316 * This varies depending on the operation.
317 * - The "load" operation should return an object containing extra values
318 * to be merged into the node object.
319 * - The "insert", "update", "delete", "validate", and "presave" operations
320 * have no return value.
321 *
322 * In most cases, only "validate" operations is relevant ; the rest
323 * have default implementations in content_field() that usually suffice.
324 */
325 function zipcode_field($op, &$node, $field, &$node_field, $teaser, $page) {
326 switch ($op) {
327 case 'validate': // corresponds to hook phone_widget validate in zipcode-5.x
328 foreach ($node_field as $delta => $item) {
329 if ($item['value'] != '') {
330 if ($field['type'] == 'fr_zipcode' && !valid_zipcode('fr', $item['value'])) {
331 form_set_error($field['field_name'], t('"%value" is not a valid French postal code.<br />Postal codes should only contains 4 or 5 numbers', array('%value' => $item['value'])));
332 }
333 if ($field['type'] == 'ca_zipcode' && !valid_zipcode('ca', $item['value'])) {
334 form_set_error($field['field_name'], t('"%value" is not a valid Canadian postal code.<br />Postal codes should be like Z5Z 5Z5 or Z5Z5Z5 ...', array('%value' => $item['value'])));
335 }
336 if ($field['type'] == 'us_zipcode' && !valid_zipcode('us', $item['value'])) {
337 form_set_error($field['field_name'], t('"%value" is not a valid US zipcode.<br />Zipcodes should be like 99999 or 99999-9999 ...', array('%value' => $item['value'])));
338 }
339 if ($field['type'] == 'gp_zipcode' && !valid_zipcode('gp', $item['value'])) {
340 form_set_error($field['field_name'], t('"%value" is not a valid Guadeloupe zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
341 }
342 if ($field['type'] == 'cl_zipcode' && !valid_zipcode('cl', $item['value'])) {
343 form_set_error($field['field_name'], t('"%value" is not a valid Chilean zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
344 }
345 if ($field['type'] == 'ar_zipcode' && !valid_zipcode('ar', $item['value'])) {
346 form_set_error($field['field_name'], t('"%value" is not a valid Argentinan zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
347 }
348 if ($field['type'] == 'br_zipcode' && !valid_zipcode('br', $item['value'])) {
349 form_set_error($field['field_name'], t('"%value" is not a valid Brazilian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
350 }
351 if ($field['type'] == 'uk_zipcode' && !valid_zipcode('uk', $item['value'])) {
352 form_set_error($field['field_name'], t('"%value" is not a valid United Kingom postal code.<br />Postal codes should be like AB1 C23 or AB1C23 ...', array('%value' => $item['value'])));
353 }
354 if ($field['type'] == 'be_zipcode' && !valid_zipcode('be', $item['value'])) {
355 form_set_error($field['field_name'], t('"%value" is not a valid Belgian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
356 }
357 if ($field['type'] == 'nl_zipcode' && !valid_zipcode('nl', $item['value'])) {
358 form_set_error($field['field_name'], t('"%value" is not a valid Dutch zipcode.<br />Zipcodes should contain 4 numbers followed by 2 letter', array('%value' => $item['value'])));
359 }
360 if ($field['type'] == 'de_zipcode' && !valid_zipcode('de', $item['value'])) {
361 form_set_error($field['field_name'], t('"%value" is not a valid German zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
362 }
363 if ($field['type'] == 'ch_zipcode' && !valid_zipcode('ch', $item['value'])) {
364 form_set_error($field['field_name'], t('"%value" is not a valid Swiss zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
365 }
366 if ($field['type'] == 'it_zipcode' && !valid_zipcode('it', $item['value'])) {
367 form_set_error($field['field_name'], t('"%value" is not a valid Italian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
368 }
369 if ($field['type'] == 'ad_zipcode' && !valid_zipcode('ad', $item['value'])) {
370 form_set_error($field['field_name'], t('"%value" is not a valid Andorran zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
371 }
372 if ($field['type'] == 'hu_zipcode' && !valid_zipcode('hu', $item['value'])) {
373 form_set_error($field['field_name'], t('"%value" is not a valid Hungarian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
374 }
375 if ($field['type'] == 'ee_zipcode' && !valid_zipcode('ee', $item['value'])) {
376 form_set_error($field['field_name'], t('"%value" is not a valid Estonian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
377 }
378 if ($field['type'] == 'hr_zipcode' && !valid_zipcode('hr', $item['value'])) {
379 form_set_error($field['field_name'], t('"%value" is not a valid Croatian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
380 }
381 if ($field['type'] == 'by_zipcode' && !valid_zipcode('by', $item['value'])) {
382 form_set_error($field['field_name'], t('"%value" is not a valid Belarusian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
383 }
384 if ($field['type'] == 'ru_zipcode' && !valid_zipcode('ru', $item['value'])) {
385 form_set_error($field['field_name'], t('"%value" is not a valid Russian zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
386 }
387 if ($field['type'] == 'cn_zipcode' && !valid_zipcode('cn', $item['value'])) {
388 form_set_error($field['field_name'], t('"%value" is not a valid Chinese zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
389 }
390 if ($field['type'] == 'jp_zipcode' && !valid_zipcode('jp', $item['value'])) {
391 form_set_error($field['field_name'], t('"%value" is not a valid Japanese zipcode.<br />Zipcodes should contain ...', array('%value' => $item['value'])));
392 }
393 if ($field['type'] == 'au_zipcode' && !valid_zipcode('au', $item['value'])) {
394 form_set_error($field['field_name'], t('"%value" is not a valid Australian zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
395 }
396 if ($field['type'] == 'nz_zipcode' && !valid_zipcode('nz', $item['value'])) {
397 form_set_error($field['field_name'], t('"%value" is not a valid New Zealand zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
398 }
399 if ($field['type'] == 'dk_zipcode' && !valid_zipcode('dk', $item['value'])) {
400 form_set_error($field['field_name'], t('"%value" is not a valid Danish zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
401 }
402 if ($field['type'] == 'se_zipcode' && !valid_zipcode('se', $item['value'])) {
403 form_set_error($field['field_name'], t('"%value" is not a valid Swedish zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
404 }
405 if ($field['type'] == 'pt_zipcode' && !valid_zipcode('pt', $item['value'])) {
406 form_set_error($field['field_name'], t('"%value" is not a valid Portuguese zipcode.<br />Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
407 }
408 if ($field['type'] == 'cy_zipcode' && !valid_zipcode('cy', $item['value'])) {
409 form_set_error($field['field_name'], t('"%value" is not a valid Cyprus zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
410 }
411 if ($field['type'] == 'dz_zipcode' && !valid_zipcode('dz', $item['value'])) {
412 form_set_error($field['field_name'], t('"%value" is not a valid Algerian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
413 }
414 if ($field['type'] == 'gw_zipcode' && !valid_zipcode('gw', $item['value'])) {
415 form_set_error($field['field_name'], t('"%value" is not a valid Guinea Bissau zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
416 }
417 if ($field['type'] == 'eg_zipcode' && !valid_zipcode('eg', $item['value'])) {
418 form_set_error($field['field_name'], t('"%value" is not a valid Egyptian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
419 }
420 if ($field['type'] == 'bn_zipcode' && !valid_zipcode('bn', $item['value'])) {
421 form_set_error($field['field_name'], t('"%value" is not a valid Brunei Darusalam zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
422 }
423 if ($field['type'] == 'bd_zipcode' && !valid_zipcode('bd', $item['value'])) {
424 form_set_error($field['field_name'], t('"%value" is not a valid Bangladeshi zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
425 }
426 if ($field['type'] == 'in_zipcode' && !valid_zipcode('in', $item['value'])) {
427 form_set_error($field['field_name'], t('"%value" is not a valid Indian zipcode.<br />Zipcodes should ...', array('%value' => $item['value'])));
428 }
429 }
430 }
431 break;
432
433 case 'presave': // corresponds to hook phone_widget 'process form values' in phone-5.x
434 foreach ($node_field as $delta => $item) {
435 //format the phone number
436 if ($item['value'] != '') {
437 if ($field['type'] == 'fr_zipcode') {
438 $node_field[$delta]['value'] = format_zipcode('fr', $node_field[$delta]['value'], $field);
439 }
440 if ($field['type'] == 'ca_zipcode') {
441 $node_field[$delta]['value'] = format_zipcode('ca', $node_field[$delta]['value'], $field);
442 }
443 if ($field['type'] == 'us_zipcode') {
444 $node_field[$delta]['value'] = format_zipcode('us', $node_field[$delta]['value'], $field);
445 }
446 if ($field['type'] == 'gp_zipcode') {
447 $node_field[$delta]['value'] = format_zipcode('gp', $node_field[$delta]['value'], $field);
448 }
449 if ($field['type'] == 'cl_zipcode') {
450 $node_field[$delta]['value'] = format_zipcode('cl', $node_field[$delta]['value'], $field);
451 }
452 if ($field['type'] == 'ar_zipcode') {
453 $node_field[$delta]['value'] = format_zipcode('ar', $node_field[$delta]['value'], $field);
454 }
455 if ($field['type'] == 'br_zipcode') {
456 $node_field[$delta]['value'] = format_zipcode('br', $node_field[$delta]['value'], $field);
457 }
458 if ($field['type'] == 'uk_zipcode') {
459 $node_field[$delta]['value'] = format_zipcode('uk', $node_field[$delta]['value'], $field);
460 }
461 if ($field['type'] == 'be_zipcode') {
462 $node_field[$delta]['value'] = format_zipcode('be', $node_field[$delta]['value'], $field);
463 }
464 if ($field['type'] == 'nl_zipcode') {
465 $node_field[$delta]['value'] = format_zipcode('nl', $node_field[$delta]['value'], $field);
466 }
467 if ($field['type'] == 'de_zipcode') {
468 $node_field[$delta]['value'] = format_zipcode('de', $node_field[$delta]['value'], $field);
469 }
470 if ($field['type'] == 'ch_zipcode') {
471 $node_field[$delta]['value'] = format_zipcode('ch', $node_field[$delta]['value'], $field);
472 }
473 if ($field['type'] == 'it_zipcode') {
474 $node_field[$delta]['value'] = format_zipcode('it', $node_field[$delta]['value'], $field);
475 }
476 if ($field['type'] == 'ad_zipcode') {
477 $node_field[$delta]['value'] = format_zipcode('ad', $node_field[$delta]['value'], $field);
478 }
479 if ($field['type'] == 'hu_zipcode') {
480 $node_field[$delta]['value'] = format_zipcode('hu', $node_field[$delta]['value'], $field);
481 }
482 if ($field['type'] == 'ee_zipcode') {
483 $node_field[$delta]['value'] = format_zipcode('ee', $node_field[$delta]['value'], $field);
484 }
485 if ($field['type'] == 'hr_zipcode') {
486 $node_field[$delta]['value'] = format_zipcode('hr', $node_field[$delta]['value'], $field);
487 }
488 if ($field['type'] == 'by_zipcode') {
489 $node_field[$delta]['value'] = format_zipcode('by', $node_field[$delta]['value'], $field);
490 }
491 if ($field['type'] == 'ru_zipcode') {
492 $node_field[$delta]['value'] = format_zipcode('ru', $node_field[$delta]['value'], $field);
493 }
494 if ($field['type'] == 'cn_zipcode') {
495 $node_field[$delta]['value'] = format_zipcode('cn', $node_field[$delta]['value'], $field);
496 }
497 if ($field['type'] == 'jp_zipcode') {
498 $node_field[$delta]['value'] = format_zipcode('jp', $node_field[$delta]['value'], $field);
499 }
500 if ($field['type'] == 'au_zipcode') {
501 $node_field[$delta]['value'] = format_zipcode('au', $node_field[$delta]['value'], $field);
502 }
503 if ($field['type'] == 'nz_zipcode') {
504 $node_field[$delta]['value'] = format_zipcode('nz', $node_field[$delta]['value'], $field);
505 }
506 if ($field['type'] == 'dk_zipcode') {
507 $node_field[$delta]['value'] = format_zipcode('dk', $node_field[$delta]['value'], $field);
508 }
509 if ($field['type'] == 'se_zipcode') {
510 $node_field[$delta]['value'] = format_zipcode('se', $node_field[$delta]['value'], $field);
511 }
512 if ($field['type'] == 'pt_zipcode') {
513 $node_field[$delta]['value'] = format_zipcode('pt', $node_field[$delta]['value'], $field);
514 }
515 if ($field['type'] == 'cy_zipcode') {
516 $node_field[$delta]['value'] = format_zipcode('cy', $node_field[$delta]['value'], $field);
517 }
518 if ($field['type'] == 'dz_zipcode') {
519 $node_field[$delta]['value'] = format_zipcode('dz', $node_field[$delta]['value'], $field);
520 }
521 if ($field['type'] == 'gw_zipcode') {
522 $node_field[$delta]['value'] = format_zipcode('gw', $node_field[$delta]['value'], $field);
523 }
524 if ($field['type'] == 'eg_zipcode') {
525 $node_field[$delta]['value'] = format_zipcode('eg', $node_field[$delta]['value'], $field);
526 }
527 if ($field['type'] == 'bn_zipcode') {
528 $node_field[$delta]['value'] = format_zipcode('bn', $node_field[$delta]['value'], $field);
529 }
530 if ($field['type'] == 'bd_zipcode') {
531 $node_field[$delta]['value'] = format_zipcode('bd', $node_field[$delta]['value'], $field);
532 }
533 if ($field['type'] == 'in_zipcode') {
534 $node_field[$delta]['value'] = format_zipcode('in', $node_field[$delta]['value'], $field);
535 }
536 }
537 }
538 break;
539 }
540 }
541
542 /**
543 * Implements hook_field_formatter_info().
544 *
545 * The default behavior of formatters is that they will create
546 * a theme for a single field value.
547 *
548 * Setting 'multiple values' to CONTENT_HANDLE_FIELD will create
549 * a formatter that will receive all the values of a field so you
550 * can, for instance, plot all the values on a map or in a graph.
551 *
552 * The 'view' operation (handled by the Content module) constructs the
553 * $node in a way that you can use drupal_render() to display the
554 * formatted output for an individual field.
555 *
556 * i.e. print drupal_render($node->field_foo);
557 *
558 * The code now supports both single value formatters, which theme an
559 * individual item value as has been done in previous version of CCK,
560 * and multiple value formatters, which theme all values for the field
561 * in a single theme. The multiple value formatters could be used, for
562 * instance, to plot field values on a single map or display them
563 * in a graph. Single value formatters are the default, multiple value
564 * formatters can be designated as such in formatter_info().
565 *
566 * The node array will look like:
567 *
568 * 'Single value' formatter:
569 * $node->content['field_foo'] = array(
570 * '#type' => 'content_field',
571 * '#title' => 'label'
572 * '#field_name' => 'field_name',
573 * '#node' => $node,
574 * 'items' =>
575 * 0 => array(
576 * '#theme' => $theme,
577 * '#field_name' => 'field_name',
578 * '#type_name' => $node->type,
579 * '#formatter' => $formatter_name,
580 * '#item' => $items[0],
581 * ),
582 * 1 => array(
583 * '#theme' => $theme,
584 * '#field_name' => 'field_name',
585 * '#type_name' => $node->type,
586 * '#formatter' => $formatter_name,
587 * '#item' => $items[1],
588 * ),
589 * ),
590 * );
591 * 'Multiple value' formatter:
592 * $node->content['field_foo'] = array(
593 * '#type' => 'content_field',
594 * '#title' => 'label'
595 * '#field_name' => 'field_name',
596 * '#node' => $node,
597 * 'items' => array(
598 * '#theme' => $theme,
599 * '#field_name' => 'field_name',
600 * '#type_name' => $node->type,
601 * '#formatter' => $formatter_name,
602 * 0 => array(
603 * '#item' => $items[0],
604 * ),
605 * 1 => array(
606 * '#item' => $items[1],
607 * ),
608 * ),
609 * );
610 */
611 function zipcode_field_formatter_info() {
612 return array(
613 'default' => array(
614 'label' => 'Default',
615 'field types' => array(
616 'fr_zipcode',
617 'ca_zipcode',
618 'us_zipcode',
619 'gp_zipcode',
620 'cl_zipcode',
621 'ar_zipcode',
622 'br_zipcode',
623 'uk_zipcode',
624 'be_zipcode',
625 'nl_zipcode',
626 'de_zipcode',
627 'ch_zipcode',
628 'it_zipcode',
629 'ad_zipcode',
630 'hu_zipcode',
631 'ee_zipcode',
632 'hr_zipcode',
633 'by_zipcode',
634 'ru_zipcode',
635 'cn_zipcode',
636 'jp_zipcode',
637 'au_zipcode',
638 'nz_zipcode',
639 'dk_zipcode',
640 'se_zipcode',
641 'pt_zipcode',
642 'cy_zipcode',
643 'dz_zipcode',
644 'gw_zipcode',
645 'eg_zipcode',
646 'bn_zipcode',
647 'bd_zipcode',
648 'in_zipcode'
649 ),
650 'multiple values' => CONTENT_HANDLE_CORE,
651 ),
652 );
653 }
654
655 /**
656 * Implements hook_field_formatter().
657 *
658 * Prepare an individual item for viewing in a browser.
659 *
660 * @param $field
661 * The field the action is being performed on.
662 * @param $item
663 * An array, keyed by column, of the data stored for this item in this field.
664 * @param $formatter
665 * The name of the formatter being used to display the field.
666 * @param $node
667 * The node object, for context. Will be NULL in some cases.
668 * Warning: when displaying field retrieved by Views, $node will not
669 * be a "full-fledged" node object, but an object containg the data returned
670 * by the Views query (at least nid, vid, changed)
671 * @return
672 * An HTML string containing the formatted item.
673 *
674 * In a multiple-value field scenario, this function will be called once per
675 * value currently stored in the field. This function is also used as the handler
676 * for viewing a field in a views.module tabular listing.
677 *
678 * It is important that this function at the minimum perform security
679 * transformations such as running check_plain() or check_markup().
680 */
681 function zipcode_field_formatter($field, $item, $formatter, $node) {
682 if (!isset($item['value'])) {
683 return '';
684 }
685 if ($field['text_processing']) {
686 $text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview));
687 }
688 else {
689 $text = check_plain($item['value']);
690 }
691 return $text;
692 }
693
694 /**
695 * Implements hook_widget_info().
696 *
697 * Here we indicate that the content module will handle
698 * the default value and multiple values for these widgets.
699 *
700 * Callbacks can be omitted if default handing is used.
701 * They're included here just so this module can be used
702 * as an example for custom modules that might do things
703 * differently.
704 *
705 * IMPORTANT! - field and widget names will be truncated to 32 characters in
706 * the database and in internal arrays, like content_fields().
707 */
708 function zipcode_widget_info() {
709 return array(
710 'zipcode_textfield' => array(
711 'label' => t('Textfield'),
712 'field types' => array(
713 'fr_zipcode',
714 'ca_zipcode',
715 'us_zipcode',
716 'gp_zipcode',
717 'cl_zipcode',
718 'ar_zipcode',
719 'br_zipcode',
720 'uk_zipcode',
721 'be_zipcode',
722 'nl_zipcode',
723 'de_zipcode',
724 'ch_zipcode',
725 'it_zipcode',
726 'ad_zipcode',
727 'hu_zipcode',
728 'ee_zipcode',
729 'hr_zipcode',
730 'by_zipcode',
731 'ru_zipcode',
732 'cn_zipcode',
733 'jp_zipcode',
734 'au_zipcode',
735 'nz_zipcode',
736 'dk_zipcode',
737 'se_zipcode',
738 'pt_zipcode',
739 'cy_zipcode',
740 'dz_zipcode',
741 'gw_zipcode',
742 'eg_zipcode',
743 'bn_zipcode',
744 'bd_zipcode',
745 'in_zipcode'
746 ),
747 'multiple values' => CONTENT_HANDLE_CORE,
748 'callbacks' => array(
749 'default value' => CONTENT_CALLBACK_DEFAULT,
750 ),
751 ),
752 );
753 }
754
755 /**
756 * Implements hook_widget_settings().
757 *
758 * Handle the parameters for a widget.
759 *
760 * @param $op
761 * The operation to be performed. Possible values:
762 * - "form": Display the widget settings form.
763 * - "validate": Check the widget settings form for errors.
764 * - "save": Declare which pieces of information to save back to the database.
765 * @param $widget
766 * The widget on which the operation is to be performed.
767 * @return
768 * This varies depending on the operation.
769 * - "form": an array of form elements to add to the settings page.
770 * - "validate": no return value. Use form_set_error().
771 * - "save": an array of names of form elements to be saved in the database.
772 */
773 function zipcode_widget_settings($op, $widget) {
774 switch ($op) {
775 case 'form':
776 $form = array();
777 $size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60;
778 $form['input']['size'] = array(
779 '#type' => 'textfield',
780 '#title' => t('Size of textfield'),
781 '#default_value' => $size,
782 '#element_validate' => array('_element_validate_integer_positive'),
783 '#required' => TRUE,
784 );
785 return $form;
786
787 case 'validate':
788 break; //do nothing
789
790 case 'save':
791 return array('size');
792 }
793 }
794
795
796 /**
797 * Implements hook_widget().
798 *
799 * Attach a single form element to the form. It will be built out and
800 * validated in the callback(s) listed in hook_elements. We build it
801 * out in the callbacks rather than here in hook_widget so it can be
802 * plugged into any module that can provide it with valid
803 * $field information.
804 *
805 * Content module will set the weight, field name and delta values
806 * for each form element. This is a change from earlier CCK versions
807 * where the widget managed its own multiple values.
808 *
809 * If there are multiple values for this field, the content module will
810 * call this function as many times as needed.
811 *
812 * @param $form
813 * the entire form array, $form['#node'] holds node information
814 * @param $form_state
815 * the form_state, $form_state['values'][$field['field_name']]
816 * holds the field's form values.
817 * @param $field
818 * the field array
819 * @param $items
820 * array of default values for this field
821 * @param $delta
822 * the order of this item in the array of subelements (0, 1, 2, etc)
823 *
824 * @return
825 * the form item for a single element for this field
826 */
827 function zipcode_widget(&$form, &$form_state, $field, $items, $delta = 0) {
828 $element = array(
829 '#type' => $field['widget']['type'],
830 '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
831 );
832 return $element;
833 }
834
835 /**
836 * Implements hook_content_is_empty().
837 *
838 * NEW REQUIRED HOOK!
839 *
840 * This function tells the content module whether or not to consider
841 * the $item to be empty. This is used by the content module
842 * to remove empty, non-required values before saving them.
843 */
844 function zipcode_content_is_empty($item, $field) {
845 if (empty($item['value'])) {
846 return TRUE;
847 }
848 return FALSE;
849 }
850
851 /**
852 * Implements FAPI hook_elements().
853 *
854 * Any FAPI callbacks needed for individual widgets can be declared here,
855 * and the element will be passed to those callbacks for processing.
856 *
857 * Drupal will automatically theme the element using a theme with
858 * the same name as the hook_elements key.
859 *
860 * Autocomplete_path is not used by text_widget but other widgets can use it
861 * (see nodereference and userreference).
862 */
863 function zipcode_elements() {
864 return array(
865 'zipcode_textfield' => array(
866 '#input' => TRUE,
867 '#columns' => array('value'), '#delta' => 0,
868 '#process' => array('zipcode_textfield_process'),
869 '#autocomplete_path' => FALSE,
870 ),
871 );
872 }
873
874 /**
875 * FAPI theme for an individual text elements.
876 *
877 * The textfield or textarea is already rendered by the
878 * textfield or textarea themes and the html output
879 * lives in $element['#children']. Override this theme to
880 * make custom changes to the output.
881 *
882 * $element['#field_name'] contains the field name
883 * $element['#delta] is the position of this element in the group
884 */
885 function theme_zipcode_textfield($element) {
886 return $element['#children'];
887 }
888
889 /**
890 * Process an individual element.
891 *
892 * Build the form element. When creating a form using FAPI #process,
893 * note that $element['#value'] is already set.
894 *
895 * The $fields array is in $form['#field_info'][$element['#field_name']].
896 */
897 function zipcode_textfield_process($element, $edit, $form_state, $form) {
898 $field = $form['#field_info'][$element['#field_name']];
899 $field_key = $element['#columns'][0];
900 $delta = $element['#delta'];
901 $element[$field_key] = array(
902 '#type' => 'textfield',
903 '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
904 '#size' => !empty($field['widget']['size']) ? $field['widget']['size'] : 60,
905 '#autocomplete_path' => FALSE,
906 // The following values were set by the content module and need
907 // to be passed down to the nested element.
908 '#title' => $element['#title'],
909 '#description' => $element['#description'],
910 '#required' => $element['#required'],
911 '#field_name' => $element['#field_name'],
912 '#type_name' => $element['#type_name'],
913 '#delta' => $element['#delta'],
914 '#columns' => $element['#columns'],
915 );
916
917 if (!empty($field['max_length'])) {
918 $element[$field_key]['#maxlength'] = $field['max_length'];
919 }
920 if (!empty($field['text_processing'])) {
921 $filter_key = $element['#columns'][1];
922 $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
923 $parents = array_merge($element['#parents'] , array($filter_key));
924 $element[$filter_key] = filter_form($format, 1, $parents);
925 }
926
927 // Used so that hook_field('validate') knows where to flag an error.
928 $element['_error_element'] = array(
929 '#type' => 'value',
930 '#value' => implode('][', array_merge($element['#parents'], array($field_key))),
931 );
932
933 return $element;
934 }
935
936 /**
937 * Theme function for 'default' text field formatter.
938 */
939 function theme_zipcode_formatter_default($element) {
940 return $element['#item']['value'];
941 }
942
943 /**
944 * Country supported or not by the module ?
945 *
946 * @param string $countrycode
947 * @return boolean Returns a boolean containting the answer to the question.
948 */
949 function zipcode_supported_countrycode($countrycode) {
950 if ($countrycode == 'fr'
951 || $countrycode == 'ca'
952 || $countrycode == 'us'
953 || $countrycode == 'gp'
954 || $countrycode == 'cl'
955 || $countrycode == 'ar'
956 || $countrycode == 'br'
957 || $countrycode == 'uk'
958 || $countrycode == 'be'
959 || $countrycode == 'nl'
960 || $countrycode == 'de'
961 || $countrycode == 'ch'
962 || $countrycode == 'it'
963 || $countrycode == 'ad'
964 || $countrycode == 'hu'
965 || $countrycode == 'ee'
966 || $countrycode == 'hr'
967 || $countrycode == 'by'
968 || $countrycode == 'ru'
969 || $countrycode == 'cn'
970 || $countrycode == 'jp'
971 || $countrycode == 'au'
972 || $countrycode == 'nz'
973 || $countrycode == 'dk'
974 || $countrycode == 'se'
975 || $countrycode == 'pt'
976 || $countrycode == 'cy'
977 || $countrycode == 'dz'
978 || $countrycode == 'gw'
979 || $countrycode == 'eg'
980 || $countrycode == 'bn'
981 || $countrycode == 'bd'
982 || $countrycode == 'in'
983 ) {
984 return TRUE;
985 }
986 else {
987 //Country not taken into account yet
988 return FALSE;
989 }
990 }
991
992 /**
993 * Verification for zipcodes.
994 *
995 * @param string $countrycode
996 * @param string $zipcodestring
997 * @return boolean Returns boolean FALSE if the zipcode is not valid.
998 */
999 function valid_zipcode($countrycode, $zipcodestring) {
1000
1001 $countrycode = trim($countrycode);
1002 $zipcodestring = trim($zipcodestring);
1003
1004 if (zipcode_supported_countrycode($countrycode)) {
1005 //drupal_set_message('langue = ' . $countrycode, 'error');
1006
1007 $valid_zipcode_function = 'valid_' . $countrycode . '_zipcode';
1008 module_load_include('inc', 'zipcode', 'countries/zipcode.' . $countrycode);
1009
1010 if (function_exists($valid_zipcode_function)) {
1011 return $valid_zipcode_function($zipcodestring);
1012 }
1013 else {
1014 return FALSE;
1015 }
1016 }
1017 else {
1018 //Country not taken into account yet
1019 return FALSE;
1020 }
1021 }
1022
1023 /**
1024 * Formatting for Zipcode.
1025 *
1026 * @param string $countrycode
1027 * @param string $zipcodestring
1028 * @return string Returns a string containting the zipcode with some formatting.
1029 */
1030 function format_zipcode($countrycode, $zipcodestring, $field) {
1031
1032 $countrycode = trim($countrycode);
1033 $zipcodestring = trim($zipcodestring);
1034
1035 if (zipcode_supported_countrycode($countrycode)) {
1036 //drupal_set_message('langue = ' . $countrycode, 'error');
1037
1038 $format_zipcode_function = 'format_' . $countrycode . '_zipcode';
1039 module_load_include('inc', 'zipcode', 'countries/zipcode.' . $countrycode);
1040
1041 if (function_exists($format_zipcode_function)) {
1042 return $format_zipcode_function($zipcodestring, $field);
1043 }
1044 else {
1045 return FALSE;
1046 }
1047 }
1048 else {
1049 //Country not taken into account yet
1050 return FALSE;
1051 }
1052 }
1053
1054 /**
1055 * Implements hook_token_list().
1056 */
1057 function zipcode_token_list($type = 'all') {
1058 if ($type == 'field' || $type == 'all') {
1059 $tokens['zipcode']['raw'] = t('Raw zipcode numbers');
1060 $tokens['zipcode']['formatted'] = t('Formatted zipcode numbers');
1061 return $tokens;
1062 }
1063 }
1064
1065 /**
1066 * Implements hook_token_values().
1067 */
1068 function zipcode_token_values($type, $object = NULL, $options = array()) {
1069 if ($type == 'field') {
1070 $item = $object[0];
1071 $tokens['raw'] = $item['value'];
1072 $tokens['formatted'] = $item['view'];
1073 return $tokens;
1074 }
1075 }
1076
1077 /**
1078 * Implements hook_node_import_fields().
1079 * Integrates with the node_import module.
1080 */
1081 function zipcode_node_import_fields($type) {
1082 $fields = array();
1083
1084 foreach (node_import_cck_fields($type, 'zipcode') as $fieldname => $fieldinfo) {
1085 foreach ($fieldinfo['columns'] as $colname => $colinfo) {
1086 $cck_fieldname = node_import_cck_name($fieldname, $colname);
1087
1088 $fields[$cck_fieldname] = node_import_cck_field_defaults($fieldinfo);
1089
1090 switch ($colname) {
1091 case 'value':
1092 $fields[$cck_fieldname]['title'] = $fieldinfo['widget']['label'];
1093 $fields[$cck_fieldname]['is_required'] = $fieldinfo['required'];
1094 break;
1095
1096 default:
1097 $fields[$cck_fieldname]['title'] = t('Unsupported:') . ' ' . $fieldinfo['widget']['label'] . ' - ' . $colname;
1098 break;
1099 }
1100 }
1101 }
1102
1103 return $fields;
1104 }