phone.hu.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Hungarian phone numbers.
  5. */
  6. /**
  7. * Verifies and mage parts from $number
  8. *
  9. * @param $number
  10. * Digits only value.
  11. * @return boolean
  12. * Array
  13. * if array['error'] is present, the validation fails
  14. * else ['area'] for area number
  15. * ['first'] ['second'] for the AA/BBB-CCC numbers
  16. * ['first'] ['second'] ['third'] for the A(A)/BBB-CC-DD numbers
  17. */
  18. function hu_parts($number) {
  19. /**
  20. * Because Hungary an telephone numbers are both in open and closed number region, the area code length may be vary. If the area code is 1, 7 more digits follow it.
  21. * If the area code is 20/30/70, also 7 more digits.
  22. * If any other 2 digits are code is appear, we want 6 more digits.
  23. *
  24. * So complecated, I know :)
  25. *
  26. */
  27. $result = array();
  28. // Last 9 digits
  29. $n = drupal_substr($number, -9);
  30. if ($n === FALSE || (drupal_substr($n, 0, 1) != 2 && drupal_substr($n, 0, 1) != 3 && drupal_substr($n, 0, 1) != 7)) {
  31. // No 9 digit, check for 8 digit
  32. if (drupal_substr($number, -8) === FALSE) {
  33. // No 8 digit, too short for valid telephone number
  34. $result['error'] = '"%phone_input" is not a valid Hungarian phone number, it should be a 9 digit number like "20/123-45-67" or a 8 digit number like "23/123-456"';
  35. }
  36. else {
  37. $n = drupal_substr($number, -8);
  38. // It is 8 digits
  39. if (drupal_substr($n, 0, 1) == 1) {
  40. // A number from Budapest
  41. $result['area'] = drupal_substr($n, 0, 1);
  42. $result['first'] = drupal_substr($n, 1, 3);
  43. $result['second'] = drupal_substr($n, 4, 2);
  44. $result['third'] = drupal_substr($n, 6, 2);
  45. }
  46. else {
  47. // A number from countryside
  48. $result['area'] = drupal_substr($n, 0, 2);
  49. $result['first'] = drupal_substr($n, 2, 3);
  50. $result['second'] = drupal_substr($n, 5, 3);
  51. }
  52. }
  53. }
  54. else {
  55. // Cell phone number
  56. $result['area'] = drupal_substr($n, 0, 2);
  57. $result['first'] = drupal_substr($n, 2, 3);
  58. $result['second'] = drupal_substr($n, 5, 2);
  59. $result['third'] = drupal_substr($n, 7, 2);
  60. }
  61. return $result;
  62. }
  63. /**
  64. * Verifies that $number is a valid nine digit Hungarian phone number.
  65. *
  66. * @param $number
  67. * Digits only value.
  68. * @param $ext
  69. * Digits only value.
  70. * @param $error
  71. * The error message to shown to user.
  72. * Available parameters to use in the error message are
  73. * - "%countrycode": the alpha-2 CC
  74. * - "%phone_input": the original number input by user (could be invalid)
  75. * - "%max_length": allowed maximum length of the phone number
  76. * @return boolean
  77. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  78. */
  79. function hu_validate_number($number, $ext = '', &$error) {
  80. // Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
  81. // We don't want to worry about separators
  82. $number = cck_phone_clean_number($number);
  83. $result = hu_parts($number);
  84. if (isset($result['error'])) {
  85. // t() is no needed
  86. $error = $result['error'];
  87. return FALSE;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Cleanup user-entered values for a phone number field for saving to DB.
  93. *
  94. * @param $number
  95. * A single phone number item.
  96. */
  97. function hu_sanitize_number(&$number) {
  98. // Remove prefix '36' or '+36'
  99. $number = preg_replace('/^(?:\+?36|06*)/', '', $number);
  100. }
  101. /**
  102. * Default formatter for international phone number.
  103. *
  104. * @param $element
  105. * $element['country_codes']: alpha-2 country code
  106. * $element['number']: phone number
  107. * $element['extension']: ext
  108. * @return boolean
  109. * STRING The formatted number
  110. */
  111. function hu_formatter_default($element) {
  112. $number = $element['number'];
  113. $ext = $element['extension'];
  114. $result = hu_parts($number);
  115. if (isset($result['error'])) {
  116. return $number . ($ext ? '/' . $ext : '');
  117. }
  118. // output as +36 (AA) BBB CCDD/ext or +36 (AA) BBB CCC/ext
  119. $phone = '+36 (' . $result['area'] . ') ' . $result['first'] . ' ' . $result['second'] . $result['third'];
  120. return $phone . ($ext ? '/' . $ext : '');
  121. }
  122. /**
  123. * Local formatter for local phone number.
  124. *
  125. * @param $element
  126. * $element['country_codes']: alpha-2 country code
  127. * $element['number']: phone number
  128. * $element['extension']: ext
  129. * @return boolean
  130. * STRING The formatted number
  131. */
  132. function hu_formatter_local($element) {
  133. $number = $element['number'];
  134. $ext = $element['extension'];
  135. $result = hu_parts($number);
  136. if (isset($result['error'])) {
  137. return $number . ($ext ? '/' . $ext : '');
  138. }
  139. // output as AA/BBB-CC-DD or AA/BBB-CCC
  140. $phone = $result['area'] . '/' . $result['first'] . '-' . $result['second'] . (isset($result['third']) ? '-' . $result['third'] : '');
  141. return $phone . ($ext ? '/' . $ext : '');
  142. }