PodsField_DateTime

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

Defined (1)

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

/classes/fields/datetime.php  
  1. class PodsField_DateTime 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 = 'datetime'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Date / Time'; 
  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. 'c' => date_i18n( 'c' ) 
  68. ),  
  69. 'dependency' => true 
  70. ),  
  71. self::$type . '_time_type' => array( 
  72. 'label' => __( 'Time Format Type', 'pods' ),  
  73. 'excludes-on' => array( self::$type . '_format' => 'c' ),  
  74. 'default' => '12',  
  75. 'type' => 'pick',  
  76. 'data' => array( 
  77. '12' => __( '12 hour', 'pods' ),  
  78. '24' => __( '24 hour', 'pods' ) 
  79. ),  
  80. 'dependency' => true 
  81. ),  
  82. self::$type . '_time_format' => array( 
  83. 'label' => __( 'Time Format', 'pods' ),  
  84. 'depends-on' => array( self::$type . '_time_type' => '12' ),  
  85. 'excludes-on' => array( self::$type . '_format' => 'c' ),  
  86. 'default' => 'h_mma',  
  87. 'type' => 'pick',  
  88. 'data' => array( 
  89. 'h_mm_A' => date_i18n( 'g:i A' ),  
  90. 'h_mm_ss_A' => date_i18n( 'g:i:s A' ),  
  91. 'hh_mm_A' => date_i18n( 'h:i A' ),  
  92. 'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),  
  93. 'h_mma' => date_i18n( 'g:ia' ),  
  94. 'hh_mma' => date_i18n( 'h:ia' ),  
  95. 'h_mm' => date_i18n( 'g:i' ),  
  96. 'h_mm_ss' => date_i18n( 'g:i:s' ),  
  97. 'hh_mm' => date_i18n( 'h:i' ),  
  98. 'hh_mm_ss' => date_i18n( 'h:i:s' ) 
  99. ),  
  100. self::$type . '_time_format_24' => array( 
  101. 'label' => __( 'Time Format', 'pods' ),  
  102. 'depends-on' => array( self::$type . '_time_type' => '24' ),  
  103. 'default' => 'hh_mm',  
  104. 'type' => 'pick',  
  105. 'data' => array( 
  106. 'hh_mm' => date_i18n( 'H:i' ),  
  107. 'hh_mm_ss' => date_i18n( 'H:i:s' ) 
  108. ),  
  109. self::$type . '_allow_empty' => array( 
  110. 'label' => __( 'Allow empty value?', 'pods' ),  
  111. 'default' => 1,  
  112. 'type' => 'boolean' 
  113. ),  
  114. self::$type . '_html5' => array( 
  115. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),  
  116. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),  
  117. 'type' => 'boolean' 
  118. ); 
  119.  
  120. // Check if PHP DateTime::createFromFormat exists for additional supported formats 
  121. if ( method_exists( 'DateTime', 'createFromFormat' ) || apply_filters( 'pods_form_ui_field_datetime_custom_formatter', false ) ) { 
  122. $options[ self::$type . '_format' ][ 'data' ] = array_merge( 
  123. $options[ self::$type . '_format' ][ 'data' ],  
  124. array( 
  125. 'dmy' => date_i18n( 'd/m/Y' ),  
  126. 'dmy_dash' => date_i18n( 'd-m-Y' ),  
  127. 'dmy_dot' => date_i18n( 'd.m.Y' ),  
  128. 'dMy' => date_i18n( 'd/M/Y' ),  
  129. 'dMy_dash' => date_i18n( 'd-M-Y' ) 
  130. ); 
  131.  
  132. $options[ self::$type . '_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_date_format_options', $options[ self::$type . '_format' ][ 'data' ] ); 
  133. $options[ self::$type . '_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_date_format_default', $options[ self::$type . '_format' ][ 'default' ] ); 
  134.  
  135. $options[ self::$type . '_time_type' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ self::$type . '_time_type' ][ 'default' ] ); 
  136. $options[ self::$type . '_time_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_options', $options[ self::$type . '_time_format' ][ 'data' ] ); 
  137. $options[ self::$type . '_time_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_default', $options[ self::$type . '_time_format' ][ 'default' ] ); 
  138. $options[ self::$type . '_time_format_24' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ self::$type . '_time_format_24' ][ 'data' ] ); 
  139. $options[ self::$type . '_time_format_24' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ self::$type . '_time_format_24' ][ 'default' ] ); 
  140.  
  141. return $options; 
  142.  
  143. /** 
  144. * Define the current field's schema for DB table storage 
  145. * @param array $options 
  146. * @return array 
  147. * @since 2.0 
  148. */ 
  149. public function schema ( $options = null ) { 
  150. $schema = 'DATETIME NOT NULL default "0000-00-00 00:00:00"'; 
  151.  
  152. return $schema; 
  153.  
  154. /** 
  155. * Change the way the value of the field is displayed with Pods::get 
  156. * @param mixed $value 
  157. * @param string $name 
  158. * @param array $options 
  159. * @param array $pod 
  160. * @param int $id 
  161. * @return mixed|null|string 
  162. * @since 2.0 
  163. */ 
  164. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  165. $format = $this->format( $options ); 
  166.  
  167. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) { 
  168. $date = $this->createFromFormat( 'Y-m-d H:i:s', (string) $value ); 
  169. $date_local = $this->createFromFormat( $format, (string) $value ); 
  170.  
  171. if ( false !== $date ) 
  172. $value = $date->format( $format ); 
  173. elseif ( false !== $date_local ) 
  174. $value = $date_local->format( $format ); 
  175. else 
  176. $value = date_i18n( $format, strtotime( (string) $value ) ); 
  177. elseif ( 0 == pods_var( self::$type . '_allow_empty', $options, 1 ) ) 
  178. $value = date_i18n( $format ); 
  179. else 
  180. $value = ''; 
  181.  
  182. return $value; 
  183.  
  184. /** 
  185. * Customize output of the form field 
  186. * @param string $name 
  187. * @param mixed $value 
  188. * @param array $options 
  189. * @param array $pod 
  190. * @param int $id 
  191. * @since 2.0 
  192. */ 
  193. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  194. $options = (array) $options; 
  195. $form_field_type = PodsForm::$field_type; 
  196.  
  197. if ( is_array( $value ) ) 
  198. $value = implode( ' ', $value ); 
  199.  
  200. // Format Value 
  201. $value = $this->display( $value, $name, $options, null, $pod, $id ); 
  202.  
  203. $field_type = 'datetime'; 
  204.  
  205. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  206. if ( pods_var( 'read_only', $options, false ) ) { 
  207. $options[ 'readonly' ] = true; 
  208.  
  209. $field_type = 'text'; 
  210. else 
  211. return; 
  212. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  213. $options[ 'readonly' ] = true; 
  214.  
  215. $field_type = 'text'; 
  216.  
  217. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  218.  
  219. /** 
  220. * Change the value or perform actions after validation but before saving to the DB 
  221. * @param mixed $value 
  222. * @param int $id 
  223. * @param string $name 
  224. * @param array $options 
  225. * @param array $fields 
  226. * @param array $pod 
  227. * @param object $params 
  228. * @return mixed|string 
  229. * @since 2.0 
  230. */ 
  231. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  232. $format = $this->format( $options ); 
  233.  
  234. 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' ) ) ) ) 
  235. $value = $this->convert_date( $value, 'Y-m-d H:i:s', $format ); 
  236. elseif ( 1 == pods_var( self::$type . '_allow_empty', $options, 1 ) ) 
  237. $value = '0000-00-00 00:00:00'; 
  238. else 
  239. $value = current_time( 'mysql' ); 
  240.  
  241. return $value; 
  242.  
  243. /** 
  244. * Customize the Pods UI manage table column output 
  245. * @param int $id 
  246. * @param mixed $value 
  247. * @param string $name 
  248. * @param array $options 
  249. * @param array $fields 
  250. * @param array $pod 
  251. * @return mixed|null|string 
  252. * @since 2.0 
  253. */ 
  254. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  255. $value = $this->display( $value, $name, $options, $pod, $id ); 
  256.  
  257. 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' ) ) ) ) 
  258. $value = false; 
  259.  
  260. return $value; 
  261.  
  262. /** 
  263. * Build date/time format string based on options 
  264. * @param $options 
  265. * @return string 
  266. * @since 2.0 
  267. */ 
  268. public function format ( $options ) { 
  269. $date_format = array( 
  270. 'mdy' => 'm/d/Y',  
  271. 'mdy_dash' => 'm-d-Y',  
  272. 'mdy_dot' => 'm.d.Y',  
  273. 'dmy' => 'd/m/Y',  
  274. 'dmy_dash' => 'd-m-Y',  
  275. 'dmy_dot' => 'd.m.Y',  
  276. 'ymd_slash' => 'Y/m/d',  
  277. 'ymd_dash' => 'Y-m-d',  
  278. 'ymd_dot' => 'Y.m.d',  
  279. 'dMy' => 'd/M/Y',  
  280. 'dMy_dash' => 'd-M-Y',  
  281. 'fjy' => 'F j, Y',  
  282. 'fjsy' => 'F jS, Y',  
  283. 'c' => 'c' 
  284. ); 
  285.  
  286. $time_format = array( 
  287. 'h_mm_A' => 'g:i A',  
  288. 'h_mm_ss_A' => 'g:i:s A',  
  289. 'hh_mm_A' => 'h:i A',  
  290. 'hh_mm_ss_A' => 'h:i:s A',  
  291. 'h_mma' => 'g:ia',  
  292. 'hh_mma' => 'h:ia',  
  293. 'h_mm' => 'g:i',  
  294. 'h_mm_ss' => 'g:i:s',  
  295. 'hh_mm' => 'h:i',  
  296. 'hh_mm_ss' => 'h:i:s' 
  297. ); 
  298.  
  299. $time_format_24 = array( 
  300. 'hh_mm' => 'H:i',  
  301. 'hh_mm_ss' => 'H:i:s' 
  302. ); 
  303.  
  304. $date_format = apply_filters( 'pods_form_ui_field_date_formats', $date_format ); 
  305.  
  306. $time_format = apply_filters( 'pods_form_ui_field_time_formats', $time_format ); 
  307. $time_format_24 = apply_filters( 'pods_form_ui_field_time_formats_24', $time_format_24 ); 
  308.  
  309. $format_value = pods_var( self::$type . '_format', $options, 'ymd_dash', null, true ); 
  310.  
  311. $format = $date_format[ $format_value ]; 
  312.  
  313. if ( 'c' != $format_value ) { 
  314. if ( 12 == pods_var( self::$type . '_time_type', $options ) ) 
  315. $format .= ' ' . $time_format[ pods_var( self::$type . '_time_format', $options, 'hh_mm', null, true ) ]; 
  316. else 
  317. $format .= ' ' . $time_format_24[ pods_var( self::$type . '_time_format_24', $options, 'hh_mm', null, true ) ]; 
  318.  
  319. return $format; 
  320.  
  321. /** 
  322. * @param $format 
  323. * @param $date 
  324. * @return DateTime 
  325. */ 
  326. public function createFromFormat ( $format, $date ) { 
  327. $datetime = false; 
  328.  
  329. if ( method_exists( 'DateTime', 'createFromFormat' ) ) { 
  330. $timezone = get_option( 'timezone_string' ); 
  331.  
  332. if ( empty( $timezone ) ) 
  333. $timezone = timezone_name_from_abbr( '', get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 0 ); 
  334.  
  335. if ( !empty( $timezone ) ) { 
  336. $datetimezone = new DateTimeZone( $timezone ); 
  337.  
  338. $datetime = DateTime::createFromFormat( $format, (string) $date, $datetimezone ); 
  339.  
  340. if ( false === $datetime ) 
  341. $datetime = new DateTime( date_i18n( 'Y-m-d H:i:s', strtotime( (string) $date ) ) ); 
  342.  
  343. return apply_filters( 'pods_form_ui_field_datetime_formatter', $datetime, $format, $date ); 
  344.  
  345. /** 
  346. * Convert a date from one format to another 
  347. * @param $value 
  348. * @param $new_format 
  349. * @param string $original_format 
  350. * @return string 
  351. */ 
  352. public function convert_date ( $value, $new_format, $original_format = 'Y-m-d H:i:s' ) { 
  353. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) { 
  354. $date = $this->createFromFormat( $original_format, (string) $value ); 
  355.  
  356. if ( false !== $date ) 
  357. $value = $date->format( $new_format ); 
  358. else 
  359. $value = date_i18n( $new_format, strtotime( (string) $value ) ); 
  360. else 
  361. $value = date_i18n( $new_format ); 
  362.  
  363. return $value;