<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberWidget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendMenu.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/RegistrationEmail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TodoList.inc.php" );

	class MyProfilePane extends MemberWidget {
		protected $oName = null;
		protected $oEmail = null;
		protected $oPassword = null;
		protected $oPassword2 = null;
		protected $oPhone = null;



		protected function generateGeneralWidgets() {
			if (!$this->bLoggedIn) return;
			$ctrlw = 200;
			$this->oName = new FrontendTextbox( "fullname" , $this );
			$this->oName->setAttribute( "maxlength" , 128 );
			$this->oName->setWidth( 370 );
			$this->oEmail = new RegistrationEmail( "email" , $this );
			$this->oEmail->setAttribute( "maxlength" , 128 );
			$this->oEmail->setWidth( 370 );

			$this->oPassword = new FrontendTextbox( "pwd" , $this );
			$this->oPassword->setPassword( true );
			$this->oPassword->setAttribute( "maxlength" , 64 );
			$this->oPassword->setWidth( $ctrlw );
			$this->oPassword2 = new FrontendTextbox( "pwd2" , $this );
			$this->oPassword2->setAttribute( "maxlength" , 64 );
			$this->oPassword2->setPassword( true );
			$this->oPassword2->setWidth( $ctrlw );
			$this->oPhone = new FrontendTextbox( "phone" , $this );
			$this->oPhone->setAttribute( "maxlength" , 96 );
			$this->oPhone->setWidth( $ctrlw );
		}

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (!$this->bLoggedIn) return;
			$this->sPageName = "registrational_datas";
			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );

			$this->generateGeneralWidgets();
			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function initializeGeneralWidgets() {	
			if (!$this->bLoggedIn) return;

			$user_id = $this->my_user_model->getLoggedInUserId();
			$user = $this->user_model->getUserFromUserId( $user_id );

		
			$this->oName->setValue( $user["user_name"] );
			$maxlength = strlen( $user["user_name"] );
			if ($maxlength<35) $maxlength=35;
			$this->oName->setAttribute( "maxlength" , $maxlength );


			$this->oEmail->setValue( $user["email"] );
			$this->oPhone->setValue( $user["contact_phone"] );
			
			if (AdminUserModel::isAdminLoggedIn()) {
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "user_name" ))
					$this->oName->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "email" ))
					$this->oEmail->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "contact_phone" ))
					$this->oPhone->addClass( "adminblue" );
			}
		}
		
		protected function saveDatas( $admin = false ) {
			$p = array();
			$old_datas = $this->user_model->getUserFromUserId( $this->iUserId );

			$p["user_name"]=$this->oName->getValue();

			$dirty = false;
			if ($p["user_name"]!=$old_datas["user_name"] && !$admin) {
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "user_name" );
				$dirty = true;
			}

			$p["email"]=$this->oEmail->getValue();
			if ($p["email"]!=$old_datas["email"] && !$admin) {
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "email" );
				$dirty = true;
			}	
			if (strlen( $this->oPassword->getValue() )) {
				$dirty = true;
				$p["pwd"]=$this->oPassword->getValue();
			}
			
			$p["contact_phone"]=$this->oPhone->getValue();
			if ($p["contact_phone"]!=$old_datas["contact_phone"] && !$admin) {
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "contact_phone" );
			}
			// update user
			$this->user_model->updateUser( $this->iUserId , $p );
			if ($dirty) {
				// $this->user_model->adminAcknowledgeUser( $this->iUserId , false );	// mar a dirtyvel van kezelve
				$this->member_model->setMemberDirty( $this->iMemberId , true );
			}
			if ($admin) {
				$this->clearDirtyObjectAdmin( $this->sPageName );
			}
		}

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

			$submitted = false;
			if (isset( $this->oSubmit ) && $this->oSubmit->isSubmitted()) $submitted = true;
			if (isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted()) $submitted = true;
			if (!$submitted) return parent::handleEvents();

			$valid = true;
			// sanity check
			if (!strlen( $this->oName->getValue() )) {
				$valid = false;
				GlobalWarningMessage( "no_name_given" , __("profile_error_No_name_given" ) );
			}
			if (!$this->oEmail->isEmailValid()) {
				$valid = false;
				GlobalWarningMessage( "wrong_email_given" , __("profile_error_Wrong_email_given" ) );
			}

			if (strlen( $this->oPassword->getValue())) {
				if ($this->oPassword->getValue()!=$this->oPassword2->getValue()) {
					$valid = false;
					GlobalWarningMessage( "password_mismatch" , __("profile_error_Password_mismatch" ) );
				}
			}

			if (!strlen( $this->oPhone->getValue() )) {
				$valid = false;
				GlobalWarningMessage( "no_phone_given" , __("profile_error_No_phone_given" ) );
			}

			if (!$valid) {
				return parent::handleEvents();
			}

			$this->saveDatas( isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted() );

			// clear to-do
			$tlm = new TodoListModel();
			$tlm->clearTodoListItem( $this->iUserId , "registrational_datas" );

			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott regisztrációs adatain" );		// LANGFIX
			
			FNTH_refresh();
			return parent::handleEvents();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "profile_error_No_name_given" );
			fo3_add_js_langdef( "profile_error_No_email_given" );
			fo3_add_js_langdef( "profile_error_Password_mismatch" );
			fo3_add_js_langdef( "profile_error_No_phone_given" );
			fo3_add_js_langdef( "Invalid_contact_phone" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();
			FNTHPage::setPageJSHead( "valid_phone_js" , ( $GLOBALS["site_www"]."/design/rp/js/valid_phone.js" ) );
			$this->initializeGeneralWidgets();
			fo3_form_change_warning( "document.frm_profile" );
			return parent::render();
		}
	}

?>
