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

	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );

	class AdminUserEditPage extends BackendPopup {
		protected $reg_tstamp;
		protected $user_name;
		protected $email;
		protected $pwd;
		protected $contact_phone;
		protected $is_acknowledged;
		protected $is_admin_acknowledged;
		protected $is_instant_sender;

		protected $npublic_comment;

		protected $user_submit;

		protected $user_log;

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

			$this->reg_tstamp = new AdminTimestamp( "reg_tstamp" , $this );
			$this->email = new AdminTextbox( "email" , $this );
			$this->user_name = new AdminTextbox( "user_name" , $this );
			$this->pwd = new AdminTextbox( "pwd" , $this );
			$this->contact_phone = new AdminTextbox( "contact_phone" , $this );

			$this->is_acknowledged = new AdminCheckbox( "is_acknowledged" , $this );
			$this->is_admin_acknowledged = new AdminCheckbox( "is_admin_acknowledged" , $this );
			$this->is_instant_sender = new AdminCheckbox( "is_instant_sender" , $this );

			$this->npublic_comment = new AdminTextarea( "npublic_comment" , $this );
			$this->user_submit = new AdminSubmit( "user_submit" , $this );

			$this->user_log = new AdminUserLogTable( "AdminUserLogTable" , $this );
		}

		protected function initializeWidgets() {
			$this->reg_tstamp->setTitle( __("users_Created_at" ) );
			$this->reg_tstamp->setShowTitle( true );
			
			$this->email->setTitle( __("users_Email") );
			$this->email->setShowTitle( true );
			$this->email->setWidth( 200 );
			$this->email->setAttribute( "maxlength" , "128" );

			$this->user_name->setTitle( __("users_Name") );
			$this->user_name->setShowTitle( true );
			$this->user_name->setWidth( 200 );
			$this->user_name->setAttribute( "maxlength" , "128" );
			


			$this->npublic_comment->setTitle( __("users_Not_public_comment") );
			$this->npublic_comment->setShowTitle( true );
			$this->npublic_comment->setWidth( 790 );
			$this->npublic_comment->setRows( 6 );

			$this->pwd->setTitle( __("users_Password" ) );
			$this->pwd->setShowTitle( true );
			$this->pwd->setWidth( 200 );
			$this->pwd->setAttribute( "maxlength" , "64" );
			
			$this->contact_phone->setTitle( __("users_Phone" ) );
			$this->contact_phone->setShowTitle( true );
			$this->contact_phone->setWidth( 200 );
			$this->contact_phone->setAttribute( "maxlength" , "96" );

			$this->is_acknowledged->setTitle( __("users_Acknowledged" ) );
			$this->is_acknowledged->setShowTitle( true );
			
			$this->is_admin_acknowledged->setTitle( __("users_Admin_acknowledged" ) );
			$this->is_admin_acknowledged->setShowTitle( true );
			
			$this->is_instant_sender->setTitle( __("users_Instant_sender" ) );
			$this->is_instant_sender->setShowTitle( true );

			switch ($this->aG["mode"]) {
			case "new":
				$this->user_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("users_New_user") );

				$this->reg_tstamp->setValue( fo3_date( "Y-m-d H:i:s" ) );
				break;
			case "modify":
				$this->user_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("users_Modify_user" ) );
				$this->user_log->setUserId( $this->aG["uid"] );
				break;
			case "delete":
				$this->user_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("users_Delete_user" ) );
				break;
			}
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
			}
		}

		protected function loadDatas( $uid ) {
			$um = new UserModel();
			$user = $um->getUserFromUserId( $uid );
			$this->reg_tstamp->setValue( substr( $user["reg_tstamp"], 0 , 19) );
			$this->email->setValue( $user["email"] );
			$this->user_name->setValue( $user["user_name"] );
			$this->npublic_comment->setValue( $user["npublic_comment"] );
			$this->pwd->setValue( $user["pwd"] );
			$this->contact_phone->setValue( $user["contact_phone"] );

			$this->is_acknowledged->setChecked( $user["is_acknowledged"]=="t" );
			$this->is_admin_acknowledged->setChecked( $user["is_admin_acknowledged"]=="t" );
			$this->is_instant_sender->setChecked( $user["is_instant_sender"]=="t" );
		}

		protected function saveDatas( $uid ) {
			$this->refreshChildValuesFromArray();
			$p = array();
			$p["reg_tstamp"]=$this->reg_tstamp->getValue();
			$p["email"]=$this->email->getValue();
			$p["user_name"]=$this->user_name->getValue();
			$p["npublic_comment"]=$this->npublic_comment->getValue();
			$p["pwd"]=$this->pwd->getValue();
			// $p["hash"]=md5( $this->pwd->getValue() );			// hash az nem jelszohash
			$p["contact_phone"]=$this->contact_phone->getValue();
			$p["is_acknowledged"]=$this->is_acknowledged->isChecked();
			$p["is_admin_acknowledged"]=$this->is_admin_acknowledged->isChecked();
			$p["is_instant_sender"]=$this->is_instant_sender->isChecked();

			$um = new UserModel();
			if ($uid) {
				$um->updateUser( $uid , $p );
				$jm = new JobModel();
				$jm->updateJobPosterUserData( $uid );
			} else
				$um->addUser( $p );
		}
		
		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->user_submit->getId()] )) return parent::handleEvents();
			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();
			if (isset( $this->aG["uid"] ))
				$this->setParam( "user_id" , $this->aG["uid"] );
			return parent::render();
		}


	}

?>
