Location: PHPKode > projects > Simple Way to Usenet > libs/core/parser/blocks/block_array.class.php
<?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_array extends tpl_parser {
	
	private static $tagmatch 	= "#(\{--)(ARRAY)(=)([\w\W]+?)(--})#";
	private static $logic		= '4';
	
	private $arg;
	private $lblk;
	
	public function __construct($hdl, $tag, $epos, $depth, $of){
		parent::__construct($hdl, $tag->end(), $epos, $depth, $of);
		$this->otag=$tag;
	}

	public function end(){ return $this->ctag->end(); }

	public function load_tagargs($tag=NULL){
		if ( $tag === NULL ){ $tag =$this->otag->read(); } 
		preg_match(self::$tagmatch, $tag, $result);
		if ( !isset($result[self::$logic]) ){ return FALSE; }
		$this->arg = trim($result[self::$logic]);
		return TRUE;
	}
	
	public function recurloads(){
		$r = $this->detect_block('ARRAY', TRUE);
		if ( !isset($this->ctag) ){
			errors::raise("Close tag could not be found for block ARRAY @ ".$this->otag->start(), CORE_LOG_ERROR, 'TPL');
			return FALSE;
		}
		$bool=TRUE;
		foreach($this->blocks as $k => $blk ){
			if ( $blk->plugin() === 'array_else' ){ 
				$bool = FALSE;
			} else {
				$this->lblk[$bool][] = $blk;
			}		 
		}
		unset($this->blocks);
		return $r;
	}
	
	public function ref(){
		if ( ereg("%|$", $this->arg) ){
			$o = substr($this->arg, 1); 
		} else {
			$o = $this->arg;
		}
		return $o;
	}
	
	public function plugparse($l, $g, $return){
		$src = $this->get_data($l, $g);
		if ( isset($this->lblk[is_array($src)]) ){
			$this->blocks = $this->lblk[is_array($src)];
		} elseif ( $return ){ 
			return ''; 
		} else {
			return TRUE;
		}
		if ( !is_array($src) ){ return $this->parse($l, $g, $return); }
		$o='';
		$c=1;
		foreach($src as $k => $entry){
			$nl = tpl_data::newcontext();
			//echo "New Context data $nl => ";print_r($entry);echo "<br/>\n";
			if ( is_array($entry) ){ tpl_data::setdata($nl, $entry); }
			tpl_data::appendata($nl, $entry, 			'ARRAY_ENTRY_VAL');
			tpl_data::appendata($nl, $k, 				'ARRAY_ENTRY_KEY');
			tpl_data::appendata($nl, $c, 				'ARRAY_ENTRY_COUNT');
			tpl_data::appendata($nl, ($c==1), 			'ARRAY_ENTRY_ISFIRST');
			tpl_data::appendata($nl, ($c==count($src)), 'ARRAY_ENTRY_ISLAST');
			$o .= $this->parse($nl, $l, $return);
			tpl_data::clearcontext($nl);
			//echo "Cleanup Context $nl<br/>\n";
			unset($k, $entry);
			if ( !$return ){ $o=''; }
			$c++;
		}
		if ( $return ){ return $o; }
		return TRUE;
	}
	
	
	
	public function plugparse_before($l, $g, $return, $bref){
		//echo "$this->depth] ARRAY plugparse_before<br/>\n";
		$src = $this->get_data($l, $g);
		if ( isset($this->lblk[is_array($src)]) ){
			$this->blocks = $this->lblk[is_array($src)];
		} elseif ( $return ){ 
			return ''; 
		} else {
			return TRUE;
		}
		if ( !is_array($src) ){
			return $this->parse_before($l, $g, $return, $bref); 
		}
		$o='';
		$c=1;
		foreach($src as $k => $entry){
			$nl = tpl_data::newcontext();
			if ( is_array($entry) ){ tpl_data::setdata($nl, $entry); }
			tpl_data::appendata($nl, $entry, 			'ARRAY_ENTRY_VAL');
			tpl_data::appendata($nl, $k, 				'ARRAY_ENTRY_KEY');
			tpl_data::appendata($nl, $c, 				'ARRAY_ENTRY_COUNT');
			tpl_data::appendata($nl, ($c==1), 			'ARRAY_ENTRY_ISFIRST');
			tpl_data::appendata($nl, ($c==count($src)), 'ARRAY_ENTRY_ISLAST');
			$o .= $this->parse_before($nl, $l, $return, $bref);
			tpl_data::clearcontext($nl);
			unset($k, $entry);
			if ( !$return ){ $o=''; }
			$c++;
		}
		if ( $return ){ return $o; }
		return TRUE;
	}
	
	public function plugparse_once($l, $g, $return, $bref, $data, $key){
		if ( $this->ref() != $bref ){ 
			//echo "$this->depth] ARRAY plugparse_once, not for us, lets proceed<br/>\n";
			$src = $this->get_data($l, $g);
			if ( isset($this->lblk[is_array($src)]) ){
				$this->blocks = $this->lblk[is_array($src)];
			} elseif ( $return ){ 
				return ''; 
			} else {
				return TRUE;
			}
			if ( !is_array($src) ){ return $this->parse_once($l, $g, $return, $bref, $data, $key);	}
			$o='';
			$c=1;
			foreach($src as $k => $entry){
				$nl = tpl_data::newcontext();
				if ( is_array($entry) ){ tpl_data::setdata($nl, $entry); }
				tpl_data::appendata($nl, $entry, 			'ARRAY_ENTRY_VAL');
				tpl_data::appendata($nl, $k, 				'ARRAY_ENTRY_KEY');
				tpl_data::appendata($nl, $c, 				'ARRAY_ENTRY_COUNT');
				tpl_data::appendata($nl, ($c==1), 			'ARRAY_ENTRY_ISFIRST');
				tpl_data::appendata($nl, ($c==count($src)), 'ARRAY_ENTRY_ISLAST');
				$o .= $this->parse_once($nl, $l, $return, $bref, $data, $key);
				tpl_data::clearcontext($nl);
				unset($k, $entry);
				if ( !$return ){ $o=''; }
				$c++;
			}
			if ( $return ){ return $o; }
			return TRUE;
		}
		//echo "$this->depth] ARRAY plugparse_once FOR block $bref<br/>\n";
		//errors::raise("$this->depth] ARRAY plugparse_once FOR block $bref", CORE_LOG_WARNING, 'TPL');
		$this->blocks = $this->lblk[TRUE];
		$nl = tpl_data::newcontext();
		if ( is_array($data) ){ tpl_data::setdata($nl, $data); }
		tpl_data::appendata($nl, $data, 			'ARRAY_ENTRY_VAL');
		tpl_data::appendata($nl, $key, 				'ARRAY_ENTRY_KEY');
		$o = $this->parse($nl, $l, $return);
		tpl_data::clearcontext($nl);
		if ( $return ){ return $o; }
		return TRUE;
	}
	
	
	
	
	
	
	public function plugparse_after($l, $g, $return, $bref){
		//echo "ARRay plugparse_after<br/>\n";
		if ( $bref == substr($this->arg, 1) ){
			if ( $return ){ ''; }
			return TRUE;
		}
		$src = $this->get_data($l, $g);
		if ( isset($this->lblk[is_array($src)]) ){
			$this->blocks = $this->lblk[is_array($src)];
		} elseif ( $return ){ 
			return ''; 
		} else {
			return TRUE;
		}
		if ( !is_array($src) ){ return $this->parse_after($l, $g, $return, $bref); }
		$o='';
		$c=1;
		foreach($src as $k => $entry){
			$nl = tpl_data::newcontext();
			//echo "New Context data $nl => ";print_r($entry);echo "<br/>\n";
			if ( is_array($entry) ){ tpl_data::setdata($nl, $entry); }
			tpl_data::appendata($nl, $entry, 			'ARRAY_ENTRY_VAL');
			tpl_data::appendata($nl, $k, 				'ARRAY_ENTRY_KEY');
			tpl_data::appendata($nl, $c, 				'ARRAY_ENTRY_COUNT');
			tpl_data::appendata($nl, ($c==1), 			'ARRAY_ENTRY_ISFIRST');
			tpl_data::appendata($nl, ($c==count($src)), 'ARRAY_ENTRY_ISLAST');
			$o .= $this->parse_after($nl, $l, $return, $bref);
			tpl_data::clearcontext($nl);
			//echo "Cleanup Context $nl<br/>\n";
			unset($k, $entry);
			if ( !$return ){ $o=''; }
			$c++;
		}
		if ( $return ){ return $o; }
		return TRUE;
	}
	
	
	
	
	public function plugparse_between($l, $g, $return, $sbref, $ebref){
		//echo "ARRay plugparse_after<br/>\n";
		if ( ! tpl_parser::isdetected($sbref) ){
			if ( $return ){ ''; }
			return TRUE;
		}
		if ( tpl_parser::isdetected($ebref) ){
			if ( $return ){ ''; }
			return TRUE;
		}
		$src = $this->get_data($l, $g);
		if ( isset($this->lblk[is_array($src)]) ){
			$this->blocks = $this->lblk[is_array($src)];
		} elseif ( $return ){ 
			return ''; 
		} else {
			return TRUE;
		}
		if ( !is_array($src) ){ return $this->parse_between($l, $g, $return, $sbref, $ebref); }
		$o='';
		$c=1;
		foreach($src as $k => $entry){
			$nl = tpl_data::newcontext();
			//echo "New Context data $nl => ";print_r($entry);echo "<br/>\n";
			if ( is_array($entry) ){ tpl_data::setdata($nl, $entry); }
			tpl_data::appendata($nl, $entry, 			'ARRAY_ENTRY_VAL');
			tpl_data::appendata($nl, $k, 				'ARRAY_ENTRY_KEY');
			tpl_data::appendata($nl, $c, 				'ARRAY_ENTRY_COUNT');
			tpl_data::appendata($nl, ($c==1), 			'ARRAY_ENTRY_ISFIRST');
			tpl_data::appendata($nl, ($c==count($src)), 'ARRAY_ENTRY_ISLAST');
			$o .= $this->parse_between($nl, $l, $return, $sbref, $ebref);
			tpl_data::clearcontext($nl);
			//echo "Cleanup Context $nl<br/>\n";
			unset($k, $entry);
			if ( !$return ){ $o=''; }
			$c++;
		}
		if ( $return ){ return $o; }
		return TRUE;
	}
	
	
	private function get_data($l, $g){
		if ( $l !== NULL AND ereg("$", $this->arg) ){
			//echo 'Searching in Local Data For '.substr($this->arg, 1)."<br/>\n";
			tpl_data::fetch_reset($l);
			while( FALSE !== ($data = tpl_data::fetch_arrays($l) ) ){
				if ( CORE::iserror($data) ){ 
					errors::raise("Invalid Local Data context in array @ $this->spos:$this->epos, no '\$' variable will be available as array source !", CORE_LOG_ERROR, 'TPL');
					break;
				} elseif ( '$'.$data[0] == $this->arg ){ $src = $data[1];unset($data); }
			}
		}
		if ( $g !== NULL AND ereg("%", $this->arg) AND !isset($src) ){
			//echo 'Searching in Global Data '.substr($this->arg, 1)."<br/>\n";
			tpl_data::fetch_reset($g);
			while( FALSE !== ($data = tpl_data::fetch_arrays($g) ) ){
				if ( CORE::iserror($data) ){ 
					errors::raise("Invalid Global Data context in array @ $this->spos:$this->epos, no '%' variable will be available as array source !", CORE_LOG_ERROR, 'TPL');
					break;
				} elseif ( '%'.$data[0] == $this->arg ){ $src = $data[1];unset($data); }
			}
		}
		if (!isset($src) ){ return FALSE; }
		return $src;
	}
		
}
return 1;
Return current item: Simple Way to Usenet