=== modified file 'sites/all/modules/phone/phone.ca.inc' --- sites/all/modules/phone/phone.ca.inc 2010-09-12 19:17:12 +0000 +++ sites/all/modules/phone/phone.ca.inc 2010-10-05 17:53:09 +0000 @@ -5,7 +5,6 @@ * @file * CCK Field for Canadian phone numbers. */ - function phone_ca_metadata() { return array( 'label' => 'US & Canadian Phone Numbers', @@ -19,28 +18,18 @@ * @param string $phonenumber * @return boolean Returns boolean FALSE if the phone number is not valid. */ - function valid_ca_phone_number($phonenumber) { - $phonenumber = trim($phonenumber); + $phonenumber = clean_ca_phone_number($phonenumber); - // define regular expression - $regex = '/ - \D* # ignore non-digits - (\d*) # an optional 1 - \D* # optional separator - [2-9][0-8]\d # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit) - \D* # optional separator - [2-9]\d{2} # 3-digit prefix (cannot start with 0 or 1) - \D* # optional separator - \d{4} # 4-digit line number - \D* # optional separator - \d* # optional extension - \D* # ignore trailing non-digits - /x'; - // return true if valid, false otherwise - $result = preg_match($regex, $phonenumber, $matches); - return ($result && ($matches[1] == '' || $matches[1] == '1')); + // Legal numbers must have at least 10 digits -- they may have more if an + // extension is provided. + if (drupal_strlen($phonenumber) < 10) { + return FALSE; + } + else { + return TRUE; + } } /** @@ -50,44 +39,26 @@ * */ function format_ca_phone_number($phonenumber, $field) { - - // define regular expression - $regex = '/ - \D* # ignore non-digits - (\d*) # an optional 1 - \D* # optional separator - ([2-9][0-8]\d) # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit) - \D* # optional separator - ([2-9]\d{2}) # 3-digit prefix (cannot start with 0 or 1) - \D* # optional separator - (\d{4}) # 4-digit line number - \D* # optional separator - (\d*) # optional extension - \D* # ignore trailing non-digits - /x'; - - // get digits of phone number - preg_match($regex, $phonenumber, $matches); - - $separator = isset($field['ca_phone_separator']) ? $field['ca_phone_separator'] : '-'; - - // construct ten-digit phone number - $phonenumber = - ( $field['ca_phone_parentheses'] ? - '(' . $matches[2] . ') ' : - $matches[2] . $separator ) . - $matches[3] . $separator . $matches[4]; - - // Optional extension - if ($matches[5] != '') { - $phonenumber .= ' x' . $matches[5]; - } - - if ($field['phone_country_code']) { - // This condition check is pointless. - if ($matches[1] != '1') { - $phonenumber = '1' . ' ' . $phonenumber; - } + if (!empty($phonenumber) && !empty($field['ca_phone_mask'])) { + // Clean the number first in case it comes in with formatting. + $phonenumber = clean_ca_phone_number($phonenumber); + $phonenumber = phone_apply_mask($phonenumber, $field['ca_phone_mask']); } return $phonenumber; } + +/** + * Removes all non-digits and country code from a phone number. + * + * @param $number + * The number to clean. + * @return + * The cleaned number. + */ +function clean_ca_phone_number($number) { + // Yank non-digits. + $number = preg_replace('/\D/', '', $number); + // Yank country code. + $number = preg_replace('/^1/', '', $number); + return $number; +} === modified file 'sites/all/modules/phone/phone.module' --- sites/all/modules/phone/phone.module 2010-09-12 19:17:12 +0000 +++ sites/all/modules/phone/phone.module 2010-10-05 17:57:24 +0000 @@ -141,16 +141,12 @@ ); } if ($field['type'] == 'ca_phone') { - $form['ca_phone_separator'] = array( + $form['ca_phone_mask'] = 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, + '#title' => t('Mask'), + '#default_value' => isset($field['ca_phone_mask']) ? $field['ca_phone_mask'] : '(XXX) XXX-XXXX', + '#size' => 30, + '#description' => t("Enter a mask used to format the number -- captital X's will be replaced by the corresponding digit, eg. 1-XXX-XXX-XXXX"), ); } return $form; @@ -158,7 +154,7 @@ case 'save': $settings = array('phone_country_code'); if ($field['type'] == 'ca_phone') { - array_push($settings, 'ca_phone_separator', 'ca_phone_parentheses'); + array_push($settings, 'ca_phone_mask'); } if ($field['type'] == 'int_phone') { array_push($settings, 'phone_int_help', 'phone_default_country_code', 'phone_int_max_length'); @@ -367,7 +363,7 @@ $node_field[$delta]['value'] = format_phone_number('ch', $node_field[$delta]['value'], $field); } if ($field['type'] == 'ca_phone') { - $node_field[$delta]['value'] = format_phone_number('ca', $node_field[$delta]['value'], $field); + $node_field[$delta]['value'] = clean_phone_number('ca', $node_field[$delta]['value']); } if ($field['type'] == 'cr_phone') { $node_field[$delta]['value'] = format_phone_number('cr', $node_field[$delta]['value'], $field); @@ -576,49 +572,6 @@ } /** - * Implementation of hook_field_formatter(). - * - * Prepare an individual item for viewing in a browser. - * - * @param $field - * The field the action is being performed on. - * @param $item - * An array, keyed by column, of the data stored for this item in this field. - * @param $formatter - * The name of the formatter being used to display the field. - * @param $node - * The node object, for context. Will be NULL in some cases. - * Warning: when displaying field retrieved by Views, $node will not - * be a "full-fledged" node object, but an object containg the data returned - * by the Views query (at least nid, vid, changed) - * @return - * An HTML string containing the formatted item. - * - * In a multiple-value field scenario, this function will be called once per - * value currently stored in the field. This function is also used as the handler - * for viewing a field in a views.module tabular listing. - * - * It is important that this function at the minimum perform security - * transformations such as running check_plain() or check_markup(). - */ -function phone_field_formatter($field, $item, $formatter, $node) { - if (!isset($item['value'])) { - return ''; - } - if ($field['text_processing']) { - $text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview)); - } - else { - $text = check_plain($item['value']); - } - // iPhone Support - if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) { - $text = '' . $text . ''; - } - return $text; -} - -/** * Implementation of hook_widget_info(). * * Here we indicate that the content module will handle @@ -828,7 +781,7 @@ $delta = $element['#delta']; $element[$field_key] = array( '#type' => 'textfield', - '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL, + '#default_value' => isset($element['#value'][$field_key]) ? format_phone_number_by_country($field, $element['#value'][$field_key]) : NULL, '#size' => !empty($field['widget']['size']) ? $field['widget']['size'] : 60, '#autocomplete_path' => FALSE, // The following values were set by the content module and need @@ -868,7 +821,9 @@ * Theme function for 'default' text field formatter. */ function theme_phone_formatter_default($element) { - return $element['#item']['value']; + $field = content_fields($element['#field_name'], $element['#type_name']); + $formatted_number = format_phone_number_by_country($field, $element['#item']['value']); + return $formatted_number; } @@ -952,7 +907,125 @@ } /** - * Verification for Phone Numbers. + * Formats a phone number for output based on the country. + * + * @param $field + * The field array definition. + * @param $number + * The number to format. + * @return + * The formatted phone number. + */ +function format_phone_number_by_country($field, $number) { + switch ($field['type']) { + case 'fr_phone': + $formatted_number = format_phone_number('fr', $number, $field); + break; + case 'be_phone': + $formatted_number = format_phone_number('be', $number, $field); + break; + case 'it_phone': + $formatted_number = format_phone_number('it', $number, $field); + break; + case 'el_phone': + $formatted_number = format_phone_number('el', $number, $field); + break; + case 'ch_phone': + $formatted_number = format_phone_number('ch', $number, $field); + break; + case 'ca_phone': + $formatted_number = format_phone_number('ca', $number, $field); + break; + case 'cr_phone': + $formatted_number = format_phone_number('cr', $number, $field); + break; + case 'pa_phone': + $formatted_number = format_phone_number('pa', $number, $field); + break; + case 'uk_phone': + $formatted_number = format_phone_number('uk', $number, $field); + break; + case 'ru_phone': + $formatted_number = format_phone_number('ru', $number, $field); + break; + case 'ua_phone': + $formatted_number = format_phone_number('ua', $number, $field); + break; + case 'es_phone': + $formatted_number = format_phone_number('es', $number, $field); + break; + case 'au_phone': + $formatted_number = format_phone_number('au', $number, $field); + break; + case 'cs_phone': + $formatted_number = format_phone_number('cs', $number, $field); + break; + case 'hu_phone': + $formatted_number = format_phone_number('hu', $number, $field); + break; + case 'pl_phone': + $formatted_number = format_phone_number('hu', $number, $field); + break; + case 'nl_phone': + $formatted_number = format_phone_number('nl', $number, $field); + break; + case 'se_phone': + $formatted_number = format_phone_number('se', $number, $field); + break; + case 'za_phone': + $formatted_number = format_phone_number('za', $number, $field); + break; + case 'il_phone': + $formatted_number = format_phone_number('il', $number, $field); + break; + case 'nz_phone': + $formatted_number = format_phone_number('nz', $number, $field); + break; + case 'br_phone': + $formatted_number = format_phone_number('br', $number, $field); + break; + case 'cl_phone': + $formatted_number = format_phone_number('cl', $number, $field); + break; + case 'cn_phone': + $formatted_number = format_phone_number('cn', $number, $field); + break; + case 'hk_phone': + $formatted_number = format_phone_number('hk', $number, $field); + break; + case 'mo_phone': + $formatted_number = format_phone_number('mo', $number, $field); + break; + case 'ph_phone': + $formatted_number = format_phone_number('ph', $number, $field); + break; + case 'sg_phone': + $formatted_number = format_phone_number('sg', $number, $field); + break; + case 'jo_phone': + $formatted_number = format_phone_number('jo', $number, $field); + break; + case 'eg_phone': + $formatted_number = format_phone_number('eg', $number, $field); + break; + case 'pk_phone': + $formatted_number = format_phone_number('pk', $number, $field); + break; + case 'int_phone': + $formatted_number = format_phone_number('int', $number, $field); + break; + } + + if (isset($formatted_number)) { + return $formatted_number; + } + else { + return FALSE; + } +} + +/** + * Formats phone numbers for output. * * @param string $countrycode * @param string $phonenumber @@ -964,8 +1037,6 @@ $phonenumber = trim($phonenumber); if (phone_supported_countrycode($countrycode)) { - //drupal_set_message('langue = ' . $countrycode, 'error'); - $format_phone_function = 'format_'. $countrycode . '_phone_number'; module_load_include('inc', 'phone', 'phone.'. $countrycode); @@ -983,6 +1054,35 @@ } /** + * Removes all non-digits from a phone number. + * + * @param string $countrycode + * @param string $phonenumber + * @return boolean Returns boolean FALSE if the phone number is not valid. + */ +function clean_phone_number($countrycode, $phonenumber) { + + $countrycode = trim($countrycode); + $phonenumber = trim($phonenumber); + + if (phone_supported_countrycode($countrycode)) { + $clean_phone_function = 'clean_'. $countrycode . '_phone_number'; + module_load_include('inc', 'phone', 'phone.'. $countrycode); + + if (function_exists($clean_phone_function)) { + return $clean_phone_function($phonenumber); + } + else { + return FALSE; + } + } + else { + //Country not taken into account yet + return FALSE; + } +} + +/** * Implementation of hook token_list */ function phone_token_list($type = 'all') { @@ -1013,3 +1113,35 @@ $tests = file_scan_directory($dir, '\.test$'); return array_keys($tests); } + +/** + * Formats a phone number according to the supplied mask. + * + * @param $phonenumber + * The phone number to format. + * @param $mask + * The mask to use for formatting. Capital X's in the mask are replaced by + * the corresponding digit in the phone number. + * @return + * The formatted phone number. + */ +function phone_apply_mask($phonenumber, $mask) { + $pos = 0; + $formatted_number = ''; + for ($i = 0, $length = drupal_strlen($mask); $i < $length; $i++) { + // Placeholder, insert the next phone digit if it exists. + if ($mask[$i] == 'X' && isset($phonenumber[$pos])) { + $formatted_number .= $phonenumber[$pos]; + $pos++; + } + // Non-placeholder, output directly. + else { + $formatted_number .= $mask[$i]; + } + } + // Extra digits are a phone extension. + if (drupal_strlen($phonenumber) > $pos) { + $formatted_number .= ' x' . drupal_substr($phonenumber, $pos); + } + return $formatted_number; +}