<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/AdminCheckbox.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/AdminArticleHtmlEdit.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/ArticleTemplate.inc.php" );

	require_once( $GLOBALS["site_root"] . "/includes/models/ParseArticleToEditor.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/ParseArticleToDb.inc.php" );

	class AdminLanguageArticleEdit extends Widget {
		protected $sLeadDelimiter = "<hr class=\"hrlead\" />";

		protected $larticle_title;
		protected $larticle_body;
		protected $larticle_url;
		protected $is_url_used;

		protected $sIds;
	

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );

			$this->sIds["larticle_title"] = $this->generateSubId( "larticle_title" );
			$this->larticle_title = new AdminTextbox( $this->sIds["larticle_title"] , $this );
			
			$this->sIds["is_url_used"] = $this->generateSubId( "is_url_used" );
			$this->is_url_used = new AdminCheckbox( $this->sIds["is_url_used"] , $this );
			
			$this->sIds["larticle_url"] = $this->generateSubId( "larticle_url" );
			$this->larticle_url = new AdminTextbox( $this->sIds["larticle_url"] , $this );

			$this->sIds["larticle_body"] = $this->generateSubId( "larticle_body" );
			$this->larticle_body = new AdminArticleHtmlEdit( $this->generateSubId( "larticle_body" ) , $this );
		}

		protected function initializeWidgets() {
			$this->larticle_title->setWidth( 730 );
			$this->larticle_title->setShowTitle( true );
			$this->larticle_title->setTitle( __("articles_Title") );
			
			$this->larticle_url->setWidth( 730 );
			$this->larticle_url->setShowTitle( true );
			$this->larticle_url->setTitle( __("articles_Url" ) );

			$this->is_url_used->setShowTitle( true );
			$this->is_url_used->setTitle( __("articles_Is_url_used" ) );

			$this->larticle_body->setWidth( 635 + 30 );
			$this->larticle_body->setHeight( 540 );
			$this->larticle_body->setShowTitle( true );
			$this->larticle_body->setTitle( __("articles_Body") );

		}

		protected function implodeLead( $arr ) {
			if (!isset( $arr["larticle_lead"] ) || !strlen( $arr["larticle_lead"] )) {
				if (isset( $arr["larticle_body"] ))
					$this->larticle_body->setValue( $this->preprocessBodyOnLoad( $arr["larticle_body"] ) );
				return;
			}
			// there is lead
			$body = $arr["larticle_lead"];
			$body.=$this->sLeadDelimiter;
			if (isset( $arr["larticle_body"] ))
				$body.=$arr["larticle_body"];
			$this->larticle_body->setValue( $this->preprocessBodyOnLoad( $body ) );
		}

		protected function preprocessBodyOnLoad( $body ) {
			$parser = new ParseArticleToEditor();
			$retval = $parser->processHtmlTags( $body );

			$tmpl = new ArticleTemplate( "_article_template" , null );
			$tmpl->setAdmin( true );
			$retval = $tmpl->renderFromString( $retval );
			return $retval;
		}

		protected function preprocessBodyOnSave( $body ) {
			$parser = new ParseArticleToDb();
			return $parser->processHtmlTags( $body );
		}

		public function setValue( $arr = array() ) {
			if (!is_array( $arr )) return;
			if (isset( $arr["larticle_title"] )) $this->larticle_title->setValue( $arr["larticle_title"] );
			if (isset( $arr["larticle_url"] )) $this->larticle_url->setValue( $arr["larticle_url"] );
			if (isset( $arr["is_url_used"] )) $this->is_url_used->setChecked( $arr["is_url_used"]=="t" );
			$this->implodeLead( $arr );
		}

		protected function explodeLead() {
			$body = $this->larticle_body->getValue();
			$body = $this->preprocessBodyOnSave( $body );
			$pos = strpos( $body , $this->sLeadDelimiter );
			if ($pos===false) return array( "larticle_lead" => "" , "larticle_body" => $body );
			return array(
				"larticle_lead" => substr( $body , 0 , $pos ),
				"larticle_body" => substr( $body , $pos + strlen( $this->sLeadDelimiter ) )
			);
		}

		public function getValue() {
			$retval = array();
			$retval["larticle_title"] = $this->larticle_title->getValue();
			$retval["larticle_url"] = $this->larticle_url->getValue();
			$retval["is_url_used"] = $this->is_url_used->isChecked();
			$merge = $this->explodeLead();
			foreach ($merge as $k=>$v) $retval[$k]=$v;
			return $retval;
		}

		public function render() {
			$this->initializeWidgets();
			$this->setParam( "sIds" , $this->sIds );
			return parent::render();
		}
	}

?>
