Skip to content

Commit

Permalink
NEW: Add support of [IF] tag other than in the core content (ex: head…
Browse files Browse the repository at this point in the history
…ers who is in stylesXml)
  • Loading branch information
kkhelifa-opendsi committed Apr 16, 2024
1 parent c778212 commit 37e17a7
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions htdocs/includes/odtphp/odf.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,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 @@ -575,13 +580,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 @@ -590,16 +595,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 @@ -608,9 +613,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

0 comments on commit 37e17a7

Please sign in to comment.