API.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * Phone Number custom country API
  5. *
  6. * 'CC' will be used throughout this document to indicate country code
  7. * abbreviation. You should replace it with the correct country code
  8. * in the following functions name. For full list of country code
  9. * abbreviation, refer to the 2 alphabet list in the countries.txt.
  10. */
  11. /**
  12. * Country field settings.
  13. * Country can provide additional field settings for the phone number
  14. * field as needed.
  15. *
  16. * @param $op
  17. * The operation to be performed. Possible values:
  18. * - "form": Display the field settings form.
  19. * - "validate": Check the field settings form for errors.
  20. * - "save": Declare which fields to save back to the database.
  21. * - "database columns": Declare the columns that content.module should create
  22. * and manage on behalf of the field. If the field module wishes to handle
  23. * its own database storage, this should be omitted.
  24. * - "filters": Declare the Views filters available for the field.
  25. * (this is used in CCK's default Views tables definition)
  26. * They always apply to the first column listed in the "database columns"
  27. * array.
  28. * @param $field
  29. * The field on which the operation is to be performed.
  30. * @return
  31. * This varies depending on the operation.
  32. * - "form": an array of form elements to add to
  33. * the settings page.
  34. * - "validate": no return value. Use form_set_error().
  35. * - "save": an array of names of form elements to
  36. * be saved in the database.
  37. * - "database columns": an array keyed by column name, with arrays of column
  38. * information as values. This column information must include "type", the
  39. * MySQL data type of the column, and may also include a "sortable" parameter
  40. * to indicate to views.module that the column contains ordered information.
  41. * TODO: Details of other information that can be passed to the database layer can
  42. * be found in the API for the Schema API.
  43. * - "filters": an array of 'filters' definitions as expected by views.module
  44. * (see Views Documentation).
  45. * When providing several filters, it is recommended to use the 'name'
  46. * attribute in order to let the user distinguish between them. If no 'name'
  47. * is specified for a filter, the key of the filter will be used instead.
  48. */
  49. function CC_phone_field_settings($op, $field) {
  50. switch ($op) {
  51. // Country specific field settings
  52. case 'form':
  53. // ...
  54. return $form;
  55. // Country specific field validation
  56. case 'validate':
  57. // ...
  58. break;
  59. // Country specific field save
  60. case 'save':
  61. // ...
  62. return $settings;
  63. }
  64. }
  65. /**
  66. * Validate country level phone number.
  67. *
  68. * @param $number
  69. * Digits only value.
  70. * @param $ext
  71. * Digits only value.
  72. * @param $error
  73. * The error message to shown to user.
  74. * Available parameters to use in the error message are
  75. * - "%countrycode": the alpha-2 CC (check_plain'ed)
  76. * - "%phone_input": the original number input by user (check_plain'ed)
  77. * - "%min_length": allowed minimum length of the phone number
  78. * - "%max_length": allowed maximum length of the phone number
  79. * - "%ext_input": the original extension input by user (check_plain'ed)
  80. * - "%ext_max_length": allowed maximum length of the phone extension
  81. * @return boolean
  82. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  83. */
  84. function CC_validate_number($number, $ext = '', &$error) {
  85. // Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
  86. // your validation
  87. if (FALSE) {
  88. // t() is no needed
  89. $error = '"%phone_input" is not a valid North American phone number, it should be a 10 digit number like "999 999 9999"';
  90. return FALSE;
  91. }
  92. return TRUE;
  93. }
  94. /**
  95. * Cleanup user-entered values for a phone number field for saving to DB.
  96. *
  97. * @param $number
  98. * A single phone number item.
  99. */
  100. function CC_sanitize_number(&$number) {
  101. // your cleanup like removing trunk prefix
  102. }
  103. /**
  104. * Default formatter for international phone number.
  105. *
  106. * @param $element
  107. * $element['#item']['country_codes']: alpha-2 country code
  108. * $element['#item']['number']: phone number
  109. * @param $error
  110. * The error message to shown to user.
  111. * Available parameters to use in the error message are
  112. * - "%countrycode": the alpha-2 CC (check_plain'ed)
  113. * - "%phone_input": the original number input by user (check_plain'ed)
  114. * - "%min_length": allowed minimum length of the phone number
  115. * - "%max_length": allowed maximum length of the phone number
  116. * - "%ext_input": the original extension input by user (check_plain'ed)
  117. * - "%ext_max_length": allowed maximum length of the phone extension
  118. * @return boolean
  119. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  120. */
  121. function CC_formatter_default($element) {
  122. $item = $element['#item'];
  123. // Display a global phone number with country code.
  124. $cc = cck_phone_countrycodes($item['country_codes']);
  125. // Format the phone number however you like, this is the default
  126. $phone = $cc['code'] . '-' . $item['number'];
  127. return $phone;
  128. }
  129. /**
  130. * Local formatter for local phone number.
  131. *
  132. * @param $element
  133. * $element['#item']['country_codes']: alpha-2 country code
  134. * $element['#item']['number']: phone number
  135. * @param $error
  136. * The error message to shown to user.
  137. * Available parameters to use in the error message are
  138. * - "%countrycode": the alpha-2 CC
  139. * - "%phone_input": the original number input by user (could be invalid)
  140. * - "%max_length": allowed maximum length of the phone number
  141. * @return boolean
  142. * TRUE if it is a valid phone number for that country, FALSE otherwise.
  143. */
  144. function CC_formatter_local($element) {
  145. // Display a local phone number without country code.
  146. // Format the phone number however you like, this is the default
  147. $phone = $element['#item']['number'];
  148. return $phone;
  149. }