<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );

	class JobHandler extends Widget {
		protected $job_id = 0;
		protected $job = array();

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

			$this->jm = new JobModel();
			$this->um = new UserModel();
			$this->mum = new MyMemberModel();
			$this->muu = new MyUserURL();
			$this->ulm = new UserLogModel();

		}

		// global dialog methods
		protected function renderDialog( $job_quote_id , $type ) {
			$sa = new SystemArticle( "system_article" , $this , "sys_".$type );
			$sa->setOnlyBody( true );

			$cm = new ConstantsModel( $sa , $GLOBALS["language"] );
			$cm->addSystemParametersToTemplate();
			$cm->addJobConstantsToTemplate( $this->job_id , "job" , $this->job );
			return $sa->render();
		}
		
		protected function renderDialogButtons( $job_id , $type ) {
			switch ($type) {
			case "dlg_activate_job":
				return array(
					__("Cancel") => false,
					__("jobspage_Activate_job") =>
						array(	"ajaxevent" => "activate_job",
								"job_id" => $job_id )
				);
			case "dlg_revoke_job":
				return array(
					__("Cancel") => false,
					__("jobspage_Revoke_job") =>
						array(	"ajaxevent" => "revoke_job",
								"job_id" => $job_id )
				);
			case "dlg_close_job":
				return array(
					__("Cancel") => false,
					__("jobspage_Close_job") =>
						array(	"ajaxevent" => "close_job",
								"job_id" => $job_id )
				);
			}
		}
		
		protected function loadJobDatasFromHash( $hash ) {
			$this->job = $this->jm->getJobFromHash( $hash , true );
			$this->job_id = $this->job["job_id"];
		}

		protected function loadJobDatasFromId( $job_id ) {
			$this->job_id = $job_id;
			$this->job = $this->jm->getJobFromId( $job_id , true );
		}

		protected function handleQuoteDialogRequest() {
			$this->loadJobDatas();
			$retval = array();
			$valid = true;
			if (!$this->job_id) {
				$retval["error"]=__("jobspage_Invalid_job");
				FNTH_echo_json_response( $retval );fo3_exit();
			}
			switch ($this->aP["ajaxevent"]) {
			case "dlg_activate_job":
				if ($this->job["poster_acknowledged"]=="t") {
					$valid = false;
					$retval["error"]=__("jobspage_Already_activated_job");
					break;
				}
				$retval["dlg_ttl"]=__("jobspage_Activate_job");
				break;
			case "dlg_close_job":
				if ($this->job["is_closed"]=="t") { $valid = false;$retval["error"]=__("jobspage_Already_closed_job");break; }
				if ($this->job["is_revoked"]=="t") { $valid = false;$retval["error"]=__("jobspage_Already_revoked_job");break; }
				$retval["dlg_ttl"]=__("jobspage_Close_job");
				break;
			case "dlg_revoke_job":
				if ($this->job["is_closed"]=="t") { $valid = false;$retval["error"]=__("jobspage_Already_closed_job");break; }
				if ($this->job["is_revoked"]=="t") { $valid = false;$retval["error"]=__("jobspage_Already_revoked_job");break; }
				$retval["dlg_ttl"]=__("jobspage_Revoke_job");
				break;
			}
			if ($valid) {
				// megkrealjuk a dialogot + buttonokat
				$retval["dlg"]=$this->renderDialog( $this->job_id , $this->aP["ajaxevent"] );
				$retval["buttons"]=$this->renderDialogButtons( $this->job_id , $this->aP["ajaxevent"] );
			}
			FNTH_echo_json_response( $retval );fo3_exit();
		}

		// activation methods		
		protected function isDialogStartActivation() {
			return isset( $this->aG["activate"] );
		}
		
		protected function handleQuoteActivateDialogStartRequest() {
			// activate email linkbol - feldobjuk a popupot
			$js = sprintf( "ActivateJob( '%s' );" , $this->aG["hash"] );
			FNTHPage::addUniquePageFullInit( $js );
			return parent::handleEvents();
		}

		
		protected function isDialogActivation() {
			if (!isset( $this->aP["ajaxevent"] )) return false;
			if (!isset( $this->aP["hash"] )) return false;
			if ($this->aP["ajaxevent"] != "dlg_activate_job") return false;
			return true;
		}

		protected function isActivation() {
			if (!isset( $this->aP["ajaxevent"] )) return false;
			if (!isset( $this->aP["job_id"] )) return false;
			if ($this->aP["ajaxevent"] != "activate_job") return false;
			return true;
		}

		protected function loadJobDatas() {
			if (isset( $this->aG["hash"] )) {
				$this->loadJobDatasFromHash( $this->aG["hash"] );
			} else {
				$this->loadJobDatasFromId( $this->aP["job_id"] );
			}
		}
		
		protected function handleQuoteActivationRequest() {
			$this->loadJobDatas();		
			$retval = array();
			if ($this->job) {
				// van, de esetleg nem valid
				if ($this->job["poster_acknowledged"]=="t") {
					$retval["error"]=__("jobspage_Already_activated_job");
				} else {
					// ha emailbol jott, akkor belogin
					if (isset( $this->aG["hash"] )) {
						// acknowledge user
						if ($this->um->acknowledgeUser( $this->job["poster_user_id"] )) {
							// ha tenyleges ack jott (meg nem volt), akkor kuldunk neki mailt is
							$um = new UserModel();
							$user = $um->getUserFromUserId( $this->job["poster_user_id"] );

							$mail = new TemplateMailModel( "sys_user_acknowledged" , $GLOBALS["language"] );
							$cm = new ConstantsModel( $mail );
							$cm->addSystemParametersToTemplate();
							$cm->addUserConstantsToTemplate( $this->job["poster_user_id"] , "user" , $user );
							$mail->sendMail( $user["email"] );
						}
						$this->mum->loginUser( $this->job["poster_user_id"] );	 // login the user
						$this->ulm->logLogin();
					}

					if ($this->mum->getLoggedinUserId()==$this->job["poster_user_id"]) {
						// ove a cumo
						$this->jm->acknowledgeJob( $this->job["job_id"] );	// acknowledge job
						$url = $this->muu->renderMyQuoteRequestsURL();

						$this->ulm->logMemberActivity( USER_LOG_JOB_OFFER | USER_JOB_OFFER_ACTIVATED ,
							sprintf( "aktiválta munkaajánlatát (#%d)" , $this->job["job_id"] ) ); // LANGFIX

						// uzemeltetoi mail
						$mail = new TemplateMailModel( "sys_admin_job_accepted" );
						$cm = new ConstantsModel( $mail );
						$cm->addSystemParametersToTemplate();
						$cm->addJobConstantsToTemplate( $this->job["job_id"] );
						$mail->sendMail( $GLOBALS["fo3_admin_email"] );

						$retval["evaluate"]=sprintf( "document.location.href='%s';" , $url );
					} else if (!$this->mum->isUserLoggedIn()) {
						// nem van belepve
						$retval["error"]=__("globalerror_User_has_to_login_to_reach_functionality");
					} else {
						// be van lepve, csak nem az ove
						$retval["error"]=__("jobspage_Invalid_job");
					}
				}
			} else {
				$retval["error"]=__("jobspage_Invalid_job");
			}
			FNTH_echo_json_response( $retval );fo3_exit();
		}

		// close methods
		protected function isDialogStartClose() {
			return isset( $this->aG["close"] );
		}
		protected function handleQuoteCloseDialogStartRequest() {
			// activate email linkbol - feldobjuk a popupot
			$js = sprintf( "CloseJob( '%s' );" , $this->aG["hash"] );
			FNTHPage::addUniquePageFullInit( $js );
			return parent::handleEvents();
		}
		
		protected function isDialogClose() {
			if (!isset( $this->aP["ajaxevent"] )) return false;
			if (!isset( $this->aP["job_id"] )) return false;
			if ($this->aP["ajaxevent"] != "dlg_close_job") return false;
			return true;
		}

		protected function isClose() {
			if (!isset( $this->aP["ajaxevent"] )) return false;
			if (!isset( $this->aP["job_id"] )) return false;
			if ($this->aP["ajaxevent"] != "close_job") return false;
			return true;
		}
		
		protected function handleQuoteCloseRequest() {
			$this->loadJobDatas();

			$retval = array();
			if ($this->job) {
				if ($this->job["is_closed"]=="t") {
					$retval["error"]=__("jobspage_Already_closed_job");
				} else {
					if (isset( $this->aG["hash"] )) {
						$this->um->acknowledgeUser( $this->job["poster_user_id"] );	// acknowledge user
						$this->mum->loginUser( $this->job["poster_user_id"] );	 // login the user
						$this->ulm->logLogin();
					}

					if ($this->mum->getLoggedinUserId()==$this->job["poster_user_id"]) {
						$this->jm->acknowledgeJob( $this->job["job_id"] );	// acknowledge job
						$this->jm->closeJob( $this->job["job_id"] );
						
						$this->ulm->logMemberActivity( USER_LOG_JOB_OFFER | USER_JOB_OFFER_ACTIVATED ,
							sprintf( "lezárta munkaajánlatát (#%d)" , $this->job["job_id"] ) ); // LANGFIX
						
						$url = $this->muu->renderMyQuoteRequestsURL();
						$retval["evaluate"]=sprintf( "document.location.href='%s';" , $url );
					} else if (!$this->mum->isUserLoggedIn()) {
						// nem van belepve
						$retval["error"]=__("globalerror_User_has_to_login_to_reach_functionality");
					} else {
						// be van lepve, csak nem az ove
						$retval["error"]=__("jobspage_Invalid_job");
					}
				}
			} else {
				$retval["error"]=__("jobspage_Invalid_job");
			}
			FNTH_echo_json_response( $retval );fo3_exit();
		}

		
		protected function isDialogStartRevoke() {
			return isset( $this->aG["revoke"] );
		}
		protected function handleQuoteRevokeDialogStartRequest() {
			// activate email linkbol - feldobjuk a popupot
			$js = sprintf( "RevokeJob( '%s' );" , $this->aG["hash"] );
			FNTHPage::addUniquePageFullInit( $js );
			return parent::handleEvents();
		}
		
		protected function isDialogRevoke() {
			if (!isset( $this->aP["ajaxevent"] )) return false;
			if (!isset( $this->aP["job_id"] )) return false;
			if ($this->aP["ajaxevent"] != "dlg_revoke_job") return false;
			return true;
		}

		protected function isRevoke() {
			if (!isset( $this->aP["ajaxevent"] )) return false;
			if (!isset( $this->aP["job_id"] )) return false;
			if ($this->aP["ajaxevent"] != "revoke_job") return false;
			return true;
		}
		
		protected function handleQuoteRevokeRequest() {
			$this->loadJobDatas();

			$retval = array();
			if ($this->job) {
				if ($this->job["is_revoked"]=="t") {
					$retval["error"]=__("jobspage_Already_revoked_job");
				} else {
					if (isset( $this->aG["hash"] )) {
						$this->um->acknowledgeUser( $this->job["poster_user_id"] );	// acknowledge user
						$this->mum->loginUser( $this->job["poster_user_id"] );	 // login the user
						$this->ulm->logLogin();
					}

					if ($this->mum->getLoggedinUserId()==$this->job["poster_user_id"]) {
						// ove a cumo
						$this->jm->acknowledgeJob( $this->job["job_id"] );	// acknowledge job
						$this->jm->revokeJob( $this->job["job_id"] );
					
						$this->ulm->logMemberActivity( USER_LOG_JOB_OFFER | USER_JOB_OFFER_ACTIVATED ,
							sprintf( "visszavonta munkaajánlatát (#%d)" , $this->job["job_id"] ) ); // LANGFIX

						$url = $this->muu->renderMyQuoteRequestsURL();
						$retval["evaluate"]=sprintf( "document.location.href='%s';" , $url );
					} else if (!$this->mum->isUserLoggedIn()) {
						// nem van belepve
						$retval["error"]=__("globalerror_User_has_to_login_to_reach_functionality");
					} else {
						// be van lepve, csak nem az ove
						$retval["error"]=__("jobspage_Invalid_job");
					}
				}
			} else {
				$retval["error"]=__("jobspage_Invalid_job");
			}
			FNTH_echo_json_response( $retval );fo3_exit();
		}

		public function handleEvents() {
			if ($this->isActivation()) {
				// ajaxos aktivalas
				return $this->handleQuoteActivationRequest();
			} else if ($this->isDialogActivation()) {
				return $this->handleQuoteDialogRequest(); // ajax request a dialogra - aktivalas
			} else if ($this->isDialogStartActivation()) {
				return $this->handleQuoteActivateDialogStartRequest(); // email link - aktivalas felpoppol
			} else if ($this->isClose()) {
				return $this->handleQuoteCloseRequest(); // ajaxos lezaras
			} else if ($this->isDialogClose()) {
				return $this->handleQuoteDialogRequest(); // ajaxos request a dialogra - lezaras
			} else if ($this->isDialogStartClose()) {
				return $this->handleQuoteCloseDialogStartRequest(); // email link - lezaras felpoppol
			} else if ($this->isRevoke()) {
				return $this->handleQuoteRevokeRequest(); // ajaxos visszavonas
			} else if ($this->isDialogRevoke()) {
				return $this->handleQuoteDialogRequest(); // ajaxos request a dialogra - visszavonas
			} else if ($this->isDialogStartRevoke()) {
				return $this->handleQuoteRevokeDialogStartRequest(); // email link - visszavonas felpoppol
			} else return parent::handleEvents();
		}
	}	

?>
