Index: phone.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.module,v retrieving revision 1.23 diff -u -r1.23 phone.module --- phone.module 8 Mar 2009 00:04:26 -0000 1.23 +++ phone.module 10 Aug 2009 15:10:18 -0000 @@ -10,6 +10,93 @@ */ /** + * Implementation of hook_init() + * This hook is called on module initialization. + */ +function phone_init() { + $path = drupal_get_path('module', 'phone'); + + // scan include phone numbers directory + $files = file_scan_directory($path, '^phone\..*\.inc$'); + + // load all phone number includes + $countrycodes = array(); + foreach ($files as $file) { + module_load_include('inc', 'phone', $file->name); + list ($dummy, $countrycode) = explode('.', $file->name); + $countrycodes[] = $countrycode; + } + + // save the list of country codes + variable_set('phone_country_codes', $countrycodes); +} + + +/** + * 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(); + + if (empty($metadata)) { + $cc = variable_get('phone_country_codes', NULL); + if (!is_null($cc)) { + foreach ($cc as $code) { + $phone_metadata_function = 'phone_'. $code . '_metadata'; + if (function_exists($phone_metadata_function)) { + $metadata[$code] = $phone_metadata_function(); + } + } + } + } + + // 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' == $fieldtype ? $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 +114,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 +204,9 @@ } 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 +255,13 @@ 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 +271,8 @@ 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 +364,7 @@ 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 +431,7 @@ 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,24 +628,9 @@ $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' - ) { - - //drupal_set_message('langue = ' . $countrycode, 'error'); + if (phone_country_metadata($countrycode)) { + $valid_phone_function = 'valid_'. $countrycode . '_phone_number'; - $valid_phone_function = 'valid_'. $countrycode . '_phone_number'; - module_load_include('inc', 'phone', 'phone.'. $countrycode); - if (function_exists($valid_phone_function)) { return $valid_phone_function($phonenumber); } @@ -678,23 +656,8 @@ $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' - ) { - - //drupal_set_message('langue = ' . $countrycode, 'error'); - - $format_phone_function = 'format_'. $countrycode . '_phone_number'; - module_load_include('inc', 'phone', 'phone.'. $countrycode); + if (phone_country_metadata($countrycode)) { + $format_phone_function = 'format_'. $countrycode . '_phone_number'; if (function_exists($format_phone_function)) { return $format_phone_function($phonenumber, $field); Index: phone.cs.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.cs.inc,v retrieving revision 1.14 diff -u -r1.14 phone.cs.inc --- phone.cs.inc 8 Mar 2009 00:04:26 -0000 1.14 +++ phone.cs.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,14 @@ * CCK Field for Czech phone numbers. */ +function phone_cs_metadata() { + // These strings are translated using t() on output. + return 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".', + ); +} + /** * Verifies that $phonenumber is a valid nine-digit Czech phone number * Index: phone.it.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.it.inc,v retrieving revision 1.18 diff -u -r1.18 phone.it.inc --- phone.it.inc 8 Mar 2009 00:04:26 -0000 1.18 +++ phone.it.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,14 @@ * CCK Field for Italian phone numbers. */ +function phone_it_metadata() { + // These strings are translated using t() on output. + return array( + 'label' => 'Italian Phone Numbers', + 'error' => '"%value" is not a valid Italian phone number
Italian phone numbers should only ...', + ); +} + /** * Verifies that $phonenumber is a valid ten-digit North American phone number * Index: phone.nl.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.nl.inc,v retrieving revision 1.1 diff -u -r1.1 phone.nl.inc --- phone.nl.inc 8 Mar 2009 00:04:26 -0000 1.1 +++ phone.nl.inc 10 Aug 2009 15:10:18 -0000 @@ -6,6 +6,14 @@ * CCK Field for Dutch phone numbers. */ +function phone_nl_metadata() { + // These strings are translated using t() on output. + return 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".', + ); +} + /** * Verifies that $phonenumber is a valid ten-digit Dutch phone number with spaces and - * Index: phone.uk.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.uk.inc,v retrieving revision 1.17 diff -u -r1.17 phone.uk.inc --- phone.uk.inc 8 Mar 2009 00:04:26 -0000 1.17 +++ phone.uk.inc 10 Aug 2009 15:10:18 -0000 @@ -6,6 +6,14 @@ * CCK Field for British phone numbers. */ +function phone_uk_metadata() { + // These strings are translated using t() on output. + return array( + 'label' => 'British (UK) Phone Numbers', + 'error' => '"%value" is not a valid British phone number
British Phone numbers should .... ', + ); +} + /** * Verifies that $phonenumber is a valid eleven-digit United Kingdom phone number * Index: phone.ru.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.ru.inc,v retrieving revision 1.17 diff -u -r1.17 phone.ru.inc --- phone.ru.inc 8 Mar 2009 00:04:26 -0000 1.17 +++ phone.ru.inc 10 Aug 2009 15:10:18 -0000 @@ -6,6 +6,14 @@ * CCK Field for Russian phone numbers. */ +function phone_ru_metadata() { + // These strings are translated using t() on output. + return array( + 'label' => 'Russian Phone Numbers', + 'error' => '"%value" is not a valid Russian phone number
Russian Phone numbers should .... ', + ); +} + /** * Verifies that $phonenumber is a valid ten-digit Russian phone number * Index: phone.hu.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.hu.inc,v retrieving revision 1.12 diff -u -r1.12 phone.hu.inc --- phone.hu.inc 8 Mar 2009 00:04:26 -0000 1.12 +++ phone.hu.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,14 @@ * CCK Field for Hungarian phone numbers. */ +function phone_hu_metadata() { + // These strings are translated using t() on output. + return 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".', + ); +} + /** * Verifies that $phonenumber is a valid nine-digit Hungarian phone number * Index: phone.cr.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.cr.inc,v retrieving revision 1.12 diff -u -r1.12 phone.cr.inc --- phone.cr.inc 8 Mar 2009 00:04:26 -0000 1.12 +++ phone.cr.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,14 @@ * CCK Field for Costa Rican phone numbers. */ +function phone_cr_metadata() { + // These strings are translated using t() on output. + return 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".', + ); +} + /** * Verifies that $phonenumber is a valid eight-digit Costa Rican phone number * Index: phone.au.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.au.inc,v retrieving revision 1.19 diff -u -r1.19 phone.au.inc --- phone.au.inc 8 Mar 2009 00:04:26 -0000 1.19 +++ phone.au.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,13 @@ * CCK Field for Australian phone numbers. */ +function phone_au_metadata() { + return 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"', + ); +} + /** * Verification for Australian Phone Numbers. * According to http://www.itu.int/itudoc/itu-t/number/a/aus/70772.html Index: phone.ca.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.ca.inc,v retrieving revision 1.25 diff -u -r1.25 phone.ca.inc --- phone.ca.inc 8 Mar 2009 00:04:26 -0000 1.25 +++ phone.ca.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,13 @@ * CCK Field for Canadian phone numbers. */ +function phone_ca_metadata() { + return 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.', + ); +} + /** * Verifies that $phonenumber is a valid ten-digit North American phone number * Index: phone.fr.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.fr.inc,v retrieving revision 1.23 diff -u -r1.23 phone.fr.inc --- phone.fr.inc 8 Mar 2009 00:04:26 -0000 1.23 +++ phone.fr.inc 10 Aug 2009 15:10:17 -0000 @@ -8,6 +8,14 @@ define('PHONE_FR_REGEX', "/(\+33|0)([1-6]\d{8}|85\d{7}|87[0-57-9]\d{6})$/"); +function phone_fr_metadata() { + // These strings are translated using t() on output. + return 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', + ); +} + /** * Verification for French Phone Numbers. * According to http://www.itu.int/itudoc/itu-t/number/f/fra/70680.html Index: phone.es.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.es.inc,v retrieving revision 1.18 diff -u -r1.18 phone.es.inc --- phone.es.inc 8 Mar 2009 00:04:26 -0000 1.18 +++ phone.es.inc 10 Aug 2009 15:10:17 -0000 @@ -6,6 +6,14 @@ * CCK Field for Spanish phone numbers. */ +function phone_es_metadata() { + // These strings are translated using t() on output. + return 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', + ); +} + /** * Verifies that $phonenumber is a valid nine-digit Spanish phone number *