5a6,20
> /*return the two-letter codes of installed country modules from the directory
> It could be possible to first cache the array of codes in a module-level variable
> Ahmad Gharbeia, October 2009
> */
> function get_country_modules() {
>         $d = drupal_get_path('module', 'phone');
>         foreach(array_diff(scandir($d), array('.', '..')) as $m) {
>                 if (is_file($d . '/' . $m) && preg_match('/.*\.inc$/', $m)) {
>                         $cc = substr($m, 6, -4);   //phone.(eg).inc
>                         $modules[$cc] = $cc . '_phone';
>                 }
>         }
>         return $modules;
> }
> 
30,45c45,54
<   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 <a href="http://www.itu.int/rec/T-REC-E.123/en">E.123</a>'))     
<    );
---
>         
>         foreach(get_country_modules() as $k => $v) {
>                 $phone_metadata_function = 'phone_' . $k . '_metadata';
>                 module_load_include('inc', 'phone', 'phone.'. $k);
>                 if (function_exists($phone_metadata_function)) {
>                     $fieldinfo[$v] = $phone_metadata_function();
>                 }
>         }
> 
>         return $fieldinfo;
112c121
<           '#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 <a href="http://www.itu.int/rec/T-REC-E.123/en">E.123 specification</a>.'),
---
>           '#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 on <a href="http://www.itu.int/rec/T-REC-E.123/en">E.123 specification</a>.'),
121c130
<           '#title' => t('Maximum length of international numbers, according to the ITU this is 15'),
---
>           '#title' => t('Maximum length of international numbers, according to the ITU, is 15'),
150,167c160,161
<       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),
<         );
---
>                 if (in_array($field['type'], get_country_modules())) {
>                 $columns = array(                       'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),         );
213,247c207,213
<           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<br>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<br>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<br>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!<br>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<br>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<br>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<br>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<br>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!<br>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!<br>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!<br>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!<br>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 (($countrycode = array_search($field['type'], get_country_modules())) !== FALSE) {
>                         if (!valid_phone_number($countrycode, $item['value'])) {
>                                                         $phone_metadata_function = 'phone_' . $countrycode . '_metadata';
>                                                         module_load_include('inc', 'phone', 'phone.'. $countrycode);
>                                                         if (function_exists($phone_metadata_function)) {
>                                                         $meta = $phone_metadata_function();
>                                                                 form_set_error($field['field_name'], t($meta['error'], array('%value' => $item['value'])));
249,250d214
<           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!<br>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'])));
252,253d215
<           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<br>International phone numbers should contain a country code prefixed by a plus sign followed by the local number.', array('%value' => $item['value'])));
263,303c225,226
<           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 = array_search($field['type'], get_country_modules())) !== FALSE) {
>             $node_field[$delta]['value'] = format_phone_number($countrycode, $node_field[$delta]['value'], $field);
395,409c318
<       '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' => get_country_modules(),
476,490c385
<       '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' => get_country_modules(),
678,683c573,576
< /**
<  * Verification for Phone Numbers.  
<  *
<  * @param string $countrycode
<  * @param string $phonenumber
<  * @return boolean Returns boolean FALSE if the phone number is not valid.  
---
> function process_phone_number($countrycode, $phonenumber, $op) {
> /* More of a macro to shorten code and reduce errors.
>  Accepts either 'valid' or 'format' and invokes the 
>  proper function, passing on its return value
685d577
< function valid_phone_number($countrycode, $phonenumber) {
690,708c582,583
<   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';       
---
>         if (array_key_exists($countrycode, get_country_modules())) {
>                 $op_phone_function = $op . '_'. $countrycode . '_phone_number';         
710,712c585,586
<         
<         if (function_exists($valid_phone_function)) {
<             return $valid_phone_function($phonenumber);
---
>                 if (function_exists($op_phone_function)) {
>                         return $op_phone_function($phonenumber);
718,719c592
<   else {
<         //Country not taken into account yet 
---
>   else {                //Country not yet implemented
731,750c604
< function format_phone_number($countrycode, $phonenumber, $field) {
< 
<   $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'                                        
<         ) { 
---
> function valid_phone_number($countrycode, $phonenumber) {
752c606,607
<         //drupal_set_message('langue = ' . $countrycode, 'error');       
---
>         return process_phone_number($countrycode, $phonenumber, 'valid');
> }  
754,755c609,616
<         $format_phone_function = 'format_'. $countrycode . '_phone_number';     
<         module_load_include('inc', 'phone', 'phone.'. $countrycode);
---
> /**
>  * Verification for Phone Numbers.  
>  *
>  * @param string $countrycode
>  * @param string $phonenumber
>  * @return boolean Returns boolean FALSE if the phone number is not valid.  
>  */
> function format_phone_number($countrycode, $phonenumber) {
757,767c618
<         if (function_exists($format_phone_function)) {
<             return $format_phone_function($phonenumber, $field);
<         }
<         else {
<             return FALSE; 
<         }
<   }
<   else {
<         //Country not taken into account yet   
<         return FALSE; 
<   }
---
>         return process_phone_number($countrycode, $phonenumber, 'format');
