<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPopup.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminArticleCategory.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/MLMDPLanguageArticle.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminDatetime.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminDate.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminButton.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminArticleLeadpicture.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminArticleSelector.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Articles.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Articlecategories.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Languages.inc.php" );
		

	class AdminArticleConnectedEditPage extends BackendPopup {
		protected $am;

		protected $source_article_id;
		protected $target_article_id;
		protected $connection_bidirectional;
		protected $ac_submit;

		public function __construct( $id = "AdminArticleConnectedEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->am = new ArticlesModel();
			if (!AdminUserModel::isAdminLoggedIn()) return;

			$this->source_article_id = new AdminArticleSelector( "source_article_id" , $this );
			$this->target_article_id = new AdminArticleSelector( "target_article_id" , $this );

			$this->connection_bidirectional = new AdminCheckbox( "connection_bidirectional" , $this );
			$this->ac_submit = new AdminSubmit( "ac_submit" , $this );
		}
		
		protected function initializeWidgets() {
			if (!AdminUserModel::isAdminLoggedIn()) return;

			$this->source_article_id->setShowTitle( true );
			$this->source_article_id->setTitle( __("articles_Source_article") );
			$this->target_article_id->setShowTitle( true );
			$this->target_article_id->setTitle( __("articles_Target_article") );

			$this->connection_bidirectional->setShowTitle( true );
			$this->connection_bidirectional->setTitle( __("articles_Bidirectional_connection" ) );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->connection_bidirectional->setChecked( true );
				$this->setParam("title" , __("articles_New_connected_article" ));
				$this->ac_submit->setValue( __( "New","attr" ) );
				break;
			case "modify":
				$this->ac_submit->setValue( __( "Modify","attr" ) );
				break;
			case "delete":
				$this->setParam("title" , __("articles_Delete_article" ));
				$this->ac_submit->setValue( __( "Delete","attr" ) );
				break;
			}
			// load datas
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
				break;
			}
		}
		
		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			$this->initializeWidgets();
		}

		protected function validateForm() {
			$valid = true;

			$source_article_id = (int)$this->source_article_id->getValue();
			if (!$source_article_id) $valid = false;
			$target_article_id = (int)$this->target_article_id->getValue();
			if (!$source_article_id) $valid = false;
			return $valid;
		}


		protected function loadDatas( $uid ) {
			$datas = $this->am->getConnectedArticleFromId( $uid );

			$this->source_article_id->setArticleId( $datas["source_article_id"] );
			$this->target_article_id->setArticleId( $datas["target_article_id"] );

			$this->connection_bidirectional->setChecked( $datas["connection_bidirectional"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$datas = array();
			$datas["source_article_id"] = $this->source_article_id->getValue();
			$datas["target_article_id"] = $this->target_article_id->getValue();
			$datas["bidirectional"] = $this->connection_bidirectional->isChecked();
			if (!$uid)
				$uid = $this->am->addArticleConnection( $datas );
			else
				$this->am->updateArticleConnection( $uid , $datas );
		}

		protected function deleteDatas( $uid = 0 ) {
			$this->am->clearLeadPicture( $uid );
			$this->am->deleteArticle( $uid );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();

			$this->ac_submit->refreshValueFromArray();
			if (!$this->ac_submit->isSubmitted()) return parent::handleEvents();

			$this->refreshChildValuesFromArray();

			$valid = $this->validateForm();
			if ($valid) {
				switch ($this->aG["mode"]) {
				case "new":$this->saveDatas( 0 );break;
				case "modify":$this->saveDatas( $this->aG["uid"] );break;
				case "delete":$this->deleteDatas( $this->aG["uid"] );break;
				}
				$this->closeAndRefreshParent();
			}
			return parent::handleEvents();
		}

		protected function generateLangDefs() {
		}

		public function render() {
			if (!$this->isLoggedIn()) return parent::render();
			$this->generateLangDefs();
			return parent::render();
		}
	}
?>
