<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/urlgen/MyUser.inc.php" );

	class ForgottenPasswordPane extends Widget {
		protected $bThanks = false;
		protected $user_id = 0;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
		}

		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			if (isset( $this->aG["sent"] )) $this->bThanks = 1;

			$this->fp_email = new FrontendTextbox( "fp_email" , $this );
			$this->fp_email->setWidth( 300 );
			if (isset( $this->aG["email"] )) $this->fp_email->setValue( $this->aG["email"] );

			$this->fp_submit = new FrontendSubmit( "fp_submit" , $this );
			$this->fp_submit->setValue( __("Send", "attr" ) );
		}

		public function handleEvents() {
			$this->refreshChildValuesFromArray();
			if (isset( $this->aG["task"] ) &&
				$this->aG["task"]=="forgotten_password" &&
				isset( $this->aG["user_id"]) &&
				isset( $this->aG["hash"]) ) {

				$mmm = new MyMemberModel();
				$um = new UserModel();
				$user = $um->getUserFromUserId( $this->aG["user_id"] );
				if (!$user) {
					// nincs ilyen user
					$this->setParam( "warning_message" , __("forgottenpassword_Invalid_user") );
					return parent::handleEvents();
				}
				if ($user["hash"]!=$this->aG["hash"]) {
					$this->setParam( "warning_message" , __("forgottenpassword_Invalid_hash") );
					return parent::handleEvents();
				}
				$mmm->loginUser( $this->aG["user_id"] );
				FNTH_redirect( MyUserURL::renderMyProfileURL() );
			}
			if ($this->fp_submit->isSubmitted()) {
				$um = new UserModel();
				$this->user_id = $um->getUserIdFromEmail( $this->fp_email->getValue() );
				if ($this->user_id) {
					$tm = new TemplateMailModel( "sys_forgotten_password" , $GLOBALS["language"] );
					$cm = new ConstantsModel( $tm );
					$cm->addUserConstantsToTemplate( $this->user_id );

					$user = $um->getUserFromUserId( $this->user_id );
					$p = array();
					$p["task"]="forgotten_password";
					$p["user_id"]=$this->user_id;
					$p["hash"]=$user["hash"];

					$url = fo3_fullurl( MyUserURL::renderForgottenPasswordURL( $p ) );
					$tm->setParam( "url_forgotten_password",$url );
					$tm->sendMail( $this->fp_email->getValue() );
					FNTH_redirect( MyUserURL::renderForgottenPasswordDoneURL() );
				} else {
					$this->setParam( "warning_message" , __("forgottenpassword_Invalid_email") );
					return parent::handleEvents();
				}
				return parent::handleEvents();
			}
		}

		public function setThanks( $v ) { $this->bThanks = $v; }

		protected function generateLangDefs() {
			fo3_add_js_langdef( "Please_fill_email" );
			fo3_add_js_langdef( "Email_not_found" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();
			$this->setParam( "bThanks" , $this->bThanks );
			return parent::render();
		}

	}

?>
