PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/fields/datetime.php

https://github.com/ElmsPark/pods
PHP | 334 lines | 180 code | 33 blank | 121 comment | 20 complexity | 341082f3cf2ad5b7f9ddeaff00e49aa4 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_DateTime extends PodsField {
  6. /**
  7. * Field Type Group
  8. *
  9. * @var string
  10. * @since 2.0.0
  11. */
  12. public static $group = 'Date / Time';
  13. /**
  14. * Field Type Identifier
  15. *
  16. * @var string
  17. * @since 2.0.0
  18. */
  19. public static $type = 'datetime';
  20. /**
  21. * Field Type Label
  22. *
  23. * @var string
  24. * @since 2.0.0
  25. */
  26. public static $label = 'Date / Time';
  27. /**
  28. * Field Type Preparation
  29. *
  30. * @var string
  31. * @since 2.0.0
  32. */
  33. public static $prepare = '%s';
  34. /**
  35. * Do things like register/enqueue scripts and stylesheets
  36. *
  37. * @since 2.0.0
  38. */
  39. public function __construct () {
  40. }
  41. /**
  42. * Add options and set defaults to
  43. *
  44. * @return array
  45. *
  46. * @since 2.0.0
  47. */
  48. public function options () {
  49. $options = array(
  50. 'datetime_format' => array(
  51. 'label' => __( 'Date Format', 'pods' ),
  52. 'default' => 'mdy',
  53. 'type' => 'pick',
  54. 'data' => array(
  55. 'mdy' => date_i18n( 'm/d/Y' ),
  56. 'mdy_dash' => date_i18n( 'm-d-Y' ),
  57. 'mdy_dot' => date_i18n( 'm.d.Y' ),
  58. 'dmy' => date_i18n( 'd/m/Y' ),
  59. 'dmy_dash' => date_i18n( 'd-m-Y' ),
  60. 'dmy_dot' => date_i18n( 'd.m.Y' ),
  61. 'ymd_slash' => date_i18n( 'Y/m/d' ),
  62. 'ymd_dash' => date_i18n( 'Y-m-d' ),
  63. 'ymd_dot' => date_i18n( 'Y.m.d' ),
  64. 'dMd' => date_i18n( 'd/M/Y' ),
  65. 'dMd_dash' => date_i18n( 'd-M-Y' ),
  66. 'fjy' => date_i18n( 'F j, Y' ),
  67. 'fjsy' => date_i18n( 'F jS, Y' )
  68. )
  69. ),
  70. 'datetime_time_type' => array(
  71. 'label' => __( 'Time Format Type', 'pods' ),
  72. 'default' => '12',
  73. 'type' => 'pick',
  74. 'data' => array(
  75. '12' => __( '12 hour', 'pods' ),
  76. '24' => __( '24 hour', 'pods' )
  77. ),
  78. 'dependency' => true
  79. ),
  80. 'datetime_time_format' => array(
  81. 'label' => __( 'Time Format', 'pods' ),
  82. 'depends-on' => array( 'datetime_time_type' => '12' ),
  83. 'default' => 'h_mma',
  84. 'type' => 'pick',
  85. 'data' => array(
  86. 'h_mm_A' => date_i18n( 'g:i A' ),
  87. 'h_mm_ss_A' => date_i18n( 'g:i:s A' ),
  88. 'hh_mm_A' => date_i18n( 'h:i A' ),
  89. 'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),
  90. 'h_mma' => date_i18n( 'g:ia' ),
  91. 'hh_mma' => date_i18n( 'h:ia' ),
  92. 'h_mm' => date_i18n( 'g:i' ),
  93. 'h_mm_ss' => date_i18n( 'g:i:s' ),
  94. 'hh_mm' => date_i18n( 'h:i' ),
  95. 'hh_mm_ss' => date_i18n( 'h:i:s' )
  96. )
  97. ),
  98. 'datetime_time_format_24' => array(
  99. 'label' => __( 'Time Format', 'pods' ),
  100. 'depends-on' => array( 'datetime_time_type' => '24' ),
  101. 'default' => 'hh_mm',
  102. 'type' => 'pick',
  103. 'data' => array(
  104. 'hh_mm' => date_i18n( 'H:i' ),
  105. 'hh_mm_ss' => date_i18n( 'H:i:s' )
  106. )
  107. ),
  108. 'datetime_allow_empty' => array(
  109. 'label' => __( 'Allow empty value?', 'pods' ),
  110. 'default' => 1,
  111. 'type' => 'boolean'
  112. ),
  113. 'datetime_html5' => array(
  114. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
  115. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
  116. 'type' => 'boolean'
  117. )
  118. );
  119. return $options;
  120. }
  121. /**
  122. * Define the current field's schema for DB table storage
  123. *
  124. * @param array $options
  125. *
  126. * @return array
  127. * @since 2.0.0
  128. */
  129. public function schema ( $options = null ) {
  130. $schema = 'DATETIME NOT NULL default "0000-00-00 00:00:00"';
  131. return $schema;
  132. }
  133. /**
  134. * Change the way the value of the field is displayed with Pods::get
  135. *
  136. * @param mixed $value
  137. * @param string $name
  138. * @param array $options
  139. * @param array $pod
  140. * @param int $id
  141. *
  142. * @return mixed|null|string
  143. * @since 2.0.0
  144. */
  145. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  146. $format = $this->format( $options );
  147. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
  148. $date = $this->createFromFormat( 'Y-m-d H:i:s', (string) $value );
  149. $date_local = $this->createFromFormat( $format, (string) $value );
  150. if ( false !== $date )
  151. $value = $date->format( $format );
  152. elseif ( false !== $date_local )
  153. $value = $date_local->format( $format );
  154. else
  155. $value = date_i18n( $format, strtotime( (string) $value ) );
  156. }
  157. elseif ( 0 == pods_var( 'datetime_allow_empty', $options, 1 ) )
  158. $value = date_i18n( $format );
  159. else
  160. $value = '';
  161. return $value;
  162. }
  163. /**
  164. * Customize output of the form field
  165. *
  166. * @param string $name
  167. * @param mixed $value
  168. * @param array $options
  169. * @param array $pod
  170. * @param int $id
  171. *
  172. * @since 2.0.0
  173. */
  174. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  175. $options = (array) $options;
  176. $form_field_type = PodsForm::$field_type;
  177. if ( is_array( $value ) )
  178. $value = implode( ' ', $value );
  179. // Format Value
  180. $value = $this->display( $value, $name, $options, null, $pod, $id );
  181. pods_view( PODS_DIR . 'ui/fields/datetime.php', compact( array_keys( get_defined_vars() ) ) );
  182. }
  183. /**
  184. * Change the value or perform actions after validation but before saving to the DB
  185. *
  186. * @param mixed $value
  187. * @param int $id
  188. * @param string $name
  189. * @param array $options
  190. * @param array $fields
  191. * @param array $pod
  192. * @param object $params
  193. *
  194. * @return mixed|string
  195. * @since 2.0.0
  196. */
  197. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  198. $format = $this->format( $options );
  199. if ( !empty( $value ) && ( 0 == pods_var( 'datetime_allow_empty', $options, 1 ) || !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
  200. $value = $this->convert_date( $value, 'Y-m-d H:i:s', $format );
  201. elseif ( 1 == pods_var( 'datetime_allow_empty', $options, 1 ) )
  202. $value = '0000-00-00 00:00:00';
  203. else
  204. $value = date_i18n( 'Y-m-d H:i:s' );
  205. return $value;
  206. }
  207. /**
  208. * Customize the Pods UI manage table column output
  209. *
  210. * @param int $id
  211. * @param mixed $value
  212. * @param string $name
  213. * @param array $options
  214. * @param array $fields
  215. * @param array $pod
  216. *
  217. * @return mixed|null|string
  218. * @since 2.0.0
  219. */
  220. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
  221. $value = $this->display( $value, $name, $options, $pod, $id );
  222. if ( 1 == pods_var( 'datetime_allow_empty', $options, 1 ) && ( empty( $value ) || in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
  223. $value = false;
  224. return $value;
  225. }
  226. /**
  227. * Build date/time format string based on options
  228. *
  229. * @param $options
  230. *
  231. * @return string
  232. * @since 2.0.0
  233. */
  234. public function format ( $options ) {
  235. $date_format = array(
  236. 'mdy' => 'm/d/Y',
  237. 'mdy_dash' => 'm-d-Y',
  238. 'mdy_dot' => 'm.d.Y',
  239. 'dmy' => 'd/m/Y',
  240. 'dmy_dash' => 'd-m-Y',
  241. 'dmy_dot' => 'd.m.Y',
  242. 'ymd_slash' => 'Y/m/d',
  243. 'ymd_dash' => 'Y-m-d',
  244. 'ymd_dot' => 'Y.m.d',
  245. 'dMd' => 'd/M/Y',
  246. 'dMd_dash' => 'd-M-Y',
  247. 'fjy' => 'F j, Y',
  248. 'fjsy' => 'F jS, Y'
  249. );
  250. $time_format = array(
  251. 'h_mm_A' => 'g:i A',
  252. 'h_mm_ss_A' => 'g:i:s A',
  253. 'hh_mm_A' => 'h:i A',
  254. 'hh_mm_ss_A' => 'h:i:s A',
  255. 'h_mma' => 'g:ia',
  256. 'hh_mma' => 'h:ia',
  257. 'h_mm' => 'g:i',
  258. 'h_mm_ss' => 'g:i:s',
  259. 'hh_mm' => 'h:i',
  260. 'hh_mm_ss' => 'h:i:s'
  261. );
  262. $format = $date_format[ pods_var( 'datetime_format', $options, 'ymd_dash', null, true ) ] . ' ';
  263. if ( 12 == pods_var( 'datetime_time_type', $options ) )
  264. $format .= $time_format[ pods_var( 'datetime_time_format', $options, 'hh_mm', null, true ) ];
  265. else
  266. $format .= str_replace( array( 'h:', 'g:' ), 'H:', $time_format[ pods_var( 'datetime_time_format', $options, 'hh_mm', null, true ) ] );
  267. return $format;
  268. }
  269. /**
  270. * @param $format
  271. * @param $date
  272. *
  273. * @return DateTime
  274. */
  275. public function createFromFormat ( $format, $date ) {
  276. if ( method_exists( 'DateTime', 'createFromFormat' ) )
  277. return DateTime::createFromFormat( $format, (string) $date );
  278. return new DateTime( date_i18n( 'Y-m-d H:i:s', strtotime( (string) $date ) ) );
  279. }
  280. /**
  281. * Convert a date from one format to another
  282. *
  283. * @param $value
  284. * @param $new_format
  285. * @param string $original_format
  286. *
  287. * @return string
  288. */
  289. public function convert_date ( $value, $new_format, $original_format = 'Y-m-d H:i:s' ) {
  290. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
  291. $date = $this->createFromFormat( $original_format, (string) $value );
  292. if ( false !== $date )
  293. $value = $date->format( $new_format );
  294. else
  295. $value = date_i18n( $new_format, strtotime( (string) $value ) );
  296. }
  297. else
  298. $value = date_i18n( $new_format );
  299. return $value;
  300. }
  301. }