/wp-content/plugins/pods/classes/fields/boolean.php

https://gitlab.com/najomie/ljm · PHP · 276 lines · 115 code · 27 blank · 134 comment · 21 complexity · 42153547822af06f8409630979dbe457 MD5 · raw file

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