Datetime_Format::print_fields()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 4
nop 1
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Datetime Formatter Field
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin\Fields;
8
9
use SimpleCalendar\Abstracts\Field;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Datetime formatter field.
17
 *
18
 * A special field to choose a format for date and time.
19
 */
20
class Datetime_Format extends Field {
21
22
	/**
23
	 * Datetime controls.
24
	 *
25
	 * @access public
26
	 * @var string 'date' or 'time'
27
	 */
28
	public $subtype = '';
29
30
	/**
31
	 * Timestamp.
32
	 *
33
	 * @access private
34
	 * @var int
35
	 */
36
	private $timestamp = 0;
37
38
	/**
39
	 * Constructor.
40
	 *
41
	 * @since 3.0.0
42
	 *
43
	 * @param array $field
44
	 */
45
	public function __construct( $field ) {
46
47
		$this->subtype     = isset( $field['subtype'] ) ? $field['subtype'] : '';
48
		$this->type_class  = 'simcal-field-datetime-format simcal-field-' . $this->subtype . '-format-field';
49
		$this->timestamp   = mktime( 13, 10, 00, 1, 1, intval( date( 'Y', time() ) ) + 1 );
50
51
		parent::__construct( $field );
52
53
		if ( empty( $this->value ) ) {
54
			if ( 'date' == $this->subtype ) {
55
				$this->value = 'l, d F Y';
56
			}
57
			if ( 'time' == $this->subtype ) {
58
				$this->value = 'G:i a';
59
			}
60
		}
61
	}
62
63
	/**
64
	 * Output field markup.
65
	 *
66
	 * @since 3.0.0
67
	 */
68
	public function html() {
69
70
		$id     = $this->id     ? ' id="' . $this->id . '" ' : '';
71
		$class  = $this->class  ? ' class="' . $this->class . '" ' : '';
72
		$style  = $this->style  ? ' style="' . $this->style . '" ' : '';
73
		$attr   = $this->attributes;
74
75
		?>
76
		<div <?php echo $id . $class . $style . $attr; ?>>
77
			<?php
78
79
			if ( ! empty( $this->description ) ) {
80
				echo '<p class="description">' . $this->description . '</p>';
81
			}
82
83
			$matches = array_unique( str_split( $this->value ) );
84
85
			if ( 'date' == $this->subtype ) {
86
				$this->print_date( $matches );
87
			}
88
89
			if ( 'time' == $this->subtype ) {
90
				$this->print_time( $matches );
91
			}
92
93
			?>
94
			<input type="hidden"
95
			       name="<?php echo $this->name; ?>"
96
			       value="<?php echo trim( $this->value ); ?>" />
97
			<span>
98
				<em><?php _e( 'Preview', 'google-calendar-events' ); ?>:</em>&nbsp;&nbsp;
99
				<code><?php echo date_i18n( $this->value, $this->timestamp ); ?></code>
100
			</span>
101
		</div>
102
		<?php
103
104
	}
105
106
	/**
107
	 * Print date input.
108
	 *
109
	 * @since  3.0.0
110
	 * @access private
111
	 *
112
	 * @param  array $matches
113
	 */
114
	private function print_date( $matches ) {
115
116
		$date = array( 'weekday' => '', 'divider' => '', 'day' => '', 'month' => '', 'year' => '' );
117
118
		foreach ( $matches as $match ) {
119
			if ( in_array( $match, array( 'D', 'l' ) ) ) {
120
				$this->weekday();
121
				unset( $date['weekday'] );
122
			} elseif ( in_array( $match, array( 'd', 'j' ) ) ) {
123
				$this->day();
124
				unset( $date['day'] );
125
			} elseif ( in_array( $match, array( 'F', 'M', 'm', 'n' ) ) ) {
126
				$this->month();
127
				unset( $date['month'] );
128
			} elseif ( in_array( $match, array( 'y', 'Y' ) ) ) {
129
				$this->year();
130
				unset( $date['year'] );
131 View Code Duplication
			} elseif ( in_array( $match, array( '.', ',', ':', '/', '-' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
132
				$this->divider();
133
				unset( $date['divider'] );
134
			}
135
		}
136
137
		$this->print_fields( $date );
138
	}
139
140
	/**
141
	 * Print time input.
142
	 *
143
	 * @since  3.0.0
144
	 * @access private
145
	 *
146
	 * @param  array $matches
147
	 */
148
	private function print_time( $matches ) {
149
150
		$time = array( 'hours' => '', 'divider' => '', 'minutes' => '', 'meridiem' => '' );
151
152
		foreach ( $matches as $match  ) {
153
			if ( in_array( $match, array( 'h', 'H', 'g', 'G' ) ) ) {
154
				$this->hours();
155
				unset( $time['hours'] );
156
			} elseif ( in_array( $match, array( 'i' ) )  ) {
157
				$this->minutes();
158
				unset( $time['minutes'] );
159
			} elseif ( in_array( $match, array( 'A', 'a' ) ) ) {
160
				$this->meridiem();
161
				unset( $time['meridiem'] );
162 View Code Duplication
			} elseif ( in_array( $match, array( '.', ',', ':', '/', '-' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
163
				$this->divider();
164
				unset( $time['divider'] );
165
			}
166
		}
167
168
		$this->print_fields( $time );
169
	}
170
171
	/**
172
	 * Print input fields.
173
	 *
174
	 * @since  3.0.0
175
	 * @access private
176
	 *
177
	 * @param  $fields
178
	 */
179
	private function print_fields( $fields ) {
180
		if ( ! empty( $fields ) && is_array( $fields ) ) {
181
			foreach ( $fields as $func => $v ) {
182
				if ( method_exists( $this, $func ) ) {
183
					$this->$func();
184
				};
185
			}
186
		}
187
	}
188
189
	/**
190
	 * Print weekday input.
191
	 *
192
	 * @since  3.0.0
193
	 * @access private
194
	 */
195 View Code Duplication
	private function weekday() {
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...
196
197
		?>
198
		<div>
199
			<label for="<?php echo $this->id; ?>-weekday">
200
				<?php _e( 'Weekday', 'google-calendar-events' ); ?>
201
				<select name="" id="<?php echo $this->id; ?>-weekday">
202
					<option value="" data-preview=""></option>
203
					<option value="D" <?php selected( 'D', strpbrk( 'D', $this->value ) ) ?> data-preview="<?php echo date_i18n( 'D', $this->timestamp ); ?>"><?php echo date_i18n( 'D', $this->timestamp ); ?></option>
204
					<option value="l" <?php selected( 'l', strpbrk( 'l', $this->value ) ) ?> data-preview="<?php echo date_i18n( 'l', $this->timestamp ); ?>"><?php echo date_i18n( 'l', $this->timestamp ); ?></option>
205
				</select>
206
			</label>
207
		</div>
208
		<?php
209
210
	}
211
212
	/**
213
	 * Print day input.
214
	 *
215
	 * @since  3.0.0
216
	 * @access private
217
	 */
218 View Code Duplication
	private function day() {
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...
219
220
		?>
221
		<div>
222
			<label for="<?php echo $this->id; ?>-day">
223
				<?php _e( 'Day', 'google-calendar-events' ); ?>
224
				<select name="" id="<?php echo $this->id; ?>-day">
225
					<option value="" data-preview=""></option>
226
					<option value="j" <?php selected( 'j', strpbrk( 'j', $this->value ) ) ?> data-preview="<?php echo date( 'j', $this->timestamp ); ?>"><?php echo date( 'j', $this->timestamp ); ?></option>
227
					<option value="d" <?php selected( 'd', strpbrk( 'd', $this->value ) ) ?> data-preview="<?php echo date( 'd', $this->timestamp ); ?>"><?php echo date( 'd', $this->timestamp ); ?></option>
228
				</select>
229
			</label>
230
		</div>
231
		<?php
232
233
	}
234
235
	/**
236
	 * Print month input.
237
	 *
238
	 * @since  3.0.0
239
	 * @access private
240
	 */
241
	private function month() {
242
243
		?>
244
		<div>
245
			<label for="<?php echo $this->id; ?>-month">
246
				<?php _e( 'Month', 'google-calendar-events' ); ?>
247
				<select name="" id="<?php echo $this->id; ?>-month">
248
					<option value="" data-preview=""></option>
249
					<option value="F" <?php selected( 'F', strpbrk( 'F', $this->value ) ) ?> data-preview="<?php echo date_i18n( 'F', $this->timestamp ); ?>"><?php echo date_i18n( 'F', $this->timestamp ); ?></option>
250
					<option value="M" <?php selected( 'M', strpbrk( 'M', $this->value ) ) ?> data-preview="<?php echo date_i18n( 'M', $this->timestamp ); ?>"><?php echo date_i18n( 'M', $this->timestamp ); ?></option>
251
					<option value="m" <?php selected( 'm', strpbrk( 'm', $this->value ) ) ?> data-preview="<?php echo date( 'm', $this->timestamp ); ?>"><?php echo date( 'm', $this->timestamp ); ?></option>
252
					<option value="n" <?php selected( 'n', strpbrk( 'n', $this->value ) ) ?> data-preview="<?php echo date( 'n', $this->timestamp ); ?>"><?php echo date( 'n', $this->timestamp ); ?></option>
253
				</select>
254
			</label>
255
		</div>
256
		<?php
257
258
	}
259
260
	/**
261
	 * Print year input.
262
	 *
263
	 * @since  3.0.0
264
	 * @access private
265
	 */
266 View Code Duplication
	private function year() {
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...
267
268
		?>
269
		<div>
270
			<label for="<?php echo $this->id; ?>-year">
271
				<?php _e( 'Year', 'google-calendar-events' ); ?>
272
				<select name="" id="<?php echo $this->id; ?>-year">
273
					<option value="" data-preview=""></option>
274
					<option value="Y" <?php selected( 'Y', strpbrk( 'Y', $this->value ) ) ?> data-preview="<?php echo date( 'Y', $this->timestamp ); ?>"><?php echo date( 'Y', $this->timestamp ); ?></option>
275
					<option value="y" <?php selected( 'y', strpbrk( 'y', $this->value ) ) ?> data-preview="<?php echo date( 'y', $this->timestamp ); ?>"><?php echo date( 'y', $this->timestamp ); ?></option>
276
				</select>
277
			</label>
278
		</div>
279
		<?php
280
281
	}
282
283
	/**
284
	 * Print hours input.
285
	 *
286
	 * @since  3.0.0
287
	 * @access private
288
	 */
289
	private function hours() {
290
291
		?>
292
		<div>
293
			<label for="<?php echo $this->id; ?>-hours">
294
				<?php _e( 'Hours', 'google-calendar-events' ) ?>
295
				<select name="" id="<?php echo $this->id; ?>-hours">
296
					<option value="" data-preview=""></option>
297
					<option value="g" <?php selected( 'g', strpbrk( 'g', $this->value ) ); ?> data-preview="<?php echo date( 'g', $this->timestamp ); ?>"><?php echo date( 'g', $this->timestamp ) . ' (12h)'; ?></option>
298
					<option value="G" <?php selected( 'G', strpbrk( 'G', $this->value ) ); ?> data-preview="<?php echo date( 'G', $this->timestamp - 43200 ); ?>"><?php echo date( 'G', $this->timestamp - 43200 ) . ' (24h)'; ?></option>
299
					<option value="h" <?php selected( 'h', strpbrk( 'h', $this->value ) ); ?> data-preview="<?php echo date( 'h', $this->timestamp ); ?>"><?php echo date( 'h', $this->timestamp ) . ' (12h)'; ?></option>
300
					<option value="H" <?php selected( 'H', strpbrk( 'H', $this->value ) ); ?> data-preview="<?php echo date( 'H', $this->timestamp - 43200 ); ?>"><?php echo date( 'H', $this->timestamp - 43200 ) . ' (24h)'; ?></option>
301
				</select>
302
			</label>
303
		</div>
304
		<?php
305
306
	}
307
308
	/**
309
	 * Print minutes input.
310
	 *
311
	 * @since  3.0.0
312
	 * @access private
313
	 */
314
	private function minutes() {
315
316
		?>
317
		<div>
318
			<label for="<?php echo $this->id; ?>-minutes">
319
				<?php _e( 'Minutes', 'google-calendar-events' ); ?>
320
				<select name="" id="<?php echo $this->id; ?>-minutes">
321
					<option value="" data-preview=""></option>
322
					<option value="i" <?php selected( 'i', strpbrk( 'i', $this->value ) ); ?> data-preview="<?php echo date( 'i', $this->timestamp ); ?>"><?php echo date( 'i', $this->timestamp ); ?></option>
323
				</select>
324
			</label>
325
		</div>
326
		<?php
327
328
	}
329
330
	/**
331
	 * Print meridiem input.
332
	 *
333
	 * @since  3.0.0
334
	 * @access private
335
	 */
336 View Code Duplication
	private function meridiem() {
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...
337
338
	    ?>
339
		<div>
340
			<label for="<?php echo $this->id; ?>-meridiem">
341
				<?php _e( 'Meridiem', 'google-calendar-events' ); ?>
342
				<select name="" id="<?php echo $this->id; ?>-meridiem">
343
					<option value="" data-preview=""></option>
344
					<option value="a" <?php selected( 'a', strpbrk( 'a', $this->value ) ); ?> data-preview="<?php echo date( 'a', $this->timestamp ); ?>"><?php echo date( 'a', $this->timestamp ); ?></option>
345
					<option value="A" <?php selected( 'A', strpbrk( 'A', $this->value ) ); ?> data-preview="<?php echo date( 'A', $this->timestamp ); ?>"><?php echo date( 'A', $this->timestamp ); ?></option>
346
				</select>
347
			</label>
348
		</div>
349
		<?php
350
351
	}
352
353
	/**
354
	 * Print divider input.
355
	 *
356
	 * @since  3.0.0
357
	 * @access private
358
	 */
359
	private function divider() {
360
361
		?>
362
		<div>
363
			<label for="<?php echo $this->id; ?>-divider">
364
				<?php _ex( 'Divider', 'A character to separate two elements', 'google-calendar-events' ); ?>
365
				<select name="" id="<?php echo $this->id; ?>-divider">
366
					<option value="" data-preview=""></option>
367
					<option value="."  <?php selected( '.', strpbrk( '.', $this->value ) ); ?> data-preview="."  data-trim="true">.</option>
368
					<option value=", " <?php selected( ',', strpbrk( ',', $this->value ) ); ?> data-preview=", " data-trim="true">,</option>
369
					<option value=":"  <?php selected( ':', strpbrk( ':', $this->value ) ); ?> data-preview=":"  data-trim="true">:</option>
370
					<option value="-"  <?php selected( '-', strpbrk( '-', $this->value ) ); ?> data-preview="-"  data-trim="true">-</option>
371
					<option value="/"  <?php selected( '/', strpbrk( '/', $this->value ) ); ?> data-preview="/"  data-trim="true">/</option>
372
				</select>
373
			</label>
374
		</div>
375
		<?php
376
377
	}
378
379
}
380