forked from contao/contao
/
FormRange.php
142 lines (127 loc) · 2.38 KB
/
FormRange.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Class FormRange
*
* @property string $value
* @property string $type
* @property boolean $mandatory
* @property integer $min
* @property integer $max
* @property integer $step
*
* @author Fritz Michael Gschwantner <https://github.com/fritzmg>
*/
class FormRange extends Widget
{
/**
* Submit user input
*
* @var boolean
*/
protected $blnSubmitInput = true;
/**
* Add a for attribute
*
* @var boolean
*/
protected $blnForAttribute = true;
/**
* Template
*
* @var string
*/
protected $strTemplate = 'form_range';
/**
* The CSS class prefix
*
* @var string
*/
protected $strPrefix = 'widget widget-range';
/**
* Add specific attributes
*
* @param string $strKey The attribute key
* @param mixed $varValue The attribute value
*/
public function __set($strKey, $varValue)
{
switch ($strKey)
{
case 'min':
case 'minval':
$this->arrAttributes['min'] = $varValue;
break;
case 'max':
case 'maxval':
$this->arrAttributes['max'] = $varValue;
break;
case 'mandatory':
if ($varValue)
{
$this->arrAttributes['required'] = 'required';
}
else
{
unset($this->arrAttributes['required']);
}
parent::__set($strKey, $varValue);
break;
case 'step':
if ($varValue > 0)
{
$this->arrAttributes[$strKey] = $varValue;
}
else
{
unset($this->arrAttributes[$strKey]);
}
break;
default:
parent::__set($strKey, $varValue);
break;
}
}
/**
* Return a parameter.
*
* @param string $strKey The parameter key
*
* @return mixed The parameter value
*/
public function __get($strKey)
{
switch ($strKey) {
case 'type':
return 'range';
break;
default:
return parent::__get($strKey);
break;
}
}
/**
* Generate the widget and return it as string.
*
* @return string The widget markup
*/
public function generate()
{
return sprintf('<input type="%s" name="%s" id="ctrl_%s" class="range%s%s" value="%s"%s%s',
$this->type,
$this->strName,
$this->strId,
($this->hideInput ? ' password' : ''),
(('' !== $this->strClass) ? ' '.$this->strClass : ''),
StringUtil::specialchars($this->value),
$this->getAttributes(),
$this->strTagEnding);
}
}