19 include_once (
'Class.Layout.php');
20 include_once (
'Lib.FileMime.php');
29 private $saved_sections = array();
30 private $added_images = array();
31 private $removed_images = array();
53 $this->LOG =
new Log(
"",
"Layout");
57 $this->generation =
"";
61 if ((!file_exists(
$file)) && (
$file[0] !=
'/')) {
62 $file = GetParam(
"CORE_PUBDIR") .
"/$file";
65 if (file_exists(
$file)) {
66 if (filesize(
$file) > 0) {
71 $this->
template =
"file [$caneva] not exists";
78 if (!$node)
return false;
79 $document = $node->ownerDocument;
80 $nodeAsString = $document->saveXML($node);
81 preg_match(
'!<.*?>(.*)</.*?>!s', $nodeAsString, $match);
94 $head =
'<?xml version="1.0" encoding="UTF-8"?>
95 <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">';
96 $foot =
'</office:document-content>';
97 $domblock =
new DOMDocument();
99 $block = trim($block);
100 if (substr($block, 0, 2) ==
'</') {
102 $firsttag = strpos($block,
'>');
103 $lasttag = strrpos($block,
'<');
104 $frag1 = substr($block, 0, $firsttag + 1);
105 $frag2 = substr($block, $lasttag);
106 $block = substr($block, $firsttag + 1, $lasttag - strlen($block));
113 if (!$domblock->loadXML($head . $block . $foot)) {
114 print
"\n=============\n";
115 print $head . trim($block) . $foot;
119 $lists = $domblock->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"list");
120 foreach ($lists as
$list) {
121 $items = $list->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"list-item");
122 if ($items->length > 0) {
123 $item = $items->item(0);
125 if (preg_match(
'/\[V_[A-Z0-9_-]+\]/', $item->textContent, $reg)) {
130 $tvkey = explode(
'<text:tab/>', $vkey);
132 foreach ($tvkey as $key) {
133 $clone = $item->cloneNode(
true);
134 $item->parentNode->appendChild($clone);
137 $item->parentNode->removeChild($item);
142 return $frag1 . $this->
innerXML($domblock->firstChild) . $frag2;
143 return $frag1 . $domblock->saveXML($domblock->firstChild->firstChild) . $frag2;
149 while (!empty($mynode->parentNode)) {
150 $mynode = $mynode->parentNode;
151 if ($mynode->tagName ==
$type) {
161 private function getNodeDepth(DOMNode & $node)
165 while (!empty($mynode->parentNode)) {
167 $mynode = $mynode->parentNode;
173 $this->
template = preg_replace(
"/(?m)\[BLOCK\s*([^\]]*)\](.*?)\[ENDBLOCK\s*\\1\]/se",
"\$this->SetBlock('\\1','\\2')", $this->
template);
188 if (is_array($val)) $val = null;
189 if ($val !== null) $cond = ($val ==
true);
191 if ($this->rif[
$name] !== null) $cond = ($this->rif[
$name] ==
true);
192 elseif ($this->rkey[
$name] !== null) $cond = ($this->rkey[
$name] ==
true);
194 if ($cond !== null) {
195 if ($cond xor $not) {
200 if ($not)
$out =
"[IFNOT $name]" . $block .
"[ENDIF $name]";
201 else $out =
"[IF $name]" . $block .
"[ENDIF $name]";
203 if ($this->strip ==
'Y')
$out = str_replace(
"\\\"",
"\"",
$out);
214 while ($templateori != $this->
template && ($level < 10)) {
216 $this->
template = preg_replace(
"/(?m)\[IF(NOT)?\s*([^\]]*)\](.*?)\[ENDIF\s*\\2\]/se",
"\$this->TestIf('\\2','\\3','\\1')", $this->
template);
220 $this->fixSpanIf($this->
template);
223 if (!$this->dom->loadXML($this->template)) {
225 throw new Exception(
"Error in parse condition");
228 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"user-field-get");
230 $domElemsToRemove = array();
231 $domElemsToClean = array();
233 foreach ($lists as
$list) {
234 if (!$list->getAttribute(
'office:string-value')) {
235 if ($list->textContent ==
'') {
236 $domElemsToRemove[] =
$list;
239 $domElemsToClean[] =
$list;
243 foreach ($domElemsToClean as $domElement) {
247 foreach ($domElemsToRemove as $domElement) {
248 $domElement->parentNode->removeChild($domElement);
251 $this->
template = $this->dom->saveXML();
259 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"user-field-decl");
260 foreach ($lists as
$list) {
261 $list->setAttribute(
'office:string-value', str_replace(
'[',
'-CROCHET-', $list->getAttribute(
'office:string-value')));
262 $list->setAttribute(
'text:name', str_replace(
'[',
'-CROCHET-', $list->getAttribute(
'text:name')));
265 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"user-field-get");
266 foreach ($lists as $list) {
267 if ($list->parentNode->tagName !=
'text:span') {
268 $nt = $this->dom->createElement(
"text:span");
269 $list->parentNode->insertBefore($nt, $list);
270 $nt->appendChild($list);
274 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"user-field-get");
275 $userFields = array();
277 foreach ($lists as $list) {
278 $textContent = $list->nodeValue;
279 if (substr($textContent, 0, 1) ==
'[') {
280 $userFields[] =
$list;
281 $nt = $this->dom->createTextNode($textContent);
282 $list->parentNode->insertBefore($nt, $list);
285 foreach ($userFields as $list) {
286 $list->parentNode->removeChild($list);
289 $this->
template = $this->dom->saveXML();
297 $this->dom->loadXML($this->
template);
298 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"user-field-decl");
299 foreach ($lists as
$list) {
300 $list->setAttribute(
'office:string-value', str_replace(
'-CROCHET-',
'[', $list->getAttribute(
'office:string-value')));
301 $list->setAttribute(
'text:name', str_replace(
'-CROCHET-',
'[', $list->getAttribute(
'text:name')));
303 $this->
template = $this->dom->saveXML();
314 if ($this->strip ==
'Y') {
316 $block = str_replace(
"\\\"",
"\"", $block);
319 if (isset($this->data) && isset($this->data[
"$name"]) && is_array($this->data[
"$name"])) {
320 foreach ($this->data[
"$name"] as $k => $v) {
323 foreach ($this->corresp[
"$name"] as $k2 => $v2) {
325 if (strstr($v[$v2],
'<text:tab/>')) {
327 } elseif ((!is_object($v[$v2])) && (!is_array($v[$v2]))) $loc = str_replace($k2, $v[$v2], $loc);
346 $out = preg_replace(
"/\[ZONE\s*([^:]*):([^\]]*)\]/e",
"\$this->execute('\\1','\\2')",
$out);
353 if (isset($this->rkey)) {
354 $this->
template = preg_replace($this->pkey, $this->rkey, $this->
template);
357 private function ParseKeyXML()
359 if (isset($this->rkeyxml)) {
361 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"p");
362 foreach ($this->rkeyxml as $k => $xmlkey) {
363 print
"\n\nserach [$k]\n";
364 foreach ($lists as
$list) {
365 $pstyle = $list->getAttribute(
"text:style-name");
366 $content = $this->dom->saveXML($list);
369 print
"\n----------------\nfind C:$xmlkey $k:\n$content";
381 if (!file_exists($odtfile))
return "file $odtfile not found";
382 $this->cibledir = uniqid(
getTmpDir() .
"/odf");
384 $cmd = sprintf(
"unzip %s -d %s >/dev/null", escapeshellarg($odtfile) , escapeshellarg($this->cibledir));
387 $contentxml = $this->cibledir .
"/content.xml";
388 if (file_exists($contentxml)) {
389 $this->content_template = file_get_contents($contentxml);
392 $contentxml = $this->cibledir .
"/META-INF/manifest.xml";
393 if (file_exists($contentxml)) {
394 $this->manifest = file_get_contents($contentxml);
397 $contentxml = $this->cibledir .
"/styles.xml";
398 if (file_exists($contentxml)) {
399 $this->style_template = file_get_contents($contentxml);
402 $contentxml = $this->cibledir .
"/meta.xml";
403 if (file_exists($contentxml)) {
404 $this->meta_template = file_get_contents($contentxml);
414 if (file_exists(
$odsfile))
return "file $odsfile must not be present";
416 $contentxml = $this->cibledir .
"/content.xml";
418 $this->content_template = preg_replace(
"!</?text:bookmark-(start|end)([^>]*)>!s",
"", $this->content_template);
429 $this->content_template = str_replace(
"<text:line-break/>",
"<text:line-break/>", $this->content_template);
431 file_put_contents($contentxml, $this->content_template);
433 $contentxml = $this->cibledir .
"/META-INF/manifest.xml";
434 file_put_contents($contentxml, $this->manifest);
436 $contentxml = $this->cibledir .
"/styles.xml";
437 file_put_contents($contentxml, $this->style_template);
439 $contentxml = $this->cibledir .
"/meta.xml";
440 file_put_contents($contentxml, $this->meta_template);
442 $cmd = sprintf(
"cd %s;zip -q -Z store -X %s mimetype ;zip -q -r -X -u %s * && /bin/rm -fr %s", escapeshellarg($this->cibledir) , escapeshellarg(
$odsfile) , escapeshellarg(
$odsfile) , escapeshellarg($this->cibledir));
452 if ($this->action ==
"")
return (
"Layout not used in a core environment");
454 $actionargn = str_replace(
":",
"--", $actionargn);
455 $acturl = parse_url($actionargn);
459 $OLD_ZONE_ARGS = $ZONE_ARGS;
460 if (isset($acturl[
"query"])) {
461 $acturl[
"query"] = str_replace(
"--",
":", $acturl[
"query"]);
462 $zargs = explode(
"&", $acturl[
"query"]);
463 foreach ($zargs as $k => $v) {
464 if (preg_match(
"/([^=]*)=(.*)/", $v, $regs)) {
466 $ZONE_ARGS[$regs[1]] = urldecode($regs[2]);
471 if (
$appname != $this->action->parent->name) {
475 $appl = & $this->action->parent;
478 if ((
$actionname != $this->action->name) || ($OLD_ZONE_ARGS != $ZONE_ARGS)) {
488 $res = $act->CompleteSet(
$appl);
491 $res = $act->execute();
493 $ZONE_ARGS = $OLD_ZONE_ARGS;
496 return (
"Fatal loop : $actionname is called in $actionname");
504 public function set($tag, $val)
506 if (!
isUTF8($val)) $val = utf8_encode($val);
507 if (!$this->isXml($val)) {
508 $this->pkey[$tag] =
"/\[$tag\]/";
509 if (is_array($val)) $val = implode(
'<text:tab/>', $val);
510 $this->rkey[$tag] = $val;
513 $this->rkeyxml[$tag] = $val;
521 public function eSet($tag, $val)
531 return str_replace(array(
548 return preg_match(
"/<text:/", $val);
554 public function get($tag)
556 if (isset($this->rkey))
return $this->rkey[$tag];
564 if ($this->encoding ==
"utf-8") bind_textdomain_codeset(
"what",
'UTF-8');
565 $this->
template = preg_replace(
'/\[TEXT:([^\]]*)\]/e',
"\$this->Text('\\1')", $this->
template);
566 if ($this->encoding ==
"utf-8") bind_textdomain_codeset(
"what",
'ISO-8859-15');
577 $manifest_root = null;
579 foreach ($items as $item) {
580 if ($item->nodeName ==
'manifest:manifest') {
581 $manifest_root = $item;
585 if ($manifest_root === null) {
589 $items =
$manifest->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0",
"file-entry");
590 foreach ($items as $item) {
591 $type = $item->getAttribute(
"manifest:media-type");
592 if (substr(
$type, 0, 6) ==
"image/") {
593 $file = $item->getAttribute(
"manifest:full-path");
594 if (in_array(
$file, $this->removed_images)) {
595 $item->parentNode->removeChild($item);
599 foreach ($this->added_images as $image) {
601 $new =
$manifest->createElement(
"manifest:file-entry");
602 $new->setAttribute(
"manifest:media-type", $mime);
603 $new->setAttribute(
"manifest:full-path", $image);
604 $manifest_root->appendChild($new);
617 if (strpos(
$file,
'<text:tab') !==
false) {
618 return 'muliple values : fail';
620 $imgs = $draw->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
"image");
622 if ($imgs->length > 0) {
623 $img = $imgs->item(0);
624 if (file_exists(
$file)) {
625 $draw->setAttribute(
'draw:name', substr(
$name, 2) .
' ' . uniqid() . mt_rand(1000, 9999));
626 $href =
'Pictures/dcp' . uniqid() . mt_rand(1000000, 9999999) . substr(
$img->getAttribute(
'xlink:href') , -9);
627 $img->setAttribute(
'xlink:href', $href);
628 $this->added_images[] = $href;
629 if (!copy(
$file, $this->cibledir .
'/' . $href)) {
634 $width = $draw->getAttribute(
'svg:width');
637 if (preg_match(
'/[0-9\.]+(.*)$/', $width, $reg)) $unit = $reg[1];
638 $height = sprintf(
"%.03f%s", (doubleval($width) /
$size[0]) *
$size[1], $unit);
639 $draw->setAttribute(
'svg:height', $height);
650 $draws = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
"frame");
651 foreach ($draws as $draw) {
652 $name = trim($draw->getAttribute(
'draw:name'));
653 if (preg_match(
'/\[(V_[A-Z0-9_-]+)\]/',
$name, $reg)) {
655 if (isset($this->rkey[$key])) {
656 $this->
setDraw($draw, $key, $this->rkey[$key]);
667 $objNodeListNested = $objNode->childNodes;
668 foreach ($objNodeListNested as $objNodeNested) {
669 if ($objNodeNested->nodeType == XML_ELEMENT_NODE) {
670 $objNodeNested->removeAttribute(
"xml:id");
681 protected function replaceNodeText(DOMNode & $objNode, $strOldContent, $strNewContent)
683 if ($strNewContent === null)
return;
684 if (is_array($strNewContent)) {
685 throw new Exception(
"node replacement must be a string : array found");
687 $objNodeListNested = & $objNode->childNodes;
688 foreach ($objNodeListNested as $objNodeNested) {
689 if ($objNodeNested->nodeType == XML_TEXT_NODE) {
690 if ($objNodeNested->nodeValue !=
"") {
691 if (strpos($strNewContent,
'<text:p>') !==
false) {
692 $strNewContent = str_replace(
'<',
'--Lower.Than--', $strNewContent);
693 $strNewContent = str_replace(
'>',
'--Greater.Than--', $strNewContent);
694 $strNewContent = htmlspecialchars_decode($strNewContent);
696 $objNodeNested->nodeValue = str_replace($strOldContent, $strNewContent, $objNodeNested->nodeValue);
698 } elseif ($objNodeNested->nodeType == XML_ELEMENT_NODE) {
699 if ($objNodeNested->nodeName ==
'text:text-input') {
700 $name = $objNodeNested->getAttribute(
'text:description');
701 if (
$name == $strOldContent) {
704 } elseif ($objNodeNested->nodeName ==
'text:drop-down') {
705 $name = $objNodeNested->getAttribute(
'text:name');
706 if (
$name == $strOldContent) {
709 } elseif ($objNodeNested->nodeName ==
'draw:frame') {
710 $name = $objNodeNested->getAttribute(
'draw:name');
711 if (substr(
$name, 0, strlen($strOldContent)) == $strOldContent) {
712 $this->
setDraw($objNodeNested, substr($strOldContent, 1, -1) , $strNewContent);
715 $this->
replaceNodeText($objNodeNested, $strOldContent, $strNewContent);
726 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"list");
727 foreach ($lists as $list) {
728 $items = $list->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"list-item");
729 if ($items->length > 0) {
730 $item = $items->item(0);
731 $skey = implode(
'|', array_keys($this->arrayMainKeys));
732 if (preg_match_all(
"/\[($skey)\]/", $this->
innerXML($list) , $reg)) {
736 foreach ($reg0 as $k => $v) {
737 $key = substr(trim($v) , 1, -1);
738 $tvkey[$key] = $this->arrayMainKeys[$key];
739 $maxk = max(count($tvkey[$key]) , $maxk);
742 for ($i = 0; $i < $maxk; $i++) {
743 $clone = $item->cloneNode(
true);
744 $item->parentNode->appendChild($clone);
745 foreach ($tvkey as $kk => $key) {
750 $item->parentNode->removeChild($item);
757 private function _section_cmp($k1, $k2)
759 if ($k2 > $k1)
return 1;
760 else if ($k2 < $k1)
return -1;
769 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"section");
773 foreach ($lists as $item) {
774 $depth = $this->getNodeDepth($item);
775 $section[($depth * 200) + $ks] = $item;
779 uksort($section, array(
784 foreach ($section as $item) {
786 $skey = implode(
'|', array_keys($this->arrayMainKeys));
787 if (preg_match_all(
"/\[($skey)\]/", $this->
innerXML($item) , $reg)) {
791 foreach ($reg0 as $k => $v) {
792 $key = substr(trim($v) , 1, -1);
793 $tvkey[$key] = $this->arrayMainKeys[$key];
794 $maxk = max(count($tvkey[$key]) , $maxk);
797 for ($i = 0; $i < $maxk; $i++) {
798 $clone = $item->cloneNode(
true);
799 $item->parentNode->insertBefore($clone, $item);
800 foreach ($tvkey as $kk => $key) {
812 $item->parentNode->removeChild($item);
825 if (strpos(
$file,
'<text:tab') !==
false) {
826 return 'muliple values : fail';
828 $node->nodeValue =
$value;
829 $node->setAttribute(
"text:description",
'[PP' .
$name .
'PP]');
840 if (strpos(
$file,
'<text:tab') !==
false) {
841 return 'muliple values : fail';
844 $node->setAttribute(
"text:name",
'[PP' .
$name .
'PP]');
845 $value = str_replace(array(
854 $item =
new DOMElement(
'text:label',
'',
'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
855 $item = $node->appendChild($item);
856 $item->setAttribute(
"text:current-selected",
'true');
857 $item->setAttribute(
"text:value",
$value);
858 $node->appendChild(
new DOMText(
$value));
867 $objNodeListNested = $objNode->childNodes;
868 $objNode->nodeValue =
'';
869 if (!empty($objNodeListNested) && $objNodeListNested->length > 0) {
870 foreach ($objNodeListNested as $objNodeNested) {
871 $objNode->removeChild($objNodeNested);
881 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:table:1.0",
"table-row");
884 $skey = implode(
'|', array_keys($this->arrayMainKeys));
885 foreach ($lists as $rowItem) {
886 if (preg_match(
"/\[($skey)\]/", $this->
innerXML($rowItem) , $reg)) {
887 $validRow[] = $rowItem;
890 foreach ($validRow as $rowItem) {
891 if (preg_match_all(
"/\[($skey)\]/", $this->
innerXML($rowItem) , $reg)) {
895 foreach ($reg0 as $k => $v) {
896 $key = substr(trim($v) , 1, -1);
897 $tvkey[$key] = $this->arrayMainKeys[$key];
898 $maxk = max(count($tvkey[$key]) , $maxk);
901 for ($i = 0; $i < $maxk; $i++) {
902 $clone = $rowItem->cloneNode(
true);
903 $rowItem->parentNode->insertBefore($clone, $rowItem);
904 foreach ($tvkey as $kk => $key) {
917 $rowItem->parentNode->removeChild($rowItem);
926 private static function getArrayDepth($v)
929 while (is_array($v)) {
945 if (count($levelPath) == 1) {
946 if (isset($this->arrayMainKeys[$key])) {
947 $index = current($levelPath);
948 return $this->arrayMainKeys[$key][$index];
951 if (!isset($this->arrayKeys[$key]))
return null;
953 $value = $this->arrayKeys[$key];
954 foreach ($levelPath as $index) {
964 private function fixSpanIf(&
$s)
966 $s = preg_replace(
'/<text:span ([^>]*)>\s*<\/text:p>/s',
"</text:p>",
$s);
967 $s = preg_replace(
'/<text:p ([^>]*)>\s*<\/text:span>/s',
"<text:p \\1>",
$s);
979 $inner = $row->ownerDocument->saveXML($row);
981 $head =
'<?xml version="1.0" encoding="UTF-8"?><office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">';
982 $foot =
'</office:document-content>';
985 while ($level < 10) {
986 $replacement = preg_replace(
"/(?m)\[IF(NOT)?\s*([^\]]*)\](.*?)\[ENDIF\s*\\2\]/se",
"\$this->TestIf('\\2','\\3','\\1',\$levelPath)", $inner);
987 if ($inner == $replacement)
break;
988 else $inner = $replacement;
991 $this->fixSpanIf($replacement);
993 $dxml =
new DomDocument();
995 $dxml->loadXML($head . $replacement . $foot);
996 $ot = $dxml->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
"document-content");
997 if ($ot->length > 0) {
998 $newnode = $this->dom->importNode($ot->item(0)->firstChild,
true);
1001 while ($row->firstChild) {
1002 $row->removeChild($row->firstChild);
1005 while ($newnode->firstChild) {
1006 $row->appendChild($newnode->firstChild);
1036 if (count($this->arrayKeys) == 0)
return;
1038 $subIndex = count($levelPath);
1039 foreach ($this->arrayKeys as $k => $v) {
1040 if ($this->getArrayDepth($v) == $subIndex) $keys[] = $k;
1043 $rowList = $row->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:$ns:1.0", $tag);
1044 if ($rowList->length > 0) {
1045 $skey = implode(
'|', $keys);
1049 foreach ($rowList as $item) {
1050 if (preg_match_all(
"/\[($skey)\]/", $this->
innerXML($item) , $reg)) {
1053 foreach ($reg[1] as $k => $v) {
1056 $maxk = max(count($tvkey[$v]) , $maxk);
1060 for ($i = 0; $i < $maxk; $i++) {
1061 $clone = $item->cloneNode(
true);
1062 $item->parentNode->appendChild($clone);
1063 foreach ($tvkey as $kk => $key) {
1066 $newPath = array_merge($levelPath, array(
1074 $item->parentNode->removeChild($item);
1084 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"text-input");
1085 foreach ($lists as $list) {
1086 $name = $list->getAttribute(
"text:description");
1087 if (preg_match(
'/\[(V_[A-Z0-9_-]+)\]/',
$name, $reg)) {
1089 if (isset($this->rkey[$key])) {
1100 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"drop-down");
1101 foreach ($lists as $list) {
1102 $name = $list->getAttribute(
"text:name");
1103 if (preg_match(
'/\[(V_[A-Z0-9_-]+)\]/',
$name, $reg)) {
1105 if (isset($this->rkey[$key])) {
1116 $this->
template = preg_replace(
'/\[PP(V_[A-Z0-9_]+)PP\]/s',
'[$1]', $this->
template);
1117 $this->
template = str_replace(
'--Lower.Than--',
'<', $this->
template);
1118 $this->
template = str_replace(
'--Greater.Than--',
'>', $this->
template);
1125 $this->saved_sections = array();
1127 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"section");
1128 foreach ($lists as $list) {
1129 $name = $list->getAttribute(
"text:name");
1130 if (substr(
$name, 0, 5) ==
'_tpl_') {
1131 $list->parentNode->removeChild($list);
1135 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"section");
1136 foreach ($lists as $list) {
1137 $name = $list->getAttribute(
"text:name");
1138 if (substr(
$name, 0, 4) ==
'tpl_') {
1141 $heads = $list->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"h");
1142 if ($heads->length > 0) {
1143 $firsthead = $heads->item(0);
1144 $firsthead->setAttribute(
"text:style-name", trim($firsthead->getAttribute(
'text:style-name') ,
'_'));
1146 $originSection = $this->saved_sections[
$name] = $list->cloneNode(
true);
1147 $list->setAttribute(
"text:name",
'_' .
$name);
1148 $list->setAttribute(
"text:protected",
'true');
1149 $list->setAttribute(
"text:display",
'true');
1151 $heads = $originSection->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"h");
1152 if ($heads->length > 0) {
1153 $firsthead = $heads->item(0);
1154 $styleName = $firsthead->getAttribute(
"text:style-name");
1156 $styleName = trim($styleName,
'_');
1157 $styles = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:style:1.0",
"style");
1159 $tStyleName = array();
1160 foreach ($styles as
$style) {
1162 $aStyleName = $style->getAttribute(
"style:name");
1163 $tStyleName[] = $aStyleName;
1164 if ($aStyleName == $styleName) {
1165 $copyName =
'_' . $styleName .
'_';
1166 if (!(in_array($copyName, $tStyleName))) {
1167 $cloneStyle = $style->cloneNode(
true);
1168 $cloneStyle->setAttribute(
"style:name", $copyName);
1169 $cloneStyle->setAttribute(
"style:list-style-name",
"");
1170 $style->parentNode->insertBefore($cloneStyle, $style);
1172 $firsthead->setAttribute(
"text:style-name", $copyName);
1187 $inserts_to_do = array();
1189 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"section");
1190 foreach ($lists as $list) {
1191 $name = $list->getAttribute(
"text:name");
1192 if (substr(
$name, 0, 5) ==
'_tpl_' && isset($this->saved_sections[substr(
$name, 1) ])) {
1193 $node = $this->dom->importNode($this->saved_sections[substr(
$name, 1) ],
true);
1194 $inserts_to_do[] = array(
1200 foreach ($inserts_to_do as $insert_to_do) {
1203 if ($insert_to_do[1]->nextSibling) {
1204 $node = $insert_to_do[1]->parentNode->insertBefore($insert_to_do[0], $insert_to_do[1]->nextSibling);
1206 $node = $insert_to_do[1]->parentNode->appenChild($insert_to_do[0]);
1208 $node->setAttribute(
"text:protected",
'true');
1209 $node->setAttribute(
"text:display",
'none');
1221 foreach ($t as $v) $ti[$k++] = $v;
1222 if (is_array(current($t))) $this->
setArray($key, $ti);
1223 else $this->arrayMainKeys[$key] = $ti;
1230 if (!$key)
throw new Exception(
'Key must not be empty');
1231 $this->arrayKeys[$key] =
$t;
1241 if ($p_nom_block !=
"") {
1242 if ($data != null && $this->encoding ==
"utf-8") {
1243 if (is_array($data)) {
1244 foreach ($data as $k => $v) {
1245 foreach ($v as $kk => $vk) {
1247 $data[$k][$kk] = utf8_encode($vk);
1252 if (!
isUTF8($data)) $data = utf8_encode($data);
1255 $this->data[$p_nom_block] = $data;
1256 if (is_array($data)) {
1259 if (isset($elem) && is_array($elem)) {
1261 while (list($k, $v) = each($elem)) {
1262 if (!isset($this->corresp[
"$p_nom_block"][
"[$k]"])) {
1280 if (is_array($data)) {
1281 foreach ($data as $k => $v) {
1282 foreach ($v as $ki => $vi) {
1289 foreach (
$t as $k => $v) {
1290 if (count($v) > $max) $max = count($v);
1292 foreach (
$t as $k => $v) {
1293 if (count($v) < $max) {
1294 $fill = array_fill(0, $max,
'');
1295 foreach ($v as $idx => $vi) {
1302 foreach (
$t as $k => $v) {
1309 $xmldata =
'<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">' .
"</xhtml:html>";
1311 $xslt =
new xsltProcessor;
1312 $xslt->importStyleSheet(DomDocument::load(
DEFAULT_PUBDIR .
"/CORE/Layout/html2odt.xsl"));
1313 $xmlout = $xslt->transformToXML(DomDocument::loadXML($xmldata));
1315 $dxml =
new DomDocument();
1316 $dxml->loadXML($xmlout);
1317 $ot = $dxml->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
"automatic-styles");
1318 $ot1 = $ot->item(0);
1319 $ass = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
"automatic-styles");
1321 $ass0 = $ass->item(0);
1322 foreach ($ot1->childNodes as $ots) {
1323 $c = $this->dom->importNode($ots,
true);
1324 $ass0->appendChild(
$c);
1332 $imgs = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
"image");
1333 $used_images = array();
1334 foreach ($imgs as
$img) {
1335 $href = basename($img->getAttribute(
'xlink:href'));
1336 if (substr($href, 0, 7) ==
'dcp') {
1337 $used_images[] = $href;
1340 $files = glob($this->cibledir .
'/Pictures/dcp*');
1341 if (is_array($files)) {
1342 foreach ($files as
$file) {
1343 if (!in_array(basename($file) , $used_images)) {
1344 $this->removed_images[] =
'Pictures/' . basename($file);
1379 $this->dom =
new DOMDocument();
1381 $this->dom->loadXML($this->style_template);
1386 $this->
template = $this->dom->saveXML();
1401 $this->dom =
new DOMDocument();
1403 $this->dom->loadXML($this->meta_template);
1421 $this->dom->loadXML($this->
template);
1422 $lists = $this->dom->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"section");
1423 $htmlSections = array();
1424 $htmlCleanSections = array();
1426 foreach ($lists as $list) {
1427 $aid = $list->getAttribute(
"aid");
1429 if ($list->parentNode->childNodes->length == 1) {
1430 $htmlSections[] =
$list;
1433 $htmlCleanSections[] =
$list;
1435 $list->removeAttribute(
"aid");
1439 foreach ($htmlSections as $htmlSection) {
1440 $pParentHtml = $htmlSection->parentNode->parentNode;
1441 $parentHtml = $htmlSection->parentNode;
1443 if ($parentHtml->nextSibling) {
1444 $pParentHtml->insertBefore($htmlSection, $parentHtml->nextSibling);
1446 $pParentHtml->appendChild($htmlSection);
1448 $pParentHtml->removeChild($parentHtml);
1450 $pParentHtml = $htmlSection->parentNode->parentNode;
1451 $parentHtml = $htmlSection->parentNode;
1453 if (($parentHtml->nodeName ==
"text:p") && ($parentHtml->childNodes->length == 1)) {
1455 if ($parentHtml->nextSibling) {
1456 $pParentHtml->insertBefore($htmlSection, $parentHtml->nextSibling);
1458 $pParentHtml->appendChild($htmlSection);
1460 $pParentHtml->removeChild($parentHtml);
1462 $htmlCleanSections[] = $htmlSection;
1464 if (in_array($htmlSection->parentNode->nodeName, array(
1469 $htmlCleanSections[] = $htmlSection;
1474 foreach ($htmlCleanSections as $htmlSection) {
1475 if ($htmlSection->parentNode->nodeName ==
"text:p") {
1476 $nbp = $htmlSection->getElementsByTagNameNS(
"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
"p")->length;
1478 while ($htmlSection->childNodes->length > 0) {
1479 if (($htmlSection->parentNode->nodeName ==
"text:p") && ($nbp > 1)) {
1480 if ($htmlSection->firstChild->nodeName ==
"text:p") {
1481 $htmlSection->firstChild->appendChild($this->dom->createElement(
"text:line-break"));
1485 $htmlSection->parentNode->insertBefore($htmlSection->firstChild, $htmlSection);
1488 $this->
template = $this->dom->saveXML();
1496 $this->dom =
new DOMDocument();
1498 $this->dom->loadXML($this->content_template);
1514 $this->
template = $this->dom->saveXML();
1527 $this->ParseHtmlText();
1528 $this->dom =
new DOMDocument();
1529 if ($this->dom->loadXML($this->template)) {
1533 $this->
template = $this->dom->saveXML();
1538 throw new Exception(
"cannot product ooo template");
1541 throw new Exception(sprintf(
"not openDocument file %s", $this->file));
1550 if (is_object($this->action)) {
1551 $list = $this->action->parent->GetAllParam();
1552 while (list($k, $v) = each($list)) {
1553 $v = str_replace(array(
1558 ) ,
'<text:line-break/>', $v);
1570 $outfile = uniqid(
getTmpDir() .
"/odf") .
'.odt';