Index: phone.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.module,v retrieving revision 1.24 diff -u -r1.24 phone.module --- phone.module 26 Aug 2009 16:06:12 -0000 1.24 +++ phone.module 5 Oct 2009 08:12:36 -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,22 +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')), - 'nz_phone' => array('label' => t('New Zealand Phone Numbers')), - 'br_phone' => array('label' => t('Brazilian Phone Numbers')), - 'int_phone'=> array('label' => t('International Phone Numbers, as per E.123')) - ); + $info = array(); + foreach (phone_country_metadata() as $cc => $item) { + $info[$cc .'_phone'] = array('label' => t($item['label'])); + } + return $info; } /** @@ -106,65 +182,55 @@ '#title' => t('Add the country code if not filled by the user'), '#default_value' => isset($field['phone_country_code']) ? $field['phone_country_code'] : '', ); - if ($field['type'] == 'int_phone') { - $form['phone_int_help'] = array( - '#type' => 'markup', - '#value' => t('International phone numbers are in the form +XX YYYYYYY where XX is a country code and YYYYYYY is the local number. This field type is based off of the E.123 specification.'), - ); - $form['phone_default_country_code'] = array( - '#type' => 'textfield', - '#title' => t('Default country code to add to international numbers without one (omit + sign)'), - '#default_value' => isset($field['phone_default_country_code']) ? $field['phone_default_country_code'] : '1', - ); - $form['phone_int_max_length'] = array( - '#type' => 'textfield', - '#title' => t('Maximum length of international numbers, according to the ITU this is 15'), - '#default_value' => isset($field['phone_int_max_length']) ? $field['phone_int_max_length'] : '15', - ); - } - if ($field['type'] == 'ca_phone') { - $form['ca_phone_separator'] = array( - '#type' => 'textfield', - '#title' => t('Separator'), - '#default_value' => isset($field['ca_phone_separator']) ? $field['ca_phone_separator'] : '-', - '#size' => 2, - ); - $form['ca_phone_parentheses'] = array( - '#type' => 'checkbox', - '#title' => t('Use parentheses around area code'), - '#default_value' => isset($field['ca_phone_parentheses']) ? $field['ca_phone_parentheses'] : 1, - ); - } + + // display country specific settings + foreach (_phone_field_types() as $type) { + $settings_function = 'phone_' . $type . '_number_settings'; + if (function_exists($settings_function)) { + $local_settings = $settings_function($op, $field); + if (isset($local_settings) && !empty($local_settings)) { + // wrap with fieldset + $wrapper = array( + '#title' => phone_country_metadata($type) . ' specific settings', + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#attributes' => array('class' => 'phone-settings phone-settings-' . $type), + ); + $wrapper[] = $local_settings; + array_push($form, $wrapper); + } + } + } return $form; + case 'validate': + // validate country specific settings + foreach (_phone_field_types() as $type) { + $validate_function = 'phone_' . $type . '_number_validate'; + if (function_exists($validate_function)) { + $validate_function($op, $field); + } + } + break; + case 'save': $settings = array('phone_country_code'); - if ($field['type'] == 'ca_phone') { - array_push($settings, 'ca_phone_separator', 'ca_phone_parentheses'); + // save country specific settings + foreach (_phone_field_types() as $type) { + $save_function = 'phone_' . $type . '_number_save'; + if (function_exists($save_function)) { + array_push($settings, $save_function($op, $field)); + } } - if ($field['type'] == 'int_phone') { - array_push($settings, 'phone_int_help', 'phone_default_country_code', 'phone_int_max_length'); - } 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' - || $field['type'] == 'nz_phone' - || $field['type'] == 'br_phone' - || $field['type'] == 'int_phone' - ) { - $columns = array( - 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE), - ); + $countrycode = phone_countrycode_from_fieldtype($field['type']); + + if (phone_country_metadata($countrycode)) { + $columns = array( + 'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE), + ); } return $columns; } @@ -210,48 +276,14 @@ 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']))); - } - if ($field['type'] == 'nz_phone' && !valid_phone_number('nz', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid New Zealand phone number!
New Zealand phone numbers should contain only only numbers, spaces, brackets and "-". They should look like 04 123 4567 or can optionally omit the leading 0 and have a prefix of "+64".', array('%value' => $item['value']))); - } - if ($field['type'] == 'br_phone' && !valid_phone_number('br', $item['value'])) { - form_set_error($field['field_name'], t('"%value" is not a valid Brazilian phone number!
Brazilian phone numbers should contain only numbers and spaces and - and be like 099 9999-9999 or 99 9999-9999 with an optional prefix of "+55".', array('%value' => $item['value']))); - } - if ($field['type'] == 'int_phone' && !valid_phone_number('int', $item['value'])) { - form_set_error($field['field_name'],t('"%value" is not a valid international phone number
International phone numbers should contain a country code prefixed by a plus sign followed by the local number.', 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']))); + } } } break; @@ -260,48 +292,9 @@ 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 ($field['type'] == 'nz_phone') { - $node_field[$delta]['value'] = format_phone_number('nz', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'br_phone') { - $node_field[$delta]['value'] = format_phone_number('br', $node_field[$delta]['value'], $field); - } - if ($field['type'] == 'int_phone') { - $node_field[$delta]['value'] = format_phone_number('int', $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); + } } } break; @@ -392,22 +385,8 @@ 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', - 'nz_phone', - 'br_phone', - 'int_phone' - ), - 'multiple values' => CONTENT_HANDLE_CORE, + 'field types' => _phone_field_types(), + 'multiple values' => CONTENT_HANDLE_CORE, ), ); } @@ -473,25 +452,11 @@ 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', - 'nz_phone', - 'br_phone', - 'int_phone' - ), + 'field types' => _phone_field_types(), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array( 'default value' => CONTENT_CALLBACK_DEFAULT, - ), + ), ), ); } @@ -687,37 +652,19 @@ $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' - || $countrycode == 'nz' - || $countrycode == 'br' - || $countrycode == 'int' - ) { - - //drupal_set_message('langue = ' . $countrycode, 'error'); - - $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); - } - else { - return FALSE; - } + if (phone_country_metadata($countrycode)) { + $valid_phone_function = 'valid_'. $countrycode . '_phone_number'; + + if (function_exists($valid_phone_function)) { + return $valid_phone_function($phonenumber); + } + else { + return FALSE; + } } else { - //Country not taken into account yet - return FALSE; + //Country not taken into account yet + return FALSE; } } @@ -733,37 +680,19 @@ $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' - || $countrycode == 'nz' - || $countrycode == 'br' - || $countrycode == 'int' - ) { - - //drupal_set_message('langue = ' . $countrycode, 'error'); - - $format_phone_function = 'format_'. $countrycode . '_phone_number'; - module_load_include('inc', 'phone', 'phone.'. $countrycode); - - if (function_exists($format_phone_function)) { - return $format_phone_function($phonenumber, $field); - } - else { - return FALSE; - } + if (phone_country_metadata($countrycode)) { + $format_phone_function = 'format_'. $countrycode . '_phone_number'; + + if (function_exists($format_phone_function)) { + return $format_phone_function($phonenumber, $field); + } + else { + return FALSE; + } } else { - //Country not taken into account yet - return FALSE; + //Country not taken into account yet + return FALSE; } } Index: phone.ca.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.ca.inc,v retrieving revision 1.26 diff -u -r1.26 phone.ca.inc --- phone.ca.inc 26 Aug 2009 16:06:12 -0000 1.26 +++ phone.ca.inc 5 Oct 2009 08:12:29 -0000 @@ -12,7 +12,28 @@ '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.', ); } - + +function phone_ca_number_settings($op, $field) { + $form = array(); + $form['ca_phone_separator'] = array( + '#type' => 'textfield', + '#title' => t('Separator'), + '#default_value' => isset($field['ca_phone_separator']) ? $field['ca_phone_separator'] : '-', + '#size' => 2, + ); + $form['ca_phone_parentheses'] = array( + '#type' => 'checkbox', + '#title' => t('Use parentheses around area code'), + '#default_value' => isset($field['ca_phone_parentheses']) ? $field['ca_phone_parentheses'] : 1, + ); + + return $form; +} + +function phone_ca_number_save($op, $field) { + return array('ca_phone_separator', 'ca_phone_parentheses'); +} + /** * Verifies that $phonenumber is a valid ten-digit North American phone number * @@ -84,7 +105,7 @@ if ($field['phone_country_code']) { if ($matches[1] != "1") { - $phonenumber = "1" . " " . $phonenumber; + $phonenumber = "1" . " " . $phonenumber; } } Index: phone.int.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.int.inc,v retrieving revision 1.1 diff -u -r1.1 phone.int.inc --- phone.int.inc 26 Aug 2009 16:06:12 -0000 1.1 +++ phone.int.inc 5 Oct 2009 08:12:32 -0000 @@ -1,6 +1,38 @@ 'International Phone Numbers per E.123', + 'error' => '"%value" is not a valid international phone number
International phone numbers should contain a country code prefixed by a plus sign followed by the local number.', + ); +} + +function phone_int_number_settings($op, $field) { + $form = array(); + $form['phone_int_help'] = array( + '#type' => 'markup', + '#value' => t('International phone numbers are in the form +XX YYYYYYY where XX is a country code and YYYYYYY is the local number. This field type is based off of the E.123 specification.'), + ); + $form['phone_default_country_code'] = array( + '#type' => 'textfield', + '#title' => t('Default country code to add to international numbers without one (omit + sign)'), + '#default_value' => isset($field['phone_default_country_code']) ? $field['phone_default_country_code'] : '1', + ); + $form['phone_int_max_length'] = array( + '#type' => 'textfield', + '#title' => t('Maximum length of international numbers, according to the ITU this is 15'), + '#default_value' => isset($field['phone_int_max_length']) ? $field['phone_int_max_length'] : '15', + ); + + return $form; +} + +function phone_int_number_save($op, $field) { + return array('phone_int_help', 'phone_default_country_code', 'phone_int_max_length'); +} + /** * Verifies that $phonenumber is a valid international phone number as per ITU or, * if a default country code is specified, a valid subscriber number.