<?php
	FNTH_class( "Template" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/RMPVideo.inc.php" );

	class ArticleTemplate extends FNTHTemplate {
		protected $bAdmin = false;

		public function setAdmin( $v ) { $this->bAdmin = $v; }

		protected $ldel="@@@@@";
		protected $rdel="#####";
		protected $processtags = array( "video" , "article","\$fnth_tab","\$fnth_nl" );
		
		public function prefilterLeaveProtected( $tmpl ) {
			$retval = str_replace( $this->smr->left_delimiter , $this->ldel , $tmpl );
			$retval = str_replace( $this->smr->right_delimiter , $this->rdel , $retval );

			foreach ($this->processtags as $tag) {
				// exact tag
				$retval = str_replace(
					$this->ldel.$tag.$this->rdel,
					$this->smr->left_delimiter.$tag.$this->smr->right_delimiter , $retval );
				// exact endmatch
				$retval = str_replace(
					$this->ldel."/".$tag.$this->rdel,
					$this->smr->left_delimiter."/".$tag.$this->smr->right_delimiter , $retval );
			}

			foreach ($this->processtags as $tag) {
				$pos = strpos( $retval , $this->ldel . $tag . " " );
				while ($pos!==false) {
					$pos2 = strpos( $retval , $this->rdel , $pos );
					if ($pos2) {
						$str = substr( $retval , $pos , $pos2-$pos ) . $this->rdel;
						$newstr = $str;
						$newstr = str_replace( $this->ldel , $this->smr->left_delimiter , $newstr );
						$newstr = str_replace( $this->rdel , $this->smr->right_delimiter , $newstr );
						$retval = str_replace( $str , $newstr , $retval );
					} else {
						$retval = "";
					}
					$pos = strpos( $retval , $this->ldel . $tag . " " );
				}
			}
			return $retval;
		}

		public function postfilterLeaveProtected( $tmpl ) {
			$retval = str_replace( $this->ldel , $this->smr->left_delimiter , $tmpl );
			$retval = str_replace( $this->rdel , $this->smr->right_delimiter , $retval );
			return $retval;
		}


		public function replaceVideo( $params , $text , &$smarty ) {
			if ($this->bAdmin) {
				$ts = $smarty->_tag_stack[0];
				$attrs = "";
				foreach ($ts[1] as $k => $v) $attrs.=sprintf( " %s=%s",$k,$v);
				return sprintf( "[{%s%s}]%s[{/%s}]",$ts[0],$attrs,$text,$ts[0] );
			}
			$video = new RMPVideo( "test" , null );
			$video->setWidth( $params["width"] );
			$video->setHeight( $params["height"] );
			$video->setFLV( $text );
			return $video->render();
			
		}
		
		public function replaceArticle( $params , $text , &$smarty ) {
			$query = sprintf( "select larticle_url from larticles where article_id=%d and site_language_id=%d",
				$params["id"],
				$GLOBALS["langcode"] );
			return sprintf( "<a href=\"%s\">%s</a>",
				FNTH_db_get_field( $query , "larticle_url" ),
				$text );
		}

		protected function prepareSmartyInstance() {
			$retval = parent::prepareSmartyInstance();
			$retval->register_block( "video" , array( $this , "replaceVideo" ) );
			$retval->register_block( "article" , array( $this , "replaceArticle" ) );
			if ($this->bAdmin) {
				$retval->register_prefilter( array( $this , "prefilterLeaveProtected" ) );
				$retval->register_postfilter( array( $this , "postfilterLeaveProtected" ) );
			}
			return $retval;
		}

		public function renderFromString( $template , $timestamp = null ) {
			return parent::renderFromString( $template , $timestamp );
		}
	}

?>
