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
|
|
|
* @param UI\ITemplateFactory $templateFactory |
80
|
|
|
* @param string|NULL $label |
81
|
|
|
* @param int|NULL $maxLength |
82
|
|
|
*/ |
83
|
|
|
public function __construct(UI\ITemplateFactory $templateFactory, string $label = NULL, int $maxLength = NULL) |
84
|
|
|
{ |
85
|
1 |
|
parent::__construct($label, $maxLength); |
86
|
|
|
|
87
|
1 |
|
$this->templateFactory = $templateFactory; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add filed from which slug will be created |
92
|
|
|
* |
93
|
|
|
* @param Forms\Controls\BaseControl $field |
94
|
|
|
* |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function addField(Forms\Controls\BaseControl $field) : self |
98
|
|
|
{ |
99
|
|
|
// Assign filed to collection |
100
|
|
|
$this->fields[$field->getHtmlId()] = $field; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Generates control's HTML element |
107
|
|
|
* |
108
|
|
|
* @return Utils\Html |
109
|
|
|
*/ |
110
|
|
|
public function getControl() : Utils\Html |
111
|
|
|
{ |
112
|
|
|
// Create form input |
113
|
1 |
|
$input = parent::getControl(); |
114
|
|
|
|
115
|
1 |
|
$template = $this->getTemplate(); |
116
|
|
|
|
117
|
|
|
// If template file was not defined before... |
118
|
1 |
|
if ($template->getFile() === NULL) { |
119
|
|
|
// ...try to get base control template file |
120
|
|
|
$templateFile = $this->getTemplateFile(); |
121
|
|
|
|
122
|
|
|
// ...& set it to template engine |
123
|
|
|
$template->setFile($templateFile); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Assign vars to template |
127
|
1 |
|
$template->input = $input; |
|
|
|
|
128
|
1 |
|
$template->value = $this->getValue(); |
|
|
|
|
129
|
1 |
|
$template->caption = $this->caption; |
|
|
|
|
130
|
1 |
|
$template->_form = $this->getForm(); |
|
|
|
|
131
|
|
|
|
132
|
|
|
// Component js settings |
133
|
1 |
|
$template->settings = [ |
|
|
|
|
134
|
1 |
|
'toggle' => $this->toggleBox, |
135
|
1 |
|
'onetime' => $this->onetimeAutoUpdate, |
136
|
1 |
|
'fields' => (array_reduce($this->fields, function (array $result, Forms\Controls\BaseControl $row) { |
137
|
|
|
$result[] = '#' . $row->getHtmlId(); |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
1 |
|
}, [])), |
141
|
|
|
]; |
142
|
|
|
|
143
|
1 |
|
return Utils\Html::el() |
144
|
1 |
|
->addHtml((string) $template); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Change default control template path |
149
|
|
|
* |
150
|
|
|
* @param string $templateFile |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
* |
154
|
|
|
* @throws Exceptions\FileNotFoundException |
155
|
|
|
*/ |
156
|
|
|
public function setTemplateFile(string $templateFile) : void |
157
|
|
|
{ |
158
|
|
|
// Check if template file exists... |
159
|
|
|
if (!is_file($templateFile)) { |
160
|
|
|
// ...check if extension template is used |
161
|
|
|
if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) { |
162
|
|
|
$templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile; |
163
|
|
|
|
164
|
|
|
} else { |
165
|
|
|
// ...if not throw exception |
166
|
|
|
throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile)); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->templateFile = $templateFile; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
private function getTemplateFile() : string |
177
|
|
|
{ |
178
|
1 |
|
return $this->templateFile !== NULL ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return UI\ITemplate|Bridges\ApplicationLatte\Template |
183
|
|
|
*/ |
184
|
|
|
private function getTemplate() : UI\ITemplate |
185
|
|
|
{ |
186
|
1 |
|
if ($this->template === NULL) { |
187
|
1 |
|
$this->template = $this->templateFactory->createTemplate(); |
188
|
1 |
|
$this->template->setFile($this->getTemplateFile()); |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
return $this->template; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param UI\ITemplateFactory $templateFactory |
196
|
|
|
* @param string $method |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
public static function register(UI\ITemplateFactory $templateFactory, $method = 'addSlug') : void |
201
|
|
|
{ |
202
|
|
|
// Check for multiple registration |
203
|
1 |
|
if (static::$registered) { |
|
|
|
|
204
|
1 |
|
throw new Exceptions\InvalidStateException('Slug control already registered.'); |
205
|
|
|
} |
206
|
|
|
|
207
|
1 |
|
static::$registered = TRUE; |
|
|
|
|
208
|
|
|
|
209
|
1 |
|
$class = function_exists('get_called_class') ? get_called_class() : __CLASS__; |
210
|
|
|
|
211
|
1 |
|
Forms\Container::extensionMethod( |
212
|
1 |
|
$method, function (Forms\Container $form, $name, $label = NULL, $maxLength = NULL) use ($class, $templateFactory) : Slug { |
213
|
1 |
|
$component = new $class($templateFactory, $label, $maxLength); |
214
|
|
|
|
215
|
1 |
|
$form->addComponent($component, $name); |
216
|
|
|
|
217
|
1 |
|
return $component; |
218
|
1 |
|
}); |
219
|
1 |
|
} |
220
|
|
|
} |
221
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: