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;
26 use wcf\util\StringUtil;
29 * Installs, updates and deletes template listeners.
31 * @author Alexander Ebert, Matthias Schmidt
32 * @copyright 2001-2019 WoltLab GmbH
33 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
34 * @package WoltLabSuite\Core\System\Package\Plugin
36 class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
37 use TXmlGuiPackageInstallationPlugin {
38 setEntryData as defaultSetEntryData;
44 public $className = TemplateListenerEditor::class;
49 protected function handleDelete(array $items) {
50 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
55 AND templateName = ?";
56 $statement = WCF::getDB()->prepareStatement($sql);
57 foreach ($items as $item) {
59 $this->installation->getPackageID(),
60 $item['elements']['environment'],
61 $item['elements']['eventname'],
62 $item['attributes']['name'],
63 $item['elements']['templatename']
71 protected function prepareImport(array $data) {
72 $niceValue = isset($data['elements']['nice']) ? intval($data['elements']['nice']) : 0;
73 if ($niceValue < -128) {
76 else if ($niceValue > 127) {
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']
95 protected function findExistingItem(array $data) {
97 FROM wcf".WCF_N."_".$this->tableName."
102 AND environment = ?";
104 $this->installation->getPackageID(),
106 $data['templateName'],
113 'parameters' => $parameters
120 protected function cleanup() {
121 // clear cache immediately
122 TemplateListenerCodeCacheBuilder::getInstance()->reset();
129 public static function getSyncDependencies() {
137 protected function addFormFields(IFormDocument $form) {
138 $ldq = preg_quote(WCF::getTPL()->getCompiler()->getLeftDelimiter(), '~');
139 $rdq = preg_quote(WCF::getTPL()->getCompiler()->getRightDelimiter(), '~');
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;
148 foreach ($matches['event'] as $event) {
149 if (!isset($templateEvents[$template->templateName])) {
150 $templateEvents[$template->templateName] = [];
153 $templateEvents[$template->templateName][] = $event;
158 foreach ($templateEvents as &$events) {
163 return $templateEvents;
166 $templateList = new TemplateList();
167 $templateList->getConditionBuilder()->add(
168 'template.packageID IN (?)',
170 [$this->installation->getPackage()->packageID],
171 array_keys($this->installation->getPackage()->getAllRequiredPackages())
174 $templateList->getConditionBuilder()->add('template.templateGroupID IS NULL');
175 $templateList->sqlOrderBy = 'template.templateName ASC';
176 $templateList->readObjects();
178 $templateEvents = $getEvents($templateList);
180 $acpTemplateList = new ACPTemplateList();
181 $acpTemplateList->getConditionBuilder()->add(
182 'acp_template.packageID IN (?)',
184 [$this->installation->getPackage()->packageID],
185 array_keys($this->installation->getPackage()->getAllRequiredPackages())
188 $acpTemplateList->sqlOrderBy = 'acp_template.templateName ASC';
189 $acpTemplateList->readObjects();
191 $acpTemplateEvents = $getEvents($acpTemplateList);
193 /** @var FormContainer $dataContainer */
194 $dataContainer = $form->getNodeById('data');
196 $dataContainer->appendChildren([
197 TextFormField::create('name')
198 ->label('wcf.acp.pip.templateListener.name')
199 ->description('wcf.acp.pip.templateListener.name.description')
201 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
202 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
203 $formField->addValidationError(
204 new FormFieldValidationError(
206 'wcf.acp.pip.templateListener.name.error.format'
212 SingleSelectionFormField::create('environment')
213 ->label('wcf.acp.pip.templateListener.environment')
214 ->description('wcf.acp.pip.templateListener.environment.description')
221 ->addValidator(new FormFieldValidator('uniqueness', function(SingleSelectionFormField $formField) {
222 /** @var TextFormField $nameField */
223 $nameField = $formField->getDocument()->getNodeById('name');
225 /** @var SingleSelectionFormField $templateNameFormField */
226 $templateNameFormField = $formField->getDocument()->getNodeById('frontendTemplateName');
228 /** @var SingleSelectionFormField $acpTemplateNameFormField */
229 $acpTemplateNameFormField = $formField->getDocument()->getNodeById('acpTemplateName');
232 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
233 $this->editedEntry->getAttribute('name') !== $nameField->getSaveValue() ||
234 $this->editedEntry->getElementsByTagName('environment')->item(0)->nodeValue !== $formField->getSaveValue() ||
236 $formField->getSaveValue() === 'admin' &&
237 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $acpTemplateNameFormField->getSaveValue()
240 $formField->getSaveValue() === 'user' &&
241 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $templateNameFormField->getSaveValue()
244 $listenerList = new TemplateListenerList();
245 $listenerList->getConditionBuilder()->add(
247 [$nameField->getSaveValue()]
250 if ($formField->getSaveValue() === 'admin') {
251 /** @var SingleSelectionFormField $templateNameField */
252 $templateNameField = $formField->getDocument()->getNodeById('acpTemplateName');
254 /** @var SingleSelectionFormField $eventNameField */
255 $eventNameField = $formField->getDocument()->getNodeById('acp_' . $templateNameField->getSaveValue() . '_eventName');
258 /** @var SingleSelectionFormField $templateNameField */
259 $templateNameField = $formField->getDocument()->getNodeById('frontendTemplateName');
261 /** @var SingleSelectionFormField $eventNameField */
262 $eventNameField = $formField->getDocument()->getNodeById($templateNameField->getSaveValue() . '_eventName');
265 $templateName = $templateNameField->getSaveValue();
266 $eventName = $eventNameField->getSaveValue();
268 $listenerList->getConditionBuilder()->add('templateName = ?', [$templateName]);
269 $listenerList->getConditionBuilder()->add('eventName = ?', [$eventName]);
270 $listenerList->getConditionBuilder()->add('environment = ?', [$formField->getSaveValue()]);
272 if ($listenerList->countObjects() > 0) {
273 $nameField->addValidationError(
274 new FormFieldValidationError(
276 'wcf.acp.pip.templateListener.name.error.notUnique'
283 SingleSelectionFormField::create('frontendTemplateName')
284 ->objectProperty('templatename')
285 ->label('wcf.acp.pip.templateListener.templateName')
286 ->description('wcf.acp.pip.templateListener.templateName.description')
288 ->options(array_combine(array_keys($templateEvents), array_keys($templateEvents)))
291 ValueFormFieldDependency::create('environment')
292 ->fieldId('environment')
296 SingleSelectionFormField::create('acpTemplateName')
297 ->objectProperty('templatename')
298 ->label('wcf.acp.pip.templateListener.templateName')
299 ->description('wcf.acp.pip.templateListener.templateName.description')
301 ->options(array_combine(array_keys($acpTemplateEvents), array_keys($acpTemplateEvents)))
304 ValueFormFieldDependency::create('environment')
305 ->fieldId('environment')
310 /** @var SingleSelectionFormField $frontendTemplateName */
311 $frontendTemplateName = $form->getNodeById('frontendTemplateName');
312 foreach ($templateEvents as $templateName => $events) {
313 $dataContainer->appendChild(
314 SingleSelectionFormField::create($templateName . '_eventName')
315 ->objectProperty('eventname')
316 ->label('wcf.acp.pip.templateListener.eventName')
317 ->description('wcf.acp.pip.templateListener.eventName.description')
319 ->options(array_combine($events, $events))
321 ValueFormFieldDependency::create('templateName')
322 ->field($frontendTemplateName)
323 ->values([$templateName])
328 /** @var SingleSelectionFormField $acpTemplateName */
329 $acpTemplateName = $form->getNodeById('acpTemplateName');
330 foreach ($acpTemplateEvents as $templateName => $events) {
331 $dataContainer->appendChild(
332 SingleSelectionFormField::create('acp_' . $templateName . '_eventName')
333 ->objectProperty('eventname')
334 ->label('wcf.acp.pip.templateListener.eventName')
335 ->description('wcf.acp.pip.templateListener.eventName.description')
337 ->options(array_combine($events, $events))
339 ValueFormFieldDependency::create('acpTemplateName')
340 ->field($acpTemplateName)
341 ->values([$templateName])
346 $dataContainer->appendChildren([
347 MultilineTextFormField::create('templateCode')
348 ->objectProperty('templatecode')
349 ->label('wcf.acp.pip.templateListener.templateCode')
350 ->description('wcf.acp.pip.templateListener.templateCode.description')
353 IntegerFormField::create('niceValue')
354 ->objectProperty('nice')
355 ->label('wcf.acp.pip.templateListener.niceValue')
356 ->description('wcf.acp.pip.templateListener.niceValue.description')
361 OptionFormField::create()
362 ->description('wcf.acp.pip.templateListener.options.description')
363 ->packageIDs(array_merge(
364 [$this->installation->getPackage()->packageID],
365 array_keys($this->installation->getPackage()->getAllRequiredPackages())
368 UserGroupOptionFormField::create()
369 ->description('wcf.acp.pip.templateListener.permissions.description')
370 ->packageIDs(array_merge(
371 [$this->installation->getPackage()->packageID],
372 array_keys($this->installation->getPackage()->getAllRequiredPackages())
376 // ensure proper normalization of template code
377 $form->getDataHandler()->add(new CustomFormFieldDataProcessor('templateCode', function(IFormDocument $document, array $parameters) {
378 $parameters['data']['templatecode'] = StringUtil::unifyNewlines(StringUtil::escapeCDATA($parameters['data']['templatecode']));
388 protected function fetchElementData(\DOMElement $element, $saveData) {
390 'environment' => $element->getElementsByTagName('environment')->item(0)->nodeValue,
391 'eventName' => $element->getElementsByTagName('eventname')->item(0)->nodeValue,
392 'name' => $element->getAttribute('name'),
393 'packageID' => $this->installation->getPackage()->packageID,
394 'templateCode' => $element->getElementsByTagName('templatecode')->item(0)->nodeValue,
395 'templateName' => $element->getElementsByTagName('templatename')->item(0)->nodeValue
398 $nice = $element->getElementsByTagName('nice')->item(0);
399 if ($nice !== null) {
400 $data['niceValue'] = $nice->nodeValue;
402 else if ($saveData) {
403 $data['niceValue'] = 0;
406 foreach (['options', 'permissions'] as $elementName) {
407 $optionalElement = $element->getElementsByTagName($elementName)->item(0);
408 if ($optionalElement !== null) {
409 $data[$elementName] = $optionalElement->nodeValue;
411 else if ($saveData) {
412 $data[$elementName] = '';
423 public function getElementIdentifier(\DOMElement $element) {
425 $element->getElementsByTagName('templatename')->item(0)->nodeValue . '/' .
426 $element->getElementsByTagName('eventname')->item(0)->nodeValue . '/' .
427 $element->getElementsByTagName('environment')->item(0)->nodeValue . '/' .
428 $element->getAttribute('name')
436 public function setEntryData($identifier, IFormDocument $document) {
437 if ($this->defaultSetEntryData($identifier, $document)) {
438 $data = $this->getElementData($this->getElementByIdentifier($this->getProjectXml(), $identifier));
440 switch ($data['environment']) {
442 /** @var SingleSelectionFormField $templateName */
443 $templateName = $document->getNodeById('acpTemplateName');
445 /** @var SingleSelectionFormField $eventName */
446 $eventName = $document->getNodeById('acp_' . $data['templateName'] . '_eventName');
450 /** @var SingleSelectionFormField $templateName */
451 $templateName = $document->getNodeById('frontendTemplateName');
453 /** @var SingleSelectionFormField $eventName */
454 $eventName = $document->getNodeById($data['templateName'] . '_eventName');
458 throw new \LogicException("Unknown enviornment '{$data['environment']}'.");
461 $templateName->value($data['templateName']);
462 $eventName->value($data['eventName']);
474 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
475 $entryList->setKeys([
476 'name' => 'wcf.acp.pip.templateListener.name',
477 'templateName' => 'wcf.acp.pip.templateListener.templateName',
478 'eventName' => 'wcf.acp.pip.templateListener.eventName',
479 'environment' => 'wcf.acp.pip.templateListener.environment'
487 protected function prepareXmlElement(\DOMDocument $document, IFormDocument $form) {
488 $data = $form->getData()['data'];
490 $listener = $document->createElement($this->tagName);
491 $listener->setAttribute('name', $data['name']);
493 $this->appendElementChildren(
516 protected function prepareDeleteXmlElement(\DOMElement $element) {
517 $templateListener = $element->ownerDocument->createElement($this->tagName);
518 $templateListener->setAttribute('name', $element->getAttribute('name'));
520 foreach (['environment', 'templatename', 'eventname'] as $childElement) {
521 $templateListener->appendChild($element->ownerDocument->createElement(
523 $element->getElementsByTagName($childElement)->item(0)->nodeValue
527 return $templateListener;
534 protected function deleteObject(\DOMElement $element) {
536 foreach (['environment', 'templatename', 'eventname'] as $childElement) {
537 $elements[$childElement] = $element->getElementsByTagName($childElement)->item(0)->nodeValue;
540 $this->handleDelete([[
541 'attributes' => ['name' => $element->getAttribute('name')],
542 'elements' => $elements