Completed
Push — master ( 2cbc74...e4e1e7 )
by Marin
02:28
created

Time_Field::get_restraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php 
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Time picker field class.
7
 */
8
class Time_Field extends Field {
9
	/**
10
	 * Timepicker type.
11
	 * 
12
	 * @var string
13
	 */
14
	protected $timepicker_type = 'timepicker';
15
16
	/**
17
	 * Time format.
18
	 *
19
	 * @var string
20
	 */
21
	public $time_format = 'hh:mm tt';
22
23
	/**
24
	 * Interval step for hours, minutes and seconds.
25
	 *
26
	 * @var array
27
	 */
28
	public $interval_step = array();
29
30
	/**
31
	 * Restraints for hours, minutes, seconds and dates.
32
	 *
33
	 * @var array
34
	 */
35
	public $restraints = array();
36
37
	/**
38
	 * Timepicker options.
39
	 *
40
	 * @var array
41
	 */
42
	public $timepicker_options = array(
43
		'dateFormat' => 'yy-mm-dd',
44
		'timeFormat' => 'HH:mm:ss',
45
	);
46
47
	/**
48
	 * You can use this method to modify the field properties that are added to the JSON object.
49
	 * The JSON object is used by the Backbone Model and the Underscore template.
50
	 *
51
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
52
	 * @return array
53
	 */
54
	public function to_json( $load ) {
55
		$field_data = parent::to_json( $load );
56
57
		$field_data = array_merge( $field_data, array(
58
			'timepicker_type' => $this->timepicker_type,
59
			'time_format' => $this->get_time_format(),
60
			'interval_step' => $this->get_interval_step(),
61
			'restraints' => $this->get_restraints(),
62
			'timepicker_options' => $this->get_timepicker_options(),
63
		) );
64
65
		return $field_data;
66
	}
67
68
	/**
69
	 * Prints the main Underscore template.
70
	 **/
71
	public function template() {
72
		?>
73
		<div class="input-with-button">
74
			<input id="{{{ id }}}" type="text" name="{{{ name }}}" value="{{ value }}" class="regular-text carbon-timepicker" />
75
			<span class="carbon-timepicker-trigger button icon-button dashicons-clock hide-if-no-js"><?php _e( 'Timepicker', 'crb' ); ?></span>
76
		</div>
77
		<?php
78
	}
79
80
	/**
81
	 * This method is called in the admin_enqueue_scripts action. It is called once per field type.
82
	 * Enqueues field scripts and styles.
83
	 */
84
	public function admin_enqueue_scripts() {
85
		# Enqueue JS
86
		wp_enqueue_script( 'carbon-jquery-timepicker', \Carbon_Fields\URL . '/assets/js/lib/jquery-ui-timepicker.js', array( 'jquery-ui-datepicker', 'jquery-ui-slider' ) );
87
88
		# Enqueue CSS
89
		wp_enqueue_style( 'jquery-ui', '//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.min.css' );
90
		wp_enqueue_style( 'carbon-jquery-ui', \Carbon_Fields\URL . '/assets/css/jquery-ui.css' );
91
	}
92
93
	/**
94
	 * Sets the time format.
95
	 */
96
	public function set_time_format( $time_format ) {
97
		$this->time_format = $time_format;
98
99
		return $this;
100
	}
101
102
	/**
103
	 * Returns the time format.
104
	 */
105
	public function get_time_format() {
106
		return $this->time_format;
107
	}
108
109
	/**
110
	 * Sets the interval step.
111
	 */
112
	public function set_interval_step( $interval_steps ) {
113
		$output = array();
114
115
		foreach ( $interval_steps as $step_name => $step_value ) {
116
			$name = 'step' . ucwords( $step_name );
117
			$output[ $name ] = (int) $step_value;
118
		}
119
120
		$this->interval_step = $output;
121
122
		return $this;
123
	}
124
125
	/**
126
	 * Returns the interval step.
127
	 */
128
	public function get_interval_step() {
129
		return $this->interval_step;
130
	}
131
132
	/**
133
	 * Sets the restraints.
134
	 */
135
	public function set_restraints( $restraints ) {
136
		$this->restraints = array_map( 'intval', $restraints );
137
138
		return $this;
139
	}
140
141
	/**
142
	 * Returns the restraints.
143
	 */
144
	public function get_restraints() {
145
		return $this->restraints;
146
	}
147
148
	/**
149
	 * Sets other timepicker options.
150
	 */
151
	public function set_timepicker_options( $timepicker_options ) {
152
		$this->timepicker_options = $timepicker_options;
153
154
		return $this;
155
	}
156
157
	/**
158
	 * Returns the timepicker options.
159
	 */
160
	public function get_timepicker_options() {
161
		return $this->timepicker_options;
162
	}
163
}
164