Index: zipcode.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/zipcode/zipcode.module,v
retrieving revision 1.4.4.6
diff -u -r1.4.4.6 zipcode.module
--- zipcode.module 16 Nov 2007 19:02:00 -0000 1.4.4.6
+++ zipcode.module 24 Mar 2008 14:37:45 -0000
@@ -1,7 +1,7 @@
array('label' => t('French postal codes')),
'ca_zipcode' => array('label' => t('Canadian postal codes')),
'us_zipcode' => array('label' => t('US zipcodes')),
- 'uk_zipcode' => array('label' => t('United Kingdom postal codes')),
+ 'uk_zipcode' => array('label' => t('United Kingdom postal codes')),
'nl_zipcode' => array('label' => t('Dutch zipcodes')),
- 'au_zipcode' => array('label' => t('Australian zipcodes')),
- );
+ 'au_zipcode' => array('label' => t('Australian zipcodes')),
+ );
}
/**
* Implementation of hook_field_settings().
*/
function zipcode_field_settings($op, $field) {
- switch ($op) {
+ switch ($op) {
case 'database columns':
- if ($field['type'] == 'fr_zipcode'){
- $columns = array(
- 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
- );
- }
- if ($field['type'] == 'ca_zipcode'){
- $columns = array(
- 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
- );
+ if ($field['type'] == 'fr_zipcode') {
+ $columns = array(
+ 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+ );
}
- if ($field['type'] == 'us_zipcode'){
- $columns = array(
- 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
- );
+ if ($field['type'] == 'ca_zipcode') {
+ $columns = array(
+ 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+ );
+ }
+ if ($field['type'] == 'us_zipcode') {
+ $columns = array(
+ 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+ );
+ }
+ if ($field['type'] == 'uk_zipcode') {
+ $columns = array(
+ 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+ );
+ }
+ if ($field['type'] == 'nl_zipcode') {
+ $columns = array(
+ 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+ );
+ }
+ if ($field['type'] == 'au_zipcode') {
+ $columns = array(
+ 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
+ );
}
- if ($field['type'] == 'uk_zipcode'){
- $columns = array(
- 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
- );
- }
- if ($field['type'] == 'nl_zipcode'){
- $columns = array(
- 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
- );
- }
- if ($field['type'] == 'au_zipcode'){
- $columns = array(
- 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
- );
- }
return $columns;
}
}
@@ -73,23 +73,12 @@
//$node_field[$delta]['view'] = zipcode_field_view_item($field, $item);
$node_field[$delta]['view'] = content_format($field, $item, 'default', $node);
}
- return theme('field', $node, $field, $node_field, $teaser, $page);
+ return theme('field', $node, $field, $node_field, $teaser, $page);
}
}
/**
- * Implementation of hook_field_view_item().
- *
- */
-/*
-function zipcode_field_view_item($field, $node_field_item) {
- $zipcode = check_plain($node_field_item['value']);
- return $zipcode;
-}
-*/
-
-/**
- *Implementation of hook_field_formatter_info
+ * Implementation of hook_field_formatter_info().
*
*/
function zipcode_field_formatter_info() {
@@ -99,11 +88,11 @@
'field types' => array('fr_zipcode', 'ca_zipcode', 'us_zipcode', 'uk_zipcode', 'nl_zipcode', 'au_zipcode'),
),
);
- }
-
+}
+
/**
- *Implementation of hook_field_formatter
- */
+ * Implementation of hook_field_formatter().
+ */
function zipcode_field_formatter($field, $item, $formatter, $node) {
if (!isset($item['value'])) {
return '';
@@ -137,7 +126,7 @@
case 'form':
case 'validate':
- break; //do nothing
+ break; //do nothing
case 'save':
return array();
@@ -149,15 +138,15 @@
* Implementation of hook_widget().
*/
function zipcode_widget($op, &$node, $field, &$node_field) {
- switch ($op) {
+ switch ($op) {
case 'form':
$form = array();
$form[$field['field_name']] = array('#tree' => TRUE);
-
+
if ($field['multiple']) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#title'] = t($field['widget']['label']);
- foreach (range(0,2) as $delta) {
+ foreach (range(0, 2) as $delta) {
$form[$field['field_name']][$delta]['value'] = array(
'#type' => 'textfield',
'#title' => '',
@@ -166,7 +155,7 @@
'#maxlength' => 255,
'#weight' => $field['widget']['weight'],
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20,
- '#description' => $field['widget']['description'],
+ '#description' => $field['widget']['description'],
);
}
}
@@ -179,121 +168,116 @@
'#maxlength' => 255,
'#weight' => $field['widget']['weight'],
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20,
- '#description' => $field['widget']['description'],
+ '#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_fr_zipcode($node_field[0]['value']);
- }
- if ($field['type'] == 'ca_zipcode') {
- $node_field[0]['value'] = format_ca_zipcode($node_field[0]['value']);
- }
- if ($field['type'] == 'us_zipcode') {
- $node_field[0]['value'] = format_us_zipcode($node_field[0]['value']);
- }
- if ($field['type'] == 'uk_zipcode') {
- $node_field[0]['value'] = format_uk_zipcode($node_field[0]['value']);
- }
- if ($field['type'] == 'nl_zipcode') {
- $node_field[0]['value'] = format_nl_zipcode($node_field[0]['value']);
- }
- if ($field['type'] == 'au_zipcode') {
- $node_field[0]['value'] = format_au_zipcode($node_field[0]['value']);
- }
- }
- }
- }
- break;
-
+ 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_fr_zipcode($node_field[0]['value']);
+ }
+ if ($field['type'] == 'ca_zipcode') {
+ $node_field[0]['value'] = format_ca_zipcode($node_field[0]['value']);
+ }
+ if ($field['type'] == 'us_zipcode') {
+ $node_field[0]['value'] = format_us_zipcode($node_field[0]['value']);
+ }
+ if ($field['type'] == 'uk_zipcode') {
+ $node_field[0]['value'] = format_uk_zipcode($node_field[0]['value']);
+ }
+ if ($field['type'] == 'nl_zipcode') {
+ $node_field[0]['value'] = format_nl_zipcode($node_field[0]['value']);
+ }
+ if ($field['type'] == 'au_zipcode') {
+ $node_field[0]['value'] = format_au_zipcode($node_field[0]['value']);
+ }
+ }
+ }
+ }
+ break;
+
case 'validate':
if (is_array($node_field)) {
- foreach ($node_field as $delta => $item) {
+ foreach ($node_field as $delta => $item) {
- if ($item['value'] != '')
- {
- if ($field['type'] == 'fr_zipcode' && !valid_fr_zipcode($item['value'])) {
- form_set_error($field['field_name'],t('"%value" is not a valid french postal code.
Postal codes should only contains 4 or 5 numbers',array('%value' => $item['value'])));
- }
- if ($field['type'] == 'ca_zipcode' && !valid_ca_zipcode($item['value'])) {
- form_set_error($field['field_name'],t('"%value" is not a canadian valid postal code.
Postal codes should be like Z5Z 5Z5 or Z5Z5Z5 ...',array('%value' => $item['value'])));
- }
- if ($field['type'] == 'us_zipcode' && !valid_us_zipcode($item['value'])) {
- form_set_error($field['field_name'],t('"%value" is not a valid us zipcode.
Zipcodes should be like 99999 or 99999-9999 ...',array('%value' => $item['value'])));
- }
- if ($field['type'] == 'uk_zipcode' && !valid_uk_zipcode($item['value'])) {
- form_set_error($field['field_name'],t('"%value" is not a valid United Kingom postal code.
Postal codes should be like AB1 C23 or AB1C23 ...',array('%value' => $item['value'])));
- }
- if ($field['type'] == 'nl_zipcode' && !valid_nl_zipcode($item['value'])) {
- form_set_error($field['field_name'],t('"%value" is not a valid Dutch zipcode.
Zipcodes should contain 4 numbers followed by 2 letter',array('%value' => $item['value'])));
- }
- if ($field['type'] == 'au_zipcode' && !valid_au_zipcode($item['value'])) {
- form_set_error($field['field_name'],t('"%value" is not a valid Australian zipcode.
Zipcodes should contain 4 numbers',array('%value' => $item['value'])));
- }
+ if ($item['value'] != '') {
+ if ($field['type'] == 'fr_zipcode' && !valid_fr_zipcode($item['value'])) {
+ form_set_error($field['field_name'], t('"%value" is not a valid french postal code.
Postal codes should only contains 4 or 5 numbers', array('%value' => $item['value'])));
+ }
+ if ($field['type'] == 'ca_zipcode' && !valid_ca_zipcode($item['value'])) {
+ form_set_error($field['field_name'], t('"%value" is not a canadian valid postal code.
Postal codes should be like Z5Z 5Z5 or Z5Z5Z5 ...', array('%value' => $item['value'])));
+ }
+ if ($field['type'] == 'us_zipcode' && !valid_us_zipcode($item['value'])) {
+ form_set_error($field['field_name'], t('"%value" is not a valid us zipcode.
Zipcodes should be like 99999 or 99999-9999 ...', array('%value' => $item['value'])));
+ }
+ if ($field['type'] == 'uk_zipcode' && !valid_uk_zipcode($item['value'])) {
+ form_set_error($field['field_name'], t('"%value" is not a valid United Kingom postal code.
Postal codes should be like AB1 C23 or AB1C23 ...', array('%value' => $item['value'])));
+ }
+ if ($field['type'] == 'nl_zipcode' && !valid_nl_zipcode($item['value'])) {
+ form_set_error($field['field_name'], t('"%value" is not a valid Dutch zipcode.
Zipcodes should contain 4 numbers followed by 2 letter', array('%value' => $item['value'])));
+ }
+ if ($field['type'] == 'au_zipcode' && !valid_au_zipcode($item['value'])) {
+ form_set_error($field['field_name'], t('"%value" is not a valid Australian zipcode.
Zipcodes should contain 4 numbers', array('%value' => $item['value'])));
}
}
+ }
}
break;
- }
+ }
}
/**
- * Verification for zipcodes.
+ * Verification for zipcodes.
*
* @param string $text
- * @return boolean Returns boolean FALSE if the zipcode is not valid.
+ * @return boolean Returns boolean FALSE if the zipcode is not valid.
* On success, returns a string containting the zipcode with some formatting.
*/
function valid_fr_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- if (!preg_match("/^[0-9]{4,5}$/i",$zipcodestring)) {
- return false;
+ if (!preg_match("/^[0-9]{4,5}$/i", $zipcodestring)) {
+ return false;
}
- //right now just do a quick check ==> has to be improved
+ //right now just do a quick check ==> has to be improved
//need to check more precisely by doing a query on a zipcode table, ...
- else
- {
- return true;
+ else {
+ return true;
}
-}
+}
function valid_ca_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- if (!preg_match("/^\D{1}\d{1}\D{1}[ ]?\d{1}\D{1}\d{1}/i",$zipcodestring)) {
- return false;
+ if (!preg_match("/^\D{1}\d{1}\D{1}[ ]?\d{1}\D{1}\d{1}/i", $zipcodestring)) {
+ return false;
}
- //Z5Z 5Z5 orZ5Z5Z5
- else
- {
- return true;
+ //Z5Z 5Z5 orZ5Z5Z5
+ else {
+ return true;
}
-}
+}
function valid_us_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- if (!preg_match("/(^\d{5}$)|(^\d{5}-\d{4}$)$/i",$zipcodestring)) {
- return false;
+ if (!preg_match("/(^\d{5}$)|(^\d{5}-\d{4}$)$/i", $zipcodestring)) {
+ return false;
}
//99999 or 99999-9999
- else
- {
- return true;
+ else {
+ return true;
}
-}
+}
function valid_uk_zipcode($zipcodestring) {
@@ -316,11 +300,10 @@
preg_match("/^[$fst][$num][$thd][$gap]*[$nom][$in][$in]$/i", $zipcodestring) ||
preg_match("/^[$fst][$sec][$num][$fth][$gap]*[$nom][$in][$in]$/i", $zipcodestring)) {
return true;
- }
- else
- {
- return false;
- }
+ }
+ else {
+ return false;
+ }
}
@@ -328,34 +311,32 @@
$zipcodestring = trim($zipcodestring);
- if (!preg_match("/^[1-9][0-9]{3}\s*[a-z]{2}$/i",$zipcodestring)) {
- return false;
+ if (!preg_match("/^[1-9][0-9]{3}\s*[a-z]{2}$/i", $zipcodestring)) {
+ return false;
}
- //right now just do a quick check ==> has to be improved
+ //right now just do a quick check ==> has to be improved
//need to check more precisely by doing a query on a zipcode table, ...
- else
- {
- return true;
+ else {
+ return true;
}
-}
+}
function valid_au_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- if (!preg_match("/^[0-9]{4}$/i",$zipcodestring)) {
- return false;
+ if (!preg_match("/^[0-9]{4}$/i", $zipcodestring)) {
+ return false;
}
- //right now just do a quick check ==> has to be improved
+ //right now just do a quick check ==> has to be improved
//need to check more precisely by doing a query on a zipcode table, ...
- else
- {
- return true;
+ else {
+ return true;
}
-}
-
+}
+
/**
- * Formatting for zipcode.
+ * Formatting for zipcode.
*
* @param string $zipcodestring
* @return string Returns a string containting the zipcode with some formatting.
@@ -363,7 +344,7 @@
function format_fr_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- //do some formatting
+ //do some formatting
return $zipcodestring;
}
@@ -371,18 +352,18 @@
function format_ca_zipcode($zipcodestring) {
$zipcodestring = strtoupper(trim($zipcodestring));
- //do some formatting
+ //do some formatting
if (strlen($zipcodestring) == 6) {
- $zipcodestring = substr($zipcodestring, 0, 3) . ' ' . substr($zipcodestring, 3, 3);
+ $zipcodestring = substr($zipcodestring, 0, 3) .' '. substr($zipcodestring, 3, 3);
}
-
+
return $zipcodestring;
}
function format_us_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- //do some formatting
+ //do some formatting
return $zipcodestring;
}
@@ -396,13 +377,13 @@
$zipcodestring = trim($zipcodestring);
//do some formatting: make letters upper case and put space between numbers and letters
- $zipcodestring = preg_replace("/(\d{4})\s*(\w{2})/","$1 $2",strtoupper($zipcodestring));
+ $zipcodestring = preg_replace("/(\d{4})\s*(\w{2})/", "$1 $2", strtoupper($zipcodestring));
return $zipcodestring;
}
function format_au_zipcode($zipcodestring) {
$zipcodestring = trim($zipcodestring);
- //do some formatting
+ //do some formatting
return $zipcodestring;
}