PodsField_Phone

The Pods - Custom Content Types and Fields PodsField Phone class.

Defined (1)

The class is defined in the following location(s).

/classes/fields/phone.php  
  1. class PodsField_Phone extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Text'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'phone'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Phone'; 
  23.  
  24. /** 
  25. * Field Type Preparation 
  26. * @var string 
  27. * @since 2.0 
  28. */ 
  29. public static $prepare = '%s'; 
  30.  
  31. /** 
  32. * Do things like register/enqueue scripts and stylesheets 
  33. * @since 2.0 
  34. */ 
  35. public function __construct () { 
  36.  
  37.  
  38. /** 
  39. * Add options and set defaults to 
  40. * @param array $options 
  41. * @since 2.0 
  42. */ 
  43. public function options () { 
  44. $options = array( 
  45. self::$type . '_repeatable' => array( 
  46. 'label' => __( 'Repeatable Field', 'pods' ),  
  47. 'default' => 0,  
  48. 'type' => 'boolean',  
  49. 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),  
  50. 'boolean_yes_label' => '',  
  51. 'dependency' => true,  
  52. 'developer_mode' => true 
  53. ),  
  54. self::$type . '_format' => array( 
  55. 'label' => __( 'Format', 'pods' ),  
  56. 'default' => '999-999-9999 x999',  
  57. 'type' => 'pick',  
  58. 'data' => array( 
  59. __( 'US', 'pods' ) => array( 
  60. '999-999-9999 x999' => '123-456-7890 x123',  
  61. '(999) 999-9999 x999' => '(123) 456-7890 x123',  
  62. '999.999.9999 x999' => '123.456.7890 x123' 
  63. ),  
  64. __( 'International', 'pods' ) => array( 
  65. 'international' => __( 'Any (no validation available)', 'pods' ) 
  66. ),  
  67. self::$type . '_options' => array( 
  68. 'label' => __( 'Phone Options', 'pods' ),  
  69. 'group' => array( 
  70. self::$type . '_enable_phone_extension' => array( 
  71. 'label' => __( 'Enable Phone Extension?', 'pods' ),  
  72. 'default' => 1,  
  73. 'type' => 'boolean' 
  74. ),  
  75. self::$type . '_max_length' => array( 
  76. 'label' => __( 'Maximum Length', 'pods' ),  
  77. 'default' => 25,  
  78. 'type' => 'number',  
  79. 'help' => __( 'Set to -1 for no limit', 'pods' ) 
  80. ),  
  81. self::$type . '_html5' => array( 
  82. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),  
  83. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),  
  84. 'type' => 'boolean' 
  85. )/**,  
  86. self::$type . '_size' => array( 
  87. 'label' => __( 'Field Size', 'pods' ),  
  88. 'default' => 'medium',  
  89. 'type' => 'pick',  
  90. 'data' => array( 
  91. 'small' => __( 'Small', 'pods' ),  
  92. 'medium' => __( 'Medium', 'pods' ),  
  93. 'large' => __( 'Large', 'pods' ) 
  94. )*/ 
  95. ); 
  96. return $options; 
  97.  
  98. /** 
  99. * Define the current field's schema for DB table storage 
  100. * @param array $options 
  101. * @return array 
  102. * @since 2.0 
  103. */ 
  104. public function schema ( $options = null ) { 
  105. $length = (int) pods_var( self::$type . '_max_length', $options, 25, null, true ); 
  106.  
  107. $schema = 'VARCHAR(' . $length . ')'; 
  108.  
  109. if ( 255 < $length || $length < 1 ) 
  110. $schema = 'LONGTEXT'; 
  111.  
  112. return $schema; 
  113.  
  114. /** 
  115. * Customize output of the form field 
  116. * @param string $name 
  117. * @param mixed $value 
  118. * @param array $options 
  119. * @param array $pod 
  120. * @param int $id 
  121. * @since 2.0 
  122. */ 
  123. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  124. $options = (array) $options; 
  125. $form_field_type = PodsForm::$field_type; 
  126.  
  127. if ( is_array( $value ) ) 
  128. $value = implode( ' ', $value ); 
  129.  
  130. $field_type = 'phone'; 
  131.  
  132. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  133. if ( pods_var( 'read_only', $options, false ) ) { 
  134. $options[ 'readonly' ] = true; 
  135.  
  136. $field_type = 'text'; 
  137. else 
  138. return; 
  139. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  140. $options[ 'readonly' ] = true; 
  141.  
  142. $field_type = 'text'; 
  143.  
  144. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  145.  
  146. /** 
  147. * Validate a value before it's saved 
  148. * @param mixed $value 
  149. * @param string $name 
  150. * @param array $options 
  151. * @param array $fields 
  152. * @param array $pod 
  153. * @param int $id 
  154. * @since 2.0 
  155. */ 
  156. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  157. $errors = array(); 
  158.  
  159. $label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) ); 
  160.  
  161. $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); 
  162.  
  163. if ( is_array( $check ) ) 
  164. $errors = $check; 
  165. else { 
  166. if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) { 
  167. if ( 1 == pods_var( 'required', $options ) ) 
  168. $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label ); 
  169. else 
  170. $errors[] = sprintf( __( 'Invalid phone number provided for the field %s.', 'pods' ), $label ); 
  171.  
  172. if ( !empty( $errors ) ) 
  173. return $errors; 
  174.  
  175. return true; 
  176.  
  177. /** 
  178. * Change the value or perform actions after validation but before saving to the DB 
  179. * @param mixed $value 
  180. * @param int $id 
  181. * @param string $name 
  182. * @param array $options 
  183. * @param array $fields 
  184. * @param array $pod 
  185. * @param object $params 
  186. * @since 2.0 
  187. */ 
  188. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  189. $options = (array) $options; 
  190.  
  191. if ( 'international' == pods_var( self::$type . '_format', $options ) ) { 
  192. // no validation/changes 
  193. else { 
  194. // Clean input 
  195. $number = preg_replace( '/([^0-9ext])/', '', $value ); 
  196.  
  197. $number = str_replace( 
  198. array( '-', '.', 'ext', 'x', 't', 'e', '(', ')' ),  
  199. array( '', '', '|', '|', '', '', '', '', ),  
  200. $number 
  201. ); 
  202.  
  203. // Get extension 
  204. $extension = explode( '|', $number ); 
  205. if ( 1 < count( $extension ) ) { 
  206. $number = preg_replace( '/([^0-9])/', '', $extension[ 0 ] ); 
  207. $extension = preg_replace( '/([^0-9])/', '', $extension[ 1 ] ); 
  208. else 
  209. $extension = ''; 
  210.  
  211. // Build number array 
  212. $numbers = str_split( $number, 3 ); 
  213.  
  214. if ( isset( $numbers[ 3 ] ) ) { 
  215. $numbers[ 2 ] .= $numbers[ 3 ]; 
  216. $numbers = array( $numbers[ 0 ], $numbers[ 1 ], $numbers[ 2 ] ); 
  217. elseif ( isset( $numbers[ 1 ] ) ) 
  218. $numbers = array( $numbers[ 0 ], $numbers[ 1 ] ); 
  219.  
  220. // Format number 
  221. if ( '(999) 999-9999 x999' == pods_var( self::$type . '_format', $options ) ) { 
  222. if ( 2 == count( $numbers ) ) 
  223. $value = implode( '-', $numbers ); 
  224. else 
  225. $value = '(' . $numbers[ 0 ] . ') ' . $numbers[ 1 ] . '-' . $numbers[ 2 ]; 
  226. elseif ( '999.999.9999 x999' == pods_var( self::$type . '_format', $options ) ) 
  227. $value = implode( '.', $numbers ); 
  228. else //if ( '999-999-9999 x999' == pods_var( self::$type . '_format', $options ) ) 
  229. $value = implode( '-', $numbers ); 
  230.  
  231. // Add extension 
  232. if ( 1 == pods_var( self::$type . '_enable_phone_extension', $options ) && 0 < strlen( $extension ) ) 
  233. $value .= ' x' . $extension; 
  234.  
  235. $length = (int) pods_var( self::$type . '_max_length', $options, 25 ); 
  236.  
  237. if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { 
  238. $value = pods_mb_substr( $value, 0, $length ); 
  239.  
  240. return $value;