Add and use `TXmlGuiPackageInstallationPlugin::appendElementChildren()`
authorMatthias Schmidt <gravatronics@live.com>
Mon, 15 Oct 2018 17:19:16 +0000 (19:19 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Mon, 15 Oct 2018 17:19:16 +0000 (19:19 +0200)
See #2545

24 files changed:
wcfsetup/install/files/lib/system/devtools/pip/TXmlGuiPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/ACLOptionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/ACPMenuPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/ACPSearchProviderPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/AbstractMenuPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/BoxPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/ClipboardActionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/CoreObjectPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/MenuItemPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/ObjectTypeDefinitionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/UserMenuPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/UserNotificationEventPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php
wcfsetup/install/files/lib/system/package/plugin/UserProfileMenuPackageInstallationPlugin.class.php

index b9a52a9c1b828d6fc2f048b1c4d9983a14aef30c..c75c3f798fac0d859c0c96e60109aaf799160c15 100644 (file)
@@ -58,6 +58,57 @@ trait TXmlGuiPackageInstallationPlugin {
                $xml->write($this->getXmlFileLocation($project));
        }
        
+       /**
+        * Adds optional child elements to the given elements based on the given
+        * child data and form.
+        *
+        * @param       \DOMElement     $element                element to which the child elements are added
+        * @param       array           $children
+        * @param       IFormDocument   $form                   form containing the children's data
+        */
+       protected function appendElementChildren(\DOMElement $element, array $children, IFormDocument $form) {
+               $data = $form->getData()['data'];
+               
+               $document = $element->ownerDocument;
+               
+               foreach ($children as $index => $key) {
+                       if (is_string($index)) {
+                               $childName = $index;
+                               if (!is_array($key)) {
+                                       $isOptional = true;
+                                       $cdata = false;
+                                       $defaultValue = $key;
+                               }
+                               else {
+                                       $isOptional = array_key_exists('defaultValue', $key);
+                                       $cdata = $key['cdata'] ?? false;
+                                       $defaultValue = $key['defaultValue'] ?? null;
+                               }
+                       }
+                       else {
+                               $childName = $key;
+                               $isOptional = false;
+                               $cdata = false;
+                               $defaultValue = null;
+                       }
+                       
+                       if (!$isOptional || (isset($data[$childName]) && $data[$childName] !== $defaultValue)) {
+                               if ($cdata) {
+                                       $childElement = $document->createElement($childName);
+                                       $childElement->appendChild($document->createCDATASection($data[$childName]));
+                               }
+                               else {
+                                       $childElement = $document->createElement(
+                                               $childName,
+                                               (string)$data[$childName]
+                                       );
+                               }
+                               
+                               $element->appendChild($childElement);
+                       }
+               }
+       }
+       
        /**
         * Creates a new XML element for the given document using the data provided
         * by the given form and return the new dom element.
index c4da644ad908cfee22d6822a6d1510b7ff79dfcd..fa4c7a909a14fbfe866e93135c6426f32aa84ebe 100644 (file)
@@ -605,7 +605,7 @@ class ACLOptionPackageInstallationPlugin extends AbstractOptionPackageInstallati
                                $category = $document->createElement('category');
                                $category->setAttribute('name', $formData['name']);
                                
-                               $category->appendChild($document->createElement('objecttype', $formData['objecttype']));
+                               $this->appendElementChildren($category, ['objecttype'], $form);
                                
                                return $category;
                                
@@ -613,11 +613,14 @@ class ACLOptionPackageInstallationPlugin extends AbstractOptionPackageInstallati
                                $option = $document->createElement('option');
                                $option->setAttribute('name', $formData['name']);
                                
-                               $option->appendChild($document->createElement('objecttype', $formData['objecttype']));
-                               
-                               if (isset($formData['categoryname'])) {
-                                       $option->appendChild($document->createElement('categoryname', $formData['categoryname']));
-                               }
+                               $this->appendElementChildren(
+                                       $option,
+                                       [
+                                               'objecttype',
+                                               'categoryname' => ''
+                                       ],
+                                       $form
+                               );
                                
                                return $option;
                }
index 1a6b71be25c242e60f2915fcc4350f6ef18a6a25..182a03a16444cefb611812eb38c0dee830d99c2a 100644 (file)
@@ -144,13 +144,9 @@ class ACPMenuPackageInstallationPlugin extends AbstractMenuPackageInstallationPl
         * @since       3.2
         */
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
-               $formData = $form->getData()['data'];
-               
                $menuItem = parent::createXmlElement($document, $form);
                
-               if (isset($formData['icon'])) {
-                       $menuItem->appendChild($document->createElement('icon', $formData['icon']));
-               }
+               $this->appendElementChildren($menuItem, ['icon' => null], $form);
                
                return $menuItem;
        }
