PodsField_Time

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

Defined (1)

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

/classes/fields/time.php  
  1. class PodsField_Time 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 = 'time'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = '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 . '_type' => array( 
  55. 'label' => __( 'Time Format Type', 'pods' ),  
  56. 'default' => '12',  
  57. 'type' => 'pick',  
  58. 'data' => array( 
  59. '12' => __( '12 hour', 'pods' ),  
  60. '24' => __( '24 hour', 'pods' ) 
  61. ),  
  62. 'dependency' => true 
  63. ),  
  64. self::$type . '_format' => array( 
  65. 'label' => __( 'Time Format', 'pods' ),  
  66. 'depends-on' => array( self::$type . '_type' => '12' ),  
  67. 'default' => 'h_mma',  
  68. 'type' => 'pick',  
  69. 'data' => array( 
  70. 'h_mm_A' => date_i18n( 'g:i A' ),  
  71. 'h_mm_ss_A' => date_i18n( 'g:i:s A' ),  
  72. 'hh_mm_A' => date_i18n( 'h:i A' ),  
  73. 'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),  
  74. 'h_mma' => date_i18n( 'g:ia' ),  
  75. 'hh_mma' => date_i18n( 'h:ia' ),  
  76. 'h_mm' => date_i18n( 'g:i' ),  
  77. 'h_mm_ss' => date_i18n( 'g:i:s' ),  
  78. 'hh_mm' => date_i18n( 'h:i' ),  
  79. 'hh_mm_ss' => date_i18n( 'h:i:s' ) 
  80. ),  
  81. self::$type . '_format_24' => array( 
  82. 'label' => __( 'Time Format', 'pods' ),  
  83. 'depends-on' => array( self::$type . '_type' => '24' ),  
  84. 'default' => 'hh_mm',  
  85. 'type' => 'pick',  
  86. 'data' => array( 
  87. 'hh_mm' => date_i18n( 'H:i' ),  
  88. 'hh_mm_ss' => date_i18n( 'H:i:s' ) 
  89. ),  
  90. self::$type . '_allow_empty' => array( 
  91. 'label' => __( 'Allow empty value?', 'pods' ),  
  92. 'default' => 1,  
  93. 'type' => 'boolean' 
  94. ),  
  95. self::$type . '_html5' => array( 
  96. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),  
  97. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),  
  98. 'type' => 'boolean' 
  99. ); 
  100.  
  101. $options[ self::$type . '_type' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ self::$type . '_type' ][ 'default' ] ); 
  102. $options[ self::$type . '_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_options', $options[ self::$type . '_format' ][ 'data' ] ); 
  103. $options[ self::$type . '_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_default', $options[ self::$type . '_format' ][ 'default' ] ); 
  104. $options[ self::$type . '_format_24' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ self::$type . '_format_24' ][ 'data' ] ); 
  105. $options[ self::$type . '_format_24' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ self::$type . '_format_24' ][ 'default' ] ); 
  106.  
  107. return $options; 
  108.  
  109. /** 
  110. * Define the current field's schema for DB table storage 
  111. * @param array $options 
  112. * @return array 
  113. * @since 2.0 
  114. */ 
  115. public function schema ( $options = null ) { 
  116. $schema = 'TIME NOT NULL default "00:00:00"'; 
  117.  
  118. return $schema; 
  119.  
  120. /** 
  121. * Change the way the value of the field is displayed with Pods::get 
  122. * @param mixed $value 
  123. * @param string $name 
  124. * @param array $options 
  125. * @param array $pod 
  126. * @param int $id 
  127. * @return mixed|null|string 
  128. * @since 2.0 
  129. */ 
  130. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  131. $format = $this->format( $options ); 
  132.  
  133. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) { 
  134. $date = $this->createFromFormat( 'H:i:s', (string) $value ); 
  135. $date_local = $this->createFromFormat( $format, (string) $value ); 
  136.  
  137. if ( false !== $date ) 
  138. $value = $date->format( $format ); 
  139. elseif ( false !== $date_local ) 
  140. $value = $date_local->format( $format ); 
  141. else 
  142. $value = date_i18n( $format, strtotime( (string) $value ) ); 
  143. elseif ( 0 == pods_var( self::$type . '_allow_empty', $options, 1 ) ) 
  144. $value = date_i18n( $format ); 
  145. else 
  146. $value = ''; 
  147.  
  148. return $value; 
  149.  
  150. /** 
  151. * Customize output of the form field 
  152. * @param string $name 
  153. * @param mixed $value 
  154. * @param array $options 
  155. * @param array $pod 
  156. * @param int $id 
  157. * @since 2.0 
  158. */ 
  159. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  160. $options = (array) $options; 
  161. $form_field_type = PodsForm::$field_type; 
  162.  
  163. if ( is_array( $value ) ) 
  164. $value = implode( ' ', $value ); 
  165.  
  166. // Format Value 
  167. $value = $this->display( $value, $name, $options, null, $pod, $id ); 
  168.  
  169. $field_type = 'time'; 
  170.  
  171. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  172. if ( pods_var( 'read_only', $options, false ) ) { 
  173. $options[ 'readonly' ] = true; 
  174.  
  175. $field_type = 'text'; 
  176. else 
  177. return; 
  178. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  179. $options[ 'readonly' ] = true; 
  180.  
  181. $field_type = 'text'; 
  182.  
  183. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  184.  
  185. /** 
  186. * Change the value or perform actions after validation but before saving to the DB 
  187. * @param mixed $value 
  188. * @param int $id 
  189. * @param string $name 
  190. * @param array $options 
  191. * @param array $fields 
  192. * @param array $pod 
  193. * @param object $params 
  194. * @return mixed|string 
  195. * @since 2.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.  
  200. 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' ) ) ) ) 
  201. $value = $this->convert_date( $value, 'H:i:s', $format ); 
  202. elseif ( 1 == pods_var( self::$type . '_allow_empty', $options, 1 ) ) 
  203. $value = '00:00:00'; 
  204. else 
  205. $value = date_i18n( 'H:i:s' ); 
  206.  
  207. return $value; 
  208.  
  209. /** 
  210. * Customize the Pods UI manage table column output 
  211. * @param int $id 
  212. * @param mixed $value 
  213. * @param string $name 
  214. * @param array $options 
  215. * @param array $fields 
  216. * @param array $pod 
  217. * @return mixed|null|string 
  218. * @since 2.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.  
  223. 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' ) ) ) ) 
  224. $value = false; 
  225.  
  226. return $value; 
  227.  
  228. /** 
  229. * Build date/time format string based on options 
  230. * @param $options 
  231. * @return string 
  232. * @since 2.0 
  233. */ 
  234. public function format ( $options ) { 
  235. $time_format = array( 
  236. 'h_mm_A' => 'g:i A',  
  237. 'h_mm_ss_A' => 'g:i:s A',  
  238. 'hh_mm_A' => 'h:i A',  
  239. 'hh_mm_ss_A' => 'h:i:s A',  
  240. 'h_mma' => 'g:ia',  
  241. 'hh_mma' => 'h:ia',  
  242. 'h_mm' => 'g:i',  
  243. 'h_mm_ss' => 'g:i:s',  
  244. 'hh_mm' => 'h:i',  
  245. 'hh_mm_ss' => 'h:i:s' 
  246. ); 
  247.  
  248. $time_format_24 = array( 
  249. 'hh_mm' => 'H:i',  
  250. 'hh_mm_ss' => 'H:i:s' 
  251. ); 
  252.  
  253. $time_format = apply_filters( 'pods_form_ui_field_time_formats', $time_format ); 
  254. $time_format_24 = apply_filters( 'pods_form_ui_field_time_formats_24', $time_format_24 ); 
  255.  
  256. if ( 12 == pods_var( self::$type . '_type', $options ) ) 
  257. $format = $time_format[ pods_var( self::$type . '_format', $options, 'hh_mm', null, true ) ]; 
  258. else 
  259. $format = $time_format_24[ pods_var( self::$type . '_format_24', $options, 'hh_mm', null, true ) ]; 
  260.  
  261. return $format; 
  262.  
  263. /** 
  264. * @param $format 
  265. * @param $date 
  266. * @return DateTime 
  267. */ 
  268. public function createFromFormat ( $format, $date ) { 
  269. $datetime = false; 
  270.  
  271. if ( method_exists( 'DateTime', 'createFromFormat' ) ) { 
  272. $timezone = get_option( 'timezone_string' ); 
  273.  
  274. if ( empty( $timezone ) ) 
  275. $timezone = timezone_name_from_abbr( '', get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, 0 ); 
  276.  
  277. if ( !empty( $timezone ) ) { 
  278. $datetimezone = new DateTimeZone( $timezone ); 
  279.  
  280. $datetime = DateTime::createFromFormat( $format, (string) $date, $datetimezone ); 
  281.  
  282. if ( false === $datetime ) 
  283. $datetime = new DateTime( date_i18n( 'H:i:s', strtotime( (string) $date ) ) ); 
  284.  
  285. return apply_filters( 'pods_form_ui_field_datetime_formatter', $datetime, $format, $date ); 
  286.  
  287. /** 
  288. * Convert a date from one format to another 
  289. * @param $date 
  290. * @param $new_format 
  291. * @param $original_format 
  292. */ 
  293. public function convert_date ( $value, $new_format, $original_format = 'H:i:s' ) { 
  294. if ( !empty( $value ) && !in_array( $value, array( '0000-00-00', '0000-00-00 00:00:00', '00:00:00' ) ) ) { 
  295. $date = $this->createFromFormat( $original_format, (string) $value ); 
  296.  
  297. if ( false !== $date ) 
  298. $value = $date->format( $new_format ); 
  299. else 
  300. $value = date_i18n( $new_format, strtotime( (string) $value ) ); 
  301. else 
  302. $value = date_i18n( $new_format ); 
  303.  
  304. return $value;