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

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

	class AdminForumpostEditPage extends BackendPopup {
		protected $fm;

		protected $ip_address;
		protected $ip_description;
		protected $is_active;

		protected $forum_submit;

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

			$this->forum_post = new AdminTextarea( "forum_post" , $this );
			$this->is_admin_acknowledged = new AdminCheckbox( "is_admin_acknowledged" , $this );
			$this->is_admin_moderated = new AdminCheckbox( "is_admin_moderated" , $this );

			$this->forum_submit = new AdminSubmit( "forum_submit" , $this );
		}
		
		protected function initializeWidgets() {
			if (!AdminUserModel::isAdminLoggedIn()) return;
	
			$this->forum_post->setTitle( __("forumposts_Post") );
			$this->forum_post->setWidth( 760 );
			$this->forum_post->setHeight( 420 );
			$this->forum_post->setShowTitle( true );
			
			$this->is_admin_acknowledged->setTitle( __("forumposts_Admin_acknowledged") );
			$this->is_admin_acknowledged->setShowTitle( true );
			
			$this->is_admin_moderated->setTitle( __("forumposts_Admin_moderated") );
			$this->is_admin_moderated->setShowTitle( true );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->setParam("title" , __("forumposts_New_forum_post" ));
				$this->forum_submit->setValue( __( "New","attr" ) );
				break;
			case "modify":
				$this->setParam("title" , __("forumposts_Modify_forum_post" ));
				$this->forum_submit->setValue( __( "Modify","attr" ) );
				break;
			case "delete":
				$this->setParam("title" , __("forumposts_Delete_forum_post" ));
				$this->forum_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;
			$forum_post = $this->forum_post->getValue();
			if (!strlen( $forum_post )) {
				$valid = false;
				$this->warningMessage( "empty_post" , __("forumposts_Empty_post" ) );
			}
			return $valid;
		}


		protected function loadDatas( $uid ) {
			$datas = $this->fm->getForumpostFromId( $uid );
			$this->forum_post->setValue( $datas["post"] );
			$this->is_admin_acknowledged->setChecked( ($datas["is_admin_acknowledged"]=="t") );
			$this->is_admin_moderated->setChecked( ($datas["is_admin_moderated"]=="t") );
		}

		protected function saveDatas( $uid = 0 ) {
			$datas = array();
			$datas["post"] = $this->forum_post->getValue();
			$datas["is_admin_acknowledged"] = $this->is_admin_acknowledged->isChecked();
			$datas["is_admin_moderated"] = $this->is_admin_moderated->isChecked();
			if (!$uid)
				$uid = $this->fm->addForumpost( $datas );
			else
				$this->fm->updateTopicPost( $uid , $datas );
		}

		protected function deleteDatas( $uid = 0 ) {
			$this->fm->deleteForumpost( $uid );
		}

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

			$this->forum_submit->refreshValueFromArray();
			if ($this->forum_submit->isSubmitted()) {
				$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();
		}
	}
?>
