<?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/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 AdminArticleEditPage extends BackendPopup {
		protected $am;

		protected $article_name;
		protected $larticle;
		protected $article_publicationtstamp;
		protected $article_depublicationtstamp;

		protected $article_submit;
		protected $article_save;
		protected $article_apply;

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

			$this->article_name = new AdminTextbox( "article_name" , $this );
			$this->article_category_id = new AdminArticleCategory( "article_category_id" , $this );
			$this->article_publicationtstamp = new AdminDatetime( "article_publicationtstamp" , $this );
			$this->article_depublicationtstamp = new AdminDatetime( "article_depublicationtstamp" , $this );
			$this->article_ordering_tstamp = new AdminDatetime( "article_ordering_tstamp" , $this );
			$this->article_leadpic = new AdminArticleLeadpicture( "article_leadpic" , $this );
			$this->article_is_active = new AdminCheckbox( "article_is_active" , $this );
			$this->article_is_memberonly = new AdminCheckbox( "article_is_memberonly" , $this );
			$this->article_is_subscriberonly = new AdminCheckbox( "article_is_subscriberonly" , $this );

			$this->larticle = new MLMDPLanguageArticle( "larticle" , $this );

			$this->article_submit = new AdminSubmit( "article_submit" , $this );
		}
		
		protected function initializeWidgets() {
			if (!AdminUserModel::isAdminLoggedIn()) return;
		
			if (isset( $this->article_save )) {
				$this->article_save->setValue( __("Save_and_continue","attr" ) );
			}

			if (isset( $this->article_apply)) {
				$this->article_apply->setValue( __("Preview","attr") );
				$this->article_apply->setAttribute( "onclick" , sprintf( "AdminArticleApply( %d );", $this->aG["uid"] ) );
			}

			$this->article_name->setTitle( __("articles_Name") );
			$this->article_name->setWidth( 275 );	// ( "width" , "350px" );
			$this->article_name->setShowTitle( true );

			$this->article_category_id->setTitle( __("articles_Category") );
			$this->article_category_id->setShowTitle( true );

			$this->article_publicationtstamp->setTitle( __("articles_Publication_timestamp" ) );
			$this->article_publicationtstamp->setShowTitle( true );
			
			$this->article_depublicationtstamp->setTitle( __("articles_Depublication_timestamp" ) );
			$this->article_depublicationtstamp->setShowTitle( true );
			
			$this->article_ordering_tstamp->setTitle( __("articles_Ordering_timestamp" ) );
			$this->article_ordering_tstamp->setShowTitle( true );
			$this->article_ordering_tstamp->setValue( fo3_date( "Y-m-d H:i:s" ) );

			$this->article_leadpic->setTitle( __("articles_Lead_picture" ) );
			$this->article_leadpic->setShowTitle( true );

			$this->article_is_active->setTitle( __("articles_Active") );
			$this->article_is_active->setShowTitle( true );

			$this->article_is_memberonly->setTitle( __("articles_Member_only" ) );
			$this->article_is_memberonly->setShowTitle( true );

			$this->article_is_subscriberonly->setTitle( __("articles_Subscriber_only" ) );
			$this->article_is_subscriberonly->setShowTitle( true );

			$this->larticle->setWidth( 740 );
			
			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->setParam("title" , __("articles_New_article" ));
				$this->article_submit->setValue( __( "New","attr" ) );
				break;
			case "modify":

				$title = "";
				$title.=__("articles_Modify_article" );
				$url = sprintf( "javascript:AdminArticlePreview( %d );",$this->aG["uid"] );
				$title.=sprintf( " [<a href=\"%s\">%s</a>]" , $url , __("preview") );
				$url = sprintf( "javascript:AdminArticleConnecteds( %d );",$this->aG["uid"] );
				$title.=sprintf( " [<a href=\"%s\">%s</a>]" , $url , __("articles_connecteds") );
				$this->setParam( "title", $title );

				$this->article_submit->setValue( __( "Modify","attr" ) );
				break;
			case "delete":
				$this->setParam("title" , __("articles_Delete_article" ));
				$this->article_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 );
			if (isset( $this->aG["uid"] )) {
				$this->article_apply = new AdminButton( "article_apply" , $this );
				$this->article_save = new AdminSubmit( "article_save" , $this );
			}
			$this->initializeWidgets();
		}

		protected function validateForm() {
			$valid = true;

			// article_category_id
			$article_category_id = (int)$this->article_category_id->getValue();
			if (!$article_category_id) {
				$valid = false;
				$this->warningMessage( "no_article_category_id" , __("articles_No_article_category_defined" ) );
			}

			// article_name
			$article_name = $this->article_name->getValue();
			if (!strlen( trim( $article_name ))) {
				$valid = false;
				$this->warningMessage( "no_article_name" , __("articles_No_article_name_defined" ) );
			}

			// article urls
			$ldatas = $this->larticle->getValues();
			
			foreach ($ldatas as $language_id => $datas) {
				if (!$datas["is_url_used"]) continue;
				if (!strlen( trim( $datas["larticle_url"] ))) {
					$valid = false;
					$this->warningMessage( "no_article_url" , __("articles_No_article_url_defined" ) );
				} else {
					$exclude_ids = array();
					if (isset( $this->aG["uid"] )) $exclude_ids[]=$this->aG["uid"];

					if ($this->am->isUrlUsed( $datas["larticle_url"] , $exclude_ids )) {
						$valid = false;
						$this->warningMessage( "no_article_url" , __("articles_Article_url_already_used" ) );
					}
				}
			}
			return $valid;
		}


		protected function loadDatas( $uid ) {
			$datas = $this->am->getArticleFromId( $uid );
			$this->article_name->setValue( $datas["article_name"] );
			$this->article_category_id->setValue( $datas["article_category_id"] );
			$this->article_publicationtstamp->setValue( $datas["article_publication_tstamp"] );
			$this->article_depublicationtstamp->setValue( $datas["article_depublication_tstamp"] );
			$this->article_ordering_tstamp->setValue( substr( $datas["article_ordering_tstamp"] , 0 , 19 ) );
			$this->article_is_active->setChecked( $datas["is_active"] );
			$this->article_is_memberonly->setChecked( $datas["is_memberonly"] );
			$this->article_is_subscriberonly->setChecked( $datas["is_subscriberonly"] );

			$this->article_leadpic->setArticleId( $uid );
			$this->article_leadpic->setValue( $datas["article_leadpicture"] );
		
			$ldatas = $this->am->getLanguageArticles( $uid );
			$this->larticle->setValues( $ldatas );
		}

		protected function saveDatas( $uid = 0 ) {
			$datas = array();
			$datas["article_name"] = $this->article_name->getValue();
			$datas["article_category_id"] = $this->article_category_id->getValue();
			$tstamp = $this->article_publicationtstamp->getValue();
			if (trim(strlen( $tstamp ))) {
				if (strlen( $tstamp )==16) $tstamp.=":00";
				$datas["article_publication_tstamp"]=$tstamp;
			} else {
				$datas["article_publication_tstamp"]=false;
			}
			
			$tstamp = $this->article_depublicationtstamp->getValue();
			if (trim(strlen( $tstamp ))) {
				if (strlen( $tstamp )==16) $tstamp.=":00";
				$datas["article_depublication_tstamp"]=$tstamp;
			} else {
				$datas["article_depublication_tstamp"]=false;
			}

			// ordering timestamp	
			$tstamp = $this->article_ordering_tstamp->getValue();
			if (trim(strlen( $tstamp ))) {
				if (strlen( $tstamp )==16) $tstamp.=":00";
				$datas["article_ordering_tstamp"]=$tstamp;
			}
			
			$datas["article_is_active"] = $this->article_is_active->isChecked();
			$datas["article_is_memberonly"] = $this->article_is_memberonly->isChecked();
			$datas["article_is_subscriberonly"] = $this->article_is_subscriberonly->isChecked();

			if (!$uid)
				$uid = $this->am->addArticle( $datas );
			else
				$this->am->updateArticle( $uid , $datas );

			$datas = $this->larticle->getValues();
			$this->am->setLanguageArticles( $uid , $datas );

			// handle leadpic events
			$lpid = $this->article_leadpic->getId();
			if (isset( $_FILES[$lpid]["error"] ) && !$_FILES[$lpid]["error"]) {
				$this->am->updateLeadPicture( $uid , $_FILES[$lpid] );
			}

			if ($this->article_leadpic->isOnDeletion()) {
				$this->am->clearLeadPicture( $uid );
			}
		}

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

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

			if (isset( $this->article_save )) $this->article_save->refreshValueFromArray();
			$this->article_submit->refreshValueFromArray();

			if (isset( $this->aP["inajax"]) && isset( $this->aP["ajaxevent"] )) {
				switch ($this->aP["ajaxevent"]) {
				case "apply":
					$this->refreshChildValuesFromArray();
					$articles = $this->larticle->getValues();
					// FNTH_da( $articles );
					$art = new ArticlesModel();
					foreach ($GLOBALS["languages"] as $langcode) {
						$p = array();
						if (isset( $articles[$langcode]["larticle_lead"] ))	
							$p["larticle_lead"] = $articles[$langcode]["larticle_lead"];
						if (isset( $articles[$langcode]["larticle_body"] ))	
							$p["larticle_body"] = $articles[$langcode]["larticle_body"];
						$art->setLanguageArticle( $this->aP["article_id"], $langcode , $p );
					}
					FNTH_direct_echo( sprintf( "AdminArticlePreview( %d );" , $this->aP["article_id"] ) );
					exit();
				}
			}

			if (isset( $this->article_save ) && $this->article_save->isSubmitted()) {
				$this->refreshChildValuesFromArray();
				$values = $this->larticle->getValues();
				$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->refreshWindow();
			}
			if ($this->article_submit->isSubmitted()) {
				$this->refreshChildValuesFromArray();
				$values = $this->larticle->getValues();

				$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();
				}
				/*
					else {
					$this->refreshWindow();
				}
				*/
			}
			return parent::handleEvents();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "articles_No_article_category_defined" );
			fo3_add_js_langdef( "articles_No_article_name_defined" );
			fo3_add_js_langdef( "articles_No_article_url_defined" );
		}

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