Skip to content

Commit

Permalink
CORE ODT: Support des balise [IF] autre par que le corps du document …
Browse files Browse the repository at this point in the history
…ODT, exemple les entêtes PR Dolibarr#29357
  • Loading branch information
kkhelifa-opendsi committed Apr 16, 2024
1 parent b8d3ca1 commit 26985fe
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions htdocs/includes/odtphp/odf.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,15 @@ private function _moveRowSegments()
*/
private function _parse($type = 'content')
{
if ($type == 'content') $xml = &$this->contentXml;
elseif ($type == 'styles') $xml = &$this->stylesXml;
elseif ($type == 'meta') $xml = &$this->metaXml;
else return;

// Search all tags found into condition to complete $this->vars, so we will proceed all tests even if not defined
$reg='@\[!--\sIF\s([{}a-zA-Z0-9\.\,_]+)\s--\]@smU';
$matches = array();
preg_match_all($reg, $this->contentXml, $matches, PREG_SET_ORDER);
preg_match_all($reg, $xml, $matches, PREG_SET_ORDER);

//var_dump($this->vars);exit;
foreach ($matches as $match) { // For each match, if there is no entry into this->vars, we add it
Expand All @@ -568,13 +573,13 @@ private function _parse($type = 'content')
// If value is true (not 0 nor false nor null nor empty string)
if ($value) {
//dol_syslog("Var ".$key." is defined, we remove the IF, ELSE and ENDIF ");
//$sav=$this->contentXml;
//$sav=$xml;
// Remove the IF tag
$this->contentXml = str_replace('[!-- IF '.$key.' --]', '', $this->contentXml);
$xml = str_replace('[!-- IF '.$key.' --]', '', $xml);
// Remove everything between the ELSE tag (if it exists) and the ENDIF tag
$reg = '@(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy
$this->contentXml = preg_replace($reg, '', $this->contentXml);
/*if ($sav != $this->contentXml)
$xml = preg_replace($reg, '', $xml);
/*if ($sav != $xml)
{
dol_syslog("We found a IF and it was processed");
//var_dump($sav);exit;
Expand All @@ -583,16 +588,16 @@ private function _parse($type = 'content')
// Else the value is false, then two cases: no ELSE and we're done, or there is at least one place where there is an ELSE clause, then we replace it

//dol_syslog("Var ".$key." is not defined, we remove the IF, ELSE and ENDIF ");
//$sav=$this->contentXml;
//$sav=$xml;
// Find all conditional blocks for this variable: from IF to ELSE and to ENDIF
$reg = '@\[!--\sIF\s' . $key . '\s--\](.*)(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy
preg_match_all($reg, $this->contentXml, $matches, PREG_SET_ORDER);
preg_match_all($reg, $xml, $matches, PREG_SET_ORDER);
foreach ($matches as $match) { // For each match, if there is an ELSE clause, we replace the whole block by the value in the ELSE clause
if (!empty($match[3])) $this->contentXml = str_replace($match[0], $match[3], $this->contentXml);
if (!empty($match[3])) $xml = str_replace($match[0], $match[3], $xml);
}
// Cleanup the other conditional blocks (all the others where there were no ELSE clause, we can just remove them altogether)
$this->contentXml = preg_replace($reg, '', $this->contentXml);
/*if ($sav != $this->contentXml)
$xml = preg_replace($reg, '', $xml);
/*if ($sav != $xml)
{
dol_syslog("We found a IF and it was processed");
//var_dump($sav);exit;
Expand All @@ -601,9 +606,7 @@ private function _parse($type = 'content')
}

// Static substitution
if ($type == 'content') $this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml);
if ($type == 'styles') $this->stylesXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->stylesXml);
if ($type == 'meta') $this->metaXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->metaXml);
$xml = str_replace(array_keys($this->vars), array_values($this->vars), $xml);
}

/**
Expand Down Expand Up @@ -1017,4 +1020,4 @@ public function getvalue($valuename)
$this->contentXml = preg_replace($searchreg, "", $this->contentXml);
return $matches[1];
}
}
}

0 comments on commit 26985fe

Please sign in to comment.