1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slug.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:FormSlug! |
9
|
|
|
* @subpackage Controls |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 08.01.15 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\FormSlug\Controls; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\Application\UI; |
21
|
|
|
use Nette\Bridges; |
22
|
|
|
use Nette\Forms; |
23
|
|
|
use Nette\Utils; |
24
|
|
|
|
25
|
|
|
use IPub; |
26
|
|
|
use IPub\FormSlug; |
27
|
|
|
use IPub\FormSlug\Exceptions; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Form slug control element |
31
|
|
|
* |
32
|
|
|
* @package iPublikuj:FormSlug! |
33
|
|
|
* @subpackage Controls |
34
|
|
|
* |
35
|
|
|
* @author Adam Kadlec <[email protected]> |
36
|
|
|
*/ |
37
|
1 |
|
class Slug extends Forms\Controls\TextInput |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string|NULL |
41
|
|
|
*/ |
42
|
|
|
private $templateFile; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var UI\ITemplate |
46
|
|
|
*/ |
47
|
|
|
private $template; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var UI\ITemplateFactory |
51
|
|
|
*/ |
52
|
|
|
private $templateFactory; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Forms\Controls\BaseControl[] |
56
|
|
|
*/ |
57
|
|
|
private $fields = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Toggle box selector |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $toggleBox = '.ipub-slug-box'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
private static $registered = FALSE; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Enable or disable one time auto updating slug field from watched fields |
73
|
|
|
* |
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
private $onetimeAutoUpdate = TRUE; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Enable or disable updating field when editing watched field |
80
|
|
|
* |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
private $forceEditUpdate = FALSE; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param UI\ITemplateFactory $templateFactory |
87
|
|
|
* @param string|NULL $label |
88
|
|
|
* @param int|NULL $maxLength |
89
|
|
|
*/ |
90
|
|
|
public function __construct(UI\ITemplateFactory $templateFactory, string $label = NULL, int $maxLength = NULL) |
91
|
|
|
{ |
92
|
1 |
|
parent::__construct($label, $maxLength); |
93
|
|
|
|
94
|
1 |
|
$this->templateFactory = $templateFactory; |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add filed from which slug will be created |
99
|
|
|
* |
100
|
|
|
* @param Forms\Controls\BaseControl $field |
101
|
|
|
* |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
|
|
public function addField(Forms\Controls\BaseControl $field) : self |
105
|
|
|
{ |
106
|
|
|
// Assign filed to collection |
107
|
|
|
$this->fields[$field->getHtmlId()] = $field; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
|
|
public function disableOneTimeUpdate() : self |
116
|
|
|
{ |
117
|
|
|
$this->onetimeAutoUpdate = FALSE; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
|
|
public function enableOneTimeUpdate() : self |
126
|
|
|
{ |
127
|
|
|
$this->onetimeAutoUpdate = TRUE; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public function disableForceEditUpdate() : self |
136
|
|
|
{ |
137
|
|
|
$this->forceEditUpdate = FALSE; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return self |
144
|
|
|
*/ |
145
|
|
|
public function enableForceEditUpdate() : self |
146
|
|
|
{ |
147
|
|
|
$this->forceEditUpdate = TRUE; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Generates control's HTML element |
154
|
|
|
* |
155
|
|
|
* @return Utils\Html |
156
|
|
|
*/ |
157
|
|
|
public function getControl() : Utils\Html |
158
|
|
|
{ |
159
|
|
|
// Create form input |
160
|
1 |
|
$input = parent::getControl(); |
161
|
|
|
|
162
|
1 |
|
$template = $this->getTemplate(); |
163
|
|
|
|
164
|
|
|
// If template file was not defined before... |
165
|
1 |
|
if ($template->getFile() === NULL) { |
166
|
|
|
// ...try to get base control template file |
167
|
|
|
$templateFile = $this->getTemplateFile(); |
168
|
|
|
|
169
|
|
|
// ...& set it to template engine |
170
|
|
|
$template->setFile($templateFile); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// Assign vars to template |
174
|
1 |
|
$template->input = $input; |
175
|
1 |
|
$template->value = $this->getValue(); |
176
|
1 |
|
$template->caption = $this->caption; |
177
|
1 |
|
$template->_form = $this->getForm(); |
178
|
|
|
|
179
|
|
|
// Component js settings |
180
|
1 |
|
$template->settings = [ |
181
|
1 |
|
'toggle' => $this->toggleBox, |
182
|
1 |
|
'onetime' => $this->onetimeAutoUpdate, |
183
|
1 |
|
'forceEdit' => $this->forceEditUpdate, |
184
|
1 |
|
'fields' => (array_reduce($this->fields, function (array $result, Forms\Controls\BaseControl $row) { |
185
|
|
|
$result[] = '#' . $row->getHtmlId(); |
186
|
|
|
|
187
|
|
|
return $result; |
188
|
1 |
|
}, [])), |
189
|
|
|
]; |
190
|
|
|
|
191
|
1 |
|
return Utils\Html::el() |
192
|
1 |
|
->addHtml((string) $template); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Change default control template path |
197
|
|
|
* |
198
|
|
|
* @param string $templateFile |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
* |
202
|
|
|
* @throws Exceptions\FileNotFoundException |
203
|
|
|
*/ |
204
|
|
|
public function setTemplateFile(string $templateFile) : void |
205
|
|
|
{ |
206
|
|
|
// Check if template file exists... |
207
|
|
|
if (!is_file($templateFile)) { |
208
|
|
|
// ...check if extension template is used |
209
|
|
|
if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) { |
210
|
|
|
$templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile; |
211
|
|
|
|
212
|
|
|
} else { |
213
|
|
|
// ...if not throw exception |
214
|
|
|
throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile)); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->templateFile = $templateFile; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
private function getTemplateFile() : string |
225
|
|
|
{ |
226
|
1 |
|
return $this->templateFile !== NULL ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte'; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return UI\ITemplate|Bridges\ApplicationLatte\Template |
231
|
|
|
*/ |
232
|
|
|
private function getTemplate() : UI\ITemplate |
233
|
|
|
{ |
234
|
1 |
|
if ($this->template === NULL) { |
235
|
1 |
|
$this->template = $this->templateFactory->createTemplate(); |
236
|
1 |
|
$this->template->setFile($this->getTemplateFile()); |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
return $this->template; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param UI\ITemplateFactory $templateFactory |
244
|
|
|
* @param string $method |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
public static function register(UI\ITemplateFactory $templateFactory, $method = 'addSlug') : void |
249
|
|
|
{ |
250
|
|
|
// Check for multiple registration |
251
|
1 |
|
if (static::$registered) { |
|
|
|
|
252
|
1 |
|
throw new Exceptions\InvalidStateException('Slug control already registered.'); |
253
|
|
|
} |
254
|
|
|
|
255
|
1 |
|
static::$registered = TRUE; |
|
|
|
|
256
|
|
|
|
257
|
1 |
|
$class = function_exists('get_called_class') ? get_called_class() : __CLASS__; |
258
|
|
|
|
259
|
1 |
|
Forms\Container::extensionMethod( |
260
|
1 |
|
$method, function (Forms\Container $form, $name, $label = NULL, $maxLength = NULL) use ($class, $templateFactory) : Slug { |
261
|
1 |
|
$component = new $class($templateFactory, $label, $maxLength); |
262
|
|
|
|
263
|
1 |
|
$form->addComponent($component, $name); |
264
|
|
|
|
265
|
1 |
|
return $component; |
266
|
1 |
|
}); |
267
|
1 |
|
} |
268
|
|
|
} |
269
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: