PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/fields/color.php

https://github.com/ElmsPark/pods
PHP | 194 lines | 62 code | 25 blank | 107 comment | 17 complexity | fb08bf70c993dfc7e72f63075fb3d95f MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_Color extends PodsField {
  6. /**
  7. * Field Type Identifier
  8. *
  9. * @var string
  10. * @since 2.0.0
  11. */
  12. public static $type = 'color';
  13. /**
  14. * Field Type Label
  15. *
  16. * @var string
  17. * @since 2.0.0
  18. */
  19. public static $label = 'Color Picker';
  20. /**
  21. * Field Type Preparation
  22. *
  23. * @var string
  24. * @since 2.0.0
  25. */
  26. public static $prepare = '%s';
  27. /**
  28. * Do things like register/enqueue scripts and stylesheets
  29. *
  30. * @since 2.0.0
  31. */
  32. public function __construct () {
  33. }
  34. /**
  35. * Add options and set defaults to
  36. *
  37. * @return array
  38. * @since 2.0.0
  39. */
  40. public function options () {
  41. $options = array();
  42. return $options;
  43. }
  44. /**
  45. * Define the current field's schema for DB table storage
  46. *
  47. * @param array $options
  48. *
  49. * @return array
  50. * @since 2.0.0
  51. */
  52. public function schema ( $options = null ) {
  53. $schema = 'VARCHAR(7)';
  54. return $schema;
  55. }
  56. /**
  57. * Change the way the value of the field is displayed with Pods::get
  58. *
  59. * @param mixed $value
  60. * @param string $name
  61. * @param array $options
  62. * @param array $pod
  63. * @param int $id
  64. *
  65. * @return mixed|null
  66. * @since 2.0.0
  67. */
  68. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  69. return $value;
  70. }
  71. /**
  72. * Customize output of the form field
  73. *
  74. * @param string $name
  75. * @param mixed $value
  76. * @param array $options
  77. * @param array $pod
  78. * @param int $id
  79. *
  80. * @since 2.0.0
  81. */
  82. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  83. $options = (array) $options;
  84. $form_field_type = PodsForm::$field_type;
  85. if ( is_array( $value ) )
  86. $value = implode( ' ', $value );
  87. // Farbtastic for below 3.5
  88. if ( pods_wp_version( '3.5', '>' ) )
  89. pods_view( PODS_DIR . 'ui/fields/farbtastic.php', compact( array_keys( get_defined_vars() ) ) );
  90. // WP Color Picker for 3.5+
  91. else
  92. pods_view( PODS_DIR . 'ui/fields/color.php', compact( array_keys( get_defined_vars() ) ) );
  93. }
  94. /**
  95. * Validate a value before it's saved
  96. *
  97. * @param mixed $value
  98. * @param string $name
  99. * @param array $options
  100. * @param array $fields
  101. * @param array $pod
  102. * @param int $id
  103. * @param array $params
  104. *
  105. * @return array|bool
  106. * @since 2.0.0
  107. */
  108. public function validate ( &$value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
  109. $errors = array();
  110. $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
  111. if ( is_array( $check ) )
  112. $errors = $check;
  113. else {
  114. $color = str_replace( '#', '', $check );
  115. if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
  116. if ( 1 == pods_var( 'required', $options ) )
  117. $errors[] = __( 'This field is required.', 'pods' );
  118. else {
  119. // @todo Ask for a specific format in error message
  120. $errors[] = __( 'Invalid value provided for this field.', 'pods' );
  121. }
  122. }
  123. elseif ( 3 != strlen( $color ) && 6 != strlen( $color ) && 1 != empty( $color ) )
  124. $errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' );
  125. }
  126. if ( !empty( $errors ) )
  127. return $errors;
  128. return true;
  129. }
  130. /**
  131. * Change the value or perform actions after validation but before saving to the DB
  132. *
  133. * @param mixed $value
  134. * @param int $id
  135. * @param string $name
  136. * @param array $options
  137. * @param array $fields
  138. * @param array $pod
  139. * @param object $params
  140. *
  141. * @return mixed|string
  142. * @since 2.0.0
  143. */
  144. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  145. $options = (array) $options;
  146. $value = str_replace( '#', '', $value );
  147. if ( 0 < strlen( $value ) )
  148. $value = '#' . $value;
  149. return $value;
  150. }
  151. /**
  152. * Customize the Pods UI manage table column output
  153. *
  154. * @param int $id
  155. * @param mixed $value
  156. * @param string $name
  157. * @param array $options
  158. * @param array $fields
  159. * @param array $pod
  160. *
  161. * @return mixed|string
  162. * @since 2.0.0
  163. */
  164. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
  165. if ( !empty( $value ) )
  166. $value = $value . ' <span style="display:inline-block;width:25px;height:25px;border:1px solid #333;background-color:' . $value . '"></span>';
  167. return $value;
  168. }
  169. }