1 | <?php |
||
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) |
||
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() |
||
147 | |||
148 | /** |
||
149 | * @param array|int $start |
||
150 | */ |
||
151 | public function setStart($start) |
||
155 | |||
156 | /** |
||
157 | * @return array |
||
158 | */ |
||
159 | public function getMin() |
||
163 | |||
164 | /** |
||
165 | * @param int $min |
||
166 | */ |
||
167 | public function setMin($min) |
||
171 | |||
172 | /** |
||
173 | * @return int |
||
174 | */ |
||
175 | public function getMax() |
||
179 | |||
180 | /** |
||
181 | * @param int $max |
||
182 | */ |
||
183 | public function setMax($max) |
||
187 | |||
188 | /** |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getRange() |
||
195 | |||
196 | /** |
||
197 | * @param array $range |
||
198 | */ |
||
199 | public function setRange($range) |
||
203 | |||
204 | /** |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isSnap() |
||
211 | |||
212 | /** |
||
213 | * @param bool $snap |
||
214 | */ |
||
215 | public function setSnap($snap) |
||
219 | |||
220 | /** |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getData() |
||
227 | |||
228 | /** |
||
229 | * @param array $data |
||
230 | */ |
||
231 | public function setData($data) |
||
235 | |||
236 | |||
237 | /** |
||
238 | * @return int |
||
239 | */ |
||
240 | public function getDensity() |
||
244 | |||
245 | /** |
||
246 | * @param int $density |
||
247 | */ |
||
248 | public function setDensity($density) |
||
252 | |||
253 | /** |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function isShowPips() |
||
260 | |||
261 | /** |
||
262 | * @param bool $showPips |
||
263 | */ |
||
264 | public function setShowPips($showPips) |
||
268 | |||
269 | /** |
||
270 | * @return bool|int |
||
271 | */ |
||
272 | public function getStep() |
||
276 | |||
277 | /** |
||
278 | * @param bool|int $step |
||
279 | */ |
||
280 | public function setStep($step) |
||
284 | } |
||
285 |