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

	class AdminAdminIpEditPage extends BackendPopup {
		protected $aim;

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

		protected $ip_submit;

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

			$this->ip_address = new AdminTextbox( "ip_address" , $this );
			$this->ip_description = new AdminTextbox( "ip_description" , $this );
			$this->is_active = new AdminCheckbox( "is_active" , $this );

			$this->ip_submit = new AdminSubmit( "ip_submit" , $this );
		}
		
		protected function initializeWidgets() {
			if (!AdminUserModel::isAdminLoggedIn()) return;
	
			$this->ip_address->setTitle( __("adminips_Admin_ip") );
			$this->ip_address->setWidth( 310 );
			$this->ip_address->setAttribute( "maxlength" , 16 );
			$this->ip_address->setShowTitle( true );
			$this->ip_address->setValue( $_SERVER["REMOTE_ADDR"] );	// on new, we initialize with our ip
			
			$this->ip_description->setTitle( __("adminips_Ip_description") );
			$this->ip_description->setWidth( 310 );
			$this->ip_description->setShowTitle( true );

			$this->is_active->setTitle( __("adminips_Active") );
			$this->is_active->setShowTitle( true );
			$this->is_active->setChecked( true );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->setParam("title" , __("adminips_New_admin_ip" ));
				$this->ip_submit->setValue( __( "New","attr" ) );
				break;
			case "modify":
				$this->setParam("title" , __("adminips_Modify_admin_ip" ));
				$this->ip_submit->setValue( __( "Modify","attr" ) );
				break;
			case "delete":
				$this->setParam("title" , __("adminips_Delete_admin_ip" ));
				$this->ip_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;
			$ip_address = $this->ip_address->getValue();
			if (!strlen( $ip_address )) {
				$valid = false;
				$this->warningMessage( "no_ip_given" , __("adminips_No_ip" ) );
			}
			return $valid;
		}


		protected function loadDatas( $uid ) {
			$datas = $this->aim->getAdminIpFromId( $uid );
			$this->ip_address->setValue( $datas["ip_address"] );
			$this->ip_description->setValue( $datas["ip_description"] );
			$this->is_active->setChecked( $datas["is_active"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$datas = array();
			$datas["ip_address"] = $this->ip_address->getValue();
			$datas["ip_description"] = $this->ip_description->getValue();
			$datas["is_active"] = $this->is_active->isChecked();
			if (!$uid)
				$uid = $this->aim->addAdminIp( $datas );
			else
				$this->aim->updateAdminIp( $uid , $datas );
		}

		protected function deleteDatas( $uid = 0 ) {
			$this->aim->deleteAdminIp( $uid );
		}

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

			$this->ip_submit->refreshValueFromArray();
			if ($this->ip_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() {
			fo3_add_js_langdef( "adminips_No_ip" );
		}

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