public function testKeyNode() { $domDocument = new DOMDocument(); $wtElement = $domDocument->createElement("w:t"); $key = "[item;repeat=record.items;repeatType=row]"; $keyNode = new KeyNode($key, true, true, $wtElement); $this->assertTrue($keyNode->key() == "item"); $this->assertTrue($keyNode->originalKey() == $key); $options = $keyNode->options(); $this->assertTrue(count($options) == 2); $this->assertTrue($options["repeat"] == "record.items"); $this->assertTrue($options["repeatType"] == "row"); $keyNode = new KeyNode($key, false, true, $wtElement); $this->assertTrue($keyNode->key() == "[item;repeat=record.items;repeatType=row]"); $options = $keyNode->options(); $this->assertTrue(count($options) == 0); }
private function parseXMLElement(DOMElement $xmlElement, $data) { $tagName = $xmlElement->tagName; switch (strtoupper($tagName)) { case "W:T": //find the template keys and replace it with data $keys = $this->getTemplateKeys($xmlElement); $textContent = ""; for ($i = 0; $i < count($keys); $i++) { $key = $keys[$i]; if ($key->isKey() && $key->isComplete()) { $keyOptions = $key->options(); $keyName = $key->key(); if (count($keyOptions) == 0) { $keyValue = $this->getValue($keyName, $data); if ($keyValue !== false) { $textContent = $textContent . $keyValue; } else { $textContent = $textContent . $this->keyStartChar . $keyName . $this->keyEndChar; } } else { if (array_key_exists("repeat", $keyOptions)) { $repeatType = "row"; if (array_key_exists("repeatType", $keyOptions)) { $repeatType = strtolower($keyOptions["repeatType"]); } switch ($repeatType) { case "row": // remove the current key from the w:t textContent // and add the remaining key's original text unprocessed for ($j = $i + 1; $j < count($keys); $j++) { $remainingKey = $keys[$j]; $textContent = $textContent . $remainingKey->originalKey(); } $this->setTextContent($xmlElement, $textContent); throw new RepeatRowException($keyName, $keyOptions["repeat"]); } } } } else { $textContent = $textContent . $key->key(); } } $this->setTextContent($xmlElement, $textContent); break; case "W:DRAWING": $docPrElement = $xmlElement->getElementsByTagNameNS("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "docPr")->item(0); if ($docPrElement !== null) { $altText = $docPrElement->getAttribute("descr"); if (strlen($altText) > 2) { $keyNode = new KeyNode($altText, true, true, $docPrElement); $imagePath = $this->getValue($keyNode->key(), $data); $aBlipElem = $xmlElement->getElementsByTagName("blip")->item(0); if (file_exists($imagePath) && $aBlipElem !== null) { $resourceId = $aBlipElem->getAttribute("r:embed"); $workingFileName = basename($this->workingFile); $relFile = $this->workingDir . '/word/_rels/' . $workingFileName . '.rels'; if (file_exists($relFile)) { $relDocument = new DOMDocument(); $relDocument->load($relFile); $relElements = $relDocument->getElementsByTagName("Relationship"); $imageExtn = ".png"; $files = array_diff(scandir($this->workingDir . '/word/media'), array(".", "..")); $templateImageRelPath = 'media/rImage' . count($files) . $imageExtn; $templateImagePath = $this->workingDir . '/word/' . $templateImageRelPath; $newResourceId = "rId" . ($relElements->length + 1); $aBlipElem->setAttribute("r:embed", $newResourceId); $newRelElement = $relDocument->createElement("Relationship"); $newRelElement->setAttribute("Id", $newResourceId); $newRelElement->setAttribute("Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"); $newRelElement->setAttribute("Target", $templateImageRelPath); $relDocument->documentElement->appendChild($newRelElement); $relDocument->save($relFile); copy($imagePath, $templateImagePath); } } } } break; default: if ($xmlElement->hasChildNodes()) { $childNodes = $xmlElement->childNodes; $childNodesArray = array(); foreach ($childNodes as $childNode) { $childNodesArray[] = $childNode; } foreach ($childNodesArray as $childNode) { if ($childNode->nodeType === XML_ELEMENT_NODE) { try { $newChild = $this->parseXMLElement($childNode, $data); $xmlElement->replaceChild($newChild, $childNode); } catch (RepeatTextException $te) { //not supported yet } catch (RepeatRowException $re) { if (strtoupper($xmlElement->tagName) === "W:TBL") { $repeatingArray = $this->getValue($re->getKey(), $data); $nextRow = $childNode->nextSibling; $repeatingRowElement = $xmlElement->removeChild($childNode); $repeatingKeyName = $re->getName(); if ($repeatingArray && is_array($repeatingArray)) { foreach ($repeatingArray as $repeatingData) { $repeatedRowElement = $repeatingRowElement->cloneNode(true); $newData = $data; $newData[$repeatingKeyName] = $repeatingData; $generatedRow = $this->parseXMLElement($repeatedRowElement, $newData); $xmlElement->insertBefore($generatedRow, $nextRow); } } } else { throw $re; } } } } } } return $xmlElement; }