<?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/Forum.inc.php" );
		

	class AdminForumtopicEditPage extends BackendPopup {
		protected $forum_topic_title;
		protected $forum_topic_submit;

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

			$this->forum_topic_title = new AdminTextbox( "forum_topic_title" , $this );
			$this->forum_topic_submit = new AdminSubmit( "forum_topic_submit" , $this );
		}

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

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

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->forum_topic_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("forumtopics_New_topic") );
				break;
			case "modify":
				$this->forum_topic_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("forumtopics_Modify_topic" ) );
				break;
			case "delete":
				$this->forum_topic_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("forumtopics_Delete_topic" ) );
				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 ForumModel();
			$datas = $cats->getForumtopicFromId( $uid );
			$this->forum_topic_title->setValue( $datas["forum_topic_title"] );
		}

		protected function saveDatas( $uid = 0 , $pid = 0 ) {
			$fm = new ForumModel();
			$datas = array( "forum_topic_title" => $this->aP[$this->forum_topic_title->getId()] );
			$datas["owner_user_id"]=296;
			if ($pid) {
				$datas["forum_topic_parentid"] = $pid;
				$par = $fm->getForumTopicFromId( $pid );
				$datas["forum_topic_level"]=$par["forum_topic_level"]+1;
				$datas["forum_topic_ordering"]=$fm->getNextTopicOrderingOnParent( $pid );
			} else {

			}
			if (!$uid)
				$fm->addTopic( $datas );
			else
				$fm->updateTopic( $uid , $datas );
		}

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

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->forum_topic_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();
		}
	}
?>
