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

	class AdminMembershipEditPage extends BackendPopup {
		protected $membership_name;
		protected $membership_description;
		protected $membership_submit;

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

			$this->membership_name = new AdminTextbox( "membership_name" , $this );
			$this->membership_description = new AdminTextbox( "membership_description" , $this );
			$this->membership_url = new AdminTextbox( "membership_url" , $this );
			$this->membership_submit = new AdminSubmit( "membership_submit" , $this );
		}

		protected function initializeWidgets() {
			$lang = new MembershipsModel();

			$this->membership_name->setTitle( __("memberships_Name") );
			$this->membership_name->setCSSAttribute( "width" , "300px" );
			$this->membership_name->setShowTitle( true );
			
			$this->membership_description->setTitle( __("memberships_Description") );
			$this->membership_description->setCSSAttribute( "width" , "300px" );
			$this->membership_description->setShowTitle( true );
			
			$this->membership_url->setTitle( __("memberships_Url") );
			$this->membership_url->setCSSAttribute( "width" , "300px" );
			$this->membership_url->setShowTitle( true );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->membership_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("memberships_New_membership") );
				break;
			case "modify":
				$this->membership_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("memberships_Modify_membership" ) );
				break;
			case "delete":
				$this->membership_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("memberships_Delete_membership" ) );
				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 ) {
			$ml = new MembershipsModel();
			$datas = $ml->getMembershipFromId( $uid );
			$this->membership_name->setValue( $datas["membership_name"] );
			$this->membership_description->setValue( $datas["membership_description"] );
			$this->membership_url->setValue( $datas["membership_url"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$lm = new MembershipsModel();
			$datas = array(
				"membership_name" => $this->membership_name->getValue(),
				"membership_description" => $this->membership_description->getValue(),
				"membership_url" => $this->membership_url->getValue()
			);

			if (!$uid) {
				$membership_id = $lm->addMembership( $datas );
			} else {
				$lm->updateMembership( $uid , $datas );
			}
		}

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

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

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