index dfcb1bf64cf248b2ebb7e8cb007d327e06855e1a..5b37b32ce6ea0c01675c1f03daf617df5338eadf 100644 (file)
@@ -203,10 +203,15 @@ class ACPSearchProviderPackageInstallationPlugin extends AbstractXMLPackageInsta
                
                $acpSearchProvider = $document->createElement($this->tagName);
                $acpSearchProvider->setAttribute('name', $data['name']);
-               $acpSearchProvider->appendChild($document->createElement('classname', $data['classname']));
-               if (isset($data['showorder'])) {
-                       $acpSearchProvider->appendChild($document->createElement('showorder', (string)$data['showorder']));
-               }
+               
+               $this->appendElementChildren(
+                       $acpSearchProvider,
+                       [
+                               'classname',
+                               'showorder' => null
+                       ],
+                       $form
+               );
                
                return $acpSearchProvider;
        }
index 3e22a9e7e2c902c38f390ac85433abd7b3ba12d2..46396462f45148a3a0700b1bff188cf59359732a 100644 (file)
@@ -264,6 +264,7 @@ abstract class AbstractMenuPackageInstallationPlugin extends AbstractXMLPackageI
                                ->description('wcf.acp.pip.abstractMenu.showOrder.description')
                                ->objectProperty('showorder')
                                ->minimum(1)
+                               ->nullable()
                ]);
        }
        
@@ -392,11 +393,18 @@ abstract class AbstractMenuPackageInstallationPlugin extends AbstractXMLPackageI
                $menuItem = $document->createElement($this->tagName);
                $menuItem->setAttribute('name', $formData['name']);
                
-               foreach (['parent', 'controller', 'link', 'options', 'permissions', 'showorder'] as $field) {
-                       if (isset($formData[$field]) && $formData[$field] !== '') {
-                               $menuItem->appendChild($document->createElement($field, (string) $formData[$field]));
-                       }
-               }
+               $this->appendElementChildren(
+                       $menuItem,
+                       [
+                               'parent' => '',
+                               'controller' => '',
+                               'link' => '',
+                               'options' => '',
+                               'permissions' => '',
+                               'showorder' => null
+                       ],
+                       $form
+               );
                
                return $menuItem;
        }
index dbd82802961a43de1e7428b59f808186257ce7eb..4242730f1c247cc1a3a4bbab897b0cd03461c684 100644 (file)
@@ -307,16 +307,16 @@ abstract class AbstractOptionPackageInstallationPlugin extends AbstractXMLPackag
                                SET     parentCategoryName = ?,
                                        permissions = ?,
                                        options = ?
-                                       ".($category['showOrder'] !== null ? ", showOrder = ?" : "")."
+                                       ".(isset($category['showOrder']) ? ", showOrder = ?" : "")."
                                WHERE   categoryID = ?";
                        $statement = WCF::getDB()->prepareStatement($sql);
                        
                        $data = [
                                $category['parentCategoryName'],
-                               $category['permissions'],
-                               $category['options']
+                               $category['permissions'] ?? '',
+                               $category['options'] ?? ''
                        ];
-                       if ($category['showOrder'] !== null) $data[] = $category['showOrder'];
+                       if (isset($category['showOrder'])) $data[] = $category['showOrder'];
                        $data[] = $row['categoryID'];
                        
                        $statement->execute($data);
@@ -959,41 +959,36 @@ abstract class AbstractOptionPackageInstallationPlugin extends AbstractXMLPackag
                                        }
                                }
                                
-                               $xpath->query('/ns:data/ns:import/ns:categories')->item(0)->appendChild($category);
-                               
                                return $category;
                        
                        case 'options':
                                $option = $document->createElement($this->tagName);
                                $option->setAttribute('name', $formData['name']);
                                
