$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.
$category = $document->createElement('category');
$category->setAttribute('name', $formData['name']);
- $category->appendChild($document->createElement('objecttype', $formData['objecttype']));
+ $this->appendElementChildren($category, ['objecttype'], $form);
return $category;
$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;
}
* @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;
}
$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;
}
->description('wcf.acp.pip.abstractMenu.showOrder.description')
->objectProperty('showorder')
->minimum(1)
+ ->nullable()
]);
}
$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;
}
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);
}
}
- $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;
$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');
$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)));
$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);
* @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;
}
$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'] !== '') {
}
}
- 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;
}
$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;
}
$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;
}
->description('wcf.acp.pip.menuItem.showOrder.description')
->objectProperty('showorder')
->minimum(1)
+ ->nullable()
]);
/** @var SingleSelectionFormField $menuField */
$data = $formData['data'];
$menuItem = $document->createElement($this->tagName);
+
$menuItem->setAttribute('identifier', $data['identifier']);
if (!empty($data['parent'])) {
$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;
}
* @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;
}
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;
}
$page->appendChild($name);
}
+ // TODO: use `appendElementChildren`
$optionalElements = [
'controller', 'handler', 'controllerCustomURL', 'hasFixedParent',
'parent', 'options', 'permissions', 'cssClassName', 'allowSpidersToIndex',
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;
->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;
+ }));
}
/**
$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;
}
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;
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()
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()
->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;
+ }));
}
/**
$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;
}
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;
}
* @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;
}
* @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;
}
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;
}
$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;
}