phone.my.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Malaysia phone numbers.
  5. */
  6. function _my_phone_rules() {
  7. $rules = array();
  8. // rule: 'area code, min length, max length'
  9. // Geographic land line number
  10. $rules[] = array("2", 7); // Domestic access code to Singapore
  11. $rules[] = array("3", 8); // Selangor & Federal Territories of Kuala Lumpur & Putrajaya
  12. $rules[] = array("4", 7); // Kedah, Penang & Perlis
  13. $rules[] = array("5", 7); // Perak & Cameron Highlands (Pahang)
  14. $rules[] = array("6", 7); // Melaka, Negeri Sembilan & Muar (Johor)
  15. $rules[] = array("7", 7); // Johor (except Muar)
  16. $rules[] = array("80", 6); // Domestic access code to Brunei (East Malaysia only)
  17. // $rules[] = array("81", 6); // reserved
  18. $rules[] = array("82", 6); // Kuching (Sarawak)
  19. $rules[] = array("83", 6); // Sri Aman (Sarawak)
  20. $rules[] = array("84", 6); // Sarikei, Bintangor, Sibu, Kanowit, Song, & Kapit (Sarawak)
  21. $rules[] = array("85", 6); // Lawas, Limbang, Miri (Sarawak)
  22. $rules[] = array("86", 6); // Bintulu, Belaga (Sarawak)
  23. $rules[] = array("87", 6); // Inner District (Sabah) & Federal Territory of Labuan
  24. $rules[] = array("88", 6); // Kota Kinabalu, Kudat (Sabah)
  25. $rules[] = array("89", 6); // Lahad Datu, Sandakan, Tawau (Sabah)
  26. $rules[] = array("9", 7); // Kelantan, Pahang (except Cameron Highlands) & Terengganu
  27. // Mobile number structure
  28. $rules[] = array("10", 7);
  29. $rules[] = array("11", 7);
  30. $rules[] = array("12", 7);
  31. $rules[] = array("13", 7);
  32. $rules[] = array("14", 7);
  33. $rules[] = array("15", 7);
  34. $rules[] = array("16", 7);
  35. $rules[] = array("17", 7);
  36. $rules[] = array("18", 7);
  37. $rules[] = array("19", 7);
  38. return $rules;
  39. }
  40. /**
  41. * Verifies that $number is a valid Malaysia phone number.
  42. *
  43. * @param $number
  44. * Digits only value.
  45. * @param $ext
  46. * Digits only value.
  47. * @param $error
  48. * The error message to shown to user.
  49. * Available parameters to use in the error message are
  50. * - "%countrycode": the alpha-2 CC
  51. * - "%phone_input": the original number input by user (could be invalid)
  52. * - "%max_length": allowed maximum length of the phone number
  53. * @return boolean
  54. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  55. */
  56. function my_validate_number($number, $ext = '', &$error) {
  57. // We don't want to worry about separators
  58. $number = cck_phone_clean_number($number);
  59. foreach (_my_phone_rules() as $rule) {
  60. // define regular expression
  61. $regex = '/^
  62. ([0]*) # an optional 0
  63. (' . $rule[0] . ') # area code
  64. \d{' . $rule[1] . '} # local number within length $rule[1] & $rule[2]
  65. $/x';
  66. $result = preg_match($regex, $number, $matches);
  67. if ($result) {
  68. return TRUE;
  69. }
  70. }
  71. // t() is no needed
  72. $error = '"%phone_input" is not a valid Malaysia phone number, it should be a 9-10 digit number like "03-2222 2222", "0" is optional and will be removed.';
  73. return FALSE;
  74. }
  75. /**
  76. * Cleanup user-entered values for a phone number field for saving to DB.
  77. *
  78. * @param $number
  79. * A single phone number item.
  80. */
  81. function my_sanitize_number(&$number) {
  82. // Remove trunk prefix '0'
  83. $number = preg_replace('/^([0]*)/', '', $number);
  84. }
  85. /**
  86. * Default formatter for international phone number.
  87. *
  88. * @param $element
  89. * $element['#item']['country_codes']: alpha-2 country code
  90. * $element['#item']['number']: phone number
  91. * @param $error
  92. * The error message to shown to user.
  93. * Available parameters to use in the error message are
  94. * - "%countrycode": the alpha-2 CC
  95. * - "%phone_input": the original number input by user (could be invalid)
  96. * - "%max_length": allowed maximum length of the phone number
  97. * @return boolean
  98. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  99. */
  100. function my_formatter_default($element) {
  101. $phone = '';
  102. // Display a global phone number with country code.
  103. $cc = cck_phone_countrycodes($element['country_codes']);
  104. // Format the phone number however you like, this is the default
  105. foreach (_my_phone_rules() as $rule) {
  106. // define regular expression
  107. $regex = '/^
  108. (' . $rule[0] . ') # area code
  109. (\d{3,4})
  110. (\d{4})
  111. $/x';
  112. $result = preg_match($regex, $element['number'], $matches);
  113. if ($result) {
  114. // output as +60A-BBB CCCC or +60A-BBBB CCCC
  115. $phone = $cc['code'] . $matches[1] . '-' . $matches[2] . ' ' . $matches[3];
  116. continue;
  117. }
  118. }
  119. return $phone;
  120. }
  121. /**
  122. * Local formatter for local phone number.
  123. *
  124. * @param $element
  125. * $element['#item']['country_codes']: alpha-2 country code
  126. * $element['#item']['number']: phone number
  127. * @param $error
  128. * The error message to shown to user.
  129. * Available parameters to use in the error message are
  130. * - "%countrycode": the alpha-2 CC
  131. * - "%phone_input": the original number input by user (could be invalid)
  132. * - "%max_length": allowed maximum length of the phone number
  133. * @return boolean
  134. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  135. */
  136. function my_formatter_local($element) {
  137. // Display a local phone number without country code.
  138. $phone = $element['number'];
  139. foreach (_my_phone_rules() as $rule) {
  140. // define regular expression
  141. $regex = '/^
  142. (' . $rule[0] . ') # area code
  143. (\d{3,4})
  144. (\d{4})
  145. $/x';
  146. $result = preg_match($regex, $phone, $matches);
  147. if ($result) {
  148. // output as 0A-BBB CCCC or 0A-BBBB CCCC
  149. $phone = '0' . $matches[1] . '-' . $matches[2] . ' ' . $matches[3];
  150. continue;
  151. }
  152. }
  153. return $phone;
  154. }