Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
04:56
created

PodsField_Time::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once PODS_DIR . 'classes/fields/datetime.php';
3
4
/**
5
 * @package Pods\Fields
6
 */
7
class PodsField_Time extends PodsField_DateTime {
8
9
	/**
10
	 * {@inheritdoc}
11
	 */
12
	public static $group = 'Date / Time';
13
14
	/**
15
	 * {@inheritdoc}
16
	 */
17
	public static $type = 'time';
18
19
	/**
20
	 * {@inheritdoc}
21
	 */
22
	public static $label = 'Time';
23
24
	/**
25
	 * {@inheritdoc}
26
	 */
27
	public static $prepare = '%s';
28
29
	/**
30
	 * Storage format.
31
	 *
32
	 * @var string
33
	 * @since 2.7
34
	 */
35
	public static $storage_format = 'H:i:s';
36
37
	/**
38
	 * The default empty value (database)
39
	 *
40
	 * @var string
41
	 * @since 2.7
42
	 */
43
	public static $empty_value = '00:00:00';
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48
	public function setup() {
49
50
		static::$label = __( 'Time', 'pods' );
51
	}
52
53
	/**
54
	 * {@inheritdoc}
55
	 */
56
	public function options() {
57
58
		$options = array(
59
			static::$type . '_repeatable'       => array(
60
				'label'             => __( 'Repeatable Field', 'pods' ),
61
				'default'           => 0,
62
				'type'              => 'boolean',
63
				'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' ),
64
				'boolean_yes_label' => '',
65
				'dependency'        => true,
66
				'developer_mode'    => true,
67
			),
68
			static::$type . '_type'             => array(
69
				'label'                        => __( 'Time Format Type', 'pods' ),
70
				'default'                      => '12',
71
				// Backwards compatibility
72
										'type' => 'pick',
73
				'help'                         => __( 'WordPress Default is the format used in Settings, General under "Time Format".', 'pods' ) . '<br>' . __( '12/24 hour will allow you to select from a list of commonly used time formats.', 'pods' ) . '<br>' . __( 'Custom will allow you to enter your own using PHP Date/Time Strings.', 'pods' ),
74
				'data'                         => array(
75
					'wp'     => __( 'WordPress default', 'pods' ) . ': ' . date_i18n( get_option( 'time_format' ) ),
76
					'12'     => __( '12 hour', 'pods' ),
77
					'24'     => __( '24 hour', 'pods' ),
78
					'custom' => __( 'Custom format', 'pods' ),
79
				),
80
				'dependency'                   => true,
81
			),
82
			static::$type . '_format_custom'    => array(
83
				'label'      => __( 'Time format for display', 'pods' ),
84
				'depends-on' => array( static::$type . '_type' => 'custom' ),
85
				'default'    => '',
86
				'type'       => 'text',
87
				'help'       => sprintf(
88
					'<a href="http://php.net/manual/function.date.php" target="_blank">%s</a>',
89
					esc_html__( 'PHP date documentation', 'pods' )
90
				),
91
			),
92
			static::$type . '_format_custom_js' => array(
93
				'label'      => __( 'Time format for input', 'pods' ),
94
				'depends-on' => array( static::$type . '_type' => 'custom' ),
95
				'default'    => '',
96
				'type'       => 'text',
97
				'help'       => sprintf(
98
					'<a href="http://trentrichardson.com/examples/timepicker/#tp-formatting" target="_blank">%1$s</a><br />%2$s',
99
					esc_html__( 'jQuery UI timepicker documentation', 'pods' ),
100
					esc_html__( 'Leave empty to auto-generate from PHP format.', 'pods' )
101
				),
102
			),
103
			static::$type . '_format'           => array(
104
				'label'      => __( 'Time Format', 'pods' ),
105
				'depends-on' => array( static::$type . '_type' => '12' ),
106
				'default'    => 'h_mma',
107
				'type'       => 'pick',
108
				'data'       => array(
109
					'h_mm_A'     => date_i18n( 'g:i A' ),
110
					'h_mm_ss_A'  => date_i18n( 'g:i:s A' ),
111
					'hh_mm_A'    => date_i18n( 'h:i A' ),
112
					'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),
113
					'h_mma'      => date_i18n( 'g:ia' ),
114
					'hh_mma'     => date_i18n( 'h:ia' ),
115
					'h_mm'       => date_i18n( 'g:i' ),
116
					'h_mm_ss'    => date_i18n( 'g:i:s' ),
117
					'hh_mm'      => date_i18n( 'h:i' ),
118
					'hh_mm_ss'   => date_i18n( 'h:i:s' ),
119
				),
120
				'dependency' => true,
121
			),
122
			static::$type . '_format_24'        => array(
123
				'label'      => __( 'Time Format', 'pods' ),
124
				'depends-on' => array( static::$type . '_type' => '24' ),
125
				'default'    => 'hh_mm',
126
				'type'       => 'pick',
127
				'data'       => array(
128
					'hh_mm'    => date_i18n( 'H:i' ),
129
					'hh_mm_ss' => date_i18n( 'H:i:s' ),
130
				),
131
			),
132
			static::$type . '_allow_empty'      => array(
133
				'label'   => __( 'Allow empty value?', 'pods' ),
134
				'default' => 1,
135
				'type'    => 'boolean',
136
			),
137
			static::$type . '_html5'            => array(
138
				'label'   => __( 'Enable HTML5 Input Field?', 'pods' ),
139
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
140
				'type'    => 'boolean',
141
			),
142
		);
143
144
		$options[ static::$type . '_type' ]['default']      = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ static::$type . '_type' ]['default'] );
