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\dependency\ValueFormFieldDependency;
15 use wcf\system\form\builder\field\validation\FormFieldValidationError;
16 use wcf\system\form\builder\field\validation\FormFieldValidator;
17 use wcf\system\form\builder\field\MultilineTextFormField;
18 use wcf\system\form\builder\field\SingleSelectionFormField;
19 use wcf\system\form\builder\field\TextFormField;
20 use wcf\system\form\builder\IFormDocument;
22 use wcf\util\StringUtil;
25 * Installs, updates and deletes template listeners.
27 * @author Alexander Ebert, Matthias Schmidt
28 * @copyright 2001-2018 WoltLab GmbH
29 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
30 * @package WoltLabSuite\Core\System\Package\Plugin
32 class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
33 use TXmlGuiPackageInstallationPlugin {
34 setEntryData as defaultSetEntryData;
40 public $className = TemplateListenerEditor::class;
45 protected function handleDelete(array $items) {
46 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
51 AND templateName = ?";
52 $statement = WCF::getDB()->prepareStatement($sql);
53 foreach ($items as $item) {
55 $this->installation->getPackageID(),
56 $item['elements']['environment'],
57 $item['elements']['eventname'],
58 $item['attributes']['name'],
59 $item['elements']['templatename']
67 protected function prepareImport(array $data) {
68 $niceValue = isset($data['elements']['nice']) ? intval($data['elements']['nice']) : 0;
69 if ($niceValue < -128) {
72 else if ($niceValue > 127) {
77 'environment' => $data['elements']['environment'],
78 'eventName' => $data['elements']['eventname'],
79 'niceValue' => $niceValue,
80 'name' => $data['attributes']['name'],
81 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
82 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
83 'templateCode' => $data['elements']['templatecode'],
84 'templateName' => $data['elements']['templatename']
91 protected function findExistingItem(array $data) {
93 FROM wcf".WCF_N."_".$this->tableName."
100 $this->installation->getPackageID(),
102 $data['templateName'],
109 'parameters' => $parameters
116 protected function cleanup() {
117 // clear cache immediately
118 TemplateListenerCodeCacheBuilder::getInstance()->reset();
125 public static function getSyncDependencies() {
133 public function addFormFields(IFormDocument $form) {
134 $ldq = preg_quote(WCF::getTPL()->getCompiler()->getLeftDelimiter(), '~');
135 $rdq = preg_quote(WCF::getTPL()->getCompiler()->getRightDelimiter(), '~');
137 $getEvents = function($templateList) use ($ldq, $rdq) {
138 $templateEvents = [];
139 /** @var ACPTemplate|Template $template */
140 foreach ($templateList as $template) {
141 if (preg_match_all("~{$ldq}event\ name\=\'(?<event>[\w]+)\'{$rdq}~", $template->getSource(), $matches)) {
142 $templates[$template->templateName] = $template->templateName;
144 foreach ($matches['event'] as $event) {
145 if (!isset($templateEvents[$template->templateName])) {
146 $templateEvents[$template->templateName] = [];
149 $templateEvents[$template->templateName][] = $event;
154 foreach ($templateEvents as &$events) {
159 return $templateEvents;
162 $templateList = new TemplateList();
163 $templateList->getConditionBuilder()->add(
164 'template.packageID IN (?)',
166 [$this->installation->getPackage()->packageID],
167 array_keys($this->installation->getPackage()->getAllRequiredPackages())
170 $templateList->getConditionBuilder()->add('template.templateGroupID IS NULL');
171 $templateList->sqlOrderBy = 'template.templateName ASC';
172 $templateList->readObjects();
174 $templateEvents = $getEvents($templateList);
176 $acpTemplateList = new ACPTemplateList();
177 $acpTemplateList->getConditionBuilder()->add(
178 'acp_template.packageID IN (?)',
180 [$this->installation->getPackage()->packageID],
181 array_keys($this->installation->getPackage()->getAllRequiredPackages())
184 $acpTemplateList->sqlOrderBy = 'acp_template.templateName ASC';
185 $acpTemplateList->readObjects();
187 $acpTemplateEvents = $getEvents($acpTemplateList);
189 /** @var FormContainer $dataContainer */
190 $dataContainer = $form->getNodeById('data');
192 $dataContainer->appendChildren([
193 TextFormField::create('name')
194 ->label('wcf.acp.pip.templateListener.name')
195 ->description('wcf.acp.pip.templateListener.name.description')
197 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
198 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
199 $formField->addValidationError(
200 new FormFieldValidationError(
202 'wcf.acp.pip.templateListener.name.error.format'
208 SingleSelectionFormField::create('frontendTemplateName')
209 ->objectProperty('templatename')
210 ->label('wcf.acp.pip.templateListener.templateName')
211 ->description('wcf.acp.pip.templateListener.templateName.description')
213 ->options(array_combine(array_keys($templateEvents), array_keys($templateEvents)))
216 SingleSelectionFormField::create('acpTemplateName')
217 ->objectProperty('templatename')
218 ->label('wcf.acp.pip.templateListener.templateName')
219 ->description('wcf.acp.pip.templateListener.templateName.description')
221 ->options(array_combine(array_keys($acpTemplateEvents), array_keys($acpTemplateEvents)))
225 foreach ($templateEvents as $templateName => $events) {
226 $dataContainer->appendChild(
227 SingleSelectionFormField::create($templateName . '_eventName')
228 ->objectProperty('eventName')
229 ->label('wcf.acp.pip.templateListener.eventName')
230 ->description('wcf.acp.pip.templateListener.eventName.description')
232 ->options(array_combine($events, $events))
234 ValueFormFieldDependency::create('templateName')
235 ->field($form->getNodeById('frontendTemplateName'))
236 ->values([$templateName])
241 foreach ($acpTemplateEvents as $templateName => $events) {
242 $dataContainer->appendChild(
243 SingleSelectionFormField::create('acp_' . $templateName . '_eventName')
244 ->objectProperty('eventName')
245 ->label('wcf.acp.pip.templateListener.eventName')
246 ->description('wcf.acp.pip.templateListener.eventName.description')
248 ->options(array_combine($events, $events))
250 ValueFormFieldDependency::create('acpTemplateName')
251 ->field($form->getNodeById('acpTemplateName'))
252 ->values([$templateName])
257 $dataContainer->appendChildren([
258 SingleSelectionFormField::create('environment')
259 ->label('wcf.acp.pip.templateListener.environment')
260 ->description('wcf.acp.pip.templateListener.environment.description')
267 ->addValidator(new FormFieldValidator('uniqueness', function(SingleSelectionFormField $formField) {
268 /** @var TextFormField $nameField */
269 $nameField = $formField->getDocument()->getNodeById('name');
271 /** @var SingleSelectionFormField $actionNameFormField */
272 $templateNameFormField = $formField->getDocument()->getNodeById('templateName');
274 /** @var SingleSelectionFormField $actionNameFormField */
275 $acpTemplateNameFormField = $formField->getDocument()->getNodeById('acpTemplateName');
278 $formField->getDocument()->getFormMode() === IFormDocument::FORM_MODE_CREATE ||
279 $this->editedEntry->getAttribute('name') !== $nameField->getSaveValue() ||
280 $this->editedEntry->getElementsByTagName('environment')->item(0)->nodeValue !== $formField->getSaveValue() ||
282 $formField->getSaveValue() === 'admin' &&
283 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $acpTemplateNameFormField->getSaveValue()
286 $formField->getSaveValue() === 'user' &&
287 $this->editedEntry->getElementsByTagName('templatename')->item(0)->nodeValue !== $templateNameFormField->getSaveValue()
290 $listenerList = new TemplateListenerList();
291 $listenerList->getConditionBuilder()->add(
293 [$nameField->getSaveValue()]
296 if ($formField->getSaveValue() === 'admin') {
297 /** @var SingleSelectionFormField $templateNameField */
298 $templateNameField = $formField->getDocument()->getNodeById('acpTemplateName');
300 /** @var SingleSelectionFormField $eventNameField */
301 $eventNameField = $formField->getDocument()->getNodeById('acp_' . $templateNameField->getSaveValue() . '_eventName');
303 /** @var SingleSelectionFormField $templateNameField */
304 $templateNameField = $formField->getDocument()->getNodeById('frontendTemplateName');
306 /** @var SingleSelectionFormField $eventNameField */
307 $eventNameField = $formField->getDocument()->getNodeById($templateNameField->getSaveValue() . '_eventName');
310 $templateName = $templateNameField->getSaveValue();
311 $eventName = $eventNameField->getSaveValue();
313 $listenerList->getConditionBuilder()->add('templateName = ?', [$templateName]);
314 $listenerList->getConditionBuilder()->add('eventName = ?', [$eventName]);
315 $listenerList->getConditionBuilder()->add('environment = ?', [$formField->getSaveValue()]);
317 if ($listenerList->countObjects() > 0) {
318 $nameField->addValidationError(
319 new FormFieldValidationError(
321 'wcf.acp.pip.templateListener.name.error.notUnique'
328 // TODO: use field with code support
329 MultilineTextFormField::create('templateCode')
330 ->objectProperty('templatecode')
331 ->label('wcf.acp.pip.templateListener.templateCode')
332 ->description('wcf.acp.pip.templateListener.templateCode.description')
336 $form->getNodeById('frontendTemplateName')->addDependency(
337 ValueFormFieldDependency::create('environment')
338 ->field($form->getNodeById('environment'))
341 $form->getNodeById('acpTemplateName')->addDependency(
342 ValueFormFieldDependency::create('environment')
343 ->field($form->getNodeById('environment'))
352 protected function getElementData(\DOMElement $element, $saveData = false) {
354 'environment' => $element->getElementsByTagName('environment')->item(0)->nodeValue,
355 'eventName' => $element->getElementsByTagName('eventname')->item(0)->nodeValue,
356 'name' => $element->getAttribute('name'),
357 'packageID' => $this->installation->getPackage()->packageID,
358 'templateCode' => $element->getElementsByTagName('templatecode')->item(0)->nodeValue,
359 'templateName' => $element->getElementsByTagName('templatename')->item(0)->nodeValue
367 public function getElementIdentifier(\DOMElement $element) {
369 $element->getElementsByTagName('templatename')->item(0)->nodeValue . '/' .
370 $element->getElementsByTagName('eventname')->item(0)->nodeValue . '/' .
371 $element->getElementsByTagName('environment')->item(0)->nodeValue . '/' .
372 $element->getAttribute('name')
380 public function setEntryData($identifier, IFormDocument $document) {
381 if ($this->defaultSetEntryData($identifier, $document)) {
382 $data = $this->getElementData($this->getElementByIdentifier($this->getProjectXml(), $identifier));
384 switch ($data['environment']) {
386 /** @var SingleSelectionFormField $templateName */
387 $templateName = $document->getNodeById('acpTemplateName');
389 /** @var SingleSelectionFormField $eventName */
390 $eventName = $document->getNodeById('acp_' . $data['templateName'] . '_eventName');
394 /** @var SingleSelectionFormField $templateName */
395 $templateName = $document->getNodeById('templateName');
397 /** @var SingleSelectionFormField $eventName */
398 $eventName = $document->getNodeById($data['templateName'] . '_eventName');
402 throw new \LogicException("Unknown enviornment '{$data['environment']}'.");
405 $templateName->value($data['templateName']);
406 $eventName->value($data['eventName']);
418 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
419 $entryList->setKeys([
420 'name' => 'wcf.acp.pip.templateListener.name',
421 'templateName' => 'wcf.acp.pip.templateListener.templateName',
422 'eventName' => 'wcf.acp.pip.templateListener.eventName',
423 'environment' => 'wcf.acp.pip.templateListener.environment'
431 protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
432 $data = $form->getData()['data'];
434 $listener = $document->createElement($this->tagName);
435 $listener->setAttribute('name', $data['name']);
437 $listener->appendChild($document->createElement('environment', $data['environment']));
438 $listener->appendChild($document->createElement('templatename', $data['templatename']));
439 $listener->appendChild($document->createElement('eventname', $data['eventName']));
441 $templateCode = $document->createElement('templatecode');
442 $templateCode->appendChild($document->createCDATASection(StringUtil::unifyNewlines(StringUtil::escapeCDATA($data['templatecode']))));
443 $listener->appendChild($templateCode);