2 declare(strict_types=1);
3 namespace wcf\system\package\plugin;
4 use wcf\data\acp\template\ACPTemplate;
5 use wcf\data\acp\template\ACPTemplateList;
6 use wcf\data\template\listener\TemplateListenerEditor;
7 use wcf\data\template\listener\TemplateListenerList;
8 use wcf\data\template\Template;
9 use wcf\data\template\TemplateList;
10 use wcf\system\cache\builder\TemplateListenerCodeCacheBuilder;
11 use wcf\system\devtools\pip\IDevtoolsPipEntryList;
12 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
13 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
14 use wcf\system\form\builder\container\FormContainer;
15 use wcf\system\form\builder\field\dependency\ValueFormFieldDependency;
16 use wcf\system\form\builder\field\validation\FormFieldValidationError;
17 use wcf\system\form\builder\field\validation\FormFieldValidator;
18 use wcf\system\form\builder\field\MultilineTextFormField;
19 use wcf\system\form\builder\field\SingleSelectionFormField;
20 use wcf\system\form\builder\field\TextFormField;
21 use wcf\system\form\builder\IFormDocument;
23 use wcf\util\StringUtil;
26 * Installs, updates and deletes template listeners.
28 * @author Alexander Ebert, Matthias Schmidt
29 * @copyright 2001-2018 WoltLab GmbH
30 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
31 * @package WoltLabSuite\Core\System\Package\Plugin
33 class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin implements IGuiPackageInstallationPlugin {
34 use TXmlGuiPackageInstallationPlugin;
39 public $className = TemplateListenerEditor::class;
44 protected function handleDelete(array $items) {
45 $sql = "DELETE FROM wcf".WCF_N."_".$this->tableName."
50 AND templateName = ?";
51 $statement = WCF::getDB()->prepareStatement($sql);
52 foreach ($items as $item) {
54 $this->installation->getPackageID(),
55 $item['elements']['environment'],
56 $item['elements']['eventname'],
57 $item['attributes']['name'],
58 $item['elements']['templatename']
66 protected function prepareImport(array $data) {
67 $niceValue = isset($data['elements']['nice']) ? intval($data['elements']['nice']) : 0;
68 if ($niceValue < -128) {
71 else if ($niceValue > 127) {
76 'environment' => $data['elements']['environment'],
77 'eventName' => $data['elements']['eventname'],
78 'niceValue' => $niceValue,
79 'name' => $data['attributes']['name'],
80 'options' => isset($data['elements']['options']) ? StringUtil::normalizeCsv($data['elements']['options']) : '',
81 'permissions' => isset($data['elements']['permissions']) ? StringUtil::normalizeCsv($data['elements']['permissions']) : '',
82 'templateCode' => $data['elements']['templatecode'],
83 'templateName' => $data['elements']['templatename']
90 protected function findExistingItem(array $data) {
92 FROM wcf".WCF_N."_".$this->tableName."
99 $this->installation->getPackageID(),
101 $data['templateName'],
108 'parameters' => $parameters
115 protected function cleanup() {
116 // clear cache immediately
117 TemplateListenerCodeCacheBuilder::getInstance()->reset();
124 public static function getSyncDependencies() {
132 public function addFormFields(IFormDocument $form) {
133 $ldq = preg_quote(WCF::getTPL()->getCompiler()->getLeftDelimiter(), '~');
134 $rdq = preg_quote(WCF::getTPL()->getCompiler()->getRightDelimiter(), '~');
136 $getEvents = function($templateList) use ($ldq, $rdq) {
137 $templateEvents = [];
138 /** @var ACPTemplate|Template $template */
139 foreach ($templateList as $template) {
140 if (preg_match_all("~{$ldq}event\ name\=\'(?<event>[\w]+)\'{$rdq}~", $template->getSource(), $matches)) {
141 $templates[$template->templateName] = $template->templateName;
143 foreach ($matches['event'] as $event) {
144 if (!isset($templateEvents[$template->templateName])) {
145 $templateEvents[$template->templateName] = [];
148 $templateEvents[$template->templateName][] = $event;
153 foreach ($templateEvents as &$events) {
158 return $templateEvents;
161 $templateList = new TemplateList();
162 $templateList->getConditionBuilder()->add(
163 'template.packageID IN (?)',
164 [array_keys($this->installation->getPackage()->getAllRequiredPackages())]
166 $templateList->getConditionBuilder()->add('template.templateGroupID IS NULL');
167 $templateList->sqlOrderBy = 'template.templateName ASC';
168 $templateList->readObjects();
170 $templateEvents = $getEvents($templateList);
172 $acpTemplateList = new ACPTemplateList();
173 $acpTemplateList->getConditionBuilder()->add(
174 'acp_template.packageID IN (?)',
175 [array_keys($this->installation->getPackage()->getAllRequiredPackages())]
177 $acpTemplateList->sqlOrderBy = 'acp_template.templateName ASC';
178 $acpTemplateList->readObjects();
180 $acpTemplateEvents = $getEvents($acpTemplateList);
182 /** @var FormContainer $dataContainer */
183 $dataContainer = $form->getNodeById('data');
185 $dataContainer->appendChildren([
186 TextFormField::create('name')
187 ->label('wcf.acp.pip.templateListener.name')
188 ->description('wcf.acp.pip.templateListener.name.description')
190 ->addValidator(new FormFieldValidator('format', function(TextFormField $formField) {
191 if (!preg_match('~^[a-z][A-z]+$~', $formField->getValue())) {
192 $formField->addValidationError(
193 new FormFieldValidationError(
195 'wcf.acp.pip.templateListener.name.error.format'
201 SingleSelectionFormField::create('templateName')
202 ->objectProperty('templatename')
203 ->label('wcf.acp.pip.templateListener.templateName')
204 ->description('wcf.acp.pip.templateListener.templateName.description')
206 ->options(array_combine(array_keys($templateEvents), array_keys($templateEvents)))
209 SingleSelectionFormField::create('acpTemplateName')
210 ->objectProperty('templatename')
211 ->label('wcf.acp.pip.templateListener.templateName')
212 ->description('wcf.acp.pip.templateListener.templateName.description')
214 ->options(array_combine(array_keys($acpTemplateEvents), array_keys($acpTemplateEvents)))
218 foreach ($templateEvents as $templateName => $events) {
219 $dataContainer->appendChild(
220 SingleSelectionFormField::create($templateName . '_eventName')
221 ->objectProperty('eventname')
222 ->label('wcf.acp.pip.templateListener.eventName')
223 ->description('wcf.acp.pip.templateListener.eventName.description')
225 ->options(array_combine($events, $events))
227 ValueFormFieldDependency::create('templateName')
228 ->field($form->getNodeById('templateName'))
229 ->values([$templateName])
234 foreach ($acpTemplateEvents as $templateName => $events) {
235 $dataContainer->appendChild(
236 SingleSelectionFormField::create('acp_' . $templateName . '_eventName')
237 ->objectProperty('eventname')
238 ->label('wcf.acp.pip.templateListener.eventName')
239 ->description('wcf.acp.pip.templateListener.eventName.description')
241 ->options(array_combine($events, $events))
243 ValueFormFieldDependency::create('acpTemplateName')
244 ->field($form->getNodeById('acpTemplateName'))
245 ->values([$templateName])
250 $dataContainer->appendChildren([
251 SingleSelectionFormField::create('environment')
252 ->label('wcf.acp.pip.templateListener.environment')
253 ->description('wcf.acp.pip.templateListener.environment.description')
260 ->addValidator(new FormFieldValidator('uniqueness', function(SingleSelectionFormField $formField) {
261 /** @var TextFormField $nameField */
262 $nameField = $formField->getDocument()->getNodeById('name');
264 $listenerList = new TemplateListenerList();
265 $listenerList->getConditionBuilder()->add(
267 [$nameField->getSaveValue()]
270 if ($formField->getSaveValue() === 'admin') {
271 /** @var SingleSelectionFormField $templateNameField */
272 $templateNameField = $formField->getDocument()->getNodeById('acpTemplateName');
274 /** @var SingleSelectionFormField $eventNameField */
275 $eventNameField = $formField->getDocument()->getNodeById('acp_' . $templateNameField->getSaveValue() . '_eventName');
278 /** @var SingleSelectionFormField $templateNameField */
279 $templateNameField = $formField->getDocument()->getNodeById('templateName');
281 /** @var SingleSelectionFormField $eventNameField */
282 $eventNameField = $formField->getDocument()->getNodeById($templateNameField->getSaveValue() . '_eventName');
285 $templateName = $templateNameField->getSaveValue();
286 $eventName = $eventNameField->getSaveValue();
288 $listenerList->getConditionBuilder()->add('templateName = ?', [$templateName]);
290 $listenerList->getConditionBuilder()->add('eventName = ?', [$eventName]);
291 $listenerList->getConditionBuilder()->add('environment = ?', [$formField->getSaveValue()]);
293 if ($listenerList->countObjects() > 0) {
294 $nameField->addValidationError(
295 new FormFieldValidationError(
297 'wcf.acp.pip.templateListener.name.error.notUnique'
303 // TODO: use field with code support
304 MultilineTextFormField::create('templateCode')
305 ->objectProperty('templatecode')
306 ->label('wcf.acp.pip.templateListener.templateCode')
307 ->description('wcf.acp.pip.templateListener.templateCode.description')
311 $form->getNodeById('templateName')->addDependency(
312 ValueFormFieldDependency::create('environment')
313 ->field($form->getNodeById('environment'))
316 $form->getNodeById('acpTemplateName')->addDependency(
317 ValueFormFieldDependency::create('environment')
318 ->field($form->getNodeById('environment'))
327 protected function getElementData(\DOMElement $element, $saveData = false) {
329 'environment' => $element->getElementsByTagName('environment')->item(0)->nodeValue,
330 'eventName' => $element->getElementsByTagName('eventname')->item(0)->nodeValue,
331 'name' => $element->getAttribute('name'),
332 'packageID' => $this->installation->getPackage()->packageID,
333 'templateCode' => $element->getElementsByTagName('templatecode')->item(0)->nodeValue,
334 'templateName' => $element->getElementsByTagName('templatename')->item(0)->nodeValue
342 public function getElementIdentifier(\DOMElement $element) {
344 $element->getElementsByTagName('templatename')->item(0)->nodeValue . '/' .
345 $element->getElementsByTagName('eventname')->item(0)->nodeValue . '/' .
346 $element->getElementsByTagName('environment')->item(0)->nodeValue . '/' .
347 $element->getAttribute('name')
355 protected function setEntryListKeys(IDevtoolsPipEntryList $entryList) {
356 $entryList->setKeys([
357 'name' => 'wcf.acp.pip.templateListener.name',
358 'templateName' => 'wcf.acp.pip.templateListener.templateName',
359 'eventName' => 'wcf.acp.pip.templateListener.eventName',
360 'environment' => 'wcf.acp.pip.templateListener.environment'
368 protected function sortDocument(\DOMDocument $document) {
369 $this->sortImportDelete($document);
371 $compareFunction = function(\DOMElement $element1, \DOMElement $element2) {
372 $templateName1 = $element1->getElementsByTagName('templatename')->item(0)->nodeValue;
373 $templateName2 = $element2->getElementsByTagName('templatename')->item(0)->nodeValue;
375 if ($templateName1 !== $templateName2) {
376 return strcmp($templateName1, $templateName2);
379 $eventName1 = $element1->getElementsByTagName('eventname')->item(0)->nodeValue;
380 $eventName2 = $element2->getElementsByTagName('eventname')->item(0)->nodeValue;
382 if ($eventName1 !== $eventName2) {
383 return strcmp($eventName1, $eventName2);
387 $element1->getElementsByTagName('environment')->item(0)->nodeValue,
388 $element2->getElementsByTagName('environment')->item(0)->nodeValue
392 $this->sortChildNodes($document->getElementsByTagName('import'), $compareFunction);
393 $this->sortChildNodes($document->getElementsByTagName('delete'), $compareFunction);
400 protected function writeEntry(\DOMDocument $document, IFormDocument $form) {
401 $data = $form->getData()['data'];
403 $listener = $document->createElement($this->tagName);
404 $listener->setAttribute('name', $data['name']);
406 if ($data['environment'] === 'user') {
407 $templateName = $data['templateName'];
409 $listener->appendChild($document->createElement('templatename', $templateName));
410 $listener->appendChild($document->createElement('eventname', $data[$templateName . '_eventName']));
413 $templateName = $data['acpTemplateName'];
415 $listener->appendChild($document->createElement('templatename', $templateName));
416 $listener->appendChild($document->createElement('eventname', $data['acp_' . $templateName . '_eventName']));
418 $listener->appendChild(
419 $document->createElement(
421 '<![CDATA[' . StringUtil::unifyNewlines(StringUtil::escapeCDATA($data['templatecode'])) . ']]>'
424 $listener->appendChild($document->createElement('environment', $data['environment']));
426 $document->getElementsByTagName('import')->item(0)->appendChild($listener);