-                               foreach (['categoryname', 'optiontype'] as $field) {
-                                       $option->appendChild($document->createElement($field, (string) $formData[$field]));
-                               }
-                               
-                               $fields = [
-                                       'defaultvalue' => '',
-                                       'validationpattern' => '',
-                                       'enableoptions' => '',
-                                       'showorder' => 0,
-                                       'options' => '',
-                                       'permissions' => '',
-                                       
-                                       // option type-specific elements
-                                       'minvalue' => null,
-                                       'maxvalue' => null,
-                                       'suffix' => '',
-                                       'minlength' => null,
-                                       'maxlength' => null,
-                                       'issortable' => 0,
-                                       'allowemptyvalue' => 0,
-                                       'disableAutocomplete' => 0
-                               ];
-                               foreach ($fields as $field => $defaultValue) {
-                                       if (isset($formData[$field]) && $formData[$field] !== $defaultValue) {
-                                               $option->appendChild($document->createElement($field, StringUtil::unifyNewlines((string) $formData[$field])));
-                                       }
-                               }
+                               $this->appendElementChildren(
+                                       $option,
+                                       [
+                                               'categoryname',
+                                               'optiontype',
+                                               'defaultvalue' => '',
+                                               'validationpattern' => '',
+                                               'enableoptions' => '',
+                                               'showorder' => 0,
+                                               'options' => '',
+                                               'permissions' => '',
+                                               
+                                               // option type-specific elements
+                                               'minvalue' => null,
+                                               'maxvalue' => null,
+                                               'suffix' => '',
+                                               'minlength' => null,
+                                               'maxlength' => null,
+                                               'issortable' => 0,
+                                               'allowemptyvalue' => 0,
+                                               'disableAutocomplete' => 0
+                                       ],
+                                       $form
+                               );
                                
                                return $option;
                        
index 45c7823674e689c82117539f2da3192ff4a94cfe..7813ddbbb51fc9acc4c07151acd9449388ed1b40 100644 (file)
@@ -500,21 +500,19 @@ class BBCodePackageInstallationPlugin extends AbstractXMLPackageInstallationPlug
                $bbcode = $document->createElement($this->tagName);
                $bbcode->setAttribute('name', $data['name']);
                
