PodsField_Color

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

Defined (1)

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

/classes/fields/color.php  
  1. class PodsField_Color extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Identifier 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $type = 'color'; 
  9.  
  10. /** 
  11. * Field Type Label 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $label = 'Color Picker'; 
  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 
  34. * @since 2.0 
  35. */ 
  36. public function options () { 
  37. $options = array( 
  38. self::$type . '_repeatable' => array( 
  39. 'label' => __( 'Repeatable Field', 'pods' ),  
  40. 'default' => 0,  
  41. 'type' => 'boolean',  
  42. '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' ),  
  43. 'boolean_yes_label' => '',  
  44. 'dependency' => true,  
  45. 'developer_mode' => true 
  46. ); 
  47.  
  48. return $options; 
  49.  
  50. /** 
  51. * Define the current field's schema for DB table storage 
  52. * @param array $options 
  53. * @return array 
  54. * @since 2.0 
  55. */ 
  56. public function schema ( $options = null ) { 
  57. $schema = 'VARCHAR(7)'; 
  58.  
  59. return $schema; 
  60.  
  61. /** 
  62. * Change the way the value of the field is displayed with Pods::get 
  63. * @param mixed $value 
  64. * @param string $name 
  65. * @param array $options 
  66. * @param array $pod 
  67. * @param int $id 
  68. * @return mixed|null 
  69. * @since 2.0 
  70. */ 
  71. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  72. return $value; 
  73.  
  74. /** 
  75. * Customize output of the form field 
  76. * @param string $name 
  77. * @param mixed $value 
  78. * @param array $options 
  79. * @param array $pod 
  80. * @param int $id 
  81. * @since 2.0 
  82. */ 
  83. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  84. $options = (array) $options; 
  85. $form_field_type = PodsForm::$field_type; 
  86.  
  87. if ( is_array( $value ) ) 
  88. $value = implode( ' ', $value ); 
  89.  
  90. // WP Color Picker for 3.5+ 
  91. if ( pods_version_check( 'wp', '3.5' ) ) { 
  92. $field_type = 'color'; 
  93. // Farbtastic for below 3.5 
  94. else { 
  95. $field_type = 'farbtastic'; 
  96.  
  97. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  98. if ( pods_v( 'read_only', $options, false ) ) { 
  99. $options[ 'readonly' ] = true; 
  100.  
  101. $field_type = 'text'; 
  102. else 
  103. return; 
  104. elseif ( !pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { 
  105. $options[ 'readonly' ] = true; 
  106.  
  107. $field_type = 'text'; 
  108.  
  109. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  110.  
  111. /** 
  112. * Validate a value before it's saved 
  113. * @param mixed $value 
  114. * @param string $name 
  115. * @param array $options 
  116. * @param array $fields 
  117. * @param array $pod 
  118. * @param int $id 
  119. * @param array $params 
  120. * @return array|bool 
  121. * @since 2.0 
  122. */ 
  123. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  124. $errors = array(); 
  125.  
  126. $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); 
  127.  
  128. if ( is_array( $check ) ) 
  129. $errors = $check; 
  130. else { 
  131. $color = str_replace( '#', '', $check ); 
  132.  
  133. if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) { 
  134. if ( 1 == pods_v( 'required', $options ) ) 
  135. $errors[] = __( 'This field is required.', 'pods' ); 
  136. else { 
  137. // @todo Ask for a specific format in error message 
  138. $errors[] = __( 'Invalid value provided for this field.', 'pods' ); 
  139. elseif ( 3 != strlen( $color ) && 6 != strlen( $color ) && 1 != empty( $color ) ) 
  140. $errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' ); 
  141.  
  142. if ( !empty( $errors ) ) 
  143. return $errors; 
  144.  
  145. return true; 
  146.  
  147. /** 
  148. * Change the value or perform actions after validation but before saving to the DB 
  149. * @param mixed $value 
  150. * @param int $id 
  151. * @param string $name 
  152. * @param array $options 
  153. * @param array $fields 
  154. * @param array $pod 
  155. * @param object $params 
  156. * @return mixed|string 
  157. * @since 2.0 
  158. */ 
  159. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  160. $options = (array) $options; 
  161.  
  162. $value = str_replace( '#', '', $value ); 
  163.  
  164. if ( 0 < strlen( $value ) ) 
  165. $value = '#' . $value; 
  166.  
  167. return $value; 
  168.  
  169. /** 
  170. * Customize the Pods UI manage table column output 
  171. * @param int $id 
  172. * @param mixed $value 
  173. * @param string $name 
  174. * @param array $options 
  175. * @param array $fields 
  176. * @param array $pod 
  177. * @return mixed|string 
  178. * @since 2.0 
  179. */ 
  180. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  181. if ( !empty( $value ) ) 
  182. $value = $value . ' <span style="display:inline-block;width:25px;height:25px;border:1px solid #333;background-color:' . $value . '"></span>'; 
  183.  
  184. return $value;