? phone-refactor.patch Index: phone.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.module,v retrieving revision 1.23 diff -u -p -w -r1.23 phone.module --- phone.module 8 Mar 2009 00:04:26 -0000 1.23 +++ phone.module 2 Aug 2009 17:58:13 -0000 @@ -10,6 +10,103 @@ */ /** + * Get all the metadata about supported countries. + * + * @param $countrycode + * Optional, two character country code. If this is ommitted all metadata + * will be returned. + * @return + * If no country code is provided an array keyed by country code, values are + * arrays with a display 'label' and 'error' message. If a countrycode is + * provided and invalid FALSE will be returned. If the country code is valid + * the metadata for just that country will be returned. + */ +function phone_country_metadata($countrycode = NULL) { + // These strings are translated using t() on output. + static $metadata = array( + 'fr' => array( + 'label' => 'French Phone Numbers', + 'error' => '"%value" is not a valid French phone number
French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', + ), + 'it' => array( + 'label' => 'Italian Phone Numbers', + 'error' => '"%value" is not a valid Italian phone number
Italian phone numbers should only ...', + ), + 'ca' => array( + 'label' => 'US & Canadian Phone Numbers', + 'error' => '"%value" is not a valid North American phone number
North American Phone numbers should only contain numbers and + and - and ( and ) and spaces and be like 999-999-9999. Please enter a valid ten-digit phone number with optional extension.', + ), + 'cr' => array( + 'label' => 'Costa Rican Phone Numbers', + 'error' => '"%value" is not a valid Costa Rican phone number!
Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".', + ), + 'uk' => array( + 'label' => 'British (UK) Phone Numbers', + 'error' => '"%value" is not a valid British phone number
British Phone numbers should .... ', + ), + 'ru' => array( + 'label' => 'Russian Phone Numbers', + 'error' => '"%value" is not a valid Russian phone number
Russian Phone numbers should .... ', + ), + 'es' => array( + 'label' => 'Spanish Phone Numbers', + 'error' => '"%value" is not a valid Spanish phone number
Spanish phone numbers should only contains numbers and spaces and be like 999 999 999', + ), + 'au' => array( + 'label' => 'Australian Phone Numbers', + 'error' => '"%value" is not a valid Australian phone number
Australian phone numbers should contain only numbers with an optional prefix of "+61"', + ), + 'cs' => array( + 'label' => 'Czech Phone Numbers', + 'error' => '"%value" is not a valid Czech phone number!
Czech phone numbers should contain only numbers and spaces be like 999 999 999 with an optional prefix of "+420" or "00420".', + ), + 'hu' => array( + 'label' => 'Hungarian Phone Numbers', + 'error' => '"%value" is not a valid Hungarian phone number!
Hungarian phone numbers should contain only numbers and spaces be like 70 999 9999 with an optional prefix of "+36" or "06".', + ), + 'nl' => array( + 'label' => 'Dutch Phone Numbers', + 'error' => '"%value" is not a valid Dutch phone number!
Dutch phone numbers should contain only numbers and spaces and - and be like 099-9999999 with an optional prefix of "+31".', + ), + ); + + // No country code specified, return the entire list. + if (is_null($countrycode)) { + return $metadata; + } + + // Try to locate the desired country. + if (isset($metadata[$countrycode])) { + return $metadata[$countrycode]; + } + return FALSE; +} + +/** + * Parse the country code from a field type. + * + * @param $fieldtype + * String with the field type: e.g. "uk_phone". + * @return + * Two character country code string. + */ +function phone_countrycode_from_fieldtype($fieldtype) { + $countrycode = substr($fieldtype, 0, 2); + return $countrycode .'_phone' == $type ? $countrycode : FALSE; +} + +/** + * Return an array of all the supported field types. + */ +function _phone_field_types() { + $types = array(); + foreach (phone_country_metadata() as $key => $item) { + $types[] = $key .'_phone'; + } + return $types; +} + +/** * Implementation of hook_field_info(). * * Here we indicate that the content module will use its default @@ -27,19 +124,11 @@ * the database and in internal arrays, like content_fields(). */ function phone_field_info() { - return array( - 'fr_phone' => array('label' => t('French Phone Numbers')), - 'it_phone' => array('label' => t('Italian Phone Numbers')), - 'ca_phone' => array('label' => t('US & Canadian Phone Numbers')), - 'cr_phone' => array('label' => t('Costa Rican Phone Numbers')), - 'uk_phone' => array('label' => t('British (UK) Phone Numbers')), - 'ru_phone' => array('label' => t('Russian Phone Numbers')), - 'es_phone' => array('label' => t('Spanish Phone Numbers')), - 'au_phone' => array('label' => t('Australian Phone Numbers')), - 'cs_phone' => array('label' => t('Czech Phone Numbers')), - 'hu_phone' => array('label' => t('Hungarian Phone Numbers')), - 'nl_phone' => array('label' => t('Dutch Phone Numbers')) - ); + $info = array(); + foreach (phone_country_metadata() as $cc => $item) { + $info[$cc .'_phone'] = array('label' => t($item['label'])); + } + return $info; } /** @@ -125,18 +214,8 @@ function phone_field_settings($op, $fiel } return $settings; case 'database columns': - if ($field['type'] == 'fr_phone' - || $field['type'] == 'it_phone' - || $field['type'] == 'ca_phone' - || $field['type'] == 'cr_phone' - || $field['type'] == 'uk_phone' - || $field['type'] == 'ru_phone' - || $field['type'] == 'es_phone' - || $field['type'] == 'au_phone' - || $field['type'] == 'cs_phone' - || $field['type'] == 'hu_phone' - || $field['type'] == 'nl_phone' - ) { + $countrycode = phone_countrycode_from_fieldtype($field['type']); + if (phone_country_metadata($countrycode)) { $columns = array( 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), ); @@ -185,38 +264,13 @@ function phone_field($op, &$node, $field case 'validate': // corresponds to hook phone_widget validate in phone-5.x foreach ($node_field as $delta => $item) { if ($item['value'] != '') { - if ($field['type'] == 'fr_phone' && !valid_phone_number('fr', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid French phone number
French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', array('%value' => $item['value']))); - } - if ($field['type'] == 'it_phone' && !valid_phone_number('it', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Italian phone number
Italian phone numbers should only ...', array('%value' => $item['value']))); - } - if ($field['type'] == 'ca_phone' && !valid_phone_number('ca', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid North American phone number
North American Phone numbers should only contain numbers and + and - and ( and ) and spaces and be like 999-999-9999. Please enter a valid ten-digit phone number with optional extension.', array('%value' => $item['value']))); - } - if ($field['type'] == 'cr_phone' && !valid_phone_number('cr', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Costa Rican phone number!
Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".', array('%value' => $item['value']))); - } - if ($field['type'] == 'uk_phone' && !valid_phone_number('uk', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid British phone number
British Phone numbers should .... ', array('%value' => $item['value']))); - } - if ($field['type'] == 'ru_phone' && !valid_phone_number('ru', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Russian phone number
Russian Phone numbers should .... ', array('%value' => $item['value']))); - } - if ($field['type'] == 'es_phone' && !valid_phone_number('es', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Spanish phone number
Spanish phone numbers should only contains numbers and spaces and be like 999 999 999', array('%value' => $item['value']))); - } - if ($field['type'] == 'au_phone' && !valid_phone_number('au', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Australian phone number
Australian phone numbers should contain only numbers with an optional prefix of "+61"', array('%value' => $item['value']))); - } - if ($field['type'] == 'cs_phone' && !valid_phone_number('cs', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Czech phone number!
Czech phone numbers should contain only numbers and spaces be like 999 999 999 with an optional prefix of "+420" or "00420".', array('%value' => $item['value']))); - } - if ($field['type'] == 'hu_phone' && !valid_phone_number('hu', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Hungarian phone number!
Hungarian phone numbers should contain only numbers and spaces be like 70 999 9999 with an optional prefix of "+36" or "06".', array('%value' => $item['value']))); - } - if ($field['type'] == 'nl_phone' && !valid_phone_number('nl', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Dutch phone number!
Dutch phone numbers should contain only numbers and spaces and - and be like 099-9999999 with an optional prefix of "+31".', array('%value' => $item['value']))); + $countrycode = phone_countrycode_from_fieldtype($field['type']); + if (!$countrycode) { + form_set_error($field['field_name'], t('Unknown country')); + } + else if (!valid_phone_number($countrycode, $item['value'])) { + $metadata = phone_country_metadata($countrycode); + form_set_error($field['field_name'], t($metadata['error'], array('%value' => $item['value']))); } } } @@ -226,38 +280,8 @@ function phone_field($op, &$node, $field foreach ($node_field as $delta => $item) { //format the phone number if ($item['value'] != '') { - if ($field['type'] == 'fr_phone') { - $node_field[$delta]['value'] = format_phone_number('fr', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'it_phone') { - $node_field[$delta]['value'] = format_phone_number('it', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'ca_phone') { - $node_field[$delta]['value'] = format_phone_number('ca', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'cr_phone') { - $node_field[$delta]['value'] = format_phone_number('cr', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'uk_phone') { - $node_field[$delta]['value'] = format_phone_number('uk', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'ru_phone') { - $node_field[$delta]['value'] = format_phone_number('ru', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'es_phone') { - $node_field[$delta]['value'] = format_phone_number('es', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'au_phone') { - $node_field[$delta]['value'] = format_phone_number('au', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'cs_phone') { - $node_field[$delta]['value'] = format_phone_number('cs', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'hu_phone') { - $node_field[$delta]['value'] = format_phone_number('hu', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'nl_phone') { - $node_field[$delta]['value'] = format_phone_number('nl', $node_field[$delta]['value'], $field); + if ($countrycode = phone_countrycode_from_fieldtype($field['type'])) { + $node_field[$delta]['value'] = format_phone_number($countrycode, $node_field[$delta]['value'], $field); } } } @@ -349,18 +373,7 @@ function phone_field_formatter_info() { return array( 'default' => array( 'label' => 'Default', - 'field types' => array('fr_phone', - 'it_phone', - 'ca_phone', - 'cr_phone', - 'uk_phone', - 'ru_phone', - 'es_phone', - 'au_phone', - 'cs_phone', - 'hu_phone', - 'nl_phone' - ), + 'field types' => _phone_field_types(), 'multiple values' => CONTENT_HANDLE_CORE, ), ); @@ -427,18 +440,7 @@ function phone_widget_info() { return array( 'phone_textfield' => array( 'label' => t('Textfield'), - 'field types' => array('fr_phone', - 'it_phone', - 'ca_phone', - 'cr_phone', - 'uk_phone', - 'ru_phone', - 'es_phone', - 'au_phone', - 'cs_phone', - 'hu_phone', - 'nl_phone' - ), + 'field types' => _phone_field_types(), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array( 'default value' => CONTENT_CALLBACK_DEFAULT, @@ -635,18 +637,7 @@ function valid_phone_number($countrycode $countrycode = trim($countrycode); $phonenumber = trim($phonenumber); - if ($countrycode == 'fr' - || $countrycode == 'it' - || $countrycode == 'ca' - || $countrycode == 'cr' - || $countrycode == 'uk' - || $countrycode == 'ru' - || $countrycode == 'es' - || $countrycode == 'au' - || $countrycode == 'cs' - || $countrycode == 'hu' - || $countrycode == 'nl' - ) { + if (phone_country_metadata($countrycode)) { //drupal_set_message('langue = ' . $countrycode, 'error'); @@ -677,19 +668,7 @@ function format_phone_number($countrycod $countrycode = trim($countrycode); $phonenumber = trim($phonenumber); - - if ($countrycode == 'fr' - || $countrycode == 'it' - || $countrycode == 'ca' - || $countrycode == 'cr' - || $countrycode == 'uk' - || $countrycode == 'ru' - || $countrycode == 'es' - || $countrycode == 'au' - || $countrycode == 'cs' - || $countrycode == 'hu' - || $countrycode == 'nl' - ) { + if (phone_country_metadata($countrycode)) { //drupal_set_message('langue = ' . $countrycode, 'error');