Index: phone.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/phone/phone.module,v
retrieving revision 1.23
diff -r1.23 phone.module
12a13,105
>  * @param  $countrycode the 2 letters country code
>  * @return the metadata of the country if found, FALSE if not found,
>  *         all the metadata if $countrycode is FALSE
>  */
> function _phone_metadata_all() {
>   static $metadata = array(
>     'fr' => array(
>       'cc'    => 'fr',
>       'label' => 'French Phone Numbers',
>       'error' => '"%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',
>     ),
>     'it' => array(
>       'cc'    => 'it',
>       'label' => 'Italian Phone Numbers',
>       'error' => '"%value" is not a valid Italian phone number<br>Italian phone numbers should only ...',
>     ),
>     'ca' => array(
>       'cc'    => 'ca',
>       'label' => 'US & Canadian Phone Numbers',
>       'error' => '"%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.',
>     ),
>     'cr' => array(
>       'cc'    => 'cr',
>       'label' => 'Costa Rican Phone Numbers',
>       'error' => '"%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".',
>     ),      
>     'uk' => array(
>       'cc'    => 'uk',
>       'label' => 'British (UK) Phone Numbers',
>       'error' => '"%value" is not a valid British phone number<br>British Phone numbers should .... ',
>     ),
>     'ru' => array(
>       'cc'    => 'ru',
>       'label' => 'Russian Phone Numbers',
>       'error' => '"%value" is not a valid Russian phone number<br>Russian Phone numbers should .... ',
>     ),
>     'es' => array(
>       'cc'    => 'es',
>       'label' => 'Spanish Phone Numbers',
>       'error' => '"%value" is not a valid Spanish phone number<br>Spanish phone numbers should only contains numbers and spaces and be like 999 999 999',
>     ),
>     'au' => array(
>       'cc'    => 'au',
>       'label' => 'Australian Phone Numbers',
>       'error' => '"%value" is not a valid Australian phone number<br>Australian phone numbers should contain only numbers with an optional prefix of "+61"',
>     ),
>     'cs' => array(
>       'cc'    => 'cs',
>       'label' => 'Czech Phone Numbers',
>       'error' => '"%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".',
>     ),
>     'hu' => array(
>       'cc'    => 'hu',
>       'label' => 'Hungarian Phone Numbers',
>       'error' => '"%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".',
>     ),
>     'nl' => array(
>       'cc'    => 'nl',
>       'label' => 'Dutch Phone Numbers',
>       'error' => '"%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".',
>     ),
>   );
>   return $metadata;
> }
> 
> /**
>  * @param  $countrycode the 2 letters country code
>  * @return the metadata of the country if found, FALSE if not found
>  */
> function _phone_metadata_by_countrycode($countrycode) {
>   $metadata = _phone_metadata_all();
>   return ($countrycode && isset($metadata[$countrycode]) ? $metadata[$countrycode] : FALSE);
>     
> 
> }
> function phone_countrycode_by_type($type) {
>   $countrycode = substr($type, 0, 2);
>   return ($countrycode .'_phone' == $type ? $countrycode : FALSE);
> }
> function phone_metadata_by_type($type) {
>   $countrycode = phone_countrycode_by_type($type);
>   return ($countrycode ? _phone_metadata_by_countrycode($countrycode) : FALSE);
> }
> 
> function _phone_all_field_types() {
>   $types = array();
>   foreach (_phone_metadata_all() as $key => $item) {
>     $types[] = $key .'_phone';
>   }
>   return $types;
> }
> 
> /**
30,42c123,127
<   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_metadata_all() as $key => $item) {
>     $info[$key .'_phone'] = array('label' => t($item['label']));
>   }
>   return $info;
128,139c213
<       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'              
<        ) { 
---
>       if (phone_metadata_by_type($field['type'])) { 
188,210c262,266
<           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'])));
---
>           $metadata = phone_metadata_by_type($field['type']);
>           if (!$metadata) {
>             form_set_error($field['field_name'], t('Unknown country'));
>           } else if (!valid_phone_number($metadata['cc'], $item['value'])) {
>             form_set_error($field['field_name'], t($metadata['error'], array('%value' => $item['value'])));
212,220d267
<           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'])));
<           }            
229,245c276,278
<           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);
---
>           $countrycode = phone_countrycode_by_type($field['type']);
>           if ($countrycode) {
>             $node_field[$delta]['value'] = format_phone_number($countrycode, $node_field[$delta]['value'], $field);
247,261d279
<           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);
<           }            
352,363c370
<       '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_all_field_types(),
430,441c437
<       '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_all_field_types(),
637,649c633,634
< 
<   if ($countrycode == 'fr' 
<   	|| $countrycode == 'it'  
<   	|| $countrycode == 'ca'
<   	|| $countrycode == 'cr'  	  	  	
<   	|| $countrycode == 'uk'
<   	|| $countrycode == 'ru'
<   	|| $countrycode == 'es'
<   	|| $countrycode == 'au'
<   	|| $countrycode == 'cs'  	
<   	|| $countrycode == 'hu' 
<   	|| $countrycode == 'nl'   	
<   	) { 
---
>   $metadata = _phone_metadata_by_countrycode($countrycode);
>   if ($metadata) { 
680,692c665,666
< 
<   if ($countrycode == 'fr' 
<   	|| $countrycode == 'it'
<   	|| $countrycode == 'ca'
<   	|| $countrycode == 'cr'  	  	  	  	
<   	|| $countrycode == 'uk'
<   	|| $countrycode == 'ru'
<   	|| $countrycode == 'es'
<   	|| $countrycode == 'au'
<    	|| $countrycode == 'cs' 	
<    	|| $countrycode == 'hu' 	   	
<    	|| $countrycode == 'nl' 	   	   	
<   	) { 
---
>   $metadata = _phone_metadata_by_countrycode($countrycode);
>   if ($metadata) { 
