<?php
/**
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2007 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class block_inc extends tpl_parser {
private static $tagmatch = "#(\{--INC)(=)([\w\W]+?)(;)([\w\W]+?)(--})#"; //preg_match for lang block
private static $tagdata = '5';
private static $tagtpl = '3';
private $data;
private $path;
public function __construct($hdl, $tag, $epos, $depth, $of){
//echo "$depth] New block Inc @ ".$tag->start()."<br/>\n";
parent::__construct($hdl, $tag->end(), $epos, $depth, $of);
$this->otag=$tag;
}
public function end(){ return $this->otag->end(); }
public function load_tagargs($tag=NULL){
//echo "$this->depth] Block Inc @ ".$this->otag->start()." : Loading tagargs<br/>\n";
if ( $tag === NULL ){ $tag = $this->otag->read(); }
preg_match(self::$tagmatch, $tag, $result);
if ( FALSE === $result ){
errors::raise('Malformed Lang tag @ '.$this->otag->start(), CORE_LOG_ERROR, 'TPL');
return FALSE;
}
$this->path = trim($result[self::$tagtpl]);
$this->data = trim($result[self::$tagdata]);
return TRUE;
}
public function recurloads(){ return TRUE; }
public function plugparse($l, $g, $return){
if ( $l !== NULL AND (ereg("$", $this->data) OR ereg("$", $this->path)) ){
//echo 'Searching in Local Data For '.substr($this->data, 1)."<br/>\n";
tpl_data::fetch_reset($l);
while( FALSE !== ($data = tpl_data::fetch($l) ) ){
if ( CORE::iserror($data) ){
errors::raise("Invalid Local Data context in inc @ $this->spos:$this->epos, no '\$' variable will be available as data source !", CORE_LOG_ERROR, 'TPL');
break;
} elseif ( '$'.$data[0] == $this->data ){ $src = $data[1]; }
if ( !is_array($data[1]) ){
$this->path = str_replace('$'.$data[0], $data[1], $this->path); //we allow local vars in tpl name ;)
}
}
}
if ( $g !== NULL AND (ereg("%", $this->data) OR ereg("%", $this->path)) ){
//echo 'Searching in Global Data '.substr($this->data, 1)."<br/>\n";
tpl_data::fetch_reset($g);
while( FALSE !== ($data = tpl_data::fetch($g) ) ){
if ( CORE::iserror($data) ){
errors::raise("Invalid Global Data context in inc @ $this->spos:$this->epos, no '%' variable will be available as data source !", CORE_LOG_ERROR, 'TPL');
break;
} elseif ( '%'.$data[0] == $this->data ){ $src = $data[1]; }
if ( !is_array($data[1]) ){
$this->path = str_replace('$'.$data[0], $data[1], $this->path); //we allow global vars in tpl name ;)
}
}
}
if ( !isset($src) ){ errors::raise('Include block could not find his source data in this context @ '.$this->otag->start(), CORE_LOG_WARNING, 'TPL'); }
$TPL = new template();
$TPL->output_function($this->of);
if ( FALSE === $TPL->load_file($this->path) ){
errors::raise("Include block could not load template $this->path @ ".$this->otag->start(), CORE_LOG_ERROR, 'TPL');
return FALSE;
}
if ( isset($src) ){ $TPL->setdata($src); }
$o = $TPL->parse($return);
$TPL->releasedata();
unset($TPL);
return $o;
}
public function plugparse_before($l, $g, $return, $bref){ return $this->plugparse($l, $g, $return); }
}
return 1;