View source
<?php
function phone_countries($code = NULL) {
static $countries;
if (!isset($countries)) {
$countries = array(
'fr' => 'France',
'be' => 'Belgium',
'it' => 'Italy',
'el' => 'Greece',
'ch' => 'Switzerland',
'ca' => 'US & Canada',
'cr' => 'Costa Rica',
'pa' => 'Panama',
'gb' => 'Great Britain - United Kingdom',
'ru' => 'Russia',
'ua' => 'Ukraine',
'es' => 'Spain',
'au' => 'Australia',
'cs' => 'Czech Republic',
'hu' => 'Hungary',
'pl' => 'Poland - mobiles only',
'nl' => 'Netherland',
'se' => 'Sweden',
'za' => 'South Africa',
'il' => 'Israel',
'nz' => 'New Zealand',
'br' => 'Brazil',
'cl' => 'Chile',
'cn' => 'China',
'hk' => 'Hong-Kong',
'mo' => 'Macao',
'ph' => 'The Philippines',
'sg' => 'Singapore',
'sn' => 'Senegal',
'jo' => 'Jordan',
'eg' => 'Egypt',
'pk' => 'Pakistan',
'int' => 'International Phone Numbers per E.123',
);
}
return $code === NULL ? $countries : (isset($countries[$code]) ? $countries[$code] : NULL);
}
function phone_field_info() {
return array(
'phone' => array(
'label' => t('Phone Number'),
'instance_settings' => array(
'phone_country_code' => 0,
'phone_default_country_code' => '1',
'phone_int_max_length' => 15,
'ca_phone_separator' => '-',
'ca_phone_parentheses' => 1,
),
'default_formatter' => 'phone',
'default_widget' => 'phone_textfield',
'property_type' => 'text',
),
);
}
function phone_field_is_empty($item, $field) {
return empty($item['value']);
}
function phone_field_settings_form($field, $instance, $has_data) {
$settings = $field['settings'];
$form = array();
$form['country'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#options' => phone_countries(),
'#default_value' => isset($settings['country']) ? $settings['country'] : NULL,
'#description' => t('Which country-specific rules should this field be validated against and formatted according to.'),
'#disabled' => $has_data,
'#required' => TRUE,
);
return $form;
}
function phone_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form['phone_country_code'] = array(
'#type' => 'checkbox',
'#title' => t('Add the country code if not filled by the user'),
'#default_value' => $settings['phone_country_code'],
);
if ($field['settings']['country'] == 'int') {
$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 <a href="http://www.itu.int/rec/T-REC-E.123/en">E.123 specification</a>.'),
);
$form['phone_default_country_code'] = array(
'#type' => 'textfield',
'#title' => t('Default country code to add to international numbers without one (omit + sign)'),
'#default_value' => $settings['phone_default_country_code'],
);
$form['phone_int_max_length'] = array(
'#type' => 'textfield',
'#title' => t('Maximum length of international numbers, according to the ITU this is 15'),
'#default_value' => $settings['phone_int_max_length'],
);
}
if ($field['settings']['country'] == 'ca') {
$form['ca_phone_separator'] = array(
'#type' => 'textfield',
'#title' => t('Separator'),
'#default_value' => $settings['ca_phone_separator'],
'#size' => 2,
);
$form['ca_phone_parentheses'] = array(
'#type' => 'checkbox',
'#title' => t('Use parentheses around area code'),
'#default_value' => $settings['ca_phone_parentheses'],
);
}
return $form;
}
function phone_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (isset($item['value']) && $item['value'] != '') {
$ccode = $field['settings']['country'];
$value = $item['value'];
if (!valid_phone_number($ccode, $value)) {
$country = phone_country_info($ccode);
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'phone_invalid_number',
'message' => t($country['error'], array(
'%value' => $value,
)),
);
}
}
}
}
function phone_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
$ccode = $field['settings']['country'];
if (phone_countries($ccode) !== NULL) {
foreach ($items as $delta => $item) {
if (isset($item['value'])) {
$items[$delta]['value'] = format_phone_number($ccode, $item['value'], $instance['settings']);
}
}
}
}
function phone_field_formatter_info() {
return array(
'phone' => array(
'label' => t('Default'),
'field types' => array(
'phone',
),
),
);
}
function phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$text = '';
if (isset($item['value'])) {
$text = check_plain($item['value']);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) {
$text = '<a href="tel:' . $text . '">' . $text . '</a>';
}
}
$element[$delta]['#markup'] = $text;
}
return $element;
}
function phone_field_widget_info() {
return array(
'phone_textfield' => array(
'label' => t('Text field'),
'field types' => array(
'phone',
),
),
);
}
function phone_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => 'textfield',
'#title' => $element['#title'],
'#description' => $element['#description'],
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
'#required' => $element['#required'],
'#size' => 17,
'#maxlength' => $field['settings']['country'] == 'int' ? isset($instance['settings']['phone_int_max_length']) ? $instance['settings']['phone_int_max_length'] : NULL : NULL,
);
return array(
'value' => $element,
);
}
function phone_content_migrate_field_alter(&$field_value, $instance_value) {
module_load_include('inc', 'phone', 'phone.migrate');
phone_field_alter($field_value, $instance_value);
}
function phone_token_list($type = 'all') {
if ($type == 'field' || $type == 'all') {
$tokens['phone']['raw'] = t('Raw phone numbers');
$tokens['phone']['formatted'] = t('Formatted phone numbers');
return $tokens;
}
}
function phone_token_values($type, $object = NULL, $options = array()) {
if ($type == 'field') {
$item = $object[0];
$tokens['raw'] = $item['value'];
$tokens['formatted'] = $item['view'];
return $tokens;
}
}
function phone_simpletest() {
$dir = drupal_get_path('module', 'phone') . '/tests';
$tests = file_scan_directory($dir, '\\.test$');
return array_keys($tests);
}
function phone_supported_countrycode($countrycode) {
return phone_country_info($countrycode) !== NULL ? TRUE : FALSE;
}
function phone_country_info($countrycode = NULL) {
static $i;
$countrycode = trim($countrycode);
if (phone_countries($countrycode) !== FALSE) {
$phone_info_function = 'phone_' . $countrycode . '_metadata';
module_load_include('inc', 'phone', 'include/phone.' . $countrycode);
if (function_exists($phone_info_function)) {
return $phone_info_function();
}
}
return FALSE;
}
function valid_phone_number($countrycode, $phonenumber) {
$countrycode = trim($countrycode);
$phonenumber = trim($phonenumber);
if (phone_supported_countrycode($countrycode)) {
$valid_phone_function = 'valid_' . $countrycode . '_phone_number';
module_load_include('inc', 'phone', 'include/phone.' . $countrycode);
if (function_exists($valid_phone_function)) {
return $valid_phone_function($phonenumber);
}
}
return FALSE;
}
function format_phone_number($countrycode, $phonenumber, $field) {
$countrycode = trim($countrycode);
$phonenumber = trim($phonenumber);
if (phone_supported_countrycode($countrycode)) {
$format_phone_function = 'format_' . $countrycode . '_phone_number';
module_load_include('inc', 'phone', 'include/phone.' . $countrycode);
if (function_exists($format_phone_function)) {
return $format_phone_function($phonenumber, $field);
}
}
return FALSE;
}