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
|
|
|
$this->setupData(); |
103
|
|
|
|
104
|
|
|
$field = parent::Field($properties); |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** @todo find a way to get this a bit nicer. It's the only way to get it in without breaking on submit */ |
108
|
|
|
Requirements::insertHeadTags("<script type='text/javascript'> |
109
|
|
|
var $this->name = " . Convert::array2json($this->getData()) . '</script>'); |
110
|
|
|
|
111
|
|
|
return $field; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
protected function setupData() |
116
|
|
|
{ |
117
|
|
|
$data = [ |
118
|
|
|
'start' => $this->getStart(), |
119
|
|
|
'snap' => $this->isSnap(), |
120
|
|
|
'animate' => true, |
121
|
|
|
'animationDuration' => 300, |
122
|
|
|
'range' => [ |
123
|
|
|
'min' => $this->getMin(), |
124
|
|
|
'max' => $this->getMax() |
125
|
|
|
] |
126
|
|
|
]; |
127
|
|
|
if ($this->showPips) { |
128
|
|
|
$data['pips'] = [ // Show a scale with the slider |
129
|
|
|
'mode' => 'steps', |
130
|
|
|
'stepped' => true, |
131
|
|
|
'density' => $this->getDensity() |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($this->getStep()) { |
136
|
|
|
$data['step'] = $this->getStep(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (count($this->getRange())) { // Update the range if we've gotten a forced range |
140
|
|
|
$data['range'] = array_merge($data['range'], $this->getRange()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->setData($data); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array|int |
148
|
|
|
*/ |
149
|
|
|
public function getStart() |
150
|
|
|
{ |
151
|
|
|
return $this->start; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array|int $start |
156
|
|
|
*/ |
157
|
|
|
public function setStart($start) |
158
|
|
|
{ |
159
|
|
|
$this->start = (array)$start; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function getMin() |
166
|
|
|
{ |
167
|
|
|
return $this->min; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int $min |
172
|
|
|
*/ |
173
|
|
|
public function setMin($min) |
174
|
|
|
{ |
175
|
|
|
$this->min = $min; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function getMax() |
182
|
|
|
{ |
183
|
|
|
return $this->max; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param int $max |
188
|
|
|
*/ |
189
|
|
|
public function setMax($max) |
190
|
|
|
{ |
191
|
|
|
$this->max = $max; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
public function getRange() |
198
|
|
|
{ |
199
|
|
|
return $this->range; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $range |
204
|
|
|
*/ |
205
|
|
|
public function setRange($range) |
206
|
|
|
{ |
207
|
|
|
$this->range = $range; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
|
public function isSnap() |
214
|
|
|
{ |
215
|
|
|
return $this->snap; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param bool $snap |
220
|
|
|
*/ |
221
|
|
|
public function setSnap($snap) |
222
|
|
|
{ |
223
|
|
|
$this->snap = $snap; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
public function getData() |
230
|
|
|
{ |
231
|
|
|
return $this->data; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param array $data |
237
|
|
|
*/ |
238
|
|
|
public function setData($data) |
239
|
|
|
{ |
240
|
|
|
$this->data = $data; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return int |
245
|
|
|
*/ |
246
|
|
|
public function getDensity() |
247
|
|
|
{ |
248
|
|
|
return $this->density; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param int $density |
253
|
|
|
*/ |
254
|
|
|
public function setDensity($density) |
255
|
|
|
{ |
256
|
|
|
$this->density = $density; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return bool |
261
|
|
|
*/ |
262
|
|
|
public function isShowPips() |
263
|
|
|
{ |
264
|
|
|
return $this->showPips; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param bool $showPips |
269
|
|
|
*/ |
270
|
|
|
public function setShowPips($showPips) |
271
|
|
|
{ |
272
|
|
|
$this->showPips = $showPips; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return bool|int |
277
|
|
|
*/ |
278
|
|
|
public function getStep() |
279
|
|
|
{ |
280
|
|
|
return $this->step; |
281
|
|
|
} |
282
|
|
|
/** |
283
|
|
|
* @param bool|int $step |
284
|
|
|
*/ |
285
|
|
|
public function setStep($step) |
286
|
|
|
{ |
287
|
|
|
$this->step = $step; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|