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