-               $fields = [
-                       'classname' => '',
-                       'htmlclose' => '',
-                       'htmlopen' => '',
-                       'isBlockElement' => 0,
-                       'sourcecode' => 0,
-                       'buttonlabel' => '',
-                       'wysiwygicon' => ''
-               ];
-               
-               foreach ($fields as $field => $defaultValue) {
-                       if (isset($data[$field]) && $data[$field] !== $defaultValue) {
-                               $bbcode->appendChild($document->createElement($field, (string) $data[$field]));
-                       }
-               }
+               $this->appendElementChildren(
+                       $bbcode,
+                       [
+                               'classname' => '',
+                               'htmlclose' => '',
+                               'htmlopen' => '',
+                               'isBlockElement' => 0,
+                               'sourcecode' => 0,
+                               'buttonlabel' => '',
+                               'wysiwygicon' => ''
+                       ],
+                       $form
+               );
                
                if (!empty($data['attributes'])) {
                        $attributes = $document->createElement('attributes');
index 0562465403ad8e2983acc0691876015796980830..cc3e207b75b0ce4558c78698d234e538a7c7266c 100644 (file)
@@ -780,16 +780,15 @@ class BoxPackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                $box->appendChild($document->createElement('boxType', $data['boxType']));
                $box->appendChild($document->createElement('position', $data['position']));
                
-               $optionals = [
-                       'objectType' => '',
-                       'cssClassName' => '',
-                       'showHeader' => 0
-               ];
-               foreach ($optionals as $field => $defaultValue) {
-                       if (isset($data[$field]) && $data[$field] !== $defaultValue) {
-                               $box->appendChild($document->createElement($field, (string)$data[$field]));
-                       }
-               }
+               $this->appendElementChildren(
+                       $box,
+                       [
+                               'objectType' => '',
+                               'cssClassName' => '',
+                               'showHeader' => 0
+                       ],
+                       $form
+               );
                
                if (!empty($data['visibilityExceptions'])) {
                        $box->appendChild($document->createElement('visibleEverywhere', (string)($data['visibleEverywhere'] ?? 0)));
index 0ded11205c06911132f263082fdb9f3c09723adc..c13af0663566cc5fff28fc5c88b9f63d0fd8545b 100644 (file)
@@ -300,11 +300,14 @@ class ClipboardActionPackageInstallationPlugin extends AbstractXMLPackageInstall
                $clipboardAction = $document->createElement($this->tagName);
                $clipboardAction->setAttribute('name', $data['name']);
                
-               $clipboardAction->appendChild($document->createElement('actionclassname', $data['actionclassname']));
-               
-               if (!empty($data['showorder'])) {
-                       $clipboardAction->appendChild($document->createElement('showorder', (string)$data['showorder']));
-               }
+               $this->appendElementChildren(
+                       $clipboardAction,
+                       [
+                               'actionclassname',
+                               'showorder' => null
+                       ],
+                       $form
+               );
                
                $pages = $document->createElement('pages');
                $clipboardAction->appendChild($pages);
index acbc37655fac24cd5dcbda346a77b494b8714086..ac3be5f5d4d976451f868f883be7d656f596e7b8 100644 (file)
@@ -156,11 +156,9 @@ class CoreObjectPackageInstallationPlugin extends AbstractXMLPackageInstallation
         * @since       3.2
         */
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
-               $data = $form->getData()['data'];
-               
                $coreObject = $document->createElement($this->tagName);
                
-               $coreObject->appendChild($document->createElement('objectname', $data['objectname']));
+               $this->appendElementChildren($coreObject, ['objectname'], $form);
                
                return $coreObject;
        }
index 3e237ab21f262a06a790808d0333e2806d5c68f3..7e305f817a9c4068cdce1f0805ee69cc20f6c777 100644 (file)
@@ -340,9 +340,7 @@ class CronjobPackageInstallationPlugin extends AbstractXMLPackageInstallationPlu
                
                $cronjob = $document->createElement($this->tagName);
                $cronjob->setAttribute('name', $formData['name']);
-               
-               $className = $document->createElement('classname', $formData['classname']);
-               $cronjob->appendChild($className);
+               $cronjob->appendChild($document->createElement('classname', $formData['classname']));
                
                if (isset($formData['description'])) {
                        if ($formData['description'] !== '') {
@@ -375,19 +373,21 @@ class CronjobPackageInstallationPlugin extends AbstractXMLPackageInstallationPlu
                        }
                }
                
-               foreach (['startmonth', 'startdom', 'startdow', 'starthour', 'startminute'] as $timeProperty) {
-                       $cronjob->appendChild($document->createElement($timeProperty, $formData[$timeProperty]));
-               }
-               
-               if (isset($formData['options']) && $formData['options'] !== '') {
-                       $cronjob->appendChild($document->createElement('options', $formData['options']));
-               }
-               
-               foreach (['canbeedited' => 1, 'canbedisabled' => 1, 'isdisabled' => 0] as $booleanProperty => $defaultValue) {
-                       if ($formData[$booleanProperty] !== $defaultValue) {
-                               $cronjob->appendChild($document->createElement($booleanProperty, (string) $formData[$booleanProperty]));
-                       }
-               }
+               $this->appendElementChildren(
+                       $cronjob,
+                       [
+                               'startmonth',
+                               'startdom',
+                               'startdow',
+                               'starthour',
+                               'startminute',
+                               'options' => '',
+                               'canbeedited' => 1,
+                               'canbedisabled' => 1,
+                               'isdisabled' => 0
+                       ],
+                       $form
+               );
                
                return $cronjob;
        }
index 2af03334ce85655e113040a57749bbc49b4fa7cc..bb964c0fa5a5db0ac8cc1ba9f62b0a2836484bb9 100644 (file)
@@ -334,15 +334,20 @@ class EventListenerPackageInstallationPlugin extends AbstractXMLPackageInstallat
                $eventListener = $document->createElement($this->tagName);
                $eventListener->setAttribute('name', $data['listenerName']);
                
-               foreach (['eventclassname', 'eventname', 'listenerclassname'] as $property) {
-                       $eventListener->appendChild($document->createElement($property, $data[$property]));
-               }
-               
-               foreach (['environment', 'inherit', 'nice', 'options', 'permissions'] as $optionalProperty) {
-                       if (!empty($data[$optionalProperty])) {
-                               $eventListener->appendChild($document->createElement($optionalProperty, (string)$data[$optionalProperty]));
-                       }
-               }
+               $this->appendElementChildren(
+                       $eventListener,
+                       [
+                               'eventclassname',
+                               'eventname',
+                               'listenerclassname',
+                               'environment' => 'user',
+                               'inherit' => 0,
+                               'nice' => null,
+                               'options' => '',
+                               'permissions' => ''
+                       ],
+                       $form
+               );
                
                return $eventListener;
        }
