PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/fields/date.php

https://github.com/ElmsPark/pods
PHP | 278 lines | 126 code | 31 blank | 121 comment | 18 complexity | b2ca5be9423c8880c5bbeeac9d7a3b7f MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_Date 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 = 'date';
  20. /**
  21. * Field Type Label
  22. *
  23. * @var string
  24. * @since 2.0.0
  25. */
  26. public static $label = 'Date';
  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. 'date_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. 'date_allow_empty' => array(
  71. 'label' => __( 'Allow empty value?', 'pods' ),
  72. 'default' => 1,
  73. 'type' => 'boolean'
  74. ),
  75. 'date_html5' => array(
  76. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
  77. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
  78. 'type' => 'boolean'
  79. )
  80. );
  81. return $options;
  82. }
  83. /**
  84. * Define the current field's schema for DB table storage
  85. *
  86. * @param array $options
  87. *
  88. * @return array
  89. * @since 2.0.0
  90. */
  91. public function schema ( $options = null ) {
  92. $schema = 'DATE NOT NULL default "0000-00-00"';
  93. return $schema;
  94. }
  95. /**
  96. * Change the way the value of the field is displayed with Pods::get
  97. *
  98. * @param mixed $value
  99. * @param string $name
  100. * @param array $options
  101. * @param array $pod
  102. * @param int $id
  103. *
  104. * @return mixed|null|string
  105. * @since 2.0.0
  106. */
  107. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  108. $format = $this->format( $options );
  109. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
  110. $date = $this->createFromFormat( 'Y-m-d', (string) $value );
  111. $date_local = $this->createFromFormat( $format, (string) $value );
  112. if ( false !== $date )
  113. $value = $date->format( $format );
  114. elseif ( false !== $date_local )
  115. $value = $date_local->format( $format );
  116. else
  117. $value = date_i18n( $format, strtotime( (string) $value ) );
  118. }
  119. elseif ( 0 == pods_var( 'date_allow_empty', $options, 1 ) )
  120. $value = date_i18n( $format );
  121. else
  122. $value = '';
  123. return $value;
  124. }
  125. /**
  126. * Customize output of the form field
  127. *
  128. * @param string $name
  129. * @param mixed $value
  130. * @param array $options
  131. * @param array $pod
  132. * @param int $id
  133. *
  134. * @since 2.0.0
  135. */
  136. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  137. $options = (array) $options;
  138. $form_field_type = PodsForm::$field_type;
  139. if ( is_array( $value ) )
  140. $value = implode( ' ', $value );
  141. // Format Value
  142. $value = $this->display( $value, $name, $options, null, $pod, $id );
  143. pods_view( PODS_DIR . 'ui/fields/date.php', compact( array_keys( get_defined_vars() ) ) );
  144. }
  145. /**
  146. * Change the value or perform actions after validation but before saving to the DB
  147. *
  148. * @param mixed $value
  149. * @param int $id
  150. * @param string $name
  151. * @param array $options
  152. * @param array $fields
  153. * @param array $pod
  154. * @param object $params
  155. *
  156. * @return mixed|string
  157. * @since 2.0.0
  158. */
  159. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  160. $format = $this->format( $options );
  161. if ( !empty( $value ) && ( 0 == pods_var( 'date_allow_empty', $options, 1 ) || !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
  162. $value = $this->convert_date( $value, 'Y-m-d', $format );
  163. elseif ( 1 == pods_var( 'date_allow_empty', $options, 1 ) )
  164. $value = '0000-00-00';
  165. else
  166. $value = date_i18n( 'Y-m-d' );
  167. return $value;
  168. }
  169. /**
  170. * Customize the Pods UI manage table column output
  171. *
  172. * @param int $id
  173. * @param mixed $value
  174. * @param string $name
  175. * @param array $options
  176. * @param array $fields
  177. * @param array $pod
  178. *
  179. * @return mixed|null|string
  180. * @since 2.0.0
  181. */
  182. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
  183. $value = $this->display( $value, $name, $options, $pod, $id );
  184. if ( 1 == pods_var( 'date_allow_empty', $options, 1 ) && ( empty( $value ) || in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
  185. $value = false;
  186. return $value;
  187. }
  188. /**
  189. * Build date/time format string based on options
  190. *
  191. * @param $options
  192. *
  193. * @return string
  194. * @since 2.0.0
  195. */
  196. public function format ( $options ) {
  197. $date_format = array(
  198. 'mdy' => 'm/d/Y',
  199. 'mdy_dash' => 'm-d-Y',
  200. 'mdy_dot' => 'm.d.Y',
  201. 'dmy' => 'd/m/Y',
  202. 'dmy_dash' => 'd-m-Y',
  203. 'dmy_dot' => 'd.m.Y',
  204. 'ymd_slash' => 'Y/m/d',
  205. 'ymd_dash' => 'Y-m-d',
  206. 'ymd_dot' => 'Y.m.d',
  207. 'dMd' => 'd/M/Y',
  208. 'dMd_dash' => 'd-M-Y',
  209. 'fjy' => 'F j, Y',
  210. 'fjsy' => 'F jS, Y'
  211. );
  212. $format = $date_format[ pods_var( 'date_format', $options, 'ymd_dash', null, true ) ];
  213. return $format;
  214. }
  215. /**
  216. * @param $format
  217. * @param $date
  218. *
  219. * @return DateTime
  220. */
  221. public function createFromFormat ( $format, $date ) {
  222. if ( method_exists( 'DateTime', 'createFromFormat' ) )
  223. return DateTime::createFromFormat( $format, (string) $date );
  224. return new DateTime( date_i18n( 'Y-m-d', strtotime( (string) $date ) ) );
  225. }
  226. /**
  227. * Convert a date from one format to another
  228. *
  229. * @param $value
  230. * @param $new_format
  231. * @param string $original_format
  232. *
  233. * @return string
  234. */
  235. public function convert_date ( $value, $new_format, $original_format = 'Y-m-d' ) {
  236. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
  237. $date = $this->createFromFormat( $original_format, (string) $value );
  238. if ( false !== $date )
  239. $value = $date->format( $new_format );
  240. else
  241. $value = date_i18n( $new_format, strtotime( (string) $value ) );
  242. }
  243. else
  244. $value = date_i18n( $new_format );
  245. return $value;
  246. }
  247. }