RangeField::setShowPips()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
111
        $this->setupData();
112
113
        /** @todo find a way to get this a bit nicer. It's the only way to get it in without breaking on submit */
114
        $properties['JSConfig'] = "var $this->name = " . Convert::array2json($this->getData());
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Core\Convert::array2json() has been deprecated with message: 4.4.0:5.0.0 Use json_encode() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
115
116
        $field = parent::Field($properties);
117
118
        return $field;
119
    }
120
121
    /**
122
     * @param string unit
123
     * @param int $decimalPlaces
124
     */
125
    public function setFormat($unit, $decimalPlaces)
126
    {
127
        $this->setUnit($unit);
128
        $this->setdecimalPlaces($decimalPlaces);
129
    }
130
131
132
    protected function setupData()
133
    {
134
        $data = [
135
            'start'             => $this->getStart(),
136
            'snap'              => $this->isSnap(),
137
            'animate'           => true,
138
            'animationDuration' => 300,
139
            'range'             => [
140
                'min' => $this->getMin(),
141
                'max' => $this->getMax()
142
            ],
143
            'unit'              => $this->getUnit(),
144
            'decimalPlaces'     => $this->getdecimalPlaces()
145
        ];
146
147
        if ($this->showPips) {
148
            $data['pips'] = [  // Show a scale with the slider
149
                'mode'    => 'steps',
150
                'stepped' => true,
151
                'density' => $this->getDensity()
152
            ];
153
        }
154
155
        if ($this->getStep()) {
156
            $data['step'] = $this->getStep();
157
        }
158
159
        if (count($this->getRange())) { // Update the range if we've gotten a forced range
160
            $data['range'] = array_merge($data['range'], $this->getRange());
161
        }
162
163
        $this->setData($data);
164
    }
165
166
    /**
167
     * @return array|int
168
     */
169
    public function getStart()
170
    {
171
        return $this->start;
172
    }
173
174
    /**
175
     * @param array|int $start
176
     */
177
    public function setStart($start)
178
    {
179
        $this->start = (array)$start;
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function getMin()
186
    {
187
        return $this->min;
188
    }
189
190
    /**
191
     * @param int $min
192
     */
193
    public function setMin($min)
194
    {
195
        $this->min = $min;
196
    }
197
198
    /**
199
     * @return int
200
     */
201
    public function getMax()
202
    {
203
        return $this->max;
204
    }
205
206
    /**
207
     * @param int $max
208
     */
209
    public function setMax($max)
210
    {
211
        $this->max = $max;
212
    }
213
214
    /**
215
     * @return array
216
     */
217
    public function getRange()
218
    {
219
        return $this->range;
220
    }
221
222
    /**
223
     * @param array $range
224
     */
225
    public function setRange($range)
226
    {
227
        $this->range = $range;
228
    }
229
230
    /**
231
     * @return bool
232
     */
233
    public function isSnap()
234
    {
235
        return $this->snap;
236
    }
237
238
    /**
239
     * @param bool $snap
240
     */
241
    public function setSnap($snap)
242
    {
243
        $this->snap = $snap;
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function getData()
250
    {
251
        return $this->data;
252
    }
253
254
    /**
255
     * @param array $data
256
     */
257
    public function setData($data)
258
    {
259
        $this->data = $data;
260
    }
261
262
    /**
263
     * @return int
264
     */
265
    public function getDensity()
266
    {
267
        return $this->density;
268
    }
269
270
    /**
271
     * @param int $density
272
     */
273
    public function setDensity($density)
274
    {
275
        $this->density = $density;
276
    }
277
278
    /**
279
     * @return bool
280
     */
281
    public function isShowPips()
282
    {
283
        return $this->showPips;
284
    }
285
286
    /**
287
     * @param bool $showPips
288
     */
289
    public function setShowPips($showPips)
290
    {
291
        $this->showPips = $showPips;
292
    }
293
294
    /**
295
     * @return bool|int
296
     */
297
    public function getStep()
298
    {
299
        return $this->step;
300
    }
301
302
    /**
303
     * @param bool|int $step
304
     */
305
    public function setStep($step)
306
    {
307
        $this->step = $step;
308
    }
309
310
    /**
311
     * @return string
312
     */
313
    public function getUnit()
314
    {
315
        return $this->unit;
316
    }
317
318
    /**
319
     * @param string $unit
320
     */
321
    public function setUnit($unit)
322
    {
323
        $this->unit = $unit;
324
    }
325
326
    /**
327
     * @return int
328
     */
329
    public function getDecimalPlaces()
330
    {
331
        return $this->decimalPlaces;
332
    }
333
334
    /**
335
     * @param int $decimalPlaces
336
     */
337
    public function setDecimalPlaces($decimalPlaces)
338
    {
339
        $this->decimalPlaces = $decimalPlaces;
340
    }
341
}
342