1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpha\View\Widget; |
4
|
|
|
|
5
|
|
|
use Alpha\Util\Config\ConfigProvider; |
6
|
|
|
use Alpha\Util\Security\SecurityUtils; |
7
|
|
|
use Alpha\Util\Http\Request; |
8
|
|
|
use Alpha\Model\Type\SmallText; |
9
|
|
|
use Alpha\Exception\IllegalArguementException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* String HTML input box custom widget. |
13
|
|
|
* |
14
|
|
|
* @since 1.0 |
15
|
|
|
* |
16
|
|
|
* @author John Collins <[email protected]> |
17
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
18
|
|
|
* @copyright Copyright (c) 2017, John Collins (founder of Alpha Framework). |
19
|
|
|
* All rights reserved. |
20
|
|
|
* |
21
|
|
|
* <pre> |
22
|
|
|
* Redistribution and use in source and binary forms, with or |
23
|
|
|
* without modification, are permitted provided that the |
24
|
|
|
* following conditions are met: |
25
|
|
|
* |
26
|
|
|
* * Redistributions of source code must retain the above |
27
|
|
|
* copyright notice, this list of conditions and the |
28
|
|
|
* following disclaimer. |
29
|
|
|
* * Redistributions in binary form must reproduce the above |
30
|
|
|
* copyright notice, this list of conditions and the |
31
|
|
|
* following disclaimer in the documentation and/or other |
32
|
|
|
* materials provided with the distribution. |
33
|
|
|
* * Neither the name of the Alpha Framework nor the names |
34
|
|
|
* of its contributors may be used to endorse or promote |
35
|
|
|
* products derived from this software without specific |
36
|
|
|
* prior written permission. |
37
|
|
|
* |
38
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
39
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
40
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
41
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
42
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
43
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
44
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
45
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
46
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
48
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
49
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
50
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
51
|
|
|
* </pre> |
52
|
|
|
*/ |
53
|
|
|
class SmallTextBox |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* The string object that will be edited by this string box. |
57
|
|
|
* |
58
|
|
|
* @var Alpha\Model\Type\SmallText |
59
|
|
|
* |
60
|
|
|
* @since 1.0 |
61
|
|
|
*/ |
62
|
|
|
public $stringObject; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The data label for the string object. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
* |
69
|
|
|
* @since 1.0 |
70
|
|
|
*/ |
71
|
|
|
public $label; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The name of the HTML input box. |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
* |
78
|
|
|
* @since 1.0 |
79
|
|
|
*/ |
80
|
|
|
public $name; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The display size of the input box. |
84
|
|
|
* |
85
|
|
|
* @var int |
86
|
|
|
* |
87
|
|
|
* @since 1.0 |
88
|
|
|
*/ |
89
|
|
|
public $size; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The constructor. |
93
|
|
|
* |
94
|
|
|
* @param Alpha\Model\Type\SmallText $string The string object that will be edited by this text box. |
95
|
|
|
* @param string $label The data label for the string object. |
96
|
|
|
* @param string $name The name of the HTML input box. |
97
|
|
|
* @param int $size The display size (characters). |
98
|
|
|
* |
99
|
|
|
* @since 1.0 |
100
|
|
|
* |
101
|
|
|
* @throws Alpha\Exception\IllegalArguementException |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function __construct($string, $label, $name, $size = 0) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$config = ConfigProvider::getInstance(); |
106
|
|
|
|
107
|
|
|
if ($string instanceof SmallText) { |
108
|
|
|
$this->stringObject = $string; |
109
|
|
|
} else { |
110
|
|
|
throw new IllegalArguementException('String object passed ['.var_export($string, true).'] is not a valid String object!'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->label = $label; |
114
|
|
|
$this->size = $size; |
115
|
|
|
|
116
|
|
|
if ($config->get('security.encrypt.http.fieldnames')) { |
117
|
|
|
$this->name = base64_encode(SecurityUtils::encrypt($name)); |
118
|
|
|
} else { |
119
|
|
|
$this->name = $name; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Renders the HTML and javascript for the string box. |
125
|
|
|
* |
126
|
|
|
* @param bool $readOnly set to true to make the text box readonly (defaults to false) |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
* |
130
|
|
|
* @since 1.0 |
131
|
|
|
*/ |
132
|
|
|
public function render($readOnly = false) |
133
|
|
|
{ |
134
|
|
|
$request = new Request(array('method' => 'GET')); |
135
|
|
|
|
136
|
|
|
$html = '<div class="form-group">'; |
137
|
|
|
$html .= ' <label for="'.$this->name.'">'.$this->label.'</label>'; |
138
|
|
|
$html .= ' <input '.($this->stringObject->checkIsPassword() ? 'type="password"' : 'type="text"').($this->size == 0 ? ' style="width:100%;"' : ' size="'.$this->size.'"').' maxlength="'.SmallText::MAX_SIZE.'" name="'.$this->name.'" id="'.$this->name.'" value="'.(($request->getParam($this->name, false) && $this->stringObject->getValue() == '' && !$this->stringObject->checkIsPassword()) ? $request->getParam($this->name) : $this->stringObject->getValue()).'" class="form-control"'.($readOnly ? ' disabled="disabled"' : '').'/>'; |
139
|
|
|
|
140
|
|
|
if ($this->stringObject->getRule() != '') { |
141
|
|
|
$html .= ' <input type="hidden" id="'.$this->name.'_msg" value="'.$this->stringObject->getHelper().'"/>'; |
142
|
|
|
$html .= ' <input type="hidden" id="'.$this->name.'_rule" value="'.$this->stringObject->getRule().'"/>'; |
143
|
|
|
} |
144
|
|
|
$html .= '</div>'; |
145
|
|
|
|
146
|
|
|
return $html; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Setter for string object. |
151
|
|
|
* |
152
|
|
|
* @param Alpha\Model\Type\SmallText $string |
153
|
|
|
* |
154
|
|
|
* @since 1.0 |
155
|
|
|
* |
156
|
|
|
* @throws Alpha\Exception\IllegalArguementException |
157
|
|
|
*/ |
158
|
|
|
public function setStringObject($string) |
159
|
|
|
{ |
160
|
|
|
if ($string instanceof SmallText) { |
161
|
|
|
$this->stringObject = $string; |
162
|
|
|
} else { |
163
|
|
|
throw new IllegalArguementException('String object passed ['.var_export($string, true).'] is not a valid String object!'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Getter for string object. |
169
|
|
|
* |
170
|
|
|
* @return Alpha\Model\Type\SmallText |
171
|
|
|
* |
172
|
|
|
* @since 1.0 |
173
|
|
|
*/ |
174
|
|
|
public function getStringObject() |
175
|
|
|
{ |
176
|
|
|
return $this->stringObject; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.