PodsField_Boolean

Handles boolean field type data and operations.

Defined (1)

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

/classes/fields/boolean.php  
  1. class PodsField_Boolean extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Identifier 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $type = 'boolean'; 
  9.  
  10. /** 
  11. * Field Type Label 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $label = 'Yes / No'; 
  16.  
  17. /** 
  18. * Field Type Preparation 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $prepare = '%s'; 
  23.  
  24. /** 
  25. * Do things like register/enqueue scripts and stylesheets 
  26. * @since 2.0 
  27. */ 
  28. public function __construct () { 
  29.  
  30.  
  31. /** 
  32. * Add options and set defaults to 
  33. * @return array Array of available options 
  34. * @since 2.0 
  35. */ 
  36. public function options () { 
  37. $options = array( 
  38. self::$type . '_format_type' => array( 
  39. 'label' => __( 'Input Type', 'pods' ),  
  40. 'default' => 'checkbox',  
  41. 'type' => 'pick',  
  42. 'data' => array( 
  43. 'checkbox' => __( 'Checkbox', 'pods' ),  
  44. 'radio' => __( 'Radio Buttons', 'pods' ),  
  45. 'dropdown' => __( 'Drop Down', 'pods' ) 
  46. ),  
  47. 'dependency' => true 
  48. ),  
  49. self::$type . '_yes_label' => array( 
  50. 'label' => __( 'Yes Label', 'pods' ),  
  51. 'default' => __( 'Yes', 'pods' ),  
  52. 'type' => 'text' 
  53. ),  
  54. self::$type . '_no_label' => array( 
  55. 'label' => __( 'No Label', 'pods' ),  
  56. 'default' => __( 'No', 'pods' ),  
  57. 'type' => 'text' 
  58. ); 
  59. return $options; 
  60.  
  61. /** 
  62. * Define the current field's schema for DB table storage 
  63. * @param array $options 
  64. * @return array 
  65. * @since 2.0 
  66. */ 
  67. public function schema ( $options = null ) { 
  68. $schema = 'BOOL DEFAULT 0'; 
  69.  
  70. return $schema; 
  71.  
  72. /** 
  73. * Change the way the value of the field is displayed with Pods::get 
  74. * @param mixed $value 
  75. * @param string $name 
  76. * @param array $options 
  77. * @param array $pod 
  78. * @param int $id 
  79. * @return mixed|null 
  80. * @since 2.0 
  81. */ 
  82. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  83. $yesno = array( 
  84. 1 => pods_var_raw( self::$type . '_yes_label', $options ),  
  85. 0 => pods_var_raw( self::$type . '_no_label', $options ) 
  86. ); 
  87.  
  88. // Deprecated handling for 1.x 
  89. if ( !parent::$deprecated && isset( $yesno[ (int) $value ] ) ) 
  90. $value = $yesno[ (int) $value ]; 
  91.  
  92. return $value; 
  93.  
  94. /** 
  95. * Customize output of the form field 
  96. * @param string $name 
  97. * @param mixed $value 
  98. * @param array $options 
  99. * @param array $pod 
  100. * @param int $id 
  101. * @since 2.0 
  102. */ 
  103. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  104. $options = (array) $options; 
  105. $form_field_type = PodsForm::$field_type; 
  106.  
  107. if ( is_array( $value ) ) 
  108. $value = !empty( $value ); 
  109.  
  110. $field_type = 'checkbox'; 
  111.  
  112. if ( 'radio' == pods_v( self::$type . '_format_type', $options ) ) 
  113. $field_type = 'radio'; 
  114. elseif ( 'dropdown' == pods_v( self::$type . '_format_type', $options ) ) 
  115. $field_type = 'select'; 
  116.  
  117. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  118. if ( pods_v( 'read_only', $options, false ) ) 
  119. $options[ 'readonly' ] = true; 
  120. else 
  121. return; 
  122. elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) 
  123. $options[ 'readonly' ] = true; 
  124.  
  125. if ( 1 === $value || '1' === $value || true === $value ) { 
  126. $value = 1; 
  127. else { 
  128. $value = 0; 
  129.  
  130. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  131.  
  132. /** 
  133. * Get the data from the field 
  134. * @param string $name The name of the field 
  135. * @param string|array $value The value of the field 
  136. * @param array $options 
  137. * @param array $pod 
  138. * @param int $id 
  139. * @param boolean $in_form 
  140. * @return array Array of possible field data 
  141. * @since 2.0 
  142. */ 
  143. public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { 
  144. if ( 'checkbox' != pods_v( self::$type . '_format_type', $options ) ) { 
  145. $data = array( 
  146. 1 => pods_var_raw( self::$type . '_yes_label', $options ),  
  147. 0 => pods_var_raw( self::$type . '_no_label', $options ) 
  148. ); 
  149. else { 
  150. $data = array( 
  151. 1 => pods_var_raw( self::$type . '_yes_label', $options ) 
  152. ); 
  153.  
  154. return $data; 
  155.  
  156. /** 
  157. * Build regex necessary for JS validation 
  158. * @param mixed $value 
  159. * @param string $name 
  160. * @param array $options 
  161. * @param string $pod 
  162. * @param int $id 
  163. * @return bool 
  164. * @since 2.0 
  165. */ 
  166. public function regex ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  167. return false; 
  168.  
  169. /** 
  170. * Validate a value before it's saved 
  171. * @param mixed $value 
  172. * @param string $name 
  173. * @param array $options 
  174. * @param array $fields 
  175. * @param array $pod 
  176. * @param int $id 
  177. * @param null $params 
  178. * @return bool 
  179. * @since 2.0 
  180. */ 
  181. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  182. return true; 
  183.  
  184. /** 
  185. * Change the value or perform actions after validation but before saving to the DB 
  186. * @param mixed $value 
  187. * @param int $id 
  188. * @param string $name 
  189. * @param array $options 
  190. * @param array $fields 
  191. * @param array $pod 
  192. * @param object $params 
  193. * @return int|mixed 
  194. * @since 2.0 
  195. */ 
  196. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  197. // Only allow 0 / 1 
  198. if ( 'yes' === strtolower( $value ) || '1' === (string) $value ) 
  199. $value = 1; 
  200. elseif ( 'no' === strtolower( $value ) || '0' === (string) $value ) 
  201. $value = 0; 
  202. elseif ( strtolower( pods_var_raw( self::$type . '_yes_label', $options, __( 'Yes', 'pods' ), null, true ) ) === strtolower( $value ) ) 
  203. $value = 1; 
  204. elseif ( strtolower( pods_var_raw( self::$type . '_no_label', $options, __( 'No', 'pods' ), null, true ) ) === strtolower( $value ) ) 
  205. $value = 0; 
  206. else 
  207. $value = ( 0 === (int) $value ? 0 : 1 ); 
  208.  
  209. return $value; 
  210.  
  211. /** 
  212. * Customize the Pods UI manage table column output 
  213. * @param int $id 
  214. * @param mixed $value 
  215. * @param string $name 
  216. * @param array $options 
  217. * @param array $fields 
  218. * @param array $pod 
  219. * @since 2.0 
  220. */ 
  221. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  222. $yesno = array( 
  223. 1 => pods_var_raw( self::$type . '_yes_label', $options, __( 'Yes', 'pods' ), null, true ),  
  224. 0 => pods_var_raw( self::$type . '_no_label', $options, __( 'No', 'pods' ), null, true ) 
  225. ); 
  226.  
  227. if ( isset( $yesno[ (int) $value ] ) ) 
  228. $value = strip_tags( $yesno[ (int) $value ], '<strong><a><em><span><img>' ); 
  229.  
  230. return $value;