Location: PHPKode > projects > Simple Way to Usenet > libs/core/parser/blocks/block_lang.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_lang extends tpl_parser {
	
	private static $tagmatch 		= "#(\{--)(LANG)(=)(W|S)(=)([\w\W]+?)(;)([\w\W]+?)(--})#";
	private static $tagtype		= '4';
	private static $tagdico		= '6';
	private static $tagref		= '8';
		
	private $ref;
	private $arg;
	private $dico;
	
	public function __construct($hdl, $tag, $epos, $depth, $of){
		//echo "$depth] New block Lang @ ".$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 Lang @ ".$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->ref  = trim($result[self::$tagref]);
		$this->dico = trim($result[self::$tagdico]);
		$this->arg=NULL;
		if ( $result[self::$tagtype] == 'S' ){
			//{--LANG=S=realm;logtrystat; try=$try, maxtry=$maxtry --}
			$tmp = split(';', $this->ref);
			$this->ref = trim($tmp[0]);
			$tmp = split(',', trim($tmp[1]) );
			foreach($tmp as $k => $arg ){
				list($arg,$val) = split('=', trim($arg));
				$this->arg[trim($arg)] = trim($val);
			}
		}
		return TRUE;
	}
	
	public function recurloads(){ return TRUE; }
		
	public function plugparse($l, $g, $return){
		if ( $this->dico == '*' ){ 
			errors::raise('Using Dico * in Lang block is not really what you should do you if expect performance @ '.$this->otag->start(), CORE_LOG_NOTICE, 'TPL');
			$this->dico = NULL; 
		}
		if ( $this->arg !== NULL ){
			foreach($this->arg as $k => &$var){ $buf[$k] = $this->get_sourceval($var, $l, $g); }
		} else {
			$buf=$this->arg;
		}
		$totranslate = $this->get_sourceval($this->ref, $l, $g);
		$o = langs::dico_translate($totranslate, $buf, $this->dico);
		if ( $return ){ return $o; };
		call_user_func($this->of, $o);
		return TRUE; 
	}
		
	public function plugparse_before($l, $g, $return, $bref){ return $this->plugparse($l, $g, $return); }
	
	private function get_sourceval($k, $l, $g){
		if ( $l !== NULL AND ereg('\$', $k) ){
			//echo 'Searching in Local Data For '.$k."<br/>\n";
			tpl_data::fetch_reset($l);
			while( FALSE !== ($data = tpl_data::fetch_strings($l) ) ){
				if ( CORE::iserror($data) ){ 
					errors::raise("Invalid Local Data context in lang @ ".$this->otag->start().", no '\$' variable will be available !", CORE_LOG_ERROR, 'TPL');
					break;
				} elseif ( ereg('\$', $k) ){
					$t =$k;
					$k = str_replace("$".$data[0], $data[1], $k);
					//errors::raise("$t => Replacing '$".$data[0]."' by '".$data[1]."' ==> $k", CORE_LOG_NOTICE, 'TPL');
				}
			}
		}
		if ( $g !== NULL AND ereg("%", $k) ){
			//echo 'Searching in Global Data For '.$k."<br/>\n";
			tpl_data::fetch_reset($g);
			while( FALSE !== ($data = tpl_data::fetch_strings($g) ) ){
				if ( CORE::iserror($data) ){ 
					errors::raise("Invalid Global Data context in lang @ ".$this->otag->start().", no '%' variable will be available !", CORE_LOG_ERROR, 'TPL');
					break;
				} elseif ( ereg("%", $k) ){ 
					$t=$k;
					$k = str_replace("%".$data[0], $data[1], $k);
					//errors::raise("$t => Replacing '%".$data[0]."' by '".$data[1]."' ==> $k", CORE_LOG_NOTICE, 'TPL');
				}
			}
		}
		return $k;
	}
}
return 1;
Return current item: Simple Way to Usenet