Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: Add support of [IF] tag other than in the core content of the ODT #29357

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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