phone.gb.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for United Kingdom phone numbers.
  5. */
  6. function _uk_phone_rules() {
  7. // TODO: more detailed check by area codes
  8. return '/^
  9. 0*(2[03489])(\d{4})(\d{4}) # 02x [eight-digit local number]
  10. |
  11. 0*(11[3-8])(\d{3})(\d{4}) # 011x [seven-digit local number]
  12. |
  13. 0*(1[2-9]1)(\d{3})(\d{4}) # 01x1 [seven-digit local number]
  14. |
  15. 0*(1[2-9][0|2-9]\d)(\d{5,6}) # 01xxx [mostly six-digit local numbers] (but not 01x1 codes)
  16. $/x';
  17. }
  18. /**
  19. * Validate country level phone number.
  20. *
  21. * @param $number
  22. * Digits only value.
  23. * @param $ext
  24. * Digits only value.
  25. * @param $error
  26. * The error message to shown to user.
  27. * Available parameters to use in the error message are
  28. * - "%countrycode": the alpha-2 CC
  29. * - "%phone_input": the original number input by user (could be invalid)
  30. * - "%max_length": allowed maximum length of the phone number
  31. * @return boolean
  32. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  33. */
  34. function gb_validate_number($number, $ext = '', &$error) {
  35. // We don't want to worry about separators
  36. $number = cck_phone_clean_number($number);
  37. if (preg_match(_uk_phone_rules(), $number)) {
  38. return TRUE;
  39. }
  40. // t() is not needed
  41. $error = '"%phone_input" is not a valid United Kingdom phone number, it should be a 10 digit number like "99 9999 9999", with optional leading "0"';
  42. return FALSE;
  43. }
  44. /**
  45. * Cleanup user-entered values for a phone number field for saving to DB.
  46. *
  47. * @param $number
  48. * A single phone number item.
  49. */
  50. function gb_sanitize_number(&$number) {
  51. // Remove trunk prefix '0'
  52. $number = preg_replace('/^([0]*)/', '', $number);
  53. }
  54. /**
  55. * Default formatter for international phone number.
  56. *
  57. * @param $element
  58. * $element['#item']['country_codes']: alpha-2 country code
  59. * $element['#item']['number']: phone number
  60. * @param $error
  61. * The error message to shown to user.
  62. * Available parameters to use in the error message are
  63. * - "%countrycode": the alpha-2 CC
  64. * - "%phone_input": the original number input by user (could be invalid)
  65. * - "%max_length": allowed maximum length of the phone number
  66. * @return boolean
  67. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  68. */
  69. function gb_formatter_default($element) {
  70. // Display a global phone number with country code.
  71. $phone = '';
  72. $number = $element['number'];
  73. if ($number) {
  74. $cc = cck_phone_countrycodes($element['country_codes']);
  75. if (preg_match(_uk_phone_rules(), $number, $matches)) {
  76. // output as +44 AA BBBB CCCC, +44 AAA BBB CCCC or +44 AAAA BBB CCC
  77. array_shift($matches);
  78. $phone = $cc['code'] . ' ' . implode(' ', $matches);
  79. }
  80. else {
  81. $phone = "$cc[code] $number";
  82. }
  83. }
  84. return $phone;
  85. }
  86. /**
  87. * Local formatter for local phone number.
  88. *
  89. * @param $element
  90. * $element['#item']['country_codes']: alpha-2 country code
  91. * $element['#item']['number']: phone number
  92. * @param $error
  93. * The error message to shown to user.
  94. * Available parameters to use in the error message are
  95. * - "%countrycode": the alpha-2 CC
  96. * - "%phone_input": the original number input by user (could be invalid)
  97. * - "%max_length": allowed maximum length of the phone number
  98. * @return boolean
  99. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  100. */
  101. function gb_formatter_local($element) {
  102. // Display a local phone number without country code.
  103. $phone = '';
  104. $number = $element['number'];
  105. if ($number) {
  106. if (preg_match(_uk_phone_rules(), $number, $matches)) {
  107. // output as 0AA BBBB CCCC, 0AAA BBB CCCC or 0AAAA BBB CCC
  108. array_shift($matches);
  109. $phone = '0' . implode(' ', $matches);
  110. }
  111. else {
  112. $phone = "0$number";
  113. }
  114. }
  115. return $phone;
  116. }