Completed
Push — master ( 9d8ca3...5cf646 )
by Simon
8s
created

RangeField::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
     * @var int
26
     */
27
    protected $min = 0;
28
29
    /**
30
     * @var int
31
     */
32
    protected $max = 100;
33
34
    /**
35
     * @var array
36
     */
37
    protected $range = [];
38
39
    /**
40
     * @var bool
41
     */
42
    protected $snap = false;
43
44
    /**
45
     * @var array
46
     */
47
    protected $data = [];
48
49
    /**
50
     * @var int
51
     */
52
    protected $density = 4;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $showPips = true;
58
59
    /**
60
     * @var int|bool
61
     */
62
    protected $step;
63
64
65
    /**
66
     * @var string
67
     */
68
    protected $unit = '';
69
70
71
    /**
72
     * @var int
73
     */
74
    protected $decimalPlaces = 2;
75
76
    /**
77
     * RangeField constructor.
78
     * @param string      $name The internal field name, passed to forms.
79
     * @param null|string $title The human-readable field label.
80
     * @param int|array   $start Starting point(s) on the line
81
     * @param mixed       $value The value of the field.
82
     * @param int|array   $min Lowest value of the range
83
     * @param int         $max Highest value of the range
84
     * @param array       $range Associative array with keys which determine the percentage point on the range
85
     *                     And values being the labels on the field
86
     */
87
    public function __construct($name, $title = null, $start = 0, $min = 0, $max = 100, $range = [], $value = null)
88
    {
89
        if (!is_array($start)) {
90
            $start = [$start];
91
        }
92
93
        $this->start = $start;
94
        $this->min = $min;
95
        $this->max = $max;
96
        $this->range = $range;
97
98
        $this->setInputType('hidden');
99
100
        parent::__construct($name, $title, $value);
101
    }
102
103
    /**
104
     * @param array $properties
105
     * @return \SilverStripe\ORM\FieldType\DBHTMLText
106
     */
107
    public function field($properties = array())
108
    {
109
        Requirements::set_force_js_to_bottom(true);
110
        Requirements::javascript('firesphere/rangefield:client/dist/main.js');
111
        Requirements::css('firesphere/rangefield:client/dist/main.css');
112
113
        $this->setupData();
114
115
        $field = parent::Field($properties);
116
117
118
        /** @todo find a way to get this a bit nicer. It's the only way to get it in without breaking on submit */
119
        Requirements::insertHeadTags("<script type='text/javascript'>
120
        var $this->name = " . Convert::array2json($this->getData()) . '</script>');
121
122
        return $field;
123
    }
124
125
    /**
126
     * @param string unit
127
     * @param int $decimalPlaces
128
     */
129
    public function setFormat($unit, $decimalPlaces)
130
    {
131
        $this->setUnit($unit);
132
        $this->setdecimalPlaces($decimalPlaces);
133
    }
134
135
136
    protected function setupData()
137
    {
138
        $data = [
139
            'start'             => $this->getStart(),
140
            'snap'              => $this->isSnap(),
141
            'animate'           => true,
142
            'animationDuration' => 300,
143
            'range'             => [
144
                'min' => $this->getMin(),
145
                'max' => $this->getMax()
146
            ],
147
            'unit'              => $this->getUnit(),
148
            'decimalPlaces'     => $this->getdecimalPlaces()
149
        ];
150
151
        if ($this->showPips) {
152
            $data['pips'] = [  // Show a scale with the slider
153
                'mode'    => 'steps',
154
                'stepped' => true,
155
                'density' => $this->getDensity()
156
            ];
157
        }
158
159
        if ($this->getStep()) {
160
            $data['step'] = $this->getStep();
161
        }
162
163
        if (count($this->getRange())) { // Update the range if we've gotten a forced range
164
            $data['range'] = array_merge($data['range'], $this->getRange());
165
        }
166
167
        $this->setData($data);
168
    }
169
170
    /**
171
     * @return array|int
172
     */
173
    public function getStart()
174
    {
175
        return $this->start;
176
    }
177
178
    /**
179
     * @param array|int $start
180
     */
181
    public function setStart($start)
182
    {
183
        $this->start = (array)$start;
184
    }
185
186
    /**
187
     * @return array
188
     */
189
    public function getMin()
190
    {
191
        return $this->min;
192
    }
193
194
    /**
195
     * @param int $min
196
     */
197
    public function setMin($min)
198
    {
199
        $this->min = $min;
200
    }
201
202
    /**
203
     * @return int
204
     */
205
    public function getMax()
206
    {
207
        return $this->max;
208
    }
209
210
    /**
211
     * @param int $max
212
     */
213
    public function setMax($max)
214
    {
215
        $this->max = $max;
216
    }
217
218
    /**
219
     * @return array
220
     */
221
    public function getRange()
222
    {
223
        return $this->range;
224
    }
225
226
    /**
227
     * @param array $range
228
     */
229
    public function setRange($range)
230
    {
231
        $this->range = $range;
232
    }
233
234
    /**
235
     * @return bool
236
     */
237
    public function isSnap()
238
    {
239
        return $this->snap;
240
    }
241
242
    /**
243
     * @param bool $snap
244
     */
245
    public function setSnap($snap)
246
    {
247
        $this->snap = $snap;
248
    }
249
250
    /**
251
     * @return array
252
     */
253
    public function getData()
254
    {
255
        return $this->data;
256
    }
257
258
    /**
259
     * @param array $data
260
     */
261
    public function setData($data)
262
    {
263
        $this->data = $data;
264
    }
265
266
    /**
267
     * @return int
268
     */
269
    public function getDensity()
270
    {
271
        return $this->density;
272
    }
273
274
    /**
275
     * @param int $density
276
     */
277
    public function setDensity($density)
278
    {
279
        $this->density = $density;
280
    }
281
282
    /**
283
     * @return bool
284
     */
285
    public function isShowPips()
286
    {
287
        return $this->showPips;
288
    }
289
290
    /**
291
     * @param bool $showPips
292
     */
293
    public function setShowPips($showPips)
294
    {
295
        $this->showPips = $showPips;
296
    }
297
298
    /**
299
     * @return bool|int
300
     */
301
    public function getStep()
302
    {
303
        return $this->step;
304
    }
305
306
    /**
307
     * @param bool|int $step
308
     */
309
    public function setStep($step)
310
    {
311
        $this->step = $step;
312
    }
313
314
    /**
315
     * @return string
316
     */
317
    public function getUnit()
318
    {
319
        return $this->unit;
320
    }
321
322
    /**
323
     * @param string $unit
324
     */
325
    public function setUnit($unit)
326
    {
327
        $this->unit = $unit;
328
    }
329
330
    /**
331
     * @return int
332
     */
333
    public function getDecimalPlaces()
334
    {
335
        return $this->decimalPlaces;
336
    }
337
338
    /**
339
     * @param int $decimalPlaces
340
     */
341
    public function setDecimalPlaces($decimalPlaces)
342
    {
343
        $this->decimalPlaces = $decimalPlaces;
344
    }
345
}
346