PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/fields/time.php

https://github.com/ElmsPark/pods
PHP | 293 lines | 144 code | 32 blank | 117 comment | 20 complexity | c79144ad09518116e50881264ff45b48 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_Time 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 = 'time';
  20. /**
  21. * Field Type Label
  22. *
  23. * @var string
  24. * @since 2.0.0
  25. */
  26. public static $label = '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. * @since 2.0.0
  46. */
  47. public function options () {
  48. $options = array(
  49. 'time_type' => array(
  50. 'label' => __( 'Time Format Type', 'pods' ),
  51. 'default' => '12',
  52. 'type' => 'pick',
  53. 'data' => array(
  54. '12' => __( '12 hour', 'pods' ),
  55. '24' => __( '24 hour', 'pods' )
  56. ),
  57. 'dependency' => true
  58. ),
  59. 'time_time_format' => array(
  60. 'label' => __( 'Time Format', 'pods' ),
  61. 'depends-on' => array( 'time_time_type' => '12' ),
  62. 'default' => 'h_mma',
  63. 'type' => 'pick',
  64. 'data' => array(
  65. 'h_mm_A' => date_i18n( 'g:i A' ),
  66. 'h_mm_ss_A' => date_i18n( 'g:i:s A' ),
  67. 'hh_mm_A' => date_i18n( 'h:i A' ),
  68. 'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),
  69. 'h_mma' => date_i18n( 'g:ia' ),
  70. 'hh_mma' => date_i18n( 'h:ia' ),
  71. 'h_mm' => date_i18n( 'g:i' ),
  72. 'h_mm_ss' => date_i18n( 'g:i:s' ),
  73. 'hh_mm' => date_i18n( 'h:i' ),
  74. 'hh_mm_ss' => date_i18n( 'h:i:s' )
  75. )
  76. ),
  77. 'time_time_format_24' => array(
  78. 'label' => __( 'Time Format', 'pods' ),
  79. 'depends-on' => array( 'datetime_time_type' => '24' ),
  80. 'default' => 'hh_mm',
  81. 'type' => 'pick',
  82. 'data' => array(
  83. 'hh_mm' => date_i18n( 'H:i' ),
  84. 'hh_mm_ss' => date_i18n( 'H:i:s' )
  85. )
  86. ),
  87. 'time_allow_empty' => array(
  88. 'label' => __( 'Allow empty value?', 'pods' ),
  89. 'default' => 1,
  90. 'type' => 'boolean'
  91. ),
  92. 'time_html5' => array(
  93. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
  94. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
  95. 'type' => 'boolean'
  96. )
  97. );
  98. return $options;
  99. }
  100. /**
  101. * Define the current field's schema for DB table storage
  102. *
  103. * @param array $options
  104. *
  105. * @return array
  106. * @since 2.0.0
  107. */
  108. public function schema ( $options = null ) {
  109. $schema = 'TIME NOT NULL default "00:00:00"';
  110. return $schema;
  111. }
  112. /**
  113. * Change the way the value of the field is displayed with Pods::get
  114. *
  115. * @param mixed $value
  116. * @param string $name
  117. * @param array $options
  118. * @param array $pod
  119. * @param int $id
  120. *
  121. * @return mixed|null|string
  122. * @since 2.0.0
  123. */
  124. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  125. $format = $this->format( $options );
  126. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
  127. $date = $this->createFromFormat( 'H:i:s', (string) $value );
  128. $date_local = $this->createFromFormat( $format, (string) $value );
  129. if ( false !== $date )
  130. $value = $date->format( $format );
  131. elseif ( false !== $date_local )
  132. $value = $date_local->format( $format );
  133. else
  134. $value = date_i18n( $format, strtotime( (string) $value ) );
  135. }
  136. elseif ( 0 == pods_var( 'time_allow_empty', $options, 1 ) )
  137. $value = date_i18n( $format );
  138. else
  139. $value = '';
  140. return $value;
  141. }
  142. /**
  143. * Customize output of the form field
  144. *
  145. * @param string $name
  146. * @param mixed $value
  147. * @param array $options
  148. * @param array $pod
  149. * @param int $id
  150. *
  151. * @since 2.0.0
  152. */
  153. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  154. $options = (array) $options;
  155. $form_field_type = PodsForm::$field_type;
  156. if ( is_array( $value ) )
  157. $value = implode( ' ', $value );
  158. // Format Value
  159. $value = $this->display( $value, $name, $options, null, $pod, $id );
  160. pods_view( PODS_DIR . 'ui/fields/time.php', compact( array_keys( get_defined_vars() ) ) );
  161. }
  162. /**
  163. * Change the value or perform actions after validation but before saving to the DB
  164. *
  165. * @param mixed $value
  166. * @param int $id
  167. * @param string $name
  168. * @param array $options
  169. * @param array $fields
  170. * @param array $pod
  171. * @param object $params
  172. *
  173. * @return mixed|string
  174. * @since 2.0.0
  175. */
  176. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  177. $format = $this->format( $options );
  178. if ( !empty( $value ) && ( 0 == pods_var( 'time_allow_empty', $options, 1 ) || !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
  179. $value = $this->convert_date( $value, 'H:i:s', $format );
  180. elseif ( 1 == pods_var( 'time_allow_empty', $options, 1 ) )
  181. $value = '00:00:00';
  182. else
  183. $value = date_i18n( 'H:i:s' );
  184. return $value;
  185. }
  186. /**
  187. * Customize the Pods UI manage table column output
  188. *
  189. * @param int $id
  190. * @param mixed $value
  191. * @param string $name
  192. * @param array $options
  193. * @param array $fields
  194. * @param array $pod
  195. *
  196. * @since 2.0.0
  197. */
  198. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
  199. $value = $this->display( $value, $name, $options, $pod, $id );
  200. if ( 1 == pods_var( 'time_allow_empty', $options, 1 ) && ( empty( $value ) || in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) )
  201. $value = false;
  202. return $value;
  203. }
  204. /**
  205. * Build date/time format string based on options
  206. *
  207. * @param $options
  208. *
  209. * @return string
  210. * @since 2.0.0
  211. */
  212. public function format ( $options ) {
  213. $time_format = array(
  214. 'h_mm_A' => 'g:i A',
  215. 'h_mm_ss_A' => 'g:i:s A',
  216. 'hh_mm_A' => 'h:i A',
  217. 'hh_mm_ss_A' => 'h:i:s A',
  218. 'h_mma' => 'g:ia',
  219. 'hh_mma' => 'h:ia',
  220. 'h_mm' => 'g:i',
  221. 'h_mm_ss' => 'g:i:s',
  222. 'hh_mm' => 'h:i',
  223. 'hh_mm_ss' => 'h:i:s'
  224. );
  225. if ( 12 == pods_var( 'time_type', $options ) )
  226. $format = $time_format[ pods_var( 'time_format', $options, 'hh_mm', null, true ) ];
  227. else
  228. $format = str_replace( array( 'h:', 'g:' ), 'H:', $time_format[ pods_var( 'time_format', $options, 'hh_mm', null, true ) ] );
  229. return $format;
  230. }
  231. /**
  232. * @param $format
  233. * @param $date
  234. *
  235. * @return DateTime
  236. */
  237. public function createFromFormat ( $format, $date ) {
  238. if ( method_exists( 'DateTime', 'createFromFormat' ) )
  239. return DateTime::createFromFormat( $format, (string) $date );
  240. return new DateTime( date_i18n( 'H:i:s', strtotime( (string) $date ) ) );
  241. }
  242. /**
  243. * Convert a date from one format to another
  244. *
  245. * @param $date
  246. * @param $new_format
  247. * @param $original_format
  248. */
  249. public function convert_date ( $value, $new_format, $original_format = 'H:i:s' ) {
  250. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) {
  251. $date = $this->createFromFormat( $original_format, (string) $value );
  252. if ( false !== $date )
  253. $value = $date->format( $new_format );
  254. else
  255. $value = date_i18n( $new_format, strtotime( (string) $value ) );
  256. }
  257. else
  258. $value = date_i18n( $new_format );
  259. return $value;
  260. }
  261. }