*/
public function addEntry(IFormDocument $form) {
foreach ($this->getProjectXmls() as $xml) {
- $document = $xml->getDocument();
-
- $newElement = $this->createXmlElement($document, $form);
- $this->insertNewXmlElement($xml, $newElement);
+ $newElement = $this->createAndInsertNewXmlElement($xml, $form);
$this->saveObject($newElement);
}
}
+ /**
+ * Creates a new XML element and insert it into the XML file.
+ *
+ * @param XML $xml
+ * @param IFormDocument $form
+ * @return \DOMElement
+ */
+ protected function createAndInsertNewXmlElement(XML $xml, IFormDocument $form) {
+ $newElement = $this->createXmlElement($xml->getDocument(), $form);
+ $this->insertNewXmlElement($xml, $newElement);
+
+ return $newElement;
+ }
+
/**
* Edits the entry of this pip with the given identifier based on the data
* provided by the given form and returns the new identifier of the entry
public function editEntry(IFormDocument $form, $identifier) {
$newElement = null;
foreach ($this->getProjectXmls() as $xml) {
- $document = $xml->getDocument();
-
- // add updated element
- $newElement = $this->createXmlElement($document, $form);
-
- // replace old element
$element = $this->getElementByIdentifier($xml, $identifier);
- DOMUtil::replaceElement($element, $newElement);
+ $newElement = $this->replaceXmlElement($xml, $form, $identifier);
$this->saveObject($newElement, $element);
return $this->getElementIdentifier($newElement);
}
+ /**
+ * Replaces an edited element with a new element and returns the new element.
+ *
+ * @param XML $xml
+ * @param IFormDocument $form
+ * @param string $identifier
+ * @return \DOMElement
+ */
+ protected function replaceXmlElement(XML $xml, IFormDocument $form, $identifier) {
+ $newElement = $this->createXmlElement($xml->getDocument(), $form);
+
+ // replace old element
+ $element = $this->getElementByIdentifier($xml, $identifier);
+ DOMUtil::replaceElement($element, $newElement);
+
+ return $newElement;
+ }
+
/**
* Returns a list of all pip entries of this pip.
*
use wcf\system\language\LanguageFactory;
use wcf\system\package\PackageArchive;
use wcf\system\WCF;
+use wcf\util\DOMUtil;
use wcf\util\StringUtil;
use wcf\util\XML;
/**
* @inheritDoc
+ * @sicne 3.2
*/
protected function getImportElements(\DOMXPath $xpath) {
return $xpath->query('/ns:language/ns:category/ns:item');
}
/**
- * Returns the xml code of an empty xml file with the appropriate structure
- * present for a new entry to be added as if it was added to an existing
- * file.
- *
- * @param string $languageCode
- * @return string
+ * @inheritDoc
+ * @sicne 3.2
*/
protected function getEmptyXml($languageCode) {
$xsdFilename = $this->getXsdFilename();
}
/** @var \DOMElement $languageCategory */
- foreach ($document->documentElement as $languageCategory) {
+ foreach ($document->documentElement->childNodes as $languageCategory) {
if ($languageCategory->getAttribute('name') === $languageCategoryName) {
$languageCategory->appendChild($languageItem);
break;
return $languageItem;
}
+
+ /**
+ * @inheritDoc
+ * @since 3.2
+ */
+ protected function createAndInsertNewXmlElement(XML $xml, IFormDocument $form) {
+ return $this->createXmlElement($xml->getDocument(), $form);
+ }
+
+ /**
+ * @inheritDoc
+ * @since 3.2
+ */
+ protected function replaceXmlElement(XML $xml, IFormDocument $form, $identifier) {
+ $newElement = $this->createXmlElement($xml->getDocument(), $form);
+
+ // replace old element
+ $element = $this->getElementByIdentifier($xml, $identifier);
+
+ if ($element->parentNode === $newElement->parentNode) {
+ DOMUtil::replaceElement($element, $newElement, false);
+ }
+ else {
+ DOMUtil::removeNode($element);
+ }
+
+ return $newElement;
+ }
}