From a5caafa705ce6e30da2ef44d45f0531c9e931e2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Wed, 19 Sep 2018 16:53:12 +0200
Subject: [PATCH] =?UTF-8?q?Gestion=20des=20sections=20avec=20:=20-=20Possi?=
=?UTF-8?q?bilit=C3=A9=20de=20les=20dupliquer=20comme=20autant=20de=20bloc?=
=?UTF-8?q?s=20-=20Possibilit=C3=A9=20de=20les=20cacher=20le=20cas=20?=
=?UTF-8?q?=C3=A9ch=C3=A9ant?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/Publisher.php | 160 +++++++++++++++++++++++++++++++++-------------
1 file changed, 116 insertions(+), 44 deletions(-)
diff --git a/src/Publisher.php b/src/Publisher.php
index ab6e8a4..1ee526a 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
--
GitLab