1 | <?php |
||
31 | class Control extends BaseControl |
||
32 | 1 | { |
|
33 | const CLASSNAME = __CLASS__; |
||
34 | |||
35 | /** |
||
36 | * @var IConfirmer |
||
37 | */ |
||
38 | protected $confirmerFactory; |
||
39 | |||
40 | /** |
||
41 | * @var Confirmer |
||
42 | */ |
||
43 | protected $confirmer; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $useAjax = TRUE; |
||
49 | |||
50 | /** |
||
51 | * @param IConfirmer $confirmerFactory |
||
52 | */ |
||
53 | public function injectFactories(IConfirmer $confirmerFactory) |
||
54 | { |
||
55 | // Get confirmer component factory |
||
56 | 1 | $this->confirmerFactory = $confirmerFactory; |
|
57 | 1 | } |
|
58 | |||
59 | /** |
||
60 | * @param NULL|string $layoutFile |
||
61 | * @param NULL|string $templateFile |
||
62 | * @param Nette\ComponentModel\IContainer $parent |
||
63 | * @param null $name |
||
64 | */ |
||
65 | public function __construct( |
||
66 | $layoutFile = NULL, |
||
67 | $templateFile = NULL, |
||
68 | Nette\ComponentModel\IContainer $parent = NULL, $name = NULL |
||
69 | ) { |
||
70 | // TODO: remove, only for tests |
||
71 | 1 | parent::__construct(NULL, NULL); |
|
72 | |||
73 | 1 | if ($layoutFile !== NULL) { |
|
74 | $this->setLayoutFile($layoutFile); |
||
75 | } |
||
76 | |||
77 | 1 | if ($templateFile !== NULL) { |
|
78 | $this->setTemplateFile($templateFile); |
||
79 | } |
||
80 | 1 | } |
|
81 | |||
82 | /** |
||
83 | * Change default dialog layout path |
||
84 | * |
||
85 | * @param string $layoutFile |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function setLayoutFile($layoutFile) |
||
90 | { |
||
91 | 1 | $this->setTemplateFilePath($layoutFile, self::TEMPLATE_LAYOUT); |
|
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Change default confirmer template path |
||
98 | * |
||
99 | * @param string $layoutFile |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setTemplateFile($layoutFile) |
||
104 | { |
||
105 | 1 | $this->setTemplateFilePath($layoutFile, self::TEMPLATE_CONFIRMER); |
|
106 | |||
107 | 1 | return $this; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getTemplateFile() |
||
114 | { |
||
115 | // ...try to get default component layout file |
||
116 | 1 | return !empty($this->templateFile) ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR .'template'. DIRECTORY_SEPARATOR .'default.latte'; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Overrides signal method formatter |
||
121 | * This provide "dynamically named signals" |
||
122 | * |
||
123 | * @param string $signal |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public static function formatSignalMethod($signal) |
||
128 | { |
||
129 | 1 | if (Utils\Strings::startsWith($signal, 'confirm')) { |
|
130 | 1 | return 'handleShowConfirmer'; |
|
131 | } |
||
132 | |||
133 | return parent::formatSignalMethod($signal); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Add confirmation handler to "dynamicaly named signals" |
||
138 | * |
||
139 | 1 | * @param string $name Confirmation/signal name |
|
140 | * @param callback|Nette\Callback $handler Callback called when confirmation succeed |
||
141 | * @param callback|string $question Callback ($confirmer, $params) or string containing question text |
||
142 | * @param callback|string $heading Callback ($confirmer, $params) or string containing heading text |
||
143 | * |
||
144 | * @return $this |
||
145 | * |
||
146 | 1 | * @throws Exceptions\InvalidArgumentException |
|
147 | */ |
||
148 | public function addConfirmer($name, $handler, $question, $heading) |
||
149 | { |
||
150 | // Confirmer name could be only A-z |
||
151 | 1 | if (!preg_match('/[A-Za-z_]+/', $name)) { |
|
152 | throw new Exceptions\InvalidArgumentException("Confirmation control name contain invalid characters."); |
||
153 | |||
154 | // Check confirmer |
||
155 | 1 | } else if ((!$confirmer = $this->getComponent('confirmer-'. $name)) || !$confirmer instanceof Confirmer || $confirmer->isConfigured()) { |
|
156 | throw new Exceptions\InvalidArgumentException("Confirmation control '$name' could not be created."); |
||
157 | |||
158 | } else { |
||
159 | $confirmer |
||
160 | // Set confirmer handler |
||
161 | 1 | ->setHandler($handler) |
|
162 | // Set confirmer heading |
||
163 | 1 | ->setHeading($heading) |
|
164 | // Set confirmer question |
||
165 | 1 | ->setQuestion($question); |
|
166 | } |
||
167 | |||
168 | 1 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string $name |
||
173 | * |
||
174 | * @return Confirmer |
||
175 | * |
||
176 | * @throws Exceptions\InvalidArgumentException |
||
177 | */ |
||
178 | public function getConfirmer($name) |
||
186 | |||
187 | /** |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function resetConfirmer() |
||
191 | { |
||
192 | 1 | $this->confirmer = NULL; |
|
193 | |||
199 | |||
200 | /** |
||
201 | * @return Application\UI\Multiplier |
||
202 | * |
||
203 | * @throws Exceptions\InvalidArgumentException |
||
204 | */ |
||
205 | protected function createComponentConfirmer() |
||
228 | |||
229 | /** |
||
230 | * Show dialog for confirmation |
||
231 | * |
||
232 | * @param string $name |
||
233 | * @param array $params |
||
234 | * |
||
235 | * @throws Exceptions\InvalidArgumentException |
||
236 | * @throws Exceptions\InvalidStateException |
||
237 | */ |
||
238 | public function showConfirm($name, array $params = []) |
||
251 | |||
252 | /** |
||
253 | * Dynamically named signal receiver |
||
254 | * |
||
255 | * @throws Exceptions\InvalidArgumentException |
||
256 | * @throws Exceptions\InvalidStateException |
||
257 | */ |
||
258 | public function handleShowConfirmer() |
||
278 | |||
279 | /** |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function enableAjax() |
||
288 | |||
289 | /** |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function disableAjax() |
||
298 | |||
299 | /** |
||
300 | * Render control |
||
301 | * |
||
302 | * @throws Exceptions\InvalidStateException |
||
303 | */ |
||
304 | public function render() |
||
328 | } |
||
329 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.