phone.module 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Function which holds an array of supported countries.
  4. *
  5. * @param string $countrycode
  6. * @return boolean Returns the whole array of countries $countrycode isn't specified and a country name for when it is specified.
  7. */
  8. function phone_countries($code = NULL) {
  9. static $countries;
  10. if (!isset($countries)) {
  11. $countries = array(
  12. 'fr' => 'France',
  13. 'be' => 'Belgium',
  14. 'it' => 'Italy',
  15. 'el' => 'Greece',
  16. 'ch' => 'Switzerland',
  17. 'ca' => 'US & Canada',
  18. 'cr' => 'Costa Rica',
  19. 'pa' => 'Panama',
  20. 'gb' => 'Great Britain - United Kingdom',
  21. 'ru' => 'Russia',
  22. 'ua' => 'Ukraine',
  23. 'es' => 'Spain',
  24. 'au' => 'Australia',
  25. 'cs' => 'Czech Republic',
  26. 'hu' => 'Hungary',
  27. 'pl' => 'Poland - mobiles only',
  28. 'nl' => 'Netherland',
  29. 'se' => 'Sweden',
  30. 'za' => 'South Africa',
  31. 'il' => 'Israel',
  32. 'nz' => 'New Zealand',
  33. 'br' => 'Brazil',
  34. 'cl' => 'Chile',
  35. 'cn' => 'China',
  36. 'hk' => 'Hong-Kong',
  37. 'mo' => 'Macao',
  38. 'ph' => 'The Philippines',
  39. 'sg' => 'Singapore',
  40. 'sn' => 'Senegal',
  41. 'jo' => 'Jordan',
  42. 'eg' => 'Egypt',
  43. 'pk' => 'Pakistan',
  44. 'int' => 'International Phone Numbers per E.123',
  45. );
  46. }
  47. return ($code === NULL) ? $countries : (isset($countries[$code]) ? $countries[$code] : NULL);
  48. }
  49. /**
  50. * @defgroup field_api_hooks Field API Hook Implementations
  51. */
  52. /**
  53. * Implementation of hook_field_info().
  54. */
  55. function phone_field_info() {
  56. return array(
  57. 'phone' => array(
  58. 'label' => t('Phone Number'),
  59. 'instance_settings' => array(
  60. 'phone_country_code' => 0,
  61. 'phone_default_country_code' => '1',
  62. 'phone_int_max_length' => 15,
  63. 'ca_phone_separator' => '-',
  64. 'ca_phone_parentheses' => 1,
  65. ),
  66. 'default_formatter' => 'phone',
  67. 'default_widget' => 'phone_textfield',
  68. 'property_type' => 'text',
  69. ),
  70. );
  71. }
  72. /**
  73. * Implements hook_field_is_empty().
  74. */
  75. function phone_field_is_empty($item, $field) {
  76. return empty($item['value']);
  77. }
  78. /**
  79. * Implements hook_field_settings_form().
  80. */
  81. function phone_field_settings_form($field, $instance, $has_data) {
  82. $settings = $field['settings'];
  83. $form = array();
  84. $form['country'] = array(
  85. '#type' => 'select',
  86. '#title' => t('Country'),
  87. '#options' => phone_countries(),
  88. '#default_value' => isset ($settings['country']) ? $settings['country'] : NULL,
  89. '#description' => t('Which country-specific rules should this field be validated against and formatted according to.'),
  90. '#disabled' => $has_data,
  91. '#required' => TRUE,
  92. );
  93. return $form;
  94. }
  95. /**
  96. * Implements hook_field_instance_settings_form().
  97. */
  98. function phone_field_instance_settings_form($field, $instance) {
  99. $settings = $instance['settings'];
  100. $form['phone_country_code'] = array(
  101. '#type' => 'checkbox',
  102. '#title' => t('Add the country code if not filled by the user'),
  103. '#default_value' => $settings['phone_country_code'],
  104. );
  105. if ($field['settings']['country'] == 'int') {
  106. $form['phone_int_help'] = array(
  107. '#type' => 'markup',
  108. '#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>.'),
  109. );
  110. $form['phone_default_country_code'] = array(
  111. '#type' => 'textfield',
  112. '#title' => t('Default country code to add to international numbers without one (omit + sign)'),
  113. '#default_value' => $settings['phone_default_country_code'],
  114. );
  115. $form['phone_int_max_length'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Maximum length of international numbers, according to the ITU this is 15'),
  118. '#default_value' => $settings['phone_int_max_length'],
  119. );
  120. }
  121. if ($field['settings']['country'] == 'ca') {
  122. $form['ca_phone_separator'] = array(
  123. '#type' => 'textfield',
  124. '#title' => t('Separator'),
  125. '#default_value' => $settings['ca_phone_separator'],
  126. '#size' => 2,
  127. );
  128. $form['ca_phone_parentheses'] = array(
  129. '#type' => 'checkbox',
  130. '#title' => t('Use parentheses around area code'),
  131. '#default_value' => $settings['ca_phone_parentheses'],
  132. );
  133. }
  134. return $form;
  135. }
  136. /**
  137. * Implements hook_field_validate().
  138. */
  139. function phone_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  140. foreach ($items as $delta => $item) {
  141. if (isset($item['value']) && $item['value'] != '') {
  142. $ccode = $field['settings']['country'];
  143. $value = $item['value'];
  144. if (!valid_phone_number($ccode, $value)) {
  145. $country = phone_country_info($ccode);
  146. $errors[$field['field_name']][$langcode][$delta][] = array(
  147. 'error' => 'phone_invalid_number',
  148. 'message' => t($country['error'], array('%value' => $value)),
  149. );
  150. }
  151. }
  152. }
  153. }
  154. /**
  155. * Implements hook_field_presave().
  156. */
  157. function phone_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  158. $ccode = $field['settings']['country'];
  159. if (phone_countries($ccode) !== NULL) {
  160. foreach ($items as $delta => $item) {
  161. if (isset($item['value'])) {
  162. $items[$delta]['value'] = format_phone_number($ccode, $item['value'], $instance['settings']);
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * Implements hook_field_formatter_info().
  169. */
  170. function phone_field_formatter_info() {
  171. return array(
  172. 'phone' => array(
  173. 'label' => t('Default'),
  174. 'field types' => array('phone'),
  175. )
  176. );
  177. }
  178. /**
  179. * Implements hook_field_formatter_view().
  180. */
  181. function phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  182. $element = array();
  183. foreach ($items as $delta => $item) {
  184. $text = '';
  185. if (isset($item['value'])) {
  186. $text = check_plain($item['value']);
  187. // iPhone Support
  188. if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) {
  189. $text = '<a href="tel:' . $text . '">' . $text . '</a>';
  190. }
  191. }
  192. $element[$delta]['#markup'] = $text;
  193. }
  194. return $element;
  195. }
  196. /**
  197. * Implements hook_field_widget_info().
  198. */
  199. function phone_field_widget_info() {
  200. return array(
  201. 'phone_textfield' => array(
  202. 'label' => t('Text field'),
  203. 'field types' => array('phone'),
  204. ),
  205. );
  206. }
  207. /**
  208. * Implements hook_field_widget_form().
  209. */
  210. function phone_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  211. $element += array(
  212. '#type' => 'textfield',
  213. '#title' => $element['#title'],
  214. '#description' => $element['#description'],
  215. '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
  216. '#required' => $element['#required'],
  217. '#size' => 17,
  218. '#maxlength' => (
  219. $field['settings']['country'] == 'int' ?
  220. (isset($instance['settings']['phone_int_max_length']) ? $instance['settings']['phone_int_max_length'] : NULL)
  221. : NULL
  222. ),
  223. );
  224. return array('value' => $element);
  225. }
  226. /**
  227. * @} End of "defgroup field_api_hooks".
  228. */
  229. /**
  230. * @defgroup other_hooks Other Hook Implementations
  231. */
  232. /**
  233. * Implements hook_content_migrate_field_alter().
  234. *
  235. * Use this to tweak the conversion of field settings
  236. * from the D6 style to the D7 style for specific
  237. * situations not handled by basic conversion,
  238. * as when field types or settings are changed.
  239. */
  240. function phone_content_migrate_field_alter(&$field_value, $instance_value) {
  241. module_load_include('inc', 'phone', 'phone.migrate');
  242. phone_field_alter($field_value, $instance_value);
  243. }
  244. /**
  245. * Implementation of hook token_list
  246. */
  247. function phone_token_list($type = 'all') {
  248. if ($type == 'field' || $type == 'all') {
  249. $tokens['phone']['raw'] = t('Raw phone numbers');
  250. $tokens['phone']['formatted'] = t('Formatted phone numbers');
  251. return $tokens;
  252. }
  253. }
  254. /**
  255. * Implementation of hook token_values
  256. */
  257. function phone_token_values($type, $object = NULL, $options = array()) {
  258. if ($type == 'field') {
  259. $item = $object[0];
  260. $tokens['raw'] = $item['value'];
  261. $tokens['formatted'] = $item['view'];
  262. return $tokens;
  263. }
  264. }
  265. /**
  266. * Implementation of hook_simpletest().
  267. */
  268. function phone_simpletest() {
  269. $dir = drupal_get_path('module', 'phone'). '/tests';
  270. $tests = file_scan_directory($dir, '\.test$');
  271. return array_keys($tests);
  272. }
  273. /**
  274. * @} End of "defgroup field_api_hooks".
  275. */
  276. /**
  277. * Country supported or not by the module ?
  278. *
  279. * @param string $countrycode
  280. * @return boolean Returns a boolean containting the answer to the question.
  281. */
  282. function phone_supported_countrycode($countrycode) {
  283. return (phone_country_info($countrycode) !== NULL ? TRUE : FALSE);
  284. }
  285. /**
  286. * Get a country meta info
  287. *
  288. * @param string $countrycode
  289. * @return array Returns a array containing country metadata
  290. */
  291. function phone_country_info($countrycode = NULL) {
  292. static $i;
  293. $countrycode = trim($countrycode);
  294. if (phone_countries($countrycode) !== FALSE) {
  295. $phone_info_function = 'phone_'. $countrycode . '_metadata';
  296. module_load_include('inc', 'phone', 'include/phone.'. $countrycode);
  297. if (function_exists($phone_info_function)) {
  298. return $phone_info_function();
  299. }
  300. }
  301. //Country not taken into account yet
  302. return FALSE;
  303. }
  304. /**
  305. * Verification for Phone Numbers.
  306. *
  307. * @param string $countrycode
  308. * @param string $phonenumber
  309. * @return boolean Returns boolean FALSE if the phone number is not valid.
  310. */
  311. function valid_phone_number($countrycode, $phonenumber) {
  312. $countrycode = trim($countrycode);
  313. $phonenumber = trim($phonenumber);
  314. if (phone_supported_countrycode($countrycode)) {
  315. $valid_phone_function = 'valid_'. $countrycode . '_phone_number';
  316. module_load_include('inc', 'phone', 'include/phone.'. $countrycode);
  317. if (function_exists($valid_phone_function)) {
  318. return $valid_phone_function($phonenumber);
  319. }
  320. }
  321. //Country not taken into account yet
  322. return FALSE;
  323. }
  324. /**
  325. * Formatting for Phone Numbers.
  326. *
  327. * @param string $countrycode
  328. * @param string $phonenumber
  329. * @return boolean Returns boolean FALSE if the phone number is not valid.
  330. */
  331. function format_phone_number($countrycode, $phonenumber, $field) {
  332. $countrycode = trim($countrycode);
  333. $phonenumber = trim($phonenumber);
  334. if (phone_supported_countrycode($countrycode)) {
  335. $format_phone_function = 'format_'. $countrycode . '_phone_number';
  336. module_load_include('inc', 'phone', 'include/phone.'. $countrycode);
  337. if (function_exists($format_phone_function)) {
  338. return $format_phone_function($phonenumber, $field);
  339. }
  340. }
  341. //Country not taken into account yet
  342. return FALSE;
  343. }