145
		$options[ static::$type . '_format' ]['data']       = apply_filters( 'pods_form_ui_field_time_format_options', $options[ static::$type . '_format' ]['data'] );
146
		$options[ static::$type . '_format' ]['default']    = apply_filters( 'pods_form_ui_field_time_format_default', $options[ static::$type . '_format' ]['default'] );
147
		$options[ static::$type . '_format_24' ]['data']    = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ static::$type . '_format_24' ]['data'] );
148
		$options[ static::$type . '_format_24' ]['default'] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ static::$type . '_format_24' ]['default'] );
149
150
		return $options;
151
	}
152
153
	/**
154
	 * {@inheritdoc}
155
	 */
156
	public function schema( $options = null ) {
157
158
		$schema = 'TIME NOT NULL default "00:00:00"';
159
160
		return $schema;
161
	}
162
163
	/**
164
	 * {@inheritdoc}
165
	 */
166
	public function format( $options, $js = false ) {
167
168
		return $this->format_time( $options, $js );
169
	}
170
171
	/**
172
	 * Build time format string based on options
173
	 *
174
	 * @param array $options Field options.
175
	 * @param bool  $js      Return formatted from jQuery UI format? (only for custom formats).
176
	 *
177
	 * @return string
178
	 * @since  2.7
179
	 */
180 View Code Duplication
	public function format_time( $options, $js = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
182
		switch ( (string) pods_v( static::$type . '_type', $options, '12', true ) ) {
183
			case '12':
184
				$time_format = $this->get_time_formats( $js );
185
				$format      = $time_format[ pods_v( static::$type . '_format', $options, 'hh_mm', true ) ];
186
				break;
187
			case '24':
188
				$time_format_24 = $this->get_time_formats_24( $js );
189
				$format         = $time_format_24[ pods_v( static::$type . '_format_24', $options, 'hh_mm', true ) ];
190
				break;
191
			case 'custom':
192
				if ( ! $js ) {
193
					$format = pods_v( static::$type . '_format_custom', $options, '' );
194
				} else {
195
					$format = pods_v( static::$type . '_format_custom_js', $options, '' );
196
					if ( empty( $format ) ) {
197
						$format = pods_v( static::$type . '_format_custom', $options, '' );
198
						$format = $this->convert_format( $format, array( 'source' => 'php' ) );
199
					}
200
				}
201
				break;
202
			default:
203
				$format = get_option( 'time_format' );
204
				if ( $js ) {
205
					$format = $this->convert_format( $format, array( 'source' => 'php' ) );
206
				}
207
				break;
208
		}//end switch
209
210
		return $format;
211
	}
212
}
213