index ab795b2881e5634ac40cb8db947afdca8d58ed1d..77bea3bbc15d98e21fa0688cdaee82e2874c566a 100644 (file)
@@ -231,23 +231,21 @@ class MediaProviderPackageInstallationPlugin extends AbstractXMLPackageInstallat
                $provider = $document->createElement($this->tagName);
                $provider->setAttribute('name', $data['name']);
                
-               $provider->appendChild($document->createElement('title', $data['title']));
-               
-               $regex = $document->createElement('regex');
-               $regex->appendChild($document->createCDATASection(
-                       StringUtil::escapeCDATA(StringUtil::unifyNewlines($data['regex']))
-               ));
-               $provider->appendChild($regex);
-               
-               if (!empty($data['html'])) {
-                       $htmlElement = $document->createElement('html');
-                       $htmlElement->appendChild($document->createCDATASection(StringUtil::escapeCDATA($data['html'])));
-                       $provider->appendChild($htmlElement);
-               }
-               
-               if (!empty($data['className'])) {
-                       $provider->appendChild($document->createElement('className', $data['className']));
-               }
+               $this->appendElementChildren(
+                       $provider,
+                       [
+                               'title',
+                               'regex' => [
+                                       'cdata' => true
+                               ],
+                               'html' => [
+                                       'cdata' => true,
+                                       'defaultValue' => ''
+                               ],
+                               'className' => ''
+                       ],
+                       $form
+               );
                
                return $provider;
        }
index e0d6dbe2b1f64ed126fb315e232fa5325a680f50..64e943b2cbf261146aae0aa15b952b1133bbab46 100644 (file)
@@ -369,6 +369,7 @@ class MenuItemPackageInstallationPlugin extends AbstractXMLPackageInstallationPl
                                ->description('wcf.acp.pip.menuItem.showOrder.description')
                                ->objectProperty('showorder')
                                ->minimum(1)
+                               ->nullable()
                ]);
                
                /** @var SingleSelectionFormField $menuField */
@@ -535,6 +536,7 @@ class MenuItemPackageInstallationPlugin extends AbstractXMLPackageInstallationPl
                $data = $formData['data'];
                
                $menuItem = $document->createElement($this->tagName);
+               
                $menuItem->setAttribute('identifier', $data['identifier']);
                
                if (!empty($data['parent'])) {
@@ -551,11 +553,15 @@ class MenuItemPackageInstallationPlugin extends AbstractXMLPackageInstallationPl
                        $menuItem->appendChild($title);
                }
                
-               foreach (['page', 'externalURL', 'showOrder'] as $property) {
-                       if (!empty($data[$property])) {
-                               $menuItem->appendChild($document->createElement($property, (string)$data[$property]));
-                       }
-               }
+               $this->appendElementChildren(
+                       $menuItem,
+                       [
+                               'page' => '',
+                               'externalURL' => '',
+                               'showOrder' => null
+                       ],
+                       $form
+               );
                
                return $menuItem;
        }
index 8098bee288e948a72fc2d646b6150faf337f23ae..c7ab5cd01f17ed69cded8e8a863c60225ecd818d 100644 (file)
@@ -210,14 +210,16 @@ class ObjectTypeDefinitionPackageInstallationPlugin extends AbstractXMLPackageIn
         * @since       3.2
         */
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
-               $data = $form->getData()['data'];
-               
                $definition = $document->createElement($this->tagName);
-               $definition->appendChild($document->createElement('name', $data['name']));
                
-               if (!empty($data['interfacename'])) {
-                       $definition->appendChild($document->createElement('interfacename', $data['interfacename']));
-               }
+               $this->appendElementChildren(
+                       $definition,
+                       [
+                               'name',
+                               'interfacename' => ''
+                       ],
+                       $form
+               );
                
                return $definition;
        }
