<?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/MultiLangDataProvider.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );

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

	class AdminArticlecategoryEditPage extends BackendPopup {
		protected $article_category_name;
		protected $article_category_title;
		protected $article_category_submit;

		public function __construct( $id = "AdminArticlecategoryEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;

			$this->article_category_name = new AdminTextbox( "article_category_name" , $this );
			$this->article_category_title = new MultiLangDataProvider( "article_category_title" , $this );
			$this->article_category_submit = new AdminSubmit( "article_category_submit" , $this );
		}

		protected function initializeWidgets() {
			$cats = new ArticlecategoriesModel();

			$this->article_category_name->setTitle( __("articlecategories_Name") );
			$this->article_category_name->setCSSAttribute( "width" , "300px" );
			$this->article_category_name->setShowTitle( true );
			$this->article_category_name->setAttribute( "tabindex" , "1" );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->article_category_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("articlecategories_New_article_category") );
				break;
			case "modify":
				$this->article_category_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("articlecategories_Modify_article_category" ) );
				break;
			case "delete":
				$this->article_category_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("articlecategories_Delete_article_category" ) );
				break;
			}
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
			// load datas
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
				break;
			}
		}

		protected function loadDatas( $uid ) {
			$cats = new ArticlecategoriesModel();
			$datas = $cats->getArticlecategoryFromId( $uid );
			$this->article_category_name->setValue( $datas["article_category_name"] );
			$langdefs = $cats->getArticleCategoryLanguageDefsFromId( $uid );
			foreach ($langdefs as $site_language_id => $v)
				$this->article_category_title->setValue( $site_language_id , $v["article_category_title"] );
		}

		protected function saveDatas( $uid = 0 , $pid = 0 ) {
			$cats = new ArticlecategoriesModel();
			$datas = array(
				"article_category_name" => $this->aP[$this->article_category_name->getId()]
			);

			$langdefs = $this->article_category_title->getValuesFromArr();
			$langdef_datas = array();
			foreach ($langdefs as $site_language_id => $v)
				$langdef_datas[$site_language_id]=array( "article_category_title" => $v );

			if (!$uid) {
				$catid = $cats->addArticleCategory( $pid , $datas );
				$cats->setArticleCategoryLanguageDefs( $catid , $langdef_datas );
			} else {
				$cats->updateArticleCategory( $uid , $datas );
				$cats->setArticleCategoryLanguageDefs( $uid , $langdef_datas );
			}

		}

		protected function deleteDatas( $uid = 0 ) {
			$lm = new ArticlecategoriesModel();
			$lm->deleteArticlecategory( $uid );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->article_category_submit->getId()] )) return parent::handleEvents();
			$uid = (isset( $this->aG["uid"] )) ? $this->aG["uid"] : 0;
			$pid = (isset( $this->aG["pid"] )) ? $this->aG["pid"] : 0;
			switch ($this->aG["mode"]) {
			case "new":$this->saveDatas( $uid , $pid );break;
			case "modify":$this->saveDatas( $uid , $pid );break;
			case "delete":$this->deleteDatas( $uid , $pid );break;
			}
			$this->closeAndRefreshParent();
		}

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