PodsField_Date

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

Defined (1)

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

/classes/fields/date.php  
  1. class PodsField_Date extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Date / Time'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'date'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Date'; 
  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. * @return array 
  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' => __( 'Date Format', 'pods' ),  
  56. 'default' => 'mdy',  
  57. 'type' => 'pick',  
  58. 'data' => array( 
  59. 'mdy' => date_i18n( 'm/d/Y' ),  
  60. 'mdy_dash' => date_i18n( 'm-d-Y' ),  
  61. 'mdy_dot' => date_i18n( 'm.d.Y' ),  
  62. 'ymd_slash' => date_i18n( 'Y/m/d' ),  
  63. 'ymd_dash' => date_i18n( 'Y-m-d' ),  
  64. 'ymd_dot' => date_i18n( 'Y.m.d' ),  
  65. 'fjy' => date_i18n( 'F j, Y' ),  
  66. 'fjsy' => date_i18n( 'F jS, Y' ),  
  67. 'y' => date_i18n( 'Y' ) 
  68. ),  
  69. self::$type . '_allow_empty' => array( 
  70. 'label' => __( 'Allow empty value?', 'pods' ),  
  71. 'default' => 1,  
  72. 'type' => 'boolean' 
  73. ),  
  74. self::$type . '_html5' => array( 
  75. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),  
  76. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),  
  77. 'type' => 'boolean' 
  78. ); 
  79.  
  80. // Check if PHP DateTime::createFromFormat exists for additional supported formats 
  81. if ( method_exists( 'DateTime', 'createFromFormat' ) || apply_filters( 'pods_form_ui_field_datetime_custom_formatter', false ) ) { 
  82. $options[ self::$type . '_format' ][ 'data' ] = array_merge( 
  83. $options[ self::$type . '_format' ][ 'data' ],  
  84. array( 
  85. 'dmy' => date_i18n( 'd/m/Y' ),  
  86. 'dmy_dash' => date_i18n( 'd-m-Y' ),  
  87. 'dmy_dot' => date_i18n( 'd.m.Y' ),  
  88. 'dMy' => date_i18n( 'd/M/Y' ),  
  89. 'dMy_dash' => date_i18n( 'd-M-Y' ) 
  90. ); 
  91.  
  92. $options[ self::$type . '_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_date_format_options', $options[ self::$type . '_format' ][ 'data' ] ); 
  93. $options[ self::$type . '_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_date_format_default', $options[ self::$type . '_format' ][ 'default' ] ); 
  94.  
  95. return $options; 
  96.  
  97. /** 
  98. * Define the current field's schema for DB table storage 
  99. * @param array $options 
  100. * @return array 
  101. * @since 2.0 
  102. */ 
  103. public function schema ( $options = null ) { 
  104. $schema = 'DATE NOT NULL default "0000-00-00"'; 
  105.  
  106. return $schema; 
  107.  
  108. /** 
  109. * Change the way the value of the field is displayed with Pods::get 
  110. * @param mixed $value 
  111. * @param string $name 
  112. * @param array $options 
  113. * @param array $pod 
  114. * @param int $id 
  115. * @return mixed|null|string 
  116. * @since 2.0 
  117. */ 
  118. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  119. $format = $this->format( $options ); 
  120.  
  121. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) { 
  122. $date = $this->createFromFormat( 'Y-m-d', (string) $value ); 
  123. $date_local = $this->createFromFormat( $format, (string) $value ); 
  124.  
  125. if ( false !== $date ) 
  126. $value = $date->format( $format ); 
  127. elseif ( false !== $date_local ) 
  128. $value = $date_local->format( $format ); 
  129. else 
  130. $value = date_i18n( $format, strtotime( (string) $value ) ); 
  131. elseif ( 0 == pods_var( self::$type . '_allow_empty', $options, 1 ) ) 
  132. $value = date_i18n( $format ); 
  133. else 
  134. $value = ''; 
  135.  
  136. return $value; 
  137.  
  138. /** 
  139. * Customize output of the form field 
  140. * @param string $name 
  141. * @param mixed $value 
  142. * @param array $options 
  143. * @param array $pod 
  144. * @param int $id 
  145. * @since 2.0 
  146. */ 
  147. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  148. $options = (array) $options; 
  149. $form_field_type = PodsForm::$field_type; 
  150.  
  151. if ( is_array( $value ) ) 
  152. $value = implode( ' ', $value ); 
  153.  
  154. // Format Value 
  155. $value = $this->display( $value, $name, $options, null, $pod, $id ); 
  156.  
  157. $field_type = 'date'; 
  158.  
  159. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  160. if ( pods_var( 'read_only', $options, false ) ) { 
  161. $options[ 'readonly' ] = true; 
  162.  
  163. $field_type = 'text'; 
  164. else 
  165. return; 
  166. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  167. $options[ 'readonly' ] = true; 
  168.  
  169. $field_type = 'text'; 
  170.  
  171. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  172.  
  173. /** 
  174. * Change the value or perform actions after validation but before saving to the DB 
  175. * @param mixed $value 
  176. * @param int $id 
  177. * @param string $name 
  178. * @param array $options 
  179. * @param array $fields 
  180. * @param array $pod 
  181. * @param object $params 
  182. * @return mixed|string 
  183. * @since 2.0 
  184. */ 
  185. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  186. $format = $this->format( $options ); 
  187.  
  188. if ( !empty( $value ) && ( 0 == pods_var( self::$type . '_allow_empty', $options, 1 ) || !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) ) 
  189. $value = $this->convert_date( $value, 'Y-m-d', $format ); 
  190. elseif ( 1 == pods_var( self::$type . '_allow_empty', $options, 1 ) ) 
  191. $value = '0000-00-00'; 
  192. else 
  193. $value = date_i18n( 'Y-m-d' ); 
  194.  
  195. return $value; 
  196.  
  197. /** 
  198. * Customize the Pods UI manage table column output 
  199. * @param int $id 
  200. * @param mixed $value 
  201. * @param string $name 
  202. * @param array $options 
  203. * @param array $fields 
  204. * @param array $pod 
  205. * @return mixed|null|string 
  206. * @since 2.0 
  207. */ 
  208. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  209. $value = $this->display( $value, $name, $options, $pod, $id ); 
  210.  
  211. if ( 1 == pods_var( self::$type . '_allow_empty', $options, 1 ) && ( empty( $value ) || in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) ) 
  212. $value = false; 
  213.  
  214. return $value; 
  215.  
  216. /** 
  217. * Build date/time format string based on options 
  218. * @param $options 
  219. * @return string 
  220. * @since 2.0 
  221. */ 
  222. public function format ( $options ) { 
  223. $date_format = array( 
  224. 'mdy' => 'm/d/Y',  
  225. 'mdy_dash' => 'm-d-Y',  
  226. 'mdy_dot' => 'm.d.Y',  
  227. 'dmy' => 'd/m/Y',  
  228. 'dmy_dash' => 'd-m-Y',  
  229. 'dmy_dot' => 'd.m.Y',  
  230. 'ymd_slash' => 'Y/m/d',  
  231. 'ymd_dash' => 'Y-m-d',  
  232. 'ymd_dot' => 'Y.m.d',  
  233. 'dMy' => 'd/M/Y',  
  234. 'dMy_dash' => 'd-M-Y',  
  235. 'fjy' => 'F j, Y',  
  236. 'fjsy' => 'F jS, Y',  
  237. 'y' => 'Y' 
  238. ); 
  239.  
  240. $date_format = apply_filters( 'pods_form_ui_field_date_formats', $date_format ); 
  241.  
  242. $format = $date_format[ pods_var( self::$type . '_format', $options, 'ymd_dash', null, true ) ]; 
  243.  
  244. return $format; 
  245.  
  246. /** 
  247. * @param $format 
  248. * @param $date 
  249. * @return DateTime 
  250. */ 
  251. public function createFromFormat ( $format, $date ) { 
  252. $datetime = false; 
  253.  
  254. if ( method_exists( 'DateTime', 'createFromFormat' ) ) { 
  255. $timezone = get_option( 'timezone_string' ); 
  256.  
  257. if ( empty( $timezone ) ) 
  258. $timezone = timezone_name_from_abbr( '', get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 0 ); 
  259.  
  260. if ( !empty( $timezone ) ) { 
  261. $datetimezone = new DateTimeZone( $timezone ); 
  262.  
  263. $datetime = DateTime::createFromFormat( $format, (string) $date, $datetimezone ); 
  264.  
  265. if ( false === $datetime ) 
  266. $datetime = new DateTime( date_i18n( 'Y-m-d', strtotime( (string) $date ) ) ); 
  267.  
  268. return apply_filters( 'pods_form_ui_field_date_formatter', $datetime, $format, $date ); 
  269.  
  270. /** 
  271. * Convert a date from one format to another 
  272. * @param $value 
  273. * @param $new_format 
  274. * @param string $original_format 
  275. * @return string 
  276. */ 
  277. public function convert_date ( $value, $new_format, $original_format = 'Y-m-d' ) { 
  278. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) { 
  279. $date = $this->createFromFormat( $original_format, (string) $value ); 
  280.  
  281. if ( false !== $date ) 
  282. $value = $date->format( $new_format ); 
  283. else 
  284. $value = date_i18n( $new_format, strtotime( (string) $value ) ); 
  285. else 
  286. $value = date_i18n( $new_format ); 
  287.  
  288. return $value;