index 55396d3ea1ddbc669ee8066b12631630627289e2..b5cda75156a5a59952784c4fd0185a73e9ae6344 100644 (file)
@@ -251,15 +251,18 @@ class OptionPackageInstallationPlugin extends AbstractOptionPackageInstallationP
        protected function writeEntry(\DOMDocument $document, IFormDocument $form) {
                $option = parent::createXmlElement($document, $form);
                
-               $formData = $form->getData()['data'];
-               
                switch ($this->entryType) {
                        case 'options':
-                               foreach (['selectoptions' => '', 'hidden' => 0, 'supporti18n' => 0, 'requirei18n' => 0] as $field => $defaultValue) {
-                                       if (isset($formData[$field]) && $formData[$field] !== $defaultValue) {
-                                               $option->appendChild($document->createElement($field, (string) $formData[$field]));
-                                       }
-                               }
+                               $this->appendElementChildren(
+                                       $option,
+                                       [
+                                               'selectoptions' => '',
+                                               'hidden' => 0,
+                                               'supporti18n' => 0,
+                                               'requirei18n' => 0
+                                       ],
+                                       $form
+                               );
                                
                                break;
                }
index a0ca0960ae2298b07f211a4e187b5818db689d20..55aefa6b8c66109ea5d11c94bf63fe94e0b8dfd1 100644 (file)
@@ -766,6 +766,7 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin
                        $page->appendChild($name);
                }
                
+               // TODO: use `appendElementChildren`
                $optionalElements = [
                        'controller', 'handler', 'controllerCustomURL', 'hasFixedParent',
                        'parent', 'options', 'permissions', 'cssClassName', 'allowSpidersToIndex',
index 3d0a968b332837492fd6505e0cf5c5c59f8573fa..9810d3a8ad0bb6d78bed7ba946767644ac31c729 100644 (file)
@@ -6,6 +6,7 @@ use wcf\system\devtools\pip\IDevtoolsPipEntryList;
 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
 use wcf\system\form\builder\container\FormContainer;
+use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor;
 use wcf\system\form\builder\field\IntegerFormField;
 use wcf\system\form\builder\field\ItemListFormField;
 use wcf\system\form\builder\field\TextFormField;
@@ -211,6 +212,13 @@ class SmileyPackageInstallationPlugin extends AbstractXMLPackageInstallationPlug
                                ->maximumLength(255)
                                ->addValidator($fileValidator)
                ]);
