From: Matthias Schmidt Date: Mon, 15 Oct 2018 17:19:16 +0000 (+0200) Subject: Add and use `TXmlGuiPackageInstallationPlugin::appendElementChildren()` X-Git-Tag: 5.2.0_Alpha_1~622 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=c5b55690c50f01527838e8d6565828ba07f732f0;p=GitHub%2FWoltLab%2FWCF.git Add and use `TXmlGuiPackageInstallationPlugin::appendElementChildren()` See #2545 --- diff --git a/wcfsetup/install/files/lib/system/devtools/pip/TXmlGuiPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/devtools/pip/TXmlGuiPackageInstallationPlugin.class.php index b9a52a9c1b..c75c3f798f 100644 --- a/wcfsetup/install/files/lib/system/devtools/pip/TXmlGuiPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/devtools/pip/TXmlGuiPackageInstallationPlugin.class.php @@ -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. diff --git a/wcfsetup/install/files/lib/system/package/plugin/ACLOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/ACLOptionPackageInstallationPlugin.class.php index c4da644ad9..fa4c7a909a 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/ACLOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/ACLOptionPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/ACPMenuPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/ACPMenuPackageInstallationPlugin.class.php index 1a6b71be25..182a03a164 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/ACPMenuPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/ACPMenuPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/ACPSearchProviderPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/ACPSearchProviderPackageInstallationPlugin.class.php index dfcb1bf64c..5b37b32ce6 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/ACPSearchProviderPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/ACPSearchProviderPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/AbstractMenuPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/AbstractMenuPackageInstallationPlugin.class.php index 3e22a9e7e2..46396462f4 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/AbstractMenuPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/AbstractMenuPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php index dbd8280296..4242730f1c 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/AbstractOptionPackageInstallationPlugin.class.php @@ -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; diff --git a/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php index 45c7823674..7813ddbbb5 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/BBCodePackageInstallationPlugin.class.php @@ -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'); diff --git a/wcfsetup/install/files/lib/system/package/plugin/BoxPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/BoxPackageInstallationPlugin.class.php index 0562465403..cc3e207b75 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/BoxPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/BoxPackageInstallationPlugin.class.php @@ -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))); diff --git a/wcfsetup/install/files/lib/system/package/plugin/ClipboardActionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/ClipboardActionPackageInstallationPlugin.class.php index 0ded11205c..c13af06635 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/ClipboardActionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/ClipboardActionPackageInstallationPlugin.class.php @@ -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); diff --git a/wcfsetup/install/files/lib/system/package/plugin/CoreObjectPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/CoreObjectPackageInstallationPlugin.class.php index acbc37655f..ac3be5f5d4 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/CoreObjectPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/CoreObjectPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php index 3e237ab21f..7e305f817a 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/CronjobPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php index 2af03334ce..bb964c0fa5 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/EventListenerPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php index ab795b2881..77bea3bbc1 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/MediaProviderPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/MenuItemPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/MenuItemPackageInstallationPlugin.class.php index e0d6dbe2b1..64e943b2cb 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/MenuItemPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/MenuItemPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/ObjectTypeDefinitionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/ObjectTypeDefinitionPackageInstallationPlugin.class.php index 8098bee288..c7ab5cd01f 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/ObjectTypeDefinitionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/ObjectTypeDefinitionPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php index 55396d3ea1..b5cda75156 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/OptionPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php index a0ca0960ae..55aefa6b8c 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/PagePackageInstallationPlugin.class.php @@ -766,6 +766,7 @@ class PagePackageInstallationPlugin extends AbstractXMLPackageInstallationPlugin $page->appendChild($name); } + // TODO: use `appendElementChildren` $optionalElements = [ 'controller', 'handler', 'controllerCustomURL', 'hasFixedParent', 'parent', 'options', 'permissions', 'cssClassName', 'allowSpidersToIndex', diff --git a/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php index 3d0a968b33..9810d3a8ad 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/SmileyPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php index aac1ff32d9..2ccf251d47 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/TemplateListenerPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php index 56c487e54a..49c13760b7 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserGroupOptionPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserMenuPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserMenuPackageInstallationPlugin.class.php index 037e311dcc..43c8252c67 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserMenuPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserMenuPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserNotificationEventPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserNotificationEventPackageInstallationPlugin.class.php index 052d636df5..95ece9d4fa 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserNotificationEventPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserNotificationEventPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php index 89c04bd9df..9ef6c592eb 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserOptionPackageInstallationPlugin.class.php @@ -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; } diff --git a/wcfsetup/install/files/lib/system/package/plugin/UserProfileMenuPackageInstallationPlugin.class.php b/wcfsetup/install/files/lib/system/package/plugin/UserProfileMenuPackageInstallationPlugin.class.php index d6e6aab1c2..b0e1fb5db5 100644 --- a/wcfsetup/install/files/lib/system/package/plugin/UserProfileMenuPackageInstallationPlugin.class.php +++ b/wcfsetup/install/files/lib/system/package/plugin/UserProfileMenuPackageInstallationPlugin.class.php @@ -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; }