1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\RangeField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Convert; |
6
|
|
|
use SilverStripe\Forms\FormField; |
7
|
|
|
use SilverStripe\View\Requirements; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RangeField |
11
|
|
|
* |
12
|
|
|
* A rangefield gives the user the option to select a value from a range, or set a range |
13
|
|
|
* @todo support for multiple handles, it seems not to work |
14
|
|
|
* @package Firesphere\Rangefield\Forms |
15
|
|
|
*/ |
16
|
|
|
class RangeField extends FormField |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array|int |
21
|
|
|
*/ |
22
|
|
|
protected $start = [0]; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $min = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $max = 100; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $range = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $snap = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $data = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $density = 4; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
protected $showPips = true; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int|bool |
62
|
|
|
*/ |
63
|
|
|
protected $step; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* RangeField constructor. |
67
|
|
|
* @param string $name The internal field name, passed to forms. |
68
|
|
|
* @param null|string $title The human-readable field label. |
69
|
|
|
* @param int|array $start Starting point(s) on the line |
70
|
|
|
* @param mixed $value The value of the field. |
71
|
|
|
* @param int|array $min Lowest value of the range |
72
|
|
|
* @param int $max Highest value of the range |
73
|
|
|
* @param array $range Associative array with keys which determine the percentage point on the range |
74
|
|
|
* And values being the labels on the field |
75
|
|
|
*/ |
76
|
|
|
public function __construct($name, $title = null, $start = 0, $min = 0, $max = 100, $range = [], $value = null) |
77
|
|
|
{ |
78
|
|
|
if (!is_array($start)) { |
79
|
|
|
$start = [$start]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->start = $start; |
83
|
|
|
$this->min = $min; |
84
|
|
|
$this->max = $max; |
85
|
|
|
$this->range = $range; |
86
|
|
|
|
87
|
|
|
$this->setInputType('hidden'); |
88
|
|
|
|
89
|
|
|
parent::__construct($name, $title, $value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $properties |
94
|
|
|
* @return \SilverStripe\ORM\FieldType\DBHTMLText |
95
|
|
|
*/ |
96
|
|
|
public function field($properties = array()) |
97
|
|
|
{ |
98
|
|
|
Requirements::set_force_js_to_bottom(true); |
99
|
|
|
Requirements::javascript('firesphere/rangefield:client/dist/main.js'); |
100
|
|
|
Requirements::css('firesphere/rangefield:client/dist/main.css'); |
101
|
|
|
|
102
|
|
|
$data = [ |
103
|
|
|
'start' => $this->start, |
104
|
|
|
'snap' => $this->snap, |
105
|
|
|
'animate' => true, |
106
|
|
|
'animationDuration' => 300, |
107
|
|
|
'range' => [ |
108
|
|
|
'min' => $this->min, |
109
|
|
|
'max' => $this->max |
110
|
|
|
] |
111
|
|
|
]; |
112
|
|
|
if ($this->showPips) { |
113
|
|
|
$data['pips'] = [ // Show a scale with the slider |
114
|
|
|
'mode' => 'steps', |
115
|
|
|
'stepped' => true, |
116
|
|
|
'density' => $this->density |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($this->getStep()) { |
121
|
|
|
$data['step'] = $this->getStep(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (count($this->range)) { // Update the range if we've gotten a forced range |
125
|
|
|
$data['range'] = array_merge($data['range'], $this->range); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->setData($data); |
129
|
|
|
|
130
|
|
|
$field = parent::Field($properties); |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** @todo find a way to get this a bit nicer. It's the only way to get it in without breaking on submit */ |
134
|
|
|
Requirements::insertHeadTags("<script type='text/javascript'> |
135
|
|
|
var $this->name = " . Convert::array2json($this->getData()) . '</script>'); |
136
|
|
|
|
137
|
|
|
return $field; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array|int |
142
|
|
|
*/ |
143
|
|
|
public function getStart() |
144
|
|
|
{ |
145
|
|
|
return $this->start; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array|int $start |
150
|
|
|
*/ |
151
|
|
|
public function setStart($start) |
152
|
|
|
{ |
153
|
|
|
$this->start = (array)$start; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function getMin() |
160
|
|
|
{ |
161
|
|
|
return $this->min; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param int $min |
166
|
|
|
*/ |
167
|
|
|
public function setMin($min) |
168
|
|
|
{ |
169
|
|
|
$this->min = $min; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return int |
174
|
|
|
*/ |
175
|
|
|
public function getMax() |
176
|
|
|
{ |
177
|
|
|
return $this->max; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param int $max |
182
|
|
|
*/ |
183
|
|
|
public function setMax($max) |
184
|
|
|
{ |
185
|
|
|
$this->max = $max; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getRange() |
192
|
|
|
{ |
193
|
|
|
return $this->range; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array $range |
198
|
|
|
*/ |
199
|
|
|
public function setRange($range) |
200
|
|
|
{ |
201
|
|
|
$this->range = $range; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function isSnap() |
208
|
|
|
{ |
209
|
|
|
return $this->snap; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param bool $snap |
214
|
|
|
*/ |
215
|
|
|
public function setSnap($snap) |
216
|
|
|
{ |
217
|
|
|
$this->snap = $snap; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
public function getData() |
224
|
|
|
{ |
225
|
|
|
return $this->data; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param array $data |
230
|
|
|
*/ |
231
|
|
|
public function setData($data) |
232
|
|
|
{ |
233
|
|
|
$this->data = $data; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return int |
239
|
|
|
*/ |
240
|
|
|
public function getDensity() |
241
|
|
|
{ |
242
|
|
|
return $this->density; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param int $density |
247
|
|
|
*/ |
248
|
|
|
public function setDensity($density) |
249
|
|
|
{ |
250
|
|
|
$this->density = $density; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
public function isShowPips() |
257
|
|
|
{ |
258
|
|
|
return $this->showPips; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param bool $showPips |
263
|
|
|
*/ |
264
|
|
|
public function setShowPips($showPips) |
265
|
|
|
{ |
266
|
|
|
$this->showPips = $showPips; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return bool|int |
271
|
|
|
*/ |
272
|
|
|
public function getStep() |
273
|
|
|
{ |
274
|
|
|
return $this->step; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param bool|int $step |
279
|
|
|
*/ |
280
|
|
|
public function setStep($step) |
281
|
|
|
{ |
282
|
|
|
$this->step = $step; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|