Overview
Comment: | ODTTemplate (alpha) |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
b2ea7743c8df47b10071b3c6947512b3 |
User & Date: | bohwaz on 2019-11-06 00:30:24 |
Other Links: | manifest | tags |
Context
2019-11-12
| ||
16:09 | HTTP: fix methods and tests to find root and app URL check-in: 21b0f91caa user: bohwaz tags: 5.6, trunk | |
2019-11-06
| ||
00:30 | ODTTemplate (alpha) check-in: b2ea7743c8 user: bohwaz tags: trunk | |
2019-10-31
| ||
16:06 | HTTP: add methods to get scheme, host, and app URI check-in: 911f62bb8f user: bohwaz tags: trunk | |
Changes
Added src/lib/KD2/ODTTemplate.php version [50cab38320].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | <?php namespace KD2; use ZipArchive; use DOMDocument; use DOMXPath; use DOMNode; /* Recherche de caractère : { dans n'importe quel nœud texte, 1ère occurrence { dans n'importe quel nœud texte tout de suite après le premier autant d'espaces que voulu a-z dans nœud texte algo : si caractère trouvé, aller au suivant (pos +1), si la chaîne est terminée, aller au nœud suivant (nextSibling), en parcourant éventuellement ses enfants (en ignorant les espaces), s'arrêter dès qu'un autre caractère que celui recherché est rencontré TODO : gérer les FODT Clean case: <text:h text:style-name="P4" text:outline-level="1">Facture n° <text:span text:style-name="T4">{{numero_facture}}</text:span></text:h> Mixed case: <text:p text:style-name="P3"><text:span text:style-name="T3">Association : </text:span>{{ nom_asso }} <text:span text:style-name="T8">(loi 1901)</text:span></text:p> Ugly case: <text:p text:style-name="P3">{{ <text:span text:style-name="T10">adres</text:span>se_<text:span text:style-name="T1">as</text:span>so }}</text:p> Still ugly: <text:p text:style-name="P4">{{ siret_asso <text:span text:style-name="T10">}</text:span>}</text:p> Table: <table:table-row> <table:table-cell table:style-name="Tableau1.A3" office:value-type="string"> <text:p text:style-name="P10">{{ <text:span text:style-name="T5">lignes.</text:span>libelle }}</text:p> </table:table-cell> */ /** * @see https://github.com/iCircle/docxTemplateInPHP/blob/master/src/Template/Docx/DocxTemplate.php * @see https://github.com/berduj/odtphp/blob/master/src/Segment.php * @see https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/TemplateProcessor.php * @see https://github.com/lkhl2003/phpdocx/blob/master/Classes/Phpdocx/Create/CreateDocxFromTemplate.inc * @see https://mustache.github.io/mustache.5.html * @see https://docxtemplater.com/demo/#loop-table * @see https://github.com/open-xml-templating/docxtemplater/blob/master/es6/xml-matcher.js * @see https://carbone.io/documentation.html#repetitions * @see https://github.com/Ideolys/carbone/blob/master/lib/extracter.js */ class ODTTemplate { const XML_FILE_NAME = 'content.xml'; protected $source; protected $xml; protected $dom; protected $zip; protected $variables = []; public function __construct($source) { assert(is_string($source)); if (!is_readable($source)) { throw new \RuntimeException('Cannot open file for reading: ' . $source); } $this->source = $source; } public function open() { $this->readZip($this->source); } protected function readZip($source) { $this->zip = new ZipArchive; $this->zip->open($source); $this->xml = $this->zip->getFromName(self::XML_FILE_NAME); $this->dom = new DOMDocument; $this->dom->preserveWhiteSpace = false; $this->dom->loadXML($this->xml); } protected function replaceInZip() { $this->zip->deleteName(self::XML_FILE_NAME); $this->zip->addFromString(self::XML_FILE_NAME, $this->replace()); } public function replace() { $p_list = $this->dom->getElementsByTagNameNS('urn:oasis:names:tc:opendocument:xmlns:text:1.0', 'p'); if (!$p_list->count()) { return; } foreach ($p_list as $p) { if (preg_match('/\{\{\s*([\w.]+)(?:\s*\+\s*(1))?\s*\}\}/', $p->nodeValue, $match)) { $this->replaceTags($p); } } $this->dom->formatOutput = true; var_dump($this->dom->saveXML()); exit; } protected function replaceTags(DOMNode $node) { $nodeList = []; $root = $this->findStartPattern($node); var_dump($root); } protected function findNextPattern(DOMNode $node, $pattern, $pos = 0) { if ($node->nodeType == XML_TEXT_NODE) { if ($pos >= strlen($node->nodeValue)) { return findNextPattern($node->nextSibling); } return strpos('{', $node->nodeValue)) { return $node->parentNode; } } elseif ($node->hasChildNodes()) { foreach ($node->childNodes as $child) { if ($this->findStartPattern($child)) { return $node; } } } return null; } protected function getNodeDepth(DOMNode $node) { $depth = 0; $a = $node; while ($a = $a->parentNode) { $depth++; } return $depth; } protected function matchPatternInNode(DOMNode $node, $pattern) { if (substr($pattern, -1) === '+') { $pattern = substr($pattern, 0, -1); return false; while ($this->matchPatternInNode($node, $pattern)) { } } $value = trim($node->textContent); $position = isset($node->matchPatternPosition) ? $node->matchPatternPosition : 0; if ($position >= strlen($value)) { return false; } if (!$this->matchPattern($value, $pattern, $position)) { return false; } $node->matchPatternPosition = $position + 1; } protected function matchPattern($value, $pattern, $position) { $char = substr($value, $position, 1); switch ($pattern) { case '{': case '}': return $char === $pattern; case ' ': return trim($char) === ''; case 'az': return ctype_alpha($char); case 'az.': return ctype_alpha(str_replace('.', '', $char)); default: return false; } } protected function matchNextPatternInNode(DOMNode $node, $pattern) { if ($node->nodeType == XML_TEXT_NODE) { return $this->matchPatternInNode($node, $pattern); } elseif ($node->hasChildNodes()) { return $this->matchNextPatternInNode($node->firstChild, $pattern); } return false; } protected function findTag(DOMNode $node) { $patterns = ['{', '{', ' ', 'az', 'az.+', 'az+', ' ', '}', '}']; $nodes = []; foreach ($patterns as $pattern) { if ($found = $this->matchNextPatternInNode($node, $pattern)) { if (!in_array($found, $nodes, true)) { $nodes[] = $found; } } else { return false; } } return $nodes; } protected function findAndReplaceTags(DOMNode &$node, $depth, $min_depth) { $text = $node->nodeValue; var_dump($text, $this->findTag($node)); preg_match_all('/\{\{\s*([\w.]+)(?:\s*\+\s*(1))?\s*\}\}/', $text, $tags, PREG_SET_ORDER); if (!count($tags)) { if ($depth > $min_depth) { $this->findAndReplaceTags($node->parentNode, --$depth, $min_depth); } return; } foreach ($tags as $tag) { $text = str_replace($tag[0], 'LOOOL', $text);//$this->variables[$tag[1]], $text); } // This will flatten the node (remove any children) $node->nodeValue = $text; } public function getXML() { return $this->xml; } public function assign($key, $value = null) { if (is_array($key) && null === $value) { foreach ($key as $subkey => $value) { $this->assign($subkey, $value); } } else { $this->variables[$key] = $value; } } public function save($destination) { if (!is_writeable($destination)) { throw new \RuntimeException('Cannot open file for writing: ' . $destination); } copy($this->source, $destination); $this->readZip($destination); $this->replaceInZip(); $this->zip->close(); } public function output() { $tempfile = tempnam(sys_get_temp_dir(), 'odttmp'); copy($this->source, $tempfile); $this->readZip($tempfile); $this->replaceInZip(); $this->zip->close(); readfile($tempfile); unlink($tempfile); } } |
Added src/tests/data/odttemplate/facture.odt version [96c269be27].
cannot compute difference between binary files
Added src/tests/odttemplate.php version [384c0daa95].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php use KD2\Test; use KD2\ODTTemplate; require __DIR__ . '/_assert.php'; test_odt(); function test_odt() { $odt = new ODTTemplate(DATA_DIR . '/odttemplate/facture.odt'); $odt->open(); $odt->assign([ 'nom_asso' => 'La rustine', 'adresse_asso' => "5 rue du Havre\n21000 DIJON", 'total' => '38,00 €', 'lignes' => [ [ 'libelle' => 'Pneu 26"', 'quantite' => 3, 'prix' => '5,00 €', 'total' => '15,00 €', ], [ 'libelle' => 'Potence occasion', 'quantite' => 1, 'prix' => '3,00 €', 'total' => '3,00 €', ], [ 'libelle' => 'Pneu 28" neuf', 'quantite' => 2, 'prix' => '10,00 €', 'total' => '20,00 €', ], ] ]); $odt->replace(); $xml = $odt->getXML(); } |