+               
+               // ensure proper normalization of template code
+               $form->getDataHandler()->add(new CustomFormFieldDataProcessor('templateCode', function(IFormDocument $document, array $parameters) {
+                       $parameters['data']['aliases'] = StringUtil::unifyNewlines(StringUtil::escapeCDATA($parameters['data']['aliases']));
+                       
+                       return $parameters;
+               }));
        }
        
        /**
@@ -269,21 +277,20 @@ class SmileyPackageInstallationPlugin extends AbstractXMLPackageInstallationPlug
                $smiley = $document->createElement($this->tagName);
                $smiley->setAttribute('name', $data['name']);
                
-               foreach (['title', 'path', 'path2x'] as $element) {
-                       $smiley->appendChild($document->createElement($element, $data[$element]));
-               }
-               
-               if ($data['aliases'] !== '') {
-                       $aliases = $document->createElement('aliases');
-                       $aliases->appendChild($document->createCDATASection(
-                               StringUtil::escapeCDATA(StringUtil::unifyNewlines($data['aliases']))
-                       ));
-                       $smiley->appendChild($aliases);
-               }
-               
-               if ($data['showorder'] !== null) {
-                       $smiley->appendChild($document->createElement('showorder', $data['showorder']));
-               }
+               $this->appendElementChildren(
+                       $smiley,
+                       [
+                               'title',
+                               'path',
+                               'path2x',
+                               'aliases' => [
+                                       'cdata' => true,
+                                       'defaultValue' => ''
+                               ],
+                               'showOrder' => null
+                       ],
+                       $form
+               );
                
                return $smiley;
        }
index aac1ff32d9afd6e3142e7dffd73d94e10e2d03eb..2ccf251d4770a0f5d3865caa1aa0af44012625bd 100644 (file)
@@ -11,6 +11,7 @@ use wcf\system\devtools\pip\IDevtoolsPipEntryList;
 use wcf\system\devtools\pip\IGuiPackageInstallationPlugin;
 use wcf\system\devtools\pip\TXmlGuiPackageInstallationPlugin;
 use wcf\system\form\builder\container\FormContainer;
+use wcf\system\form\builder\field\data\CustomFormFieldDataProcessor;
 use wcf\system\form\builder\field\dependency\ValueFormFieldDependency;
 use wcf\system\form\builder\field\validation\FormFieldValidationError;
 use wcf\system\form\builder\field\validation\FormFieldValidator;
@@ -225,7 +226,7 @@ class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstal
                foreach ($templateEvents as $templateName => $events) {
                        $dataContainer->appendChild(
                                SingleSelectionFormField::create($templateName . '_eventName')
-                                       ->objectProperty('eventName')
+                                       ->objectProperty('eventname')
                                        ->label('wcf.acp.pip.templateListener.eventName')
                                        ->description('wcf.acp.pip.templateListener.eventName.description')
                                        ->required()
@@ -241,7 +242,7 @@ class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstal
                foreach ($acpTemplateEvents as $templateName => $events) {
                        $dataContainer->appendChild(
                                SingleSelectionFormField::create('acp_' . $templateName . '_eventName')
-                                       ->objectProperty('eventName')
+                                       ->objectProperty('eventname')
                                        ->label('wcf.acp.pip.templateListener.eventName')
                                        ->description('wcf.acp.pip.templateListener.eventName.description')
                                        ->required()
@@ -343,6 +344,13 @@ class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstal
                                ->field($form->getNodeById('environment'))
                                ->values(['admin'])
                );
+               
+               // ensure proper normalization of template code
+               $form->getDataHandler()->add(new CustomFormFieldDataProcessor('templateCode', function(IFormDocument $document, array $parameters) {
+                       $parameters['data']['templatecode'] = StringUtil::unifyNewlines(StringUtil::escapeCDATA($parameters['data']['templatecode']));
+                       
+                       return $parameters;
+               }));
        }
        
        /**
@@ -434,13 +442,18 @@ class TemplateListenerPackageInstallationPlugin extends AbstractXMLPackageInstal
                $listener = $document->createElement($this->tagName);
                $listener->setAttribute('name', $data['name']);
                
-               $listener->appendChild($document->createElement('environment', $data['environment']));
-               $listener->appendChild($document->createElement('templatename', $data['templatename']));
-               $listener->appendChild($document->createElement('eventname', $data['eventName']));
-               
-               $templateCode = $document->createElement('templatecode');
-               $templateCode->appendChild($document->createCDATASection(StringUtil::unifyNewlines(StringUtil::escapeCDATA($data['templatecode']))));
-               $listener->appendChild($templateCode);
+               $this->appendElementChildren(
+                       $listener,
+                       [
+                               'environment',
+                               'templatename',
+                               'eventname',
+                               'templatecode' => [
+                                       'cdata' => true
+                               ]
+                       ],
+                       $form
+               );
                
                return $listener;
        }
index 56c487e54ac483a64bad1c1445a97a5a8e858ba4..49c13760b7af01a3496ff9dc7de8efebf9ca217a 100644 (file)
@@ -320,23 +320,19 @@ class UserGroupOptionPackageInstallationPlugin extends AbstractOptionPackageInst
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
                $option = parent::createXmlElement($document, $form);
                
-               $formData = $form->getData()['data'];
-               
                switch ($this->entryType) {
                        case 'options':
-                               $fields = [
-                                       'admindefaultvalue' => '',
-                                       'moddefaultvalue' => '',
-                                       'usersonly' => 0,
-                                       'excludedInTinyBuild' => 0,
-                                       'wildcard' => ''
-                               ];
-                               
-                               foreach ($fields as $field => $defaultValue) {
-                                       if (isset($formData[$field]) && $formData[$field] !== $defaultValue) {
-                                               $option->appendChild($document->createElement($field, (string) $formData[$field]));
-                                       }
-                               }
+                               $this->appendElementChildren(
+                                       $option,
+                                       [
+                                               'admindefaultvalue' => '',
+                                               'moddefaultvalue' => '',
+                                               'usersonly' => 0,
+                                               'excludedInTinyBuild' => 0,
+                                               'wildcard' => ''
+                                       ],
+                                       $form
+                               );
                                
                                break;
                }
index 037e311dccb5b51c9b3c9c5fdaeca8b17ac1f25e..43c8252c67003e7bff39c611d260fac93f9c2a0b 100644 (file)
@@ -146,16 +146,16 @@ class UserMenuPackageInstallationPlugin extends AbstractMenuPackageInstallationP
         * @since       3.2
         */
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
-               $formData = $form->getData()['data'];
-               
                $menuItem = parent::createXmlElement($document, $form);
                
-               if (!empty($formData['classname'])) {
-                       $menuItem->appendChild($document->createElement('classname', $formData['classname']));
-               }
-               if (!empty($formData['iconclassname'])) {
-                       $menuItem->appendChild($document->createElement('iconclassname', $formData['iconclassname']));
-               }
+               $this->appendElementChildren(
+                       $menuItem,
+                       [
+                               'classname' => '',
+                               'iconclassname' => ''
+                       ],
+                       $form
+               );
                
                return $menuItem;
        }
index 052d636df564ed138c5095613c8e4fc15d0963ce..95ece9d4fa78e16f954d1d75eb204729a2a5de3b 100644 (file)
@@ -352,29 +352,21 @@ class UserNotificationEventPackageInstallationPlugin extends AbstractXMLPackageI
         * @since       3.2
         */
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
-               $data = $form->getData()['data'];
-               
                $event = $document->createElement($this->tagName);
                
-               foreach (['name', 'objecttype', 'classname'] as $element) {
-                       $event->appendChild(
-                               $document->createElement(
-                                       $element,
-                                       (string)$data[$element]
-                               )
-                       );
-               }
-               
-               foreach (['options', 'permissions', 'preset', 'presetmailnotificationtype'] as $optionalElement) {
-                       if (!empty($data[$optionalElement])) {
-                               $event->appendChild(
-                                       $document->createElement(
-                                               $optionalElement,
-                                               (string)$data[$optionalElement]
-                                       )
-                               );
-                       }
-               }
+               $this->appendElementChildren(
+                       $event,
+                       [
+                               'name',
+                               'objecttype',
+                               'classname',
+                               'options' => '',
+                               'permissions' => '',
+                               'preset' => 0,
+                               'presetmailnotificationtype' => ''
+                       ],
+                       $form
+               );
                
                return $event;
        }
index 89c04bd9dfc6fdaab6ee5df5a77c3994b05d5ece..9ef6c592eb6af906c02e0bcea55ed0dc419d33ac 100644 (file)
@@ -383,27 +383,23 @@ class UserOptionPackageInstallationPlugin extends AbstractOptionPackageInstallat
        protected function createXmlElement(\DOMDocument $document, IFormDocument $form) {
                $option = parent::createXmlElement($document, $form);
                
-               $formData = $form->getData()['data'];
-               
                switch ($this->entryType) {
                        case 'options':
-                               $fields = [
-                                       'outputclass' => '',
-                                       'required' => 0,
-                                       'askduringregistration' => 0,
-                                       'editable' => 0,
-                                       'visible' => 0,
-                                       'searchable' => 0,
-                                       'isdisabled' => 0,
-                                       'messageObjectType' => '',
-                                       'contentpattern' => ''
-                               ];
-                               
-                               foreach ($fields as $field => $defaultValue) {
-                                       if (isset($formData[$field]) && $formData[$field] !== $defaultValue) {
-                                               $option->appendChild($document->createElement($field, (string) $formData[$field]));
-                                       }
-                               }
+                               $this->appendElementChildren(
+                                       $option,
+                                       [
+                                               'outputclass' => '',
+                                               'required' => 0,
+                                               'askduringregistration' => 0,
+                                               'editable' => 0,
+                                               'visible' => 0,
+                                               'searchable' => 0,
+                                               'isdisabled' => 0,
+                                               'messageObjectType' => '',
+                                               'contentpattern' => ''
+                                       ],
+                                       $form
+                               );
                                
                                break;
                }
index d6e6aab1c25f0af42c330551286d126fc1be2c8d..b0e1fb5db51d59d36306a74e5f5311d667d4b113 100644 (file)
@@ -230,18 +230,17 @@ class UserProfileMenuPackageInstallationPlugin extends AbstractXMLPackageInstall
                
                $userProfileMenuItem = $document->createElement($this->tagName);
                $userProfileMenuItem->setAttribute('name', $data['name']);
-               $userProfileMenuItem->appendChild($document->createElement('classname', $data['classname']));
                
-               foreach (['options', 'permissions', 'showorder'] as $optionalElement) {
-                       if (!empty($data[$optionalElement])) {
-                               $userProfileMenuItem->appendChild(
-                                       $document->createElement(
-                                               $optionalElement,
-                                               (string)$data[$optionalElement]
-                                       )
-                               );
-                       }
-               }
+               $this->appendElementChildren(
+                       $userProfileMenuItem,
+                       [
+                               'classname',
+                               'options' => '',
+                               'permissions' => '',
+                               'showorder' => null
+                       ],
+                       $form
+               );
                
                return $userProfileMenuItem;
        }