5f2188d3e6d0468895ba62d87fae0ace14ee2c53
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\package\plugin;
3 use wcf\data\acp\template\ACPTemplate;
4 use wcf\data\acp\template\ACPTemplateList;
5 use wcf\data\template\listener\TemplateListenerEditor;
6 use wcf\data\template\listener\TemplateListenerList;
7 use wcf\data\template\Template;
8 use wcf\data\template\TemplateList;
9 use wcf\system\cache\builder\TemplateListenerCodeCacheBuilder;
10 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
11 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
12 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
13 use wcf\system\form\builder\container\FormContainer;
14 use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor;
15 use wcf\system\form\builder\field\dependency\ValueFormFieldDependency;
16 use wcf\system\form\builder\field\IntegerFormField;
17 use wcf\system\form\builder\field\option\OptionFormField;
18 use wcf\system\form\builder\field\user\group\option\UserGroupOptionFormField;
19 use wcf\system\form\builder\field\validation\FormFieldValidationError;
20 use wcf\system\form\builder\field\validation\FormFieldValidator;
21 use wcf\system\form\builder\field\MultilineTextFormField;
22 use wcf\system\form\builder\field\SingleSelectionFormField;
23 use wcf\system\form\builder\field\TextFormField;
24 use wcf\system\form\builder\IFormDocument;
25 use wcf\system\WCF;
26 use wcf\util\StringUtil;
27
28 /**
29 * Installs, updates and deletes template listeners.
30 *
31 * @author Alexander Ebert, Matthias Schmidt
32 * @copyright 2001-2018 WoltLab GmbH
33 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
34 * @package WoltLabSuite\Core\System\Package\Plugin
35 */
36 class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
37 use TXmlGuiPackageInstallationPlugin {
38 setEntryData as defaultSetEntryData;
39 }
40
41 /**
42 * @inheritDoc
43 */
44 public $className = TemplateListenerEditor::class;
45
46 /**
47 * @inheritDoc
48 */
49 protected function handleDelete(array $items) {
50 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
51 WHERE packageID = ?
52 AND environment = ?
53 AND eventName = ?
54 AND name = ?
55 AND templateName = ?";
56 $statement = WCF::getDB()->prepareStatement($sql);
57 foreach ($items as $item) {
58 $statement->execute([
59 $this->installation->getPackageID(),
60 $item['elements']['environment'],
61 $item['elements']['eventname'],
62 $item['attributes']['name'],
63 $item['elements']['templatename']
64 ]);
65 }
66 }
67
68 /**
69 * @inheritDoc
70 */
71 protected function prepareImport(array $data) {
72 $niceValue = isset($data['elements']['nice']) ? intval($data['elements']['nice']) : 0;
73 if ($niceValue < -128) {
74 $niceValue = -128;
75 }
76 else if ($niceValue > 127) {
77 $niceValue = 127;
78 }
79
80 return [
81 'environment' => $data['elements']['environment'],
82 'eventName' => $data['elements']['eventname'],
83 'niceValue' => $niceValue,
84 'name' => $data['attributes']['name'],
85 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
86 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
87 'templateCode' => $data['elements']['templatecode'],
88 'templateName' => $data['elements']['templatename']
89 ];
90 }
91
92 /**
93 * @inheritDoc
94 */
95 protected function findExistingItem(array $data) {
96 $sql = "SELECT *
97 FROM wcf".WCF_N."_".$this->tableName."
98 WHERE packageID = ?
99 AND name = ?
100 AND templateName = ?
101 AND eventName = ?
102 AND environment = ?";
103 $parameters = [
104 $this->installation->getPackageID(),
105 $data['name'],
106 $data['templateName'],
107 $data['eventName'],
108 $data['environment']
109 ];
110
111 return [
112 'sql' => $sql,
113 'parameters' => $parameters
114 ];
115 }
116
117 /**
118 * @inheritDoc
119 */
120 protected function cleanup() {
121 // clear cache immediately
122 TemplateListenerCodeCacheBuilder::getInstance()->reset();
123 }
124
125 /**
126 * @inheritDoc
127 * @since 3.1
128 */
129 public static function getSyncDependencies() {
130 return [];
131 }
132
133 /**
134 * @inheritDoc
135 * @since 3.2
136 */
137 protected function addFormFields(IFormDocument $form) {
138 $ldq = preg_quote(WCF::getTPL()->getCompiler()->getLeftDelimiter(), '~');
139 $rdq = preg_quote(WCF::getTPL()->getCompiler()->getRightDelimiter(), '~');
140
141 $getEvents = function($templateList) use ($ldq, $rdq) {
142 $templateEvents = [];
143 /** @var ACPTemplate|Template $template */
144 foreach ($templateList as $template) {
145 if (preg_match_all("~{$ldq}event\ name\=\'(?<event>[\w]+)\'{$rdq}~", $template->getSource(), $matches)) {
146 $templates[$template->templateName] = $template->templateName;
147
148 foreach ($matches['event'] as $event) {
149 if (!isset($templateEvents[$template->templateName])) {
150 $templateEvents[$template->templateName] = [];
151 }
152
153 $templateEvents[$template->templateName][] = $event;
154 }
155 }
156 }
157
158 foreach ($templateEvents as &$events) {
159 sort($events);
160 }
161 unset($events);
162
163 return $templateEvents;
164 };
165
166 $templateList = new TemplateList();
167 $templateList->getConditionBuilder()->add(
168 'template.packageID IN (?)',
169 [array_merge(
170 [$this->installation->getPackage()->packageID],
171 array_keys($this->installation->getPackage()->getAllRequiredPackages())
172 )]
173 );
174 $templateList->getConditionBuilder()->add('template.templateGroupID IS NULL');
175 $templateList->sqlOrderBy = 'template.templateName ASC';
176 $templateList->readObjects();
177
178 $templateEvents = $getEvents($templateList);
179
180 $acpTemplateList = new ACPTemplateList();
181 $acpTemplateList->getConditionBuilder()->add(
182 'acp_template.packageID IN (?)',
183 [array_merge(
184 [$this->installation->getPackage()->packageID],
185 array_keys($this->installation->getPackage()->getAllRequiredPackages())
186 )]
187 );
188 $acpTemplateList->sqlOrderBy = 'acp_template.templateName ASC';
189 $acpTemplateList->readObjects();
190
191 $acpTemplateEvents = $getEvents($acpTemplateList);
192
193 /** @var FormContainer $dataContainer */
194 $dataContainer = $form->getNodeById('data');
195
196 $dataContainer->appendChildren([
197 TextFormField::create('name')
198 ->label('wcf.acp.pip.templateListener.name')
199 ->description('wcf.acp.pip.templateListener.name.description')
200 ->required()
201 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
202 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
203 $formField->addValidationError(
204 new FormFieldValidationError(
205 'format',
206 'wcf.acp.pip.templateListener.name.error.format'
207 )
208 );
209 }
210 })),
211
212 SingleSelectionFormField::create('frontendTemplateName')
213 ->objectProperty('templatename')
214 ->label('wcf.acp.pip.templateListener.templateName')
215 ->description('wcf.acp.pip.templateListener.templateName.description')
216 ->required()
217 ->options(array_combine(array_keys($templateEvents), array_keys($templateEvents)))
218 ->filterable(),
219
220 SingleSelectionFormField::create('acpTemplateName')
221 ->objectProperty('templatename')
222 ->label('wcf.acp.pip.templateListener.templateName')
223 ->description('wcf.acp.pip.templateListener.templateName.description')
224 ->required()
225 ->options(array_combine(array_keys($acpTemplateEvents), array_keys($acpTemplateEvents)))
226 ->filterable()
227 ]);
228
229 /** @var SingleSelectionFormField $frontendTemplateName */
230 $frontendTemplateName = $form->getNodeById('frontendTemplateName');
231 foreach ($templateEvents as $templateName => $events) {
232 $dataContainer->appendChild(
233 SingleSelectionFormField::create($templateName . '_eventName')
234 ->objectProperty('eventname')
235 ->label('wcf.acp.pip.templateListener.eventName')
236 ->description('wcf.acp.pip.templateListener.eventName.description')
237 ->required()
238 ->options(array_combine($events, $events))
239 ->addDependency(
240 ValueFormFieldDependency::create('templateName')
241 ->field($frontendTemplateName)
242 ->values([$templateName])
243 )
244 );
245 }
246
247 /** @var SingleSelectionFormField $acpTemplateName */
248 $acpTemplateName = $form->getNodeById('acpTemplateName');
249 foreach ($acpTemplateEvents as $templateName => $events) {
250 $dataContainer->appendChild(
251 SingleSelectionFormField::create('acp_' . $templateName . '_eventName')
252 ->objectProperty('eventname')
253 ->label('wcf.acp.pip.templateListener.eventName')
254 ->description('wcf.acp.pip.templateListener.eventName.description')
255 ->required()
256 ->options(array_combine($events, $events))
257 ->addDependency(
258 ValueFormFieldDependency::create('acpTemplateName')
259 ->field($acpTemplateName)
260 ->values([$templateName])
261 )
262 );
263 }
264
265 $dataContainer->appendChildren([
266 SingleSelectionFormField::create('environment')
267 ->label('wcf.acp.pip.templateListener.environment')
268 ->description('wcf.acp.pip.templateListener.environment.description')
269 ->required()
270 ->options([
271 'admin' => 'admin',
272 'user' => 'user'
273 ])
274 ->value('user')
275 ->addValidator(new FormFieldValidator('uniqueness', function(SingleSelectionFormField $formField) {
276 /** @var TextFormField $nameField */
277 $nameField = $formField->getDocument()->getNodeById('name');
278
279 /** @var SingleSelectionFormField $templateNameFormField */
280 $templateNameFormField = $formField->getDocument()->getNodeById('frontendTemplateName');
281
282 /** @var SingleSelectionFormField $acpTemplateNameFormField */
283 $acpTemplateNameFormField = $formField->getDocument()->getNodeById('acpTemplateName');
284
285 if (
286 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
287 $this->editedEntry->getAttribute('name') !== $nameField->getSaveValue() ||
288 $this->editedEntry->getElementsByTagName('environment')->item(0)->nodeValue !== $formField->getSaveValue() ||
289 (
290 $formField->getSaveValue() === 'admin' &&
291 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $acpTemplateNameFormField->getSaveValue()
292 ) ||
293 (
294 $formField->getSaveValue() === 'user' &&
295 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $templateNameFormField->getSaveValue()
296 )
297 ) {
298 $listenerList = new TemplateListenerList();
299 $listenerList->getConditionBuilder()->add(
300 'name = ?',
301 [$nameField->getSaveValue()]
302 );
303
304 if ($formField->getSaveValue() === 'admin') {
305 /** @var SingleSelectionFormField $templateNameField */
306 $templateNameField = $formField->getDocument()->getNodeById('acpTemplateName');
307
308 /** @var SingleSelectionFormField $eventNameField */
309 $eventNameField = $formField->getDocument()->getNodeById('acp_' . $templateNameField->getSaveValue() . '_eventName');
310 } else {
311 /** @var SingleSelectionFormField $templateNameField */
312 $templateNameField = $formField->getDocument()->getNodeById('frontendTemplateName');
313
314 /** @var SingleSelectionFormField $eventNameField */
315 $eventNameField = $formField->getDocument()->getNodeById($templateNameField->getSaveValue() . '_eventName');
316 }
317
318 $templateName = $templateNameField->getSaveValue();
319 $eventName = $eventNameField->getSaveValue();
320
321 $listenerList->getConditionBuilder()->add('templateName = ?', [$templateName]);
322 $listenerList->getConditionBuilder()->add('eventName = ?', [$eventName]);
323 $listenerList->getConditionBuilder()->add('environment = ?', [$formField->getSaveValue()]);
324
325 if ($listenerList->countObjects() > 0) {
326 $nameField->addValidationError(
327 new FormFieldValidationError(
328 'notUnique',
329 'wcf.acp.pip.templateListener.name.error.notUnique'
330 )
331 );
332 }
333 }
334 })),
335
336 MultilineTextFormField::create('templateCode')
337 ->objectProperty('templatecode')
338 ->label('wcf.acp.pip.templateListener.templateCode')
339 ->description('wcf.acp.pip.templateListener.templateCode.description')
340 ->required(),
341
342 IntegerFormField::create('niceValue')
343 ->objectProperty('nice')
344 ->label('wcf.acp.pip.templateListener.niceValue')
345 ->description('wcf.acp.pip.templateListener.niceValue.description')
346 ->nullable()
347 ->minimum(-128)
348 ->maximum(127),
349
350 OptionFormField::create()
351 ->description('wcf.acp.pip.templateListener.options.description')
352 ->packageIDs(array_merge(
353 [$this->installation->getPackage()->packageID],
354 array_keys($this->installation->getPackage()->getAllRequiredPackages())
355 )),
356
357 UserGroupOptionFormField::create()
358 ->description('wcf.acp.pip.templateListener.permissions.description')
359 ->packageIDs(array_merge(
360 [$this->installation->getPackage()->packageID],
361 array_keys($this->installation->getPackage()->getAllRequiredPackages())
362 ))
363 ]);
364
365 /** @var SingleSelectionFormField $environment */
366 $environment = $form->getNodeById('environment');
367
368 $form->getNodeById('frontendTemplateName')->addDependency(
369 ValueFormFieldDependency::create('environment')
370 ->field($environment)
371 ->values(['user'])
372 );
373 $form->getNodeById('acpTemplateName')->addDependency(
374 ValueFormFieldDependency::create('environment')
375 ->field($environment)
376 ->values(['admin'])
377 );
378
379 // ensure proper normalization of template code
380 $form->getDataHandler()->add(new CustomFormFieldDataProcessor('templateCode', function(IFormDocument $document, array $parameters) {
381 $parameters['data']['templatecode'] = StringUtil::unifyNewlines(StringUtil::escapeCDATA($parameters['data']['templatecode']));
382
383 return $parameters;
384 }));
385 }
386
387 /**
388 * @inheritDoc
389 * @since 3.2
390 */
391 protected function fetchElementData(\DOMElement $element, $saveData) {
392 $data = [
393 'environment' => $element->getElementsByTagName('environment')->item(0)->nodeValue,
394 'eventName' => $element->getElementsByTagName('eventname')->item(0)->nodeValue,
395 'name' => $element->getAttribute('name'),
396 'packageID' => $this->installation->getPackage()->packageID,
397 'templateCode' => $element->getElementsByTagName('templatecode')->item(0)->nodeValue,
398 'templateName' => $element->getElementsByTagName('templatename')->item(0)->nodeValue
399 ];
400
401 $nice = $element->getElementsByTagName('nice')->item(0);
402 if ($nice !== null) {
403 $data['niceValue'] = $nice->nodeValue;
404 }
405 else if ($saveData) {
406 $data['niceValue'] = 0;
407 }
408
409 foreach (['options', 'permissions'] as $elementName) {
410 $optionalElement = $element->getElementsByTagName($elementName)->item(0);
411 if ($optionalElement !== null) {
412 $data[$elementName] = $optionalElement->nodeValue;
413 }
414 else if ($saveData) {
415 $data[$elementName] = '';
416 }
417 }
418
419 return $data;
420 }
421
422 /**
423 * @inheritDoc
424 * @since 3.2
425 */
426 public function getElementIdentifier(\DOMElement $element) {
427 return sha1(
428 $element->getElementsByTagName('templatename')->item(0)->nodeValue . '/' .
429 $element->getElementsByTagName('eventname')->item(0)->nodeValue . '/' .
430 $element->getElementsByTagName('environment')->item(0)->nodeValue . '/' .
431 $element->getAttribute('name')
432 );
433 }
434
435 /**
436 * @inheritDoc
437 * @since 3.2
438 */
439 public function setEntryData($identifier, IFormDocument $document) {
440 if ($this->defaultSetEntryData($identifier, $document)) {
441 $data = $this->getElementData($this->getElementByIdentifier($this->getProjectXml(), $identifier));
442
443 switch ($data['environment']) {
444 case 'admin':
445 /** @var SingleSelectionFormField $templateName */
446 $templateName = $document->getNodeById('acpTemplateName');
447
448 /** @var SingleSelectionFormField $eventName */
449 $eventName = $document->getNodeById('acp_' . $data['templateName'] . '_eventName');
450 break;
451
452 case 'user':
453 /** @var SingleSelectionFormField $templateName */
454 $templateName = $document->getNodeById('frontendTemplateName');
455
456 /** @var SingleSelectionFormField $eventName */
457 $eventName = $document->getNodeById($data['templateName'] . '_eventName');
458 break;
459
460 default:
461 throw new \LogicException("Unknown enviornment '{$data['environment']}'.");
462 }
463
464 $templateName->value($data['templateName']);
465 $eventName->value($data['eventName']);
466
467 return true;
468 }
469
470 return false;
471 }
472
473 /**
474 * @inheritDoc
475 * @since 3.2
476 */
477 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
478 $entryList->setKeys([
479 'name' => 'wcf.acp.pip.templateListener.name',
480 'templateName' => 'wcf.acp.pip.templateListener.templateName',
481 'eventName' => 'wcf.acp.pip.templateListener.eventName',
482 'environment' => 'wcf.acp.pip.templateListener.environment'
483 ]);
484 }
485
486 /**
487 * @inheritDoc
488 * @since 3.2
489 */
490 protected function prepareXmlElement(\DOMDocument $document, IFormDocument $form) {
491 $data = $form->getData()['data'];
492
493 $listener = $document->createElement($this->tagName);
494 $listener->setAttribute('name', $data['name']);
495
496 $this->appendElementChildren(
497 $listener,
498 [
499 'environment',
500 'templatename',
501 'eventname',
502 'templatecode' => [
503 'cdata' => true
504 ],
505 'nice' => 0,
506 'options' => '',
507 'permissions' => ''
508 ],
509 $form
510 );
511
512 return $listener;
513 }
514
515 /**
516 * @inheritDoc
517 * @since 3.2
518 */
519 protected function prepareDeleteXmlElement(\DOMElement $element) {
520 $templateListener = $element->ownerDocument->createElement($this->tagName);
521 $templateListener->setAttribute('name', $element->getAttribute('name'));
522
523 foreach (['environment', 'templatename', 'eventname'] as $childElement) {
524 $templateListener->appendChild($element->ownerDocument->createElement(
525 $childElement,
526 $element->getElementsByTagName($childElement)->item(0)->nodeValue
527 ));
528 }
529
530 return $templateListener;
531 }
532
533 /**
534 * @inheritDoc
535 * @since 3.2
536 */
537 protected function deleteObject(\DOMElement $element) {
538 $elements= [];
539 foreach (['environment', 'templatename', 'eventname'] as $childElement) {
540 $elements[$childElement] = $element->getElementsByTagName($childElement)->item(0)->nodeValue;
541 }
542
543 $this->handleDelete([[
544 'attributes' => ['name' => $element->getAttribute('name')],
545 'elements' => $elements
546 ]]);
547 }
548 }