| 1 |
<?php |
<?php |
| 2 |
// $Id: zipcode.module,v 1.4.4.7 2008/04/21 00:03:45 thierrygd Exp $ |
// $Id: zipcode.module,v 1.4.4.7 2008/11/16 00:03:45 thierrygd Exp $ |
| 3 |
|
|
| 4 |
// Copyright 2008 Thierry GUEGAN http://www.arvoriad.com |
// Copyright 2008 Thierry GUEGAN http://www.arvoriad.com |
| 5 |
|
|
| 10 |
*/ |
*/ |
| 11 |
|
|
| 12 |
/** |
/** |
| 13 |
|
* @addtogroup install |
| 14 |
|
* @{ |
| 15 |
|
*/ |
| 16 |
|
/** |
| 17 |
|
* Implementation of hook_content_notify(). |
| 18 |
|
* |
| 19 |
|
* This hook should be implemented inside hook_install(), hook_uninstall(), |
| 20 |
|
* hook_enable() and hook_disable(), and is used to notify the content |
| 21 |
|
* module when a field module is added or removed so it can respond |
| 22 |
|
* appropriately. One use of this hook is to allow the content module |
| 23 |
|
* to remove fields and field data created by this module when the |
| 24 |
|
* module is uninstalled. |
| 25 |
|
* |
| 26 |
|
* The recommended location for these hooks is in the module's .install file. |
| 27 |
|
*/ |
| 28 |
|
/** |
| 29 |
|
* Implementation of hook_install(). |
| 30 |
|
*/ |
| 31 |
|
function zipcode_install() { |
| 32 |
|
content_notify('install', 'zipcode'); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
/** |
| 36 |
|
* Implementation of hook_uninstall(). |
| 37 |
|
*/ |
| 38 |
|
function zipcode_uninstall() { |
| 39 |
|
content_notify('uninstall', 'zipcode'); |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
/** |
| 43 |
|
* Implementation of hook_enable(). |
| 44 |
|
* |
| 45 |
|
* Notify content module when this module is enabled. |
| 46 |
|
*/ |
| 47 |
|
function zipcode_enable() { |
| 48 |
|
content_notify('enable', 'zipcode'); |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
/** |
| 52 |
|
* Implementation of hook_disable(). |
| 53 |
|
* |
| 54 |
|
* Notify content module when this module is disabled. |
| 55 |
|
*/ |
| 56 |
|
function zipcode_disable() { |
| 57 |
|
content_notify('disable', 'zipcode'); |
| 58 |
|
} |
| 59 |
|
/** |
| 60 |
|
* @} End of "addtogroup install". |
| 61 |
|
*/ |
| 62 |
|
|
| 63 |
|
/** |
| 64 |
|
* Implementation of hook_field_info(). |
| 65 |
* Implementation of hook_field_info(). |
* Implementation of hook_field_info(). |
| 66 |
|
* |
| 67 |
|
* Here we indicate that the content module will use its default |
| 68 |
|
* handling for the view of this field. |
| 69 |
|
* |
| 70 |
|
* Callbacks can be omitted if default handing is used. |
| 71 |
|
* They're included here just so this module can be used |
| 72 |
|
* as an example for custom modules that might do things |
| 73 |
|
* differently. |
| 74 |
|
* |
| 75 |
|
* If your module will provide its own Views tables or arguments, |
| 76 |
|
* change CONTENT_CALLBACK_DEFAULT to CONTENT_CALLBACK_CUSTOM. |
| 77 |
|
* |
| 78 |
|
* IMPORTANT! - field and widget names will be truncated to 32 characters in |
| 79 |
|
* the database and in internal arrays, like content_fields(). |
| 80 |
*/ |
*/ |
| 81 |
function zipcode_field_info() { |
function zipcode_field_info() { |
| 82 |
return array( |
return array( |
| 86 |
'uk_zipcode' => array('label' => t('United Kingdom postal codes')), |
'uk_zipcode' => array('label' => t('United Kingdom postal codes')), |
| 87 |
'nl_zipcode' => array('label' => t('Dutch zipcodes')), |
'nl_zipcode' => array('label' => t('Dutch zipcodes')), |
| 88 |
'au_zipcode' => array('label' => t('Australian zipcodes')), |
'au_zipcode' => array('label' => t('Australian zipcodes')), |
| 89 |
|
'dk_zipcode' => array('label' => t('Danish zipcodes')), |
| 90 |
|
'pt_zipcode' => array('label' => t('Portuguese zipcodes')), |
| 91 |
); |
); |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
/** |
/** |
| 95 |
|
* Implementation of hook_theme(). |
| 96 |
|
*/ |
| 97 |
|
function zipcode_theme() { |
| 98 |
|
return array( |
| 99 |
|
'zipcode_textfield' => array( |
| 100 |
|
'arguments' => array('element' => NULL), |
| 101 |
|
), |
| 102 |
|
'zipcode_formatter_default' => array( |
| 103 |
|
'arguments' => array('element' => NULL), |
| 104 |
|
), |
| 105 |
|
); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
/** |
| 109 |
* Implementation of hook_field_settings(). |
* Implementation of hook_field_settings(). |
| 110 |
|
* |
| 111 |
|
* Handle the settings for a field. |
| 112 |
|
* |
| 113 |
|
* @param $op |
| 114 |
|
* The operation to be performed. Possible values: |
| 115 |
|
* - "form": Display the field settings form. |
| 116 |
|
* - "validate": Check the field settings form for errors. |
| 117 |
|
* - "save": Declare which fields to save back to the database. |
| 118 |
|
* - "database columns": Declare the columns that content.module should create |
| 119 |
|
* and manage on behalf of the field. If the field module wishes to handle |
| 120 |
|
* its own database storage, this should be omitted. |
| 121 |
|
* - "filters": Declare the Views filters available for the field. |
| 122 |
|
* (this is used in CCK's default Views tables definition) |
| 123 |
|
* They always apply to the first column listed in the "database columns" |
| 124 |
|
* array. |
| 125 |
|
* @param $field |
| 126 |
|
* The field on which the operation is to be performed. |
| 127 |
|
* @return |
| 128 |
|
* This varies depending on the operation. |
| 129 |
|
* - "form": an array of form elements to add to |
| 130 |
|
* the settings page. |
| 131 |
|
* - "validate": no return value. Use form_set_error(). |
| 132 |
|
* - "save": an array of names of form elements to |
| 133 |
|
* be saved in the database. |
| 134 |
|
* - "database columns": an array keyed by column name, with arrays of column |
| 135 |
|
* information as values. This column information must include "type", the |
| 136 |
|
* MySQL data type of the column, and may also include a "sortable" parameter |
| 137 |
|
* to indicate to views.module that the column contains ordered information. |
| 138 |
|
* TODO: Details of other information that can be passed to the database layer can |
| 139 |
|
* be found in the API for the Schema API. |
| 140 |
|
* - "filters": an array of 'filters' definitions as expected by views.module |
| 141 |
|
* (see Views Documentation). |
| 142 |
|
* When providing several filters, it is recommended to use the 'name' |
| 143 |
|
* attribute in order to let the user distinguish between them. If no 'name' |
| 144 |
|
* is specified for a filter, the key of the filter will be used instead. |
| 145 |
*/ |
*/ |
| 146 |
function zipcode_field_settings($op, $field) { |
function zipcode_field_settings($op, $field) { |
| 147 |
switch ($op) { |
switch ($op) { |
| 175 |
$columns = array( |
$columns = array( |
| 176 |
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), |
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), |
| 177 |
); |
); |
| 178 |
} |
} |
| 179 |
|
if ($field['type'] == 'dk_zipcode'){ |
| 180 |
|
$columns = array( |
| 181 |
|
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), |
| 182 |
|
); |
| 183 |
|
} |
| 184 |
|
if ($field['type'] == 'pt_zipcode'){ |
| 185 |
|
$columns = array( |
| 186 |
|
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), |
| 187 |
|
); |
| 188 |
|
} |
| 189 |
return $columns; |
return $columns; |
| 190 |
} |
} |
| 191 |
} |
} |
| 192 |
|
|
| 193 |
/** |
/** |
| 194 |
* Implementation of hook_field(). |
* Implementation of hook_field(). |
| 195 |
|
* |
| 196 |
|
* Define the behavior of a field type. |
| 197 |
|
* |
| 198 |
|
* @param $op |
| 199 |
|
* What kind of action is being performed. Possible values: |
| 200 |
|
* - "load": The node is about to be loaded from the database. This hook |
| 201 |
|
* should be used to load the field. |
| 202 |
|
* - "validate": The user has just finished editing the node and is |
| 203 |
|
* trying to preview or submit it. This hook can be used to check or |
| 204 |
|
* even modify the node. Errors should be set with form_set_error(). |
| 205 |
|
* - "presave": The user has just finished editing the node and the node has |
| 206 |
|
* passed validation. This hook can be used to modify the node. |
| 207 |
|
* - "insert": The node is being created (inserted in the database). |
| 208 |
|
* - "update": The node is being updated. |
| 209 |
|
* - "delete": The node is being deleted. |
| 210 |
|
* @param &$node |
| 211 |
|
* The node the action is being performed on. This argument is passed by |
| 212 |
|
* reference for performance only; do not modify it. |
| 213 |
|
* @param $field |
| 214 |
|
* The field the action is being performed on. |
| 215 |
|
* @param &$node_field |
| 216 |
|
* The contents of the field in this node. Changes to this variable will |
| 217 |
|
* be saved back to the node object. |
| 218 |
|
* @return |
| 219 |
|
* This varies depending on the operation. |
| 220 |
|
* - The "load" operation should return an object containing extra values |
| 221 |
|
* to be merged into the node object. |
| 222 |
|
* - The "insert", "update", "delete", "validate", and "presave" operations |
| 223 |
|
* have no return value. |
| 224 |
|
* |
| 225 |
|
* In most cases, only "validate" operations is relevant ; the rest |
| 226 |
|
* have default implementations in content_field() that usually suffice. |
| 227 |
*/ |
*/ |
| 228 |
function zipcode_field($op, &$node, $field, &$node_field, $teaser, $page) { |
function zipcode_field($op, &$node, $field, &$node_field, $teaser, $page) { |
| 229 |
switch ($op) { |
switch ($op) { |
| 230 |
case 'view': |
case 'validate': // corresponds to hook phone_widget validate in zipcode-5.x |
| 231 |
foreach ($node_field as $delta => $item) { |
foreach ($node_field as $delta => $item) { |
| 232 |
//$node_field[$delta]['view'] = zipcode_field_view_item($field, $item); |
if ($item['value'] != '') { |
| 233 |
$node_field[$delta]['view'] = content_format($field, $item, 'default', $node); |
if ($field['type'] == 'fr_zipcode' && !valid_zipcode('fr', $item['value'])) { |
| 234 |
|
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']))); |
| 235 |
|
} |
| 236 |
|
if ($field['type'] == 'ca_zipcode' && !valid_zipcode('ca', $item['value'])) { |
| 237 |
|
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']))); |
| 238 |
|
} |
| 239 |
|
if ($field['type'] == 'us_zipcode' && !valid_zipcode('us', $item['value'])) { |
| 240 |
|
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']))); |
| 241 |
|
} |
| 242 |
|
if ($field['type'] == 'uk_zipcode' && !valid_zipcode('uk', $item['value'])) { |
| 243 |
|
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']))); |
| 244 |
|
} |
| 245 |
|
if ($field['type'] == 'nl_zipcode' && !valid_zipcode('nl', $item['value'])) { |
| 246 |
|
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']))); |
| 247 |
|
} |
| 248 |
|
if ($field['type'] == 'au_zipcode' && !valid_zipcode('au', $item['value'])) { |
| 249 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Australian zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value']))); |
| 250 |
|
} |
| 251 |
|
if ($field['type'] == 'dk_zipcode' && !valid_zipcode('dk', $item['value'])) { |
| 252 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Danish zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value']))); |
| 253 |
|
} |
| 254 |
|
if ($field['type'] == 'pt_zipcode' && !valid_zipcode('pt', $item['value'])) { |
| 255 |
|
form_set_error($field['field_name'],t('"%value" is not a valid Portuguese zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value']))); |
| 256 |
|
} |
| 257 |
|
} |
| 258 |
|
} |
| 259 |
|
break; |
| 260 |
|
|
| 261 |
|
case 'presave': // corresponds to hook phone_widget 'process form values' in phone-5.x |
| 262 |
|
foreach ($node_field as $delta => $item) { |
| 263 |
|
//format the phone number |
| 264 |
|
if ($item['value'] != '') { |
| 265 |
|
if ($field['type'] == 'fr_zipcode') { |
| 266 |
|
$node_field[$delta]['value'] = format_zipcode_number('fr', $node_field[$delta]['value'], $field); |
| 267 |
|
} |
| 268 |
|
if ($field['type'] == 'ca_zipcode') { |
| 269 |
|
$node_field[$delta]['value'] = format_zipcode_number('ca', $node_field[$delta]['value'], $field); |
| 270 |
|
} |
| 271 |
|
if ($field['type'] == 'us_zipcode') { |
| 272 |
|
$node_field[$delta]['value'] = format_zipcode_number('us', $node_field[$delta]['value'], $field); |
| 273 |
|
} |
| 274 |
|
if ($field['type'] == 'uk_zipcode') { |
| 275 |
|
$node_field[$delta]['value'] = format_zipcode_number('uk', $node_field[$delta]['value'], $field); |
| 276 |
|
} |
| 277 |
|
if ($field['type'] == 'nl_zipcode') { |
| 278 |
|
$node_field[$delta]['value'] = format_zipcode_number('nl', $node_field[$delta]['value'], $field); |
| 279 |
|
} |
| 280 |
|
if ($field['type'] == 'au_zipcode') { |
| 281 |
|
$node_field[$delta]['value'] = format_zipcode_number('au', $node_field[$delta]['value'], $field); |
| 282 |
|
} |
| 283 |
|
if ($field['type'] == 'dk_zipcode') { |
| 284 |
|
$node_field[$delta]['value'] = format_zipcode_number('dk', $node_field[$delta]['value'], $field); |
| 285 |
|
} |
| 286 |
|
if ($field['type'] == 'pt_zipcode') { |
| 287 |
|
$node_field[$delta]['value'] = format_zipcode_number('pt', $node_field[$delta]['value'], $field); |
| 288 |
|
} |
| 289 |
|
} |
| 290 |
} |
} |
| 291 |
return theme('field', $node, $field, $node_field, $teaser, $page); |
break; |
| 292 |
} |
} |
| 293 |
} |
} |
| 294 |
|
|
| 304 |
*/ |
*/ |
| 305 |
|
|
| 306 |
/** |
/** |
| 307 |
*Implementation of hook_field_formatter_info |
*Implementation of hook_field_formatter_info(). |
| 308 |
|
* Implementation of hook_field_formatter_info(). |
| 309 |
* |
* |
| 310 |
|
* The default behavior of formatters is that they will create |
| 311 |
|
* a theme for a single field value. |
| 312 |
|
* |
| 313 |
|
* Setting 'multiple values' to CONTENT_HANDLE_FIELD will create |
| 314 |
|
* a formatter that will receive all the values of a field so you |
| 315 |
|
* can, for instance, plot all the values on a map or in a graph. |
| 316 |
|
* |
| 317 |
|
* The 'view' operation (handled by the Content module) constructs the |
| 318 |
|
* $node in a way that you can use drupal_render() to display the |
| 319 |
|
* formatted output for an individual field. |
| 320 |
|
* |
| 321 |
|
* i.e. print drupal_render($node->field_foo); |
| 322 |
|
* |
| 323 |
|
* The code now supports both single value formatters, which theme an |
| 324 |
|
* individual item value as has been done in previous version of CCK, |
| 325 |
|
* and multiple value formatters, which theme all values for the field |
| 326 |
|
* in a single theme. The multiple value formatters could be used, for |
| 327 |
|
* instance, to plot field values on a single map or display them |
| 328 |
|
* in a graph. Single value formatters are the default, multiple value |
| 329 |
|
* formatters can be designated as such in formatter_info(). |
| 330 |
|
* |
| 331 |
|
* The node array will look like: |
| 332 |
|
* |
| 333 |
|
* 'Single value' formatter: |
| 334 |
|
* $node->content['field_foo'] = array( |
| 335 |
|
* '#type' => 'content_field', |
| 336 |
|
* '#title' => 'label' |
| 337 |
|
* '#field_name' => 'field_name', |
| 338 |
|
* '#node' => $node, |
| 339 |
|
* 'items' => |
| 340 |
|
* 0 => array( |
| 341 |
|
* '#theme' => $theme, |
| 342 |
|
* '#field_name' => 'field_name', |
| 343 |
|
* '#type_name' => $node->type, |
| 344 |
|
* '#formatter' => $formatter_name, |
| 345 |
|
* '#item' => $items[0], |
| 346 |
|
* ), |
| 347 |
|
* 1 => array( |
| 348 |
|
* '#theme' => $theme, |
| 349 |
|
* '#field_name' => 'field_name', |
| 350 |
|
* '#type_name' => $node->type, |
| 351 |
|
* '#formatter' => $formatter_name, |
| 352 |
|
* '#item' => $items[1], |
| 353 |
|
* ), |
| 354 |
|
* ), |
| 355 |
|
* ); |
| 356 |
|
* 'Multiple value' formatter: |
| 357 |
|
* $node->content['field_foo'] = array( |
| 358 |
|
* '#type' => 'content_field', |
| 359 |
|
* '#title' => 'label' |
| 360 |
|
* '#field_name' => 'field_name', |
| 361 |
|
* '#node' => $node, |
| 362 |
|
* 'items' => array( |
| 363 |
|
* '#theme' => $theme, |
| 364 |
|
* '#field_name' => 'field_name', |
| 365 |
|
* '#type_name' => $node->type, |
| 366 |
|
* '#formatter' => $formatter_name, |
| 367 |
|
* 0 => array( |
| 368 |
|
* '#item' => $items[0], |
| 369 |
|
* ), |
| 370 |
|
* 1 => array( |
| 371 |
|
* '#item' => $items[1], |
| 372 |
|
* ), |
| 373 |
|
* ), |
| 374 |
|
* ); |
| 375 |
*/ |
*/ |
| 376 |
function zipcode_field_formatter_info() { |
function zipcode_field_formatter_info() { |
| 377 |
return array( |
return array( |
| 378 |
'default' => array( |
'default' => array( |
| 379 |
'label' => 'Default', |
'label' => 'Default', |
| 380 |
'field types' => array('fr_zipcode', 'ca_zipcode', 'us_zipcode', 'uk_zipcode', 'nl_zipcode', 'au_zipcode'), |
'field types' => array('fr_zipcode', |
| 381 |
|
'ca_zipcode', |
| 382 |
|
'us_zipcode', |
| 383 |
|
'uk_zipcode', |
| 384 |
|
'nl_zipcode', |
| 385 |
|
'au_zipcode', |
| 386 |
|
'dk_zipcode', |
| 387 |
|
'pt_zipcode' |
| 388 |
|
), |
| 389 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 390 |
), |
), |
| 391 |
); |
); |
| 392 |
} |
} |
| 393 |
|
|
| 394 |
/** |
/** |
| 395 |
*Implementation of hook_field_formatter |
*Implementation of hook_field_formatter(). |
| 396 |
*/ |
* |
| 397 |
|
* Prepare an individual item for viewing in a browser. |
| 398 |
|
* |
| 399 |
|
* @param $field |
| 400 |
|
* The field the action is being performed on. |
| 401 |
|
* @param $item |
| 402 |
|
* An array, keyed by column, of the data stored for this item in this field. |
| 403 |
|
* @param $formatter |
| 404 |
|
* The name of the formatter being used to display the field. |
| 405 |
|
* @param $node |
| 406 |
|
* The node object, for context. Will be NULL in some cases. |
| 407 |
|
* Warning: when displaying field retrieved by Views, $node will not |
| 408 |
|
* be a "full-fledged" node object, but an object containg the data returned |
| 409 |
|
* by the Views query (at least nid, vid, changed) |
| 410 |
|
* @return |
| 411 |
|
* An HTML string containing the formatted item. |
| 412 |
|
* |
| 413 |
|
* In a multiple-value field scenario, this function will be called once per |
| 414 |
|
* value currently stored in the field. This function is also used as the handler |
| 415 |
|
* for viewing a field in a views.module tabular listing. |
| 416 |
|
* |
| 417 |
|
* It is important that this function at the minimum perform security |
| 418 |
|
* transformations such as running check_plain() or check_markup(). |
| 419 |
|
*/ |
| 420 |
function zipcode_field_formatter($field, $item, $formatter, $node) { |
function zipcode_field_formatter($field, $item, $formatter, $node) { |
| 421 |
if (!isset($item['value'])) { |
if (!isset($item['value'])) { |
| 422 |
return ''; |
return ''; |
| 432 |
|
|
| 433 |
/** |
/** |
| 434 |
* Implementation of hook_widget_info(). |
* Implementation of hook_widget_info(). |
| 435 |
|
* |
| 436 |
|
* Here we indicate that the content module will handle |
| 437 |
|
* the default value and multiple values for these widgets. |
| 438 |
|
* |
| 439 |
|
* Callbacks can be omitted if default handing is used. |
| 440 |
|
* They're included here just so this module can be used |
| 441 |
|
* as an example for custom modules that might do things |
| 442 |
|
* differently. |
| 443 |
|
* |
| 444 |
|
* IMPORTANT! - field and widget names will be truncated to 32 characters in |
| 445 |
|
* the database and in internal arrays, like content_fields(). |
| 446 |
*/ |
*/ |
| 447 |
function zipcode_widget_info() { |
function zipcode_widget_info() { |
| 448 |
return array( |
return array( |
| 449 |
'zipcode' => array( |
'zipcode_textfield' => array( |
| 450 |
'label' => t('Textfield'), |
'label' => t('Textfield'), |
| 451 |
'field types' => array('fr_zipcode', 'ca_zipcode', 'us_zipcode', 'uk_zipcode', 'nl_zipcode', 'au_zipcode'), |
'field types' => array('fr_zipcode', |
| 452 |
|
'ca_zipcode', |
| 453 |
|
'us_zipcode', |
| 454 |
|
'uk_zipcode', |
| 455 |
|
'nl_zipcode', |
| 456 |
|
'au_zipcode', |
| 457 |
|
'dk_zipcode', |
| 458 |
|
'pt_zipcode' |
| 459 |
|
), |
| 460 |
|
'multiple values' => CONTENT_HANDLE_CORE, |
| 461 |
|
'callbacks' => array( |
| 462 |
|
'default value' => CONTENT_CALLBACK_DEFAULT, |
| 463 |
|
), |
| 464 |
), |
), |
| 465 |
); |
); |
| 466 |
} |
} |
| 467 |
|
|
| 468 |
/** |
/** |
| 469 |
* Implementation of hook_widget_settings(). |
* Implementation of hook_widget_settings(). |
| 470 |
|
* |
| 471 |
|
* Handle the parameters for a widget. |
| 472 |
|
* |
| 473 |
|
* @param $op |
| 474 |
|
* The operation to be performed. Possible values: |
| 475 |
|
* - "form": Display the widget settings form. |
| 476 |
|
* - "validate": Check the widget settings form for errors. |
| 477 |
|
* - "save": Declare which pieces of information to save back to the database. |
| 478 |
|
* @param $widget |
| 479 |
|
* The widget on which the operation is to be performed. |
| 480 |
|
* @return |
| 481 |
|
* This varies depending on the operation. |
| 482 |
|
* - "form": an array of form elements to add to the settings page. |
| 483 |
|
* - "validate": no return value. Use form_set_error(). |
| 484 |
|
* - "save": an array of names of form elements to be saved in the database. |
| 485 |
*/ |
*/ |
| 486 |
function zipcode_widget_settings($op, $widget) { |
function zipcode_widget_settings($op, $widget) { |
| 487 |
switch ($op) { |
switch ($op) { |
| 498 |
|
|
| 499 |
/** |
/** |
| 500 |
* Implementation of hook_widget(). |
* Implementation of hook_widget(). |
| 501 |
|
* |
| 502 |
|
* Attach a single form element to the form. It will be built out and |
| 503 |
|
* validated in the callback(s) listed in hook_elements. We build it |
| 504 |
|
* out in the callbacks rather than here in hook_widget so it can be |
| 505 |
|
* plugged into any module that can provide it with valid |
| 506 |
|
* $field information. |
| 507 |
|
* |
| 508 |
|
* Content module will set the weight, field name and delta values |
| 509 |
|
* for each form element. This is a change from earlier CCK versions |
| 510 |
|
* where the widget managed its own multiple values. |
| 511 |
|
* |
| 512 |
|
* If there are multiple values for this field, the content module will |
| 513 |
|
* call this function as many times as needed. |
| 514 |
|
* |
| 515 |
|
* @param $form |
| 516 |
|
* the entire form array, $form['#node'] holds node information |
| 517 |
|
* @param $form_state |
| 518 |
|
* the form_state, $form_state['values'][$field['field_name']] |
| 519 |
|
* holds the field's form values. |
| 520 |
|
* @param $field |
| 521 |
|
* the field array |
| 522 |
|
* @param $items |
| 523 |
|
* array of default values for this field |
| 524 |
|
* @param $delta |
| 525 |
|
* the order of this item in the array of subelements (0, 1, 2, etc) |
| 526 |
|
* |
| 527 |
|
* @return |
| 528 |
|
* the form item for a single element for this field |
| 529 |
*/ |
*/ |
| 530 |
function zipcode_widget($op, &$node, $field, &$node_field) { |
function zipcode_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
| 531 |
switch ($op) { |
$element = array( |
| 532 |
case 'form': |
'#type' => $field['widget']['type'], |
| 533 |
$form = array(); |
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL, |
| 534 |
$form[$field['field_name']] = array('#tree' => TRUE); |
); |
| 535 |
|
return $element; |
| 536 |
if ($field['multiple']) { |
} |
|
$form[$field['field_name']]['#type'] = 'fieldset'; |
|
|
$form[$field['field_name']]['#title'] = t($field['widget']['label']); |
|
|
foreach (range(0,2) as $delta) { |
|
|
$form[$field['field_name']][$delta]['value'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => '', |
|
|
'#default_value' => isset($node_field[$delta]['value']) ? $node_field[$delta]['value'] : '', |
|
|
'#required' => $field['required'] ? $field['required'] : FALSE, |
|
|
'#maxlength' => 255, |
|
|
'#weight' => $field['widget']['weight'], |
|
|
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20, |
|
|
'#description' => $field['widget']['description'], |
|
|
); |
|
|
} |
|
|
} |
|
|
else { |
|
|
$form[$field['field_name']][0]['value'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => $field['widget']['label'], |
|
|
'#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '', |
|
|
'#required' => $field['required'] ? $field['required'] : FALSE, |
|
|
'#maxlength' => 255, |
|
|
'#weight' => $field['widget']['weight'], |
|
|
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20, |
|
|
'#description' => $field['widget']['description'], |
|
|
); |
|
|
} |
|
|
|
|
|
return $form; |
|
|
|
|
|
case 'process form values': |
|
|
if (is_array($node_field)) { |
|
|
foreach ($node_field as $delta => $item) { |
|
|
//format the zipcode |
|
|
if ($item['value'] != '') |
|
|
{ |
|
|
if ($field['type'] == 'fr_zipcode') { |
|
|
$node_field[0]['value'] = format_zipcode('fr', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'ca_zipcode') { |
|
|
$node_field[0]['value'] = format_zipcode('ca', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'us_zipcode') { |
|
|
$node_field[0]['value'] = format_zipcode('us', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'uk_zipcode') { |
|
|
$node_field[0]['value'] = format_zipcode('uk', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'nl_zipcode') { |
|
|
$node_field[0]['value'] = format_zipcode('nl', $node_field[0]['value'], $field); |
|
|
} |
|
|
if ($field['type'] == 'au_zipcode') { |
|
|
$node_field[0]['value'] = format_zipcode('nl', $node_field[0]['value'], $field); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
break; |
|
|
|
|
|
case 'validate': |
|
|
if (is_array($node_field)) { |
|
|
foreach ($node_field as $delta => $item) { |
|
| 537 |
|
|
| 538 |
if ($item['value'] != '') |
/** |
| 539 |
{ |
* Implementation of hook_content_is_empty(). |
| 540 |
if ($field['type'] == 'fr_zipcode' && !valid_zipcode('fr', $item['value'])) { |
* |
| 541 |
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']))); |
* NEW REQUIRED HOOK! |
| 542 |
} |
* |
| 543 |
if ($field['type'] == 'ca_zipcode' && !valid_zipcode('ca', $item['value'])) { |
* This function tells the content module whether or not to consider |
| 544 |
form_set_error($field['field_name'],t('"%value" is not a canadian valid postal code.<br>Postal codes should be like Z5Z 5Z5 or Z5Z5Z5 ...',array('%value' => $item['value']))); |
* the $item to be empty. This is used by the content module |
| 545 |
} |
* to remove empty, non-required values before saving them. |
| 546 |
if ($field['type'] == 'us_zipcode' && !valid_zipcode('us', $item['value'])) { |
*/ |
| 547 |
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']))); |
function zipcode_content_is_empty($item, $field) { |
| 548 |
} |
return FALSE; |
| 549 |
if ($field['type'] == 'uk_zipcode' && !valid_zipcode('uk', $item['value'])) { |
} |
| 550 |
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']))); |
|
| 551 |
} |
/** |
| 552 |
if ($field['type'] == 'nl_zipcode' && !valid_zipcode('nl', $item['value'])) { |
* Implementation of FAPI hook_elements(). |
| 553 |
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']))); |
* |
| 554 |
} |
* Any FAPI callbacks needed for individual widgets can be declared here, |
| 555 |
if ($field['type'] == 'au_zipcode' && !valid_zipcode('au', $item['value'])) { |
* and the element will be passed to those callbacks for processing. |
| 556 |
form_set_error($field['field_name'],t('"%value" is not a valid Australian zipcode.<br>Zipcodes should contain 4 numbers',array('%value' => $item['value']))); |
* |
| 557 |
} |
* Drupal will automatically theme the element using a theme with |
| 558 |
} |
* the same name as the hook_elements key. |
| 559 |
} |
* |
| 560 |
} |
* Autocomplete_path is not used by text_widget but other widgets can use it |
| 561 |
break; |
* (see nodereference and userreference). |
| 562 |
} |
*/ |
| 563 |
|
function zipcode_elements() { |
| 564 |
|
return array( |
| 565 |
|
'zipcode_textfield' => array( |
| 566 |
|
'#input' => TRUE, |
| 567 |
|
'#columns' => array('value'), '#delta' => 0, |
| 568 |
|
'#process' => array('zipcode_textfield_process'), |
| 569 |
|
'#autocomplete_path' => FALSE, |
| 570 |
|
), |
| 571 |
|
); |
| 572 |
|
} |
| 573 |
|
|
| 574 |
|
/** |
| 575 |
|
* FAPI theme for an individual text elements. |
| 576 |
|
* |
| 577 |
|
* The textfield or textarea is already rendered by the |
| 578 |
|
* textfield or textarea themes and the html output |
| 579 |
|
* lives in $element['#children']. Override this theme to |
| 580 |
|
* make custom changes to the output. |
| 581 |
|
* |
| 582 |
|
* $element['#field_name'] contains the field name |
| 583 |
|
* $element['#delta] is the position of this element in the group |
| 584 |
|
*/ |
| 585 |
|
function theme_zipcode_textfield($element) { |
| 586 |
|
return $element['#children']; |
| 587 |
|
} |
| 588 |
|
|
| 589 |
|
/** |
| 590 |
|
* Process an individual element. |
| 591 |
|
* |
| 592 |
|
* Build the form element. When creating a form using FAPI #process, |
| 593 |
|
* note that $element['#value'] is already set. |
| 594 |
|
* |
| 595 |
|
* The $fields array is in $form['#field_info'][$element['#field_name']]. |
| 596 |
|
*/ |
| 597 |
|
function zipcode_textfield_process($element, $edit, $form_state, $form) { |
| 598 |
|
$field = $form['#field_info'][$element['#field_name']]; |
| 599 |
|
$field_key = $element['#columns'][0]; |
| 600 |
|
$delta = $element['#delta']; |
| 601 |
|
$element[$field_key] = array( |
| 602 |
|
'#type' => 'textfield', |
| 603 |
|
'#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL, |
| 604 |
|
'#autocomplete_path' => FALSE, |
| 605 |
|
// The following values were set by the content module and need |
| 606 |
|
// to be passed down to the nested element. |
| 607 |
|
'#title' => $element['#title'], |
| 608 |
|
'#description' => $element['#description'], |
| 609 |
|
'#required' => $element['#required'], |
| 610 |
|
'#field_name' => $element['#field_name'], |
| 611 |
|
'#type_name' => $element['#type_name'], |
| 612 |
|
'#delta' => $element['#delta'], |
| 613 |
|
'#columns' => $element['#columns'], |
| 614 |
|
); |
| 615 |
|
|
| 616 |
|
if (!empty($field['max_length'])) { |
| 617 |
|
$element[$field_key]['#maxlength'] = $field['max_length']; |
| 618 |
|
} |
| 619 |
|
if (!empty($field['text_processing'])) { |
| 620 |
|
$filter_key = $element['#columns'][1]; |
| 621 |
|
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT; |
| 622 |
|
$parents = array_merge($element['#parents'] , array($filter_key)); |
| 623 |
|
$element[$filter_key] = filter_form($format, 1, $parents); |
| 624 |
|
} |
| 625 |
|
|
| 626 |
|
// Used so that hook_field('validate') knows where to flag an error. |
| 627 |
|
$element['_error_element'] = array( |
| 628 |
|
'#type' => 'value', |
| 629 |
|
'#value' => implode('][', array_merge($element['#parents'], array($field_key))), |
| 630 |
|
); |
| 631 |
|
|
| 632 |
|
return $element; |
| 633 |
|
} |
| 634 |
|
|
| 635 |
|
/** |
| 636 |
|
* Theme function for 'default' text field formatter. |
| 637 |
|
*/ |
| 638 |
|
function theme_zipcode_formatter_default($element) { |
| 639 |
|
return $element['#item']['value']; |
| 640 |
} |
} |
| 641 |
|
|
| 642 |
/** |
/** |
| 664 |
|| $countrycode == 'us' |
|| $countrycode == 'us' |
| 665 |
|| $countrycode == 'uk' |
|| $countrycode == 'uk' |
| 666 |
|| $countrycode == 'nl' |
|| $countrycode == 'nl' |
| 667 |
|| $countrycode == 'au') { |
|| $countrycode == 'au' |
| 668 |
|
|| $countrycode == 'dk' |
| 669 |
|
|| $countrycode == 'pt' |
| 670 |
|
) { |
| 671 |
|
|
| 672 |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
| 673 |
|
|
| 674 |
$valid_zipcode_function = 'valid_'. $countrycode . '_zipcode'; |
$valid_zipcode_function = 'valid_'. $countrycode . '_zipcode'; |
| 675 |
include_once('./'. drupal_get_path('module', 'zipcode') . '/zipcode.'. $countrycode . '.inc'); |
module_load_include('inc', 'zipcode', './'. drupal_get_path('module', 'zipcode') . '/phone.'. $countrycode); |
| 676 |
|
|
| 677 |
if (function_exists($valid_zipcode_function)) { |
if (function_exists($valid_zipcode_function)) { |
| 678 |
return $valid_zipcode_function($zipcodestring); |
return $valid_zipcode_function($zipcodestring); |
| 704 |
|| $countrycode == 'us' |
|| $countrycode == 'us' |
| 705 |
|| $countrycode == 'uk' |
|| $countrycode == 'uk' |
| 706 |
|| $countrycode == 'nl' |
|| $countrycode == 'nl' |
| 707 |
|| $countrycode == 'au') { |
|| $countrycode == 'au' |
| 708 |
|
|| $countrycode == 'dk' |
| 709 |
|
|| $countrycode == 'pt' |
| 710 |
|
) { |
| 711 |
|
|
| 712 |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
//drupal_set_message('langue = ' . $countrycode, 'error'); |
| 713 |
|
|
| 714 |
$format_zipcode_function = 'format_'. $countrycode . '_zipcode'; |
$format_zipcode_function = 'format_'. $countrycode . '_zipcode'; |
| 715 |
include_once('./'. drupal_get_path('module', 'zipcode') . '/zipcode.'. $countrycode . '.inc'); |
module_load_include('inc', 'zipcode', './'. drupal_get_path('module', 'zipcode') . '/phone.'. $countrycode); |
| 716 |
|
|
| 717 |
if (function_exists($format_zipcode_function)) { |
if (function_exists($format_zipcode_function)) { |
| 718 |
return $format_zipcode_function($zipcodestring, $field); |
return $format_zipcode_function($zipcodestring, $field); |