<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/FrontendEmail.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );

	class QuoteLoginEmail extends FrontendEmail {
		protected $bLoggedIn = false;
		
		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->setAttribute( "maxlength" , 64 );
		}

		protected function handleExistingUser() {
			$user = new MyMemberModel();
			if ($user->isFalseLoginsExceeded()) {
				// tul sokszor probalt (ilyenkor kell humancheckes megerosites
				return array( "logins_exceeded" => true );
			}
			// mar van ilyenunk, csak be kene leptetni
			$luser_id = $user->getUserIdFromEmailAndPassword( $this->aP["uname"] , $this->aP["pwd"] );
			if (!$luser_id) {
				if (strpos( $this->aP["uname"] , "@fordit.hu" )!==false) {
					$username = str_replace( "@fordit.hu" , "" , $this->aP["uname"] );
					$luser_id = $user->getUserIdFromUsernameAndPassword( $username , $this->aP["pwd"] );
				}
			}
			if ($luser_id) {
				// sikeres login
				$user->clearFalseLoginTries();
				$user->loginUser( $luser_id );
				require_once( $GLOBALS["site_root"] . "/includes/models/User.inc.php" );
				$um = new UserModel();
				$userdata = $um->getUserFromUserId( $luser_id );
				return array(
					"login_succeeded" => $luser_id,
					"user_name" => $userdata["user_name"],
					"contact_phone" => $userdata["contact_phone"]
				);
			} else {
				$user_id = $user->getUserIdFromEmail( $this->aP["uname"] );
				$user->falseLoginTry();

				$retval = array();
				$retval["login_failed"] = $user_id;
				$retval["url_forgotten_password"]=MyUserURL::renderForgottenPasswordURL( array( "email" => $this->aP["uname"] ) );
				$retval["ttl_forgotten_password"]=__("Forgotten_password") . " &raquo;";
				return $retval;
			}
		}

		protected function handleNonExistingUser() {
			return array( "new_user" => true );
		}

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "login-on-the-fly":
				if (!isset( $this->aP["uname"] )) return parent::handleEvents();
				$user = new MyMemberModel();
				$user_id = $user->getUserIdFromEmail( $this->aP["uname"] );
				if (!$user_id) {
					if (strpos( $this->aP["uname"] , "@fordit.hu" )!==false) {
						$username = str_replace( "@fordit.hu" , "" , $this->aP["uname"] );
						$user_id = $user->getUserIdFromUsername( $username );
					}
				}
				if (!isset( $this->aP["pwd"] )) {
					// csak emailcim van megadva
					$json =array( "user_existing" => ( $user_id>0) );
				} else {
					$json = ($user_id) ? $this->handleExistingUser() : $this->handleNonExistingUser();
				}
				FNTH_echo_json_response( $json , 0 , "fo3_exit" );
			case "login-email-request":
				if (!isset( $this->aP["email"] )) return parent::handleEvents();
				$um = new UserModel();
				$user_id = $um->getUserIdFromEmail( $this->aP["email"] );
				FNTH_echo_json_response( sprintf( "QuoteLogin%s('%s')",
					($user_id) ? "Open" : "Close" , $this->getId() ) , 0 );
				fo3_exit();
			}
			return parent::handleEvents();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "login_Login" );
			fo3_add_js_langdef( "login_Password" );
			fo3_add_js_langdef( "Login_tries_exceeded" );
			fo3_add_js_langdef( "login_Failed" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();
			$myuser = new MyUserModel();
			$this->bLoggedIn = $myuser->isUserLoggedin();
			$this->setParam( "bLoggedIn" , $this->bLoggedIn );
			FNTHPage::addUniquePageFullInit( sprintf( "InitQuoteLoginEmail('%s',%s);\n",
				$this->getId(),($this->bLoggedIn) ? "true" : "false" ) );
			return parent::render();
		}
	}
?>
