Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

PodsField_Time::display()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 5
nop 5
dl 0
loc 21
rs 8.7624
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 6 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
 * @package Pods\Fields
5
 */
6
class PodsField_Time extends PodsField_DateTime {
7
8
	/**
9
	 * Field Type Group
10
	 *
11
	 * @var string
12
	 * @since 2.0
13
	 */
14
	public static $group = 'Date / Time';
15
16
	/**
17
	 * Field Type Identifier
18
	 *
19
	 * @var string
20
	 * @since 2.0
21
	 */
22
	public static $type = 'time';
23
24
	/**
25
	 * Field Type Label
26
	 *
27
	 * @var string
28
	 * @since 2.0
29
	 */
30
	public static $label = 'Time';
31
32
	/**
33
	 * Field Type Preparation
34
	 *
35
	 * @var string
36
	 * @since 2.0
37
	 */
38
	public static $prepare = '%s';
39
40
	/**
41
	 * Storage format.
42
	 *
43
	 * @var string
44
	 * @since 2.7
45
	 */
46
	public static $storage_format = 'H:i:s';
47
48
	/**
49
	 * The default empty value (database)
50
	 *
51
	 * @var string
52
	 * @since 2.7
53
	 */
54
	public static $empty_value = '00:00:00';
55
56
	/**
57
	 * Do things like register/enqueue scripts and stylesheets
58
	 *
59
	 * @since 2.0
60
	 */
61
	public function __construct () {
62
		static::$label = __( 'Time', 'pods' );
63
	}
64
65
	/**
66
	 * Add options and set defaults to
67
	 *
68
	 * @return array
69
	 * @since 2.0
70
	 */
71
	public function options () {
72
		$options = array(
73
			static::$type . '_repeatable' => array(
74
				'label' => __( 'Repeatable Field', 'pods' ),
75
				'default' => 0,
76
				'type' => 'boolean',
77
				'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' ),
78
				'boolean_yes_label' => '',
79
				'dependency' => true,
80
				'developer_mode' => true
81
			),
82
			static::$type . '_type' => array(
83
				'label' => __( 'Time Format Type', 'pods' ),
84
				'default' => '12', // Backwards compatibility
85
				'type' => 'pick',
86
				'help' => __( 'WordPress Default is the format used in Settings, General under "Time Format".', 'pods' ) . '<br>'
87
						  . __( '12/24 hour will allow you to select from a list of commonly used time formats.', 'pods' ) . '<br>'
88
						  . __( 'Custom will allow you to enter your own using PHP Date/Time Strings.', 'pods' ),
89
				'data' => array(
90
					'wp' => __( 'WordPress default', 'pods' ) . ': ' . date_i18n( get_option( 'time_format' ) ),
91
					'12' => __( '12 hour', 'pods' ),
92
					'24' => __( '24 hour', 'pods' ),
93
					'custom' => __( 'Custom format', 'pods' ),
94
				),
95
				'dependency' => true
96
			),
97
			static::$type . '_format_custom' => array(
98
				'label' => __( 'Time format for display', 'pods' ),
99
				'depends-on' => array( static::$type . '_type' => 'custom' ),
100
				'default' => '',
101
				'type' => 'text',
102
				'help' => '<a href="http://php.net/manual/function.date.php" target="_blank">' . __( 'PHP date documentation', 'pods' ) . '</a>',
103
			),
104
			static::$type . '_format_custom_js' => array(
105
				'label' => __( 'Time format for input', 'pods' ),
106
				'depends-on' => array( static::$type . '_type' => 'custom' ),
107
				'default' => '',
108
				'type' => 'text',
109
				'help' => '<a href="http://trentrichardson.com/examples/timepicker/#tp-formatting" target="_blank">' . __( 'jQuery UI timepicker documentation', 'pods' ) . '</a>'
110
						  . '<br>' . __( 'Leave empty to auto-generate from PHP format.', 'pods' ),
111
			),
112
			static::$type . '_format' => array(
113
				'label' => __( 'Time Format', 'pods' ),
114
				'depends-on' => array( static::$type . '_type' => '12' ),
115
				'default' => 'h_mma',
116
				'type' => 'pick',
117
				'data' => array(
118
					'h_mm_A' => date_i18n( 'g:i A' ),
119
					'h_mm_ss_A' => date_i18n( 'g:i:s A' ),
120
					'hh_mm_A' => date_i18n( 'h:i A' ),
121
					'hh_mm_ss_A' => date_i18n( 'h:i:s A' ),
122
					'h_mma' => date_i18n( 'g:ia' ),
123
					'hh_mma' => date_i18n( 'h:ia' ),
124
					'h_mm' => date_i18n( 'g:i' ),
125
					'h_mm_ss' => date_i18n( 'g:i:s' ),
126
					'hh_mm' => date_i18n( 'h:i' ),
127
					'hh_mm_ss' => date_i18n( 'h:i:s' )
128
				),
129
				'dependency' => true
130
			),
131
			static::$type . '_format_24' => array(
132
				'label' => __( 'Time Format', 'pods' ),
133
				'depends-on' => array( static::$type . '_type' => '24' ),
134
				'default' => 'hh_mm',
135
				'type' => 'pick',
136
				'data' => array(
137
					'hh_mm' => date_i18n( 'H:i' ),
138
					'hh_mm_ss' => date_i18n( 'H:i:s' )
139
				)
140
			),
141
			static::$type . '_allow_empty' => array(
142
				'label' => __( 'Allow empty value?', 'pods' ),
143
				'default' => 1,
144
				'type' => 'boolean'
145
			),
146
			static::$type . '_html5' => array(
147
				'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
148
				'default' => apply_filters( 'pods_form_ui_field_html5', 0, static::$type ),
149
				'type' => 'boolean'
150
			)
151
		);
152
153
		$options[ static::$type . '_type' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_type_default', $options[ static::$type . '_type' ][ 'default' ] );
154
		$options[ static::$type . '_format' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_options', $options[ static::$type . '_format' ][ 'data' ] );
155
		$options[ static::$type . '_format' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_default', $options[ static::$type . '_format' ][ 'default' ] );
156
		$options[ static::$type . '_format_24' ][ 'data' ] = apply_filters( 'pods_form_ui_field_time_format_24_options', $options[ static::$type . '_format_24' ][ 'data' ] );
157
		$options[ static::$type . '_format_24' ][ 'default' ] = apply_filters( 'pods_form_ui_field_time_format_24_default', $options[ static::$type . '_format_24' ][ 'default' ] );
158
159
		return $options;
160
	}
161
162
	/**
163
	 * Define the current field's schema for DB table storage
164
	 *
165
	 * @param array $options
166
	 *
167
	 * @return string
168
	 * @since 2.0
169
	 */
170
	public function schema ( $options = null ) {
171
		$schema = 'TIME NOT NULL default "00:00:00"';
172
173
		return $schema;
174
	}
175
176
	/**
177
	 * Build date/time format string based on options
178
	 *
179
	 * @param array $options
180
	 * @param bool  $js       Return format for jQuery UI?
181
	 *
182
	 * @return string
183
	 * @since 2.0
184
	 */
185
	public function format ( $options, $js = false ) {
186
		return $this->format_time( $options, $js );
187
	}
188
189
	/**
190
	 * Build time format string based on options
191
	 *
192
	 * @since  2.7
193
	 *
194
	 * @param  array $options
195
	 * @param  bool  $js       Return format for jQuery UI?
196
	 * @return string
197
	 */
198 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...
199
200
		switch ( (string) pods_v( static::$type . '_type', $options, '12', true ) ) {
201
			case '12':
202
				$time_format = $this->get_time_formats( $js );
203
				$format = $time_format[ pods_v( static::$type . '_format', $options, 'hh_mm', true ) ];
204
			break;
205
			case '24':
206
				$time_format_24 = $this->get_time_formats_24( $js );
207
				$format = $time_format_24[ pods_v( static::$type . '_format_24', $options, 'hh_mm', true ) ];
208
			break;
209
			case 'custom':
210
				if ( ! $js ) {
211
					$format = pods_v( static::$type . '_format_custom', $options, '' );
212
				} else {
213
					$format = pods_v( static::$type . '_format_custom_js', $options, '' );
214
					if ( empty( $format ) ) {
215
						$format = pods_v( static::$type . '_format_custom', $options, '' );
216
						$format = $this->convert_format( $format, array( 'source' => 'php' ) );
217
					}
218
				}
219
			break;
220
			default:
221
				$format = get_option( 'time_format' );
222
				if ( $js ) {
223
					$format = $this->convert_format( $format, array( 'source' => 'php' ) );
224
				}
225
			break;
226
		}
227
228
		return $format;
229
	}
230
}
231