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
|
|
|
namespace IPub\FormSlug\Controls; |
16
|
|
|
|
17
|
|
|
use Nette; |
18
|
|
|
use Nette\Application\UI; |
19
|
|
|
use Nette\Bridges; |
20
|
|
|
use Nette\Forms; |
21
|
|
|
|
22
|
|
|
use IPub; |
23
|
|
|
use IPub\FormSlug; |
24
|
|
|
use IPub\FormSlug\Exceptions; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Form slug control element |
28
|
|
|
* |
29
|
|
|
* @package iPublikuj:FormSlug! |
30
|
|
|
* @subpackage Controls |
31
|
|
|
* |
32
|
|
|
* @author Adam Kadlec <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class Slug extends Forms\Controls\TextInput |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string|NULL |
38
|
|
|
*/ |
39
|
|
|
private $templateFile; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var UI\ITemplate |
43
|
|
|
*/ |
44
|
|
|
private $template; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var UI\ITemplateFactory |
48
|
|
|
*/ |
49
|
|
|
private $templateFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Forms\Controls\BaseControl[] |
53
|
|
|
*/ |
54
|
|
|
private $fields = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Toggle box selector |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $toggleBox = '.ipub-slug-box'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
private static $registered = FALSE; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Enable or disable one time auto updating slug field from watched fields |
70
|
|
|
* |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
private $onetimeAutoUpdate = TRUE; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param UI\ITemplateFactory $templateFactory |
77
|
|
|
* @param string|NULL $label |
78
|
|
|
* @param int|NULL $maxLength |
79
|
|
|
*/ |
80
|
|
|
public function __construct(UI\ITemplateFactory $templateFactory, string $label = NULL, int $maxLength = NULL) |
81
|
|
|
{ |
82
|
|
|
parent::__construct($label, $maxLength); |
83
|
|
|
|
84
|
|
|
$this->templateFactory = $templateFactory; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add filed from which slug will be created |
89
|
|
|
* |
90
|
|
|
* @param Forms\Controls\BaseControl $field |
91
|
|
|
* |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
|
|
public function addField(Forms\Controls\BaseControl $field) |
95
|
|
|
{ |
96
|
|
|
// Assign filed to collection |
97
|
|
|
$this->fields[$field->getHtmlId()] = $field; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generates control's HTML element |
104
|
|
|
*/ |
105
|
|
|
public function getControl() : FormSlug\Utils\Html |
106
|
|
|
{ |
107
|
|
|
// Create form input |
108
|
|
|
$input = parent::getControl(); |
109
|
|
|
|
110
|
|
|
$template = $this->getTemplate(); |
111
|
|
|
|
112
|
|
|
// If template file was not defined before... |
113
|
|
|
if ($template->getFile() === NULL) { |
114
|
|
|
// ...try to get base control template file |
115
|
|
|
$templateFile = $this->getTemplateFile(); |
116
|
|
|
|
117
|
|
|
// ...& set it to template engine |
118
|
|
|
$template->setFile($templateFile); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Assign vars to template |
122
|
|
|
$template->input = $input; |
|
|
|
|
123
|
|
|
$template->value = $this->getValue(); |
|
|
|
|
124
|
|
|
$template->caption = $this->caption; |
|
|
|
|
125
|
|
|
$template->_form = $this->getForm(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
// Component js settings |
128
|
|
|
$template->settings = [ |
|
|
|
|
129
|
|
|
'toggle' => $this->toggleBox, |
130
|
|
|
'onetime' => $this->onetimeAutoUpdate, |
131
|
|
|
'fields' => (array_reduce($this->fields, function (array $result, Forms\Controls\BaseControl $row) { |
132
|
|
|
$result[] = '#' . $row->getHtmlId(); |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
}, [])), |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return FormSlug\Utils\Html::el() |
139
|
|
|
->addHtml($template); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return UI\ITemplate|Bridges\ApplicationLatte\Template |
144
|
|
|
*/ |
145
|
|
|
public function getTemplate() : UI\ITemplate |
146
|
|
|
{ |
147
|
|
|
if ($this->template === NULL) { |
148
|
|
|
$this->template = $this->templateFactory->createTemplate(); |
149
|
|
|
$this->template->setFile($this->getTemplateFile()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->template; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Change default control template path |
157
|
|
|
* |
158
|
|
|
* @param string $templateFile |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
* |
162
|
|
|
* @throws Exceptions\FileNotFoundException |
163
|
|
|
*/ |
164
|
|
|
public function setTemplateFile(string $templateFile) |
165
|
|
|
{ |
166
|
|
|
// Check if template file exists... |
167
|
|
|
if (!is_file($templateFile)) { |
168
|
|
|
// ...check if extension template is used |
169
|
|
|
if (is_file(__DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) { |
170
|
|
|
$templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile; |
171
|
|
|
|
172
|
|
|
} else { |
173
|
|
|
// ...if not throw exception |
174
|
|
|
throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->templateFile = $templateFile; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
private function getTemplateFile() : string |
185
|
|
|
{ |
186
|
|
|
return $this->templateFile !== NULL ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param UI\ITemplateFactory $templateFactory |
191
|
|
|
* @param string $method |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
public static function register(UI\ITemplateFactory $templateFactory, $method = 'addSlug') |
196
|
|
|
{ |
197
|
|
|
// Check for multiple registration |
198
|
|
|
if (static::$registered) { |
|
|
|
|
199
|
|
|
throw new Exceptions\InvalidStateException('Slug control already registered.'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
static::$registered = TRUE; |
|
|
|
|
203
|
|
|
|
204
|
|
|
$class = function_exists('get_called_class') ? get_called_class() : __CLASS__; |
205
|
|
|
Forms\Container::extensionMethod( |
206
|
|
|
$method, function (Forms\Container $form, $name, $label = NULL, $maxLength = NULL) use ($class, $templateFactory) { |
207
|
|
|
$component = new $class($templateFactory, $label, $maxLength); |
208
|
|
|
$form->addComponent($component, $name); |
209
|
|
|
|
210
|
|
|
return $component; |
211
|
|
|
} |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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: