diff --git a/src/Publisher.php b/src/Publisher.php index ab6e8a410a82009ac26c5f217cb9b13b016c1455..1ee526af9f5995965efeb50177e7a0cb53f4e6ee 100644 --- a/src/Publisher.php +++ b/src/Publisher.php @@ -5,6 +5,8 @@ namespace Unicaen\OpenDocument; use Exception; use DOMDocument; use DOMElement; +use DOMNode; +use DOMNodeList; use ZipArchive; class Publisher @@ -135,33 +137,6 @@ class Publisher - /** - * Crée un sous-document à parcourir ensuite - * - * @param string $node_name - * @param string $variable_name - * - * @return Query - */ - public function subDoc($nodeName, $variableName) - { - return $this->query->subDoc($nodeName, $variableName); - } - - - - /** - * Cache une section - * - * @param string $sectionName - */ - public function hideSection($sectionName) - { - $this->query->hideSection($sectionName); - } - - - /** * @param DOMElement $element * @@ -186,6 +161,33 @@ class Publisher + public function getSections(DOMElement $element): array + { + $textNs = $this->getDocument()->getNamespaceUrl('text'); + + $sections = []; + $vElements = $this->findFrom($element, 'text:section'); + foreach ($vElements as $vElement) { + $name = $vElement->getAttributeNS($textNs, 'name'); + + if (!isset($sections[$name])) $sections[$name] = []; + $sections[$name][] = $vElement; + } + + return $sections; + } + + + + public function hideSection(DOMElement $section): Publisher + { + $section->parentNode->removeChild($section); + + return $this; + } + + + /** * @param DOMElement $element * @param string $value @@ -248,9 +250,6 @@ class Publisher $pageBreak->setAttributeNS($textNs, 'text:style-name', self::PAGE_BREAK_NAME); $element->insertBefore($pageBreak, $element->firstChild); - /*$pageBreak = "<text:p text:style-name=\"".self::PAGE_BREAK_NAME."\"></text:p>"; - $xml = str_replace('<office:text>','<office:text>'.$pageBreak, $xml);*/ - return $this; } @@ -314,6 +313,7 @@ class Publisher private function publishValues(DOMElement $element, array $values): Publisher { $variables = $this->getVariables($element); + $sections = $this->getSections($element); foreach ($values as $name => $val) { if (is_array($val)) { @@ -324,9 +324,33 @@ class Publisher $this->publishSubData($elVar, $vparent, $val); } } - } elseif (isset($variables[$name])) { - foreach ($variables[$name] as $vElement) { - $this->setVariable($vElement, $val); + if (isset($sections[$vname])) { + foreach ($sections[$vname] as $elSec) { + $this->publishSubData($elSec, $vparent, $val); + } + } + } else { + if (isset($variables[$name])) { + foreach ($variables[$name] as $vElement) { + $this->setVariable($vElement, $val); + } + } + list($sName, $sType) = explode("@", $name); + if ($sType == 'text:section') { + $name = $sName; + } + if (isset($sections[$name])) { + foreach ($sections[$name] as $sElement) { + if ($val === null + || $val === 0 + || $val === '0' + || $val === false + || $val === '' + || strtolower($val) === 'false' + ) { + $this->hideSection($sElement); + } + } } } } @@ -345,21 +369,25 @@ class Publisher */ private function publishSubData(DOMElement $element, string $parent, array $values): Publisher { - $i = 10; - $found = false; - for ($i = 0; $i < 10; $i++) { - $parentNode = isset($parentNode) ? $parentNode->parentNode : $element->parentNode; - if ($parentNode->nodeName == $parent) { - $found = true; - break; + if ($element->tagName == 'text:section') { + $parentNode = $element; // C'est la section qui est dupliquée + } else { + $i = 10; + $found = false; + for ($i = 0; $i < 10; $i++) { + $parentNode = isset($parentNode) ? $parentNode->parentNode : $element->parentNode; + if ($parentNode->nodeName == $parent) { + $found = true; + break; + } } - } - if (!$found) { - throw new \Exception('Le noeud parent de type ' . $parent . ' n\'a pas été trouvé'); + if (!$found) { + throw new \Exception('Le noeud parent de type ' . $parent . ' n\'a pas été trouvé'); + } } - foreach( $values as $vals ){ + foreach ($values as $vals) { $clone = $parentNode->cloneNode(true); $this->publishValues($clone, $vals); @@ -410,6 +438,50 @@ class Publisher + /** + * @param string $name + * + * @return DOMNode[] + * @throws Exception + */ + private function find(string $name): DOMNodeList + { + $document = $this->getDocument(); + + return $document->find($document->getContent(), $name); + } + + + + /** + * @param DOMNode $node + * @param $name + * + * @return DOMNodeList + * @throws Exception + */ + private function findFrom(DOMNode $node, $name): DOMNodeList + { + return $this->getDocument()->find($node, $name); + } + + + + /** + * @param string $name + * @param array $attrs + * + * @return DOMElement + */ + private function newElement(string $name, array $attrs = []): DOMElement + { + $document = $this->getDocument(); + + return $document->newElement($document->getContent(), $name, $attrs); + } + + + /** * Ajoute du contenu au fichier content.xml * Méthode à ne pas exploiter