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

Contents of /contributions/modules/cck_address/cck_address.module

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


Revision 1.18 - (show annotations) (download) (as text)
Thu Jul 23 04:28:39 2009 UTC (4 months, 1 week ago) by newzeal
Branch: MAIN
CVS Tags: DRUPAL-5--2-4, HEAD
Changes since 1.17: +3 -3 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 /**
3 * This is the cck_address module for use with CCK.
4 *
5 * <p>This file contains information on the cck_address module. The module adds to
6 * the field types available for inclusion in a content type definition. This field
7 * is made up of several standard html form fields - street 1, street 2, apartment/suite
8 * number, city, state, ZIP code, country and other.</p>
9 * <p>Street 1, street 2, apartment/suite number, other and city can each have a maximum length
10 * set by the admin. State is a dropdown list with values supplied via a database table
11 * which is part of this module. ZIP is a 5 digit integer field.</p>
12 * TODO add a hook that will allow field names to change via jquery or ajax when a country is
13 * selected. ex. if CANADA is selected, 'State' could change to 'Province'.
14 * TODO add an option in the admin settings that if some kind of geo-location module is enabled
15 * that the address field defaults can be populated with data from them INSTEAD of the fields
16 * that can be filled in now manually
17 *
18 * @version $Id: cck_address.module,v 1.17 2009/07/23 04:16:06 newzeal Exp $;
19 * @package CCK_Address
20 * @category NeighborForge
21 * @author Ryan Constantine
22 * @filesource
23 * @license http://www.gnu.org/licenses/gpl.txt GNU_GENERAL_PUBLIC_LICENSE
24 * @link none yet
25 */
26
27 /**
28 * Implementation of hook_menu
29 *
30 * Required for Activeselect's AJAX capabilities.
31 * @return array An array of arrays, to add menu entries to the system menu.
32 */
33 function cck_address_menu($may_cache) {
34
35 $items = array();
36 if ($may_cache) {
37 $items[] = array(
38 'path' => 'cck_address/activeselect',
39 'title' => t('Activeselect CCK Address'),
40 'callback' => 'drupal_get_form',
41 'callback arguments' => array('cck_address_activeselect'),
42 'access' => TRUE,
43 'type' => MENU_CALLBACK,
44 );
45 $items[] = array(
46 'path' => 'cck_address/activeaddress1',
47 'title' => t('Activeselect CCK Address of prestored addresses'),
48 'callback' => 'drupal_get_form',
49 'callback arguments' => array('cck_address_activeaddress1'),
50 'access' => user_access('administer content'),
51 'type' => MENU_CALLBACK,
52 );
53 $items[] = array(
54 'path' => 'admin/settings/cck_address',
55 'title' => t('CCK Address Settings'),
56 'callback' => 'drupal_get_form',
57 'callback arguments' => array('cck_address_settings_form'),
58 'access' => user_access('administer site configuration'),
59 'type' => MENU_NORMAL_ITEM
60 );
61 }
62
63 return $items;
64 } // function cck_address_menu()
65
66
67 function cck_address_settings_form() {
68 // general settings
69 $form['cck_address_validation'] = array(
70 '#type' => 'checkbox',
71 '#title' => t('Address Validation'),
72 '#default_value' => variable_get('cck_address_validation', 1),
73 '#description' => t("The CCK Address module has a validation routine. While this can be useful it doesn't take into account unusual characters. Turn this off if you want to disable the validation routine. Note that this only works with the Canadian data."),
74 );
75 return system_settings_form($form);
76 }
77
78 /**
79 * Implementation of hook_perm in order to restrict database access to bonafide site admins.
80 * See note below in cck_address_field_settings.
81 */
82 function cck_address_perm() {
83 return array('administer databases');
84 }
85
86 /**
87 * Implementation of hook_field_info().
88 *
89 * @return
90 * An array keyed by field type name. Each element of the array is an associative
91 * array with these keys and values:
92 * - "label": The human-readable label for the field type.
93 */
94 function cck_address_field_info() {
95 return array(
96 'cck_address' => array('label' => 'Address'),
97 );
98 } //function cck_address_field_info()
99
100 /**
101 * Implementation of hook_field_settings().
102 *
103 * See the README.txt for more info.
104 *
105 * @param $op
106 * The operation to be performed.
107 * @param $field
108 * The field on which the operation is to be performed.
109 * @return
110 * This varies depending on the operation.
111 * - "form": an array of form elements to add to
112 * the settings page.
113 * - "validate": no return value. Use form_set_error().
114 * - "save": an array of names of form elements to
115 * be saved in the database.
116 * - "database columns": an array keyed by column name, with arrays of column
117 * information as values.
118 * - "filters": an array whose values are 'filters'
119 * definitions as expected by views.module (see Views Documentation).
120 * - "callbacks": an array describing the field's behaviour regarding hook_field
121 * operations. The array is keyed by hook_field operations ('view', 'validate'...)
122 * and has the following possible values :
123 * CONTENT_CALLBACK_NONE : do nothing for this operation
124 * CONTENT_CALLBACK_CUSTOM : use the behaviour in hook_field(operation)
125 * CONTENT_CALLBACK_DEFAULT : use content.module's default bahaviour
126 * Note : currently only the 'view' operation implements this feature.
127 * All other field operation implemented by the module _will_ be executed
128 * no matter what.
129 */
130 function cck_address_field_settings($op, $field) {
131 switch ($op) {
132 case 'form':
133 $activeselect = module_exists('activeselect');
134 $form = array();
135 if (module_exists('location_node')) {
136 $form['location_integration'] = array(
137 '#type' => 'checkbox',
138 '#title' => t("Copy address to the node's location fields"),
139 '#description' => t("If a node is location enabled, overwite any location information with the location entered into this field when saving."),
140 '#default_value' => isset($field['location_integration']) ? $field['location_integration'] : 0,
141 '#return_value' => 1,
142 );
143 }
144 $form['state_abbrv'] = array(
145 '#type' => 'select',
146 '#title' => t('Display States/Provinces as'),
147 '#description' => t('Be sure to check <em>Allow other countries?</em> below if you select Free-text.'),
148 '#default_value' => isset($field['state_abbrv']) ? $field['state_abbrv'] : 0,
149 '#options' => array('Select with full names', 'Select with abbreviations', 'Free-text entry'),
150 );
151 $form['country_abbrv'] = array(
152 '#type' => 'checkbox',
153 '#title' => t('Use abbreviations for display of Countries?'),
154 '#default_value' => isset($field['country_abbrv']) ? $field['country_abbrv'] : 0,
155 '#return_value' => 1,
156 );
157 $form['countries'] = array(
158 '#type' => 'fieldset',
159 '#title' => t('Allowed Countries'),
160 '#description' => t('Check the countries from which you\'ll allow user addresses.'),
161 '#collapsible' => TRUE,
162 '#collapsed' => TRUE,
163 );
164 $all_countries = array();
165 $sql = "SELECT cas.country_name, cas.country_code FROM {cck_address_countries} cas ORDER BY cas.country_name ASC";
166 $results = db_query($sql);
167 while ($result = db_fetch_object($results)) {
168 $all_countries[$result->country_code] = $result->country_name;
169 }
170 $state_options = cck_address_get_states($all_countries);
171 $form['countries']['other_countries'] = array(
172 '#type' => 'checkbox',
173 '#title' => t('Allow <em>other</em> countries?'),
174 '#description' => t('Disable validation of state field against the state database. Admin users should validate the state field of sumitted addresses manually.'),
175 '#default_value' => isset($field['other_countries']) ? $field['other_countries'] : 0,
176 '#return_value' => 1,
177 );
178 $form['countries']['clist'] = array(
179 '#type' => 'checkboxes',
180 '#title' => t('Countries'),
181 '#default_value' => isset($field['clist']) ? $field['clist'] : '',
182 '#options' => $all_countries,
183 );
184
185 //for selecting which fields to display
186 $field_display_default = array();
187 if (is_array($field['fielddisplay'])) {
188 foreach ($field['fielddisplay'] as $part => $val) {
189 if ($val) { $field_display_default[] = $part; }
190 }
191 }
192 else {
193 $field_display_default = array('field_display_street1', 'field_display_street2', 'field_display_apt', 'field_display_city', 'field_display_state', 'field_display_country', 'field_display_zip', 'field_display_other');
194 }
195 $form['field_display'] = array(
196 '#type' => 'fieldset',
197 '#title' => t('Customize fields to use'),
198 '#collapsible' => TRUE,
199 '#collapsed' => TRUE,
200 '#description' => t('Here you can select which fields to use. Note: normally, the following fields are REQUIRED but will not be (naturally) when not used: Country, State, Zip, City, Address'),
201 );
202 $form['field_display']['fielddisplay'] = array(
203 '#type' => 'checkboxes',
204 '#required' => FALSE,
205 '#default_value' => $field_display_default,
206 '#options' => array(
207 'field_display_street1' => t("Address"),
208 'field_display_street2' => t("Address continued"),
209 'field_display_apt' => t("Apt/suite number"),
210 'field_display_city' => t("City"),
211 'field_display_state' => t("State"),
212 'field_display_zip' => t("Zip"),
213 'field_display_country' => t("Country"),
214 'field_display_other' => t("Other"),
215 ),
216 );
217
218 //maximum lengths of fields
219 $form['max_lengths'] = array(
220 '#type' => 'fieldset',
221 '#title' => t('Maximum field lengths'),
222 '#collapsible' => TRUE,
223 '#collapsed' => TRUE,
224 '#description' => t('You may find that if you don\'t set values here that the placement of the form elements are funky. I designed the CSS file using
225 30, 30, 7, 30, 30 as maximums. Feel free to adjust maximums as you see fit, but realize you may have to adjust the CSS file or override the CSS values
226 in your theme\'s style.css file.'),
227 );
228 $form['max_lengths']['max_length_street1'] = array(
229 '#type' => 'textfield',
230 '#title' => t('Maximum length: address field'),
231 '#size' => 6,
232 '#default_value' => isset($field['max_length_street1']) ? $field['max_length_street1'] : '',
233 '#required' => FALSE,
234 '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
235 );
236 $form['max_lengths']['max_length_street2'] = array(
237 '#type' => 'textfield',
238 '#title' => t('Maximum length: address continued field'),
239 '#size' => 6,
240 '#default_value' => isset($field['max_length_street2']) ? $field['max_length_street2'] : '',
241 '#required' => FALSE,
242 '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
243 );
244 $form['max_lengths']['max_length_apt'] = array(
245 '#type' => 'textfield',
246 '#title' => t('Maximum length: apt field'),
247 '#size' => 6,
248 '#default_value' => isset($field['max_length_apt']) ? $field['max_length_apt'] : '',
249 '#required' => FALSE,
250 '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
251 );
252 $form['max_lengths']['max_length_city'] = array(
253 '#type' => 'textfield',
254 '#title' => t('Maximum length: city field'),
255 '#size' => 6,
256 '#default_value' => isset($field['max_length_city']) ? $field['max_length_city'] : '',
257 '#required' => FALSE,
258 '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
259 );
260 $form['max_lengths']['max_length_other'] = array(
261 '#type' => 'textfield',
262 '#title' => t('Maximum length: other field'),
263 '#size' => 6,
264 '#default_value' => isset($field['max_length_other']) ? $field['max_length_other'] : '',
265 '#required' => FALSE,
266 '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
267 );
268
269 //for customizing the field titles
270 $form['field_names'] = array(
271 '#type' => 'fieldset',
272 '#title' => t('Customize field names'),
273 '#collapsible' => TRUE,
274 '#collapsed' => TRUE,
275 '#description' => t('Here you can change the field name of any address field. This will aid in translating for other languages.'),
276 );
277 $form['field_names']['field_names_street1'] = array(
278 '#type' => 'textfield',
279 '#title' => t('Address'),
280 '#default_value' => isset($field['field_names_street1']) && $field['field_names_street1'] != '' ? $field['field_names_street1'] : t('Address'),
281 '#required' => FALSE,
282 );
283 $form['field_names']['field_names_street2'] = array(
284 '#type' => 'textfield',
285 '#title' => t('Address continued'),
286 '#default_value' => isset($field['field_names_street2']) && $field['field_names_street2'] != '' ? $field['field_names_street2'] : t('Address continued'),
287 '#required' => FALSE,
288 );
289 $form['field_names']['field_names_apt'] = array(
290 '#type' => 'textfield',
291 '#title' => t('Apt/suite number'),
292 '#default_value' => isset($field['field_names_apt']) && $field['field_names_apt'] != '' ? $field['field_names_apt'] : t('Apt/suite number'),
293 '#required' => FALSE,
294 );
295 $form['field_names']['field_names_city'] = array(
296 '#type' => 'textfield',
297 '#title' => t('City'),
298 '#default_value' => isset($field['field_names_city']) && $field['field_names_city'] != '' ? $field['field_names_city'] : t('City'),
299 '#required' => FALSE,
300 );
301 $form['field_names']['field_names_state'] = array(
302 '#type' => 'textfield',
303 '#title' => t('State'),
304 '#default_value' => isset($field['field_names_state']) && $field['field_names_state'] != '' ? $field['field_names_state'] : t('State'),
305 '#required' => FALSE,
306 );
307 $form['field_names']['field_names_zip'] = array(
308 '#type' => 'textfield',
309 '#title' => t('ZIP'),
310 '#default_value' => isset($field['field_names_zip']) && $field['field_names_zip'] != '' ? $field['field_names_zip'] : t('ZIP'),
311 '#required' => FALSE,
312 );
313 $form['field_names']['field_names_country'] = array(
314 '#type' => 'textfield',
315 '#title' => t('Country'),
316 '#default_value' => isset($field['field_names_country']) && $field['field_names_country'] != '' ? $field['field_names_country'] : t('Country'),
317 '#required' => FALSE,
318 );
319 $form['field_names']['field_names_other'] = array(
320 '#type' => 'textfield',
321 '#title' => t('Other'),
322 '#default_value' => isset($field['field_names_other']) && $field['field_names_other'] != '' ? $field['field_names_other'] : t('Other'),
323 '#required' => FALSE,
324 );
325
326 //for customizing the field order
327 $order_options = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8);
328 $form['field_order'] = array(
329 '#type' => 'fieldset',
330 '#tree' => TRUE,
331 '#title' => t('Customize field display order'),
332 '#collapsible' => TRUE,
333 '#collapsed' => TRUE,
334 '#description' => t('Here you can order the address fields as you wish. This will affect both the form used for input and display on nodes.
335 Be careful to not double up the numbers, there\'s no validation against duplicates here.'),
336 );
337 $form['field_order']['street1'] = array(
338 '#type' => 'select',
339 '#title' => t('Address'),
340 '#default_value' => isset($field['field_order']['street1']) ? $field['field_order']['street1'] : 1,
341 '#options' => $order_options,
342 '#required' => FALSE,
343 );
344 $form['field_order']['street2'] = array(
345 '#type' => 'select',
346 '#title' => t('Address continued'),
347 '#default_value' => isset($field['field_order']['street2']) ? $field['field_order']['street2'] : 2,
348 '#options' => $order_options,
349 '#required' => FALSE,
350 );
351 $form['field_order']['apt'] = array(
352 '#type' => 'select',
353 '#title' => t('Apt/suite number'),
354 '#default_value' => isset($field['field_order']['apt']) ? $field['field_order']['apt'] : 3,
355 '#options' => $order_options,
356 '#required' => FALSE,
357 );
358 $form['field_order']['city'] = array(
359 '#type' => 'select',
360 '#title' => t('City'),
361 '#default_value' => isset($field['field_order']['city']) ? $field['field_order']['city'] : 4,
362 '#options' => $order_options,
363 '#required' => FALSE,
364 );
365 $form['field_order']['state'] = array(
366 '#type' => 'select',
367 '#title' => t('State'),
368 '#default_value' => isset($field['field_order']['state']) ? $field['field_order']['state'] : 5,
369 '#options' => $order_options,
370 '#required' => FALSE,
371 );
372 $form['field_order']['zip'] = array(
373 '#type' => 'select',
374 '#title' => t('ZIP'),
375 '#default_value' => isset($field['field_order']['zip']) ? $field['field_order']['zip'] : 6,
376 '#options' => $order_options,
377 '#required' => FALSE,
378 );
379 $form['field_order']['country'] = array(
380 '#type' => 'select',
381 '#title' => t('Country'),
382 '#default_value' => isset($field['field_order']['country']) ? $field['field_order']['country'] : 7,
383 '#options' => $order_options,
384 '#required' => FALSE,
385 );
386 $form['field_order']['other'] = array(
387 '#type' => 'select',
388 '#title' => t('Other'),
389 '#default_value' => isset($field['field_order']['other']) ? $field['field_order']['other'] : 8,
390 '#options' => $order_options,
391 '#required' => FALSE,
392 );
393
394 //for customizing the field defaults
395 $form['field_defaults'] = array(
396 '#type' => 'fieldset',
397 '#title' => t('Customize field defaults'),
398 '#collapsible' => TRUE,
399 '#collapsed' => TRUE,
400 '#description' => t('Here you can set any defaults that should appear when a new address is to be entered.'),
401 );
402 $form['field_defaults']['field_defaults_street1'] = array(
403 '#type' => 'textfield',
404 '#title' => t('Address'),
405 '#default_value' => isset($field['field_defaults_street1']) && $field['field_defaults_street1'] != '' ? $field['field_defaults_street1'] : '',
406 '#required' => FALSE,
407 );
408 $form['field_defaults']['field_defaults_street2'] = array(
409 '#type' => 'textfield',
410 '#title' => t('Address continued'),
411 '#default_value' => isset($field['field_defaults_street2']) && $field['field_defaults_street2'] != '' ? $field['field_defaults_street2'] : '',
412 '#required' => FALSE,
413 );
414 $form['field_defaults']['field_defaults_apt'] = array(
415 '#type' => 'textfield',
416 '#title' => t('Apt/suite number'),
417 '#default_value' => isset($field['field_defaults_apt']) && $field['field_defaults_apt'] != '' ? $field['field_defaults_apt'] : '',
418 '#required' => FALSE,
419 );
420 $form['field_defaults']['field_defaults_city'] = array(
421 '#type' => 'textfield',
422 '#title' => t('City'),
423 '#default_value' => isset($field['field_defaults_city']) && $field['field_defaults_city'] != '' ? $field['field_defaults_city'] : '',
424 '#required' => FALSE,
425 );
426 if ($activeselect) {
427 $form['field_defaults']['field_defaults_state'] = array(
428 '#type' => 'select',
429 '#title' => t('State'),
430 '#multiple' => FALSE,
431 '#default_value' => isset($field['field_defaults_state']) ? $field['field_defaults_state'] : array(),
432 '#required' => FALSE,
433 '#options' => array(),
434 '#DANGEROUS_SKIP_CHECK' => TRUE,
435 );
436 }
437 else {
438 $form['field_defaults']['field_defaults_state'] = array(
439 '#type' => 'select',
440 '#title' => t('State'),
441 '#multiple' => FALSE,
442 '#default_value' => isset($field['field_defaults_state']) && $field['field_defaults_state'] != '' ? $field['field_defaults_state'] : '',
443 '#required' => FALSE,
444 '#options' => $state_options,
445 );
446 }
447 $form['field_defaults']['field_defaults_zip'] = array(
448 '#type' => 'textfield',
449 '#title' => t('ZIP'),
450 '#default_value' => isset($field['field_defaults_zip']) && $field['field_defaults_zip'] != '' ? $field['field_defaults_zip'] : '',
451 '#required' => FALSE,
452 );
453 if ($activeselect) {
454 $form['field_defaults']['field_defaults_country'] = array(
455 '#type' => 'activeselect',
456 '#title' => t('Country'),
457 '#multiple' => FALSE,
458 '#default_value' => isset($field['field_defaults_country']) ? $field['field_defaults_country'] : array(),
459 '#required' => FALSE,
460 '#options' => $all_countries,
461 '#activeselect_path' => 'cck_address/activeselect',
462 '#activeselect_targets' => str_replace('_', '-', 'field_defaults_state'),
463 '#activeselect_extra' => isset($field['field_defaults_state']) ? $field['field_defaults_state'] : '',
464 );
465 }
466 else {
467 $form['field_defaults']['field_defaults_country'] = array(
468 '#type' => 'select',
469 '#title' => t('Country'),
470 '#multiple' => FALSE,
471 '#default_value' => isset($field['field_defaults_country']) && $field['field_defaults_country'] != '' ? $field['field_defaults_country'] : '',
472 '#required' => FALSE,
473 '#options' => $all_countries,
474 );
475 }
476 $form['field_defaults']['field_defaults_other'] = array(
477 '#type' => 'textfield',
478 '#title' => t('Other'),
479 '#default_value' => isset($field['field_defaults_other']) && $field['field_defaults_other'] != '' ? $field['field_defaults_other'] : '',
480 '#required' => FALSE,
481 );
482
483 //for validating addresses against a known database
484 if ($activeselect && user_access('administer databases')) {
485 $form['select_address_from_db'] = array(
486 '#type' => 'fieldset',
487 '#title' => t('Select from DB'),
488 '#description' => t('Requires the activeselect module. This is for when you know all users\' addresses ahead of time, or you have a limited geography that you\'ve set up.'),
489 '#collapsible' => TRUE,
490 '#collapsed' => TRUE,
491 );
492 $form['select_address_from_db']['select_a_f_d'] = array(
493 '#type' => 'checkbox',
494 '#title' => t('Should users select addresses from a database?'),
495 '#default_value' => isset($field['select_a_f_d']) ? $field['select_a_f_d'] : 0,
496 '#return_value' => 1,
497 );
498 $all_tables = array();
499 $sql = "SHOW tables";
500 $results = db_query($sql);
501 global $db_url;
502 $db_name = substr(strrchr($db_url, "/"), 1);
503 $db_tables = 'Tables_in_' .$db_name;
504 while ($result = db_fetch_object($results)) {
505 $all_tables[$result->$db_tables] = $result->$db_tables;
506 }
507 if (isset($field['select_streetcolumn']) && isset($field['select_street_numcolumn']) && isset($field['select_city']) && isset($field['select_zip'])) {
508 $extra_street_num_city_zip = $field['select_streetcolumn']. ',' .$field['select_street_numcolumn']. ',' .$field['select_city']. ',' .$field['select_zip'];
509 }
510 else {
511 $extra_street_num_city_zip = '';
512 }
513 $form['select_address_from_db']['select_table'] = array(
514 '#type' => 'activeselect',
515 '#title' => t('Select table to use for addresses'),
516 '#default_value' => isset($field['select_table']) ? array($field['select_table']) : array(),
517 '#multiple' => FALSE,
518 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
519 '#options' => $all_tables,
520 '#activeselect_path' => 'cck_address/activeaddress1',
521 '#activeselect_targets' => str_replace('_', '-', 'select_streetcolumn'). ',' .str_replace('_', '-', 'select_street_numcolumn'). ',' .str_replace('_', '-', 'select_city'). ',' .str_replace('_', '-', 'select_zip'),
522 '#activeselect_extra' => $extra_street_num_city_zip,//should be string, not array or object
523 );
524 $form['select_address_from_db']['select_streetcolumn'] = array(
525 '#type' => 'activeselect',
526 '#title' => t('Select column for street'),
527 '#default_value' => isset($field['select_streetcolumn']) ? array($field['select_streetcolumn']) : array(),
528 '#multiple' => FALSE,
529 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
530 '#options' => array(),
531 '#DANGEROUS_SKIP_CHECK' => TRUE,
532 );
533 $form['select_address_from_db']['select_street_numcolumn'] = array(
534 '#type' => 'select',
535 '#title' => t('Select column for street number'),
536 '#default_value' => isset($field['select_street_numcolumn']) ? array($field['select_street_numcolumn']) : array(),
537 '#multiple' => FALSE,
538 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
539 '#options' => array(),
540 '#DANGEROUS_SKIP_CHECK' => TRUE,
541 );
542 $form['select_address_from_db']['select_city'] = array(
543 '#type' => 'select',
544 '#title' => t('Select column for city'),
545 '#default_value' => isset($field['select_city']) ? array($field['select_city']) : array(),
546 '#multiple' => FALSE,
547 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
548 '#options' => array(),
549 '#DANGEROUS_SKIP_CHECK' => TRUE,
550 );
551 $form['select_address_from_db']['select_zip'] = array(
552 '#type' => 'select',
553 '#title' => t('Select column for postal code'),
554 '#default_value' => isset($field['select_zip']) ? array($field['select_zip']) : array(),
555 '#multiple' => FALSE,
556 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
557 '#options' => array(),
558 '#DANGEROUS_SKIP_CHECK' => TRUE,
559 );
560 $form['select_address_from_db']['select_country'] = array(
561 '#type' => 'activeselect',
562 '#title' => t('Select a mandatory country'),
563 '#description' => t('Be sure to select the right country in <em>Allowed Countries</em> above.'),
564 '#default_value' => isset($field['select_country']) ? array($field['select_country']) : array(),
565 '#multiple' => FALSE,
566 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
567 '#options' => $all_countries,
568 '#activeselect_path' => 'cck_address/activeselect',
569 '#activeselect_targets' => str_replace('_', '-', 'select_state'),
570 '#activeselect_extra' => isset($field['select_state']) ? $field['select_state'] : '',
571 );
572 $form['select_address_from_db']['select_state'] = array(
573 '#type' => 'select',
574 '#title' => t('Select a mandatory state'),
575 '#default_value' => isset($field['select_state']) ? array($field['select_state']) : array(),
576 '#multiple' => FALSE,
577 '#required' => isset($field['select_address_from_db']) ? $field['select_address_from_db'] : FALSE,
578 '#options' => array(),
579 '#DANGEROUS_SKIP_CHECK' => TRUE,
580 );
581 }
582 $form['multiple']['#type'] = 'hidden';
583 return $form;
584
585 case 'validate':
586 if ($field['max_length_street1'] && !is_numeric($field['max_length_street1'])) {
587 form_set_error('max_length_street1', t('"Maximum length: first street field" must be a number.'));
588 }
589 if ($field['max_length_street2'] && !is_numeric($field['max_length_street2'])) {
590 form_set_error('max_length_street2', t('"Maximum length: second street field" must be a number.'));
591 }
592 if ($field['max_length_city'] && !is_numeric($field['max_length_city'])) {
593 form_set_error('max_length_city', t('"Maximum length: city name" must be a number.'));
594 }
595 if ($field['max_length_other'] && !is_numeric($field['max_length_other'])) {
596 form_set_error('max_length_other', t('"Maximum length: other" must be a number.'));
597 }
598 if (($field['other_countries'] == 0) && ($field['state_abbrv'] == 2)) {
599 form_set_error('other_countries', t('"Allow <em>other</em> countries?" must be checked if you are using <em>Free-text entry</em> for states.'));
600 }
601 if (($field['other_countries'] == 1) && ($field['state_abbrv'] != 2)) {
602 form_set_error('state_abbrv', t('You must use "<em>Free text entry</em>" for states if you "Allow <em>other</em> countries".'));
603 }
604 if (isset($field['multiple'])) {
605 unset ($field['multiple']);
606 }
607 break;
608
609 case 'save':
610 return array('location_integration',
611 'state_abbrv',
612 'country_abbrv',
613 'clist',
614 'fielddisplay',
615 'max_length_street1',
616 'max_length_street2',
617 'max_length_apt',
618 'max_length_city',
619 'max_length_other',
620 'field_names_street1',
621 'field_names_street2',
622 'field_names_apt',
623 'field_names_city',
624 'field_names_state',
625 'field_names_country',
626 'field_names_zip',
627 'field_names_other',
628 'field_order',
629 'field_defaults_street1',
630 'field_defaults_street2',
631 'field_defaults_apt',
632 'field_defaults_city',
633 'field_defaults_state',
634 'field_defaults_country',
635 'field_defaults_zip',
636 'field_defaults_other',
637 'select_a_f_d',
638 'select_table',
639 'select_streetcolumn',
640 'select_street_numcolumn',
641 'select_city',
642 'select_zip',
643 'select_country',
644 'select_state',
645 'other_countries',
646 );
647
648 case 'database columns':
649 $columns = array(
650 'street1' => array('type' => 'varchar', 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
651 'street2' => array('type' => 'varchar', 'not null' => FALSE, 'default' => NULL, 'sortable' => FALSE),
652 'apt' => array('type' => 'varchar', 'length' => 15, 'not null' => FALSE, 'default' => NULL, 'sortable' => FALSE),
653 'city' => array('type' => 'varchar', 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
654 'state' => array('type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
655 'zip' => array('type' => 'varchar', 'length' => 15, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
656 'country' => array('type' => 'varchar', 'length' => 2, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
657 'other' => array('type' => 'varchar', 'not null' => FALSE, 'default' => NULL, 'sortable' => FALSE),
658 );
659
660 if ($field['max_lengths']['max_length_street1'] == 0 || $field['max_lengths']['max_length_street1'] > 255) {
661 $columns['street1']['type'] = 'longtext';
662 }
663 else {
664 $columns['street1']['length'] = $field['max_lengths']['max_length_street1'];
665 }
666 if ($field['max_lengths']['max_length_street2'] == 0 || $field['max_lengths']['max_length_street2'] > 255) {
667 $columns['street2']['type'] = 'longtext';
668 }
669 else {
670 $columns['street2']['length'] = $field['max_lengths']['max_length_street2'];
671 }
672 if ($field['max_lengths']['max_length_city'] == 0 || $field['max_lengths']['max_length_city'] > 255) {
673 $columns['city']['type'] = 'longtext';
674 }
675 else {
676 $columns['city']['length'] = $field['max_lengths']['max_length_city'];
677 }
678 if ($field['max_lengths']['max_length_other'] == 0 || $field['max_lengths']['max_length_other'] > 255) {
679 $columns['other']['type'] = 'longtext';
680 }
681 else {
682 $columns['other']['length'] = $field['max_lengths']['max_length_other'];
683 }
684
685 return $columns;
686
687 case 'filters':
688 return array(
689 'default' => array(
690 'operator' => 'views_handler_operator_like',
691 'handler' => 'views_handler_filter_like',
692 ),
693 );
694
695 case 'callbacks'://pairs up with cck_address_field::view
696 return array(
697 'view' => CONTENT_CALLBACK_CUSTOM,
698 );
699
700 //case 'tables':
701 // $tables = content_views_field_tables($field);
702 // whatever additions / modifications needed on the default definitions
703 // return $tables;
704
705 // case 'arguments':
706 // $arguments = content_views_field_arguments($field);
707 // whatever additions / modifications needed on the default definitions
708 //return $arguments;*/
709 }
710 } //function cck_address_field_settings()
711
712 /**
713 * Implementation of hook_field().
714 *
715 * <p>-Validate the user's input.</p>
716 * <p>-Alternatively, present the data for viewing.</p>
717 * @param $op
718 * What kind of action is being performed.
719 * @param &$node
720 * The node the action is being performed on.
721 * @param $field
722 * The field the action is being performed on.
723 * @param &$node_field
724 * The contents of the field in this node. Changes to this variable will
725 * be saved back to the node object.
726 * @return
727 * This varies depending on the operation.
728 * - The "load" operation should return an object containing extra values
729 * to be merged into the node object.
730 * - The "view" operation should return a string containing an HTML
731 * representation of the field data.
732 * - The "insert", "update", "delete", "validate", and "submit" operations
733 * have no return value.
734 */
735 function cck_address_field($op, &$node, $field, &$items, $teaser, $page) {
736 switch ($op) {
737 case 'submit':
738 if ($field['select_a_f_d'] == 1) {
739 if (is_array($items)) {//should this go into the process section of the widget function???
740 foreach ($items as $delta => $item) {
741 //switch the user's input to what is found in the DB
742 $sql1 = "SELECT %s FROM {%s} WHERE %s LIKE '%s'";
743 $results1 = db_fetch_object(db_query($sql1, $field['select_streetcolumn'], $field['select_table'], $field['select_streetcolumn'], $item['street2']. '%'));
744 $items[$delta]['street2'] = $results1->$field['select_streetcolumn'];
745 }
746 }
747 }
748 else {
749 if (is_array($items)) {//should this go into the process section of the widget function???
750 foreach ($items as $delta => $item) {
751 if (($item['street1'] == '') && ($item['city'] == '') && ($item['zip'] == '')) {
752 $items[$delta]['state'] = '';
753 $items[$delta]['country'] = '';
754 $items[$delta]['street2'] = '';
755 $items[$delta]['apt'] = '';
756 $items[$delta]['other'] = '';
757 }
758 }
759 }
760 }
761 if (module_exists('location_node') && $field['location_integration']) {
762 $field_name = $field['field_name'];
763 foreach ($node->$field_name as $delta => $item) {
764 if (isset($node->locations[$delta])) {
765 $node->locations[$delta]['street'] = $item['street1'];
766 $node->locations[$delta]['city'] = $item['city'];
767 $node->locations[$delta]['province'] = $item['state'];
768 $node->locations[$delta]['postal_code'] = $item['zip'];
769 $node->locations[$delta]['country'] = $item['country'];
770 }
771 }
772 }
773 break;
774
775 case 'validate':
776 if ($field['select_a_f_d'] == 1) {
777 if (is_array($items)) {
778 foreach ($items as $delta => $item) {
779 //setup error handling fields
780 $error_field_street1 = $field['field_name']. '][' .$delta. '][street1';
781 $error_field_street2 = $field['field_name']. '][' .$delta. '][street2';
782 $error_field_city = $field['field_name']. '][' .$delta. '][city';
783 $error_field_state = $field['field_name']. '][' .$delta. '][state';
784 $error_field_zip = $field['field_name']. '][' .$delta. '][zip';
785 $error_field_country = $field['field_name']. '][' .$delta. '][country';
786 $errors = array(
787 'street1' => '',
788 'street2' => '',
789 'city' => '',
790 'state' => '',
791 'zip' => '',
792 'country' => '',
793 );
794
795 //do the actual validation
796 if (($item['country'] != '') && ($item['country'] == $field['country'])) {
797 $errors['country'] = t('Illegal value for <em>Country</em>. It should have been set for you. Contact the site administrator.');
798 }
799 if (($item['state'] != '') && ($item['state'] == $field['state'])) {
800 $errors['state'] = t('Illegal value for <em>State</em>. It should have been set for you. Contact the site administrator.');
801 }
802 $table = $field['select_table'];
803 $column_city = $field['select_city'];
804 $column_zip = $field['select_zip'];
805 $sql = "SELECT %s FROM {%s} ORDER BY %s ASC";
806 $results = db_query($sql, $column_city, $table, $column_city);
807 while ($result = db_fetch_object($results)) {
808 $all_cities[$result->$column_city] = $result->$column_city;
809 }
810 if (!array_key_exists($item['city'], $all_cities)) {
811 $errors['city'] = t('Illegal value for <em>City</em>. You must select from the list.');
812 }
813 $results = db_query($sql, $column_zip, $table, $column_zip);
814 while ($result = db_fetch_object($results)) {
815 $all_zips[$result->$column_zip] = $result->$column_zip;
816 }
817 if (!array_key_exists($item['zip'], $all_zips)) {
818 $errors['zip'] = t('Illegal value for <em>Zip Code</em>. You must select from the list.');
819 }
820 //switch the user's input to what is found in the DB
821 $sql1 = "SELECT %s FROM {%s} WHERE %s LIKE '%s'";
822 $results1 = db_fetch_object(db_query($sql1, $field['select_streetcolumn'], $table, $field['select_streetcolumn'], $item['street2']. '%'));
823 if (!$results1) {
824 $errors['street2'] = t('Illegal value for <em>Street Name</em>.');
825 }
826 else {
827 $items[$delta]['street2'] = $results1->$field['select_streetcolumn'];
828 }
829 $sql2 = "SELECT %s, %s FROM {%s} WHERE %s = %d AND %s LIKE '%s'";
830 $results2 = db_fetch_object(db_query($sql2, $field['select_street_numcolumn'], $field['select_streetcolumn'], $table, $field['select_street_numcolumn'], $item['street1'], $field['select_streetcolumn'], $item['street2']. '%'));
831 if (!$results2) {
832 $errors['street1'] = t('Possible illegal value for <em>Street Number</em>. This number was not found on the street you indicated.');
833 if ($errors['street2'] != '') {
834 $errors['street2'] = t('Possible illegal value for <em>Street Name</em>. This street was not found with the number you indicated.');
835 }
836 }
837 $sql3 = "SELECT * FROM {%s} WHERE %s = %d AND %s LIKE '%s' AND %s = '%s' AND %s = %d";
838 $values = array(
839 $table,
840 $field['select_street_numcolumn'], $item['street1'],
841 $field['select_streetcolumn'], $item['street2']. '%',
842 $field['select_city'], $item['city'],
843 $field['select_zip'], $item['zip'],
844 );
845 $result = db_fetch_object(db_query($sql3, $values));
846 if (!$result && ($errors['street1'] != '') && ($errors['street2'] != '') && ($errors['city'] != '') && ($errors['state'] != '') && ($errors['zip'] != '') && ($errors['country'] != '')) {
847 $errors['street1'] = t('Error. Cannot find this address in the database.');
848 $errors['street2'] = t('Error. Cannot find this address in the database.');
849 $errors['city'] = t('Error. Cannot find this address in the database.');
850 $errors['zip'] = t('Error. Cannot find this address in the database.');
851 $errors['state'] = t('Error. Cannot find this address in the database.');
852 $errors['country'] = t('Error. Cannot find this address in the database.');
853 }
854
855 //report errors
856 if ($errors['street1'] != '') {
857 form_set_error($error_field_street1, $errors['street1']);
858 }
859 if ($errors['street2'] != '') {
860 form_set_error($error_field_street2, $errors['street2']);
861 }
862 if ($errors['city'] != '') {
863 form_set_error($error_field_city, $errors['city']);
864 }
865 if ($errors['state'] != '') {
866 form_set_error($error_field_state, $errors['state']);
867 }
868 if ($errors['zip'] != '') {
869 form_set_error($error_field_zip, $errors['zip']);
870 }
871 if ($errors['country'] != '') {
872 form_set_error($error_field_country, $errors['country']);
873 }
874 }
875 }
876 }
877 else {
878 $all_countries = array();
879 $sql = "SELECT cas.country_name, cas.country_code FROM {cck_address_countries} cas ORDER BY cas.country_name ASC";
880 $results = db_query($sql);
881 while ($result = db_fetch_object($results)) {
882 $all_countries[$result->country_code] = $result->country_name;//master list of countries to check against
883 }
884 if (isset($field['clist'])) {
885 $allowed_countries = $field['clist'];//just the country codes
886 }
887 else {
888 $allowed_countries = array();
889 }
890 $allowed_countries = array_filter($allowed_countries);//get rid of those not allowed
891 foreach ($allowed_countries as $code => $code2) {
892 $allowed_countries[$code] = $all_countries[$code];//add names to codes
893 }
894
895
896 if (is_array($items)) {
897 foreach ($items as $delta => $item) {//although we aren't allowing multiples at this time, go ahead an leave this in, just in case
898 $error_field_street1 = $field['field_name']. '][' .$delta. '][street1';
899 $error_field_street2 = $field['field_name']. '][' .$delta. '][street2';
900 $error_field_apt = $field['field_name']. '][' .$delta. '][apt';
901 $error_field_city = $field['field_name']. '][' .$delta. '][city';
902 $error_field_state = $field['field_name']. '][' .$delta. '][state';
903 $error_field_zip = $field['field_name']. '][' .$delta. '][zip';
904 $error_field_country = $field['field_name']. '][' .$delta. '][country';
905 $error_field_other = $field['field_name']. '][' .$delta. '][other';
906
907 $errors = array(
908 'street1' => '',
909 'street2' => '',
910 'apt' => '',
911 'city' => '',
912 'state' => '',
913 'zip' => '',
914 'country' => '',
915 'other' => ''
916 );
917
918 //since states and countries come from drop-downs, we can validate them here
919 if (array_key_exists($item['country'], $allowed_countries)) {
920 $selected_country[$item['country']] = $allowed_countries[$item['country']];
921 }
922 elseif (!$field['other_countries']) {
923 $errors['country'] = t('Illegal value for %name\'s Country field. You must select a Country from the dropdown list.', array('%name' => t($field['widget']['label'])));
924 }
925 $state_options = cck_address_get_states($selected_country);
926 if (!$field['other_countries'] && !array_key_exists($item['state'], $state_options)) {
927 $errors['state'] = t('Illegal value for %name\'s State field. You must select a State from the dropdown list.', array('%name' => t($field['widget']['label'])));
928 }
929
930 //pass all other fields on to the various address modules for validation
931 foreach (module_implements('validate_address_fields') as $name) {
932 $function = $name. '_validate_address_fields';
933 $function($errors, $item['country'], $item, t($field['widget']['label']));
934 }
935
936 if ($errors['street1'] != '') {
937 form_set_error($error_field_street1, $errors['street1']);
938 }
939 if ($errors['street2'] != '') {
940 form_set_error($error_field_street2, $errors['street2']);
941 }
942 if ($errors['apt']