Skip to content

Commit

Permalink
New - <!--@elseif: >
Browse files Browse the repository at this point in the history
  • Loading branch information
yama committed May 10, 2016
1 parent f0ff830 commit b9b3f51
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions manager/includes/document.parser.class.inc.php
Expand Up @@ -1645,41 +1645,57 @@ function mergeConditionalTagsContent($content, $left='<!--@IF:', $right='<@ENDIF


if(strpos($content,'<!--@IF ')!==false) $content = str_replace('<!--@IF ',$left,$content); if(strpos($content,'<!--@IF ')!==false) $content = str_replace('<!--@IF ',$left,$content);
if(strpos($content,$left)===false) return $content; if(strpos($content,$left)===false) return $content;
if(strpos($content,'<!--@ELSE-->')!==false) $content = str_replace('<!--@ELSE-->','<@ELSE>',$content); if(strpos($content,'<!--@ELSEIF')!==false) $content = str_replace('<!--@ELSEIF', '<@ELSEIF', $content);
if(strpos($content,'<!--@ELSE-->')!==false) $content = str_replace('<!--@ELSE-->', '<@ELSE>', $content);
if(strpos($content,'<!--@ENDIF-->')!==false) $content = str_replace('<!--@ENDIF-->','<@ENDIF-->',$content); if(strpos($content,'<!--@ENDIF-->')!==false) $content = str_replace('<!--@ENDIF-->','<@ENDIF-->',$content);
$matches = $this->getTagsFromContent($content,$left,$right);
if(!empty($matches)) $s = array('<!--@IF:', '<@ELSE', '<@ENDIF-->');
{ $r = array('<!--@CONDTAG@IF:','<!--@CONDTAG@ELSE', '<!--@CONDTAG@ENDIF-->');
foreach($matches[0] as $i=>$v) $content = str_replace($s, $r, $content);
{ $splits = explode('<!--@CONDTAG@', $content);
$cmd = substr($v,8,strpos($v,'>')-8); foreach($splits as $i=>$split) {
$cmd = trim($cmd); if($i===0) {
if(substr($cmd,-2)==='--') $cmd = substr($cmd,0,-2); $content = $split;
$cond = substr($cmd,0,1)!=='!' ? true : false; $excute = false;
if($cond===false) $cmd = ltrim($cmd,'!'); continue;
if(strpos($cmd,'[!')!==false) }
$cmd = str_replace(array('[!','!]'),array('[[',']]'),$cmd); if(substr($split,0,2)==='IF' || substr($split,0,6)==='ELSEIF') {
if($excute) continue;
list($cmd, $text) = explode('>', $split, 2);
$dlen = substr($split,0,2)==='IF' ? 2 : 6;

$cmd = rtrim(substr($cmd,$dlen+1));
if(substr($cmd,-$dlen)==='--') $cmd = substr($cmd,0,-$dlen);
// echo ' -|||' . $cmd . '|||- ';
$flag = substr($cmd,0,1)!=='!' ? true : false;
if($flag===false) $cmd = ltrim($cmd,'!');

if(strpos($cmd,'[!')!==false) $cmd = str_replace(array('[!','!]'),array('[[',']]'),$cmd);

$cmd = $this->parseDocumentSource($cmd); $cmd = $this->parseDocumentSource($cmd);
$cmd = trim($cmd); $cmd = ltrim($cmd);
if(strpos($matches[1][$i],'<@ELSE>')!==false) {
list($if_content,$else_content) = explode('<@ELSE>',$matches[1][$i]); if(!preg_match('@^[0-9]*$@', $cmd) && preg_match('@^[0-9<= \-\+\*/\(\)%!]*$@', $cmd))
} else { $cmd = (int) eval("return {$cmd};");
$if_content = $matches[1][$i]; if($cmd < 0) $cmd = 0;
$else_content = '';
if( ($flag===true && !empty($cmd)) || ($flag===false && empty($cmd)) ) {
$content .= $text;
$excute = true;
} }
if(preg_match('@^[0-9<= \-\+\*/\(\)%!]*$@', $cmd)) $cmd = (int) eval("return {$cmd};"); else $excute = false;
if( ($cond===true && empty($cmd)) || ($cond===false && !empty($cmd)) )
$matches[1][$i] = trim($else_content);
else
$matches[1][$i] = substr($if_content,strpos($if_content,'>')+1);
} }
foreach($matches[0] as $i=>$v) elseif(substr($split,0,4)==='ELSE') {
{ if($excute) continue;
$addBreakMatches[$i] = $v."\n"; list(, $text) = explode('>', $split, 2);
$content .= $text;
$excute = true;
}
else {
list(, $text) = explode('>', $split, 2);
$content .= $text;
$excute = false;
} }
$content = str_replace($addBreakMatches,$matches[1],$content);
if(strpos($content,$left)!==false)
$content = str_replace($matches[0],$matches[1],$content);
} }
if ($this->debug) $this->addLogEntry('$modx->'.__FUNCTION__,$fstart); if ($this->debug) $this->addLogEntry('$modx->'.__FUNCTION__,$fstart);
return $content; return $content;
Expand Down

0 comments on commit b9b3f51

Please sign in to comment.