<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/CommunityToolbar.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/JobDetails.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/JobHandler.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Languages.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/ServiceTypes.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Specialties.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/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/TranslatorJobs.inc.php" );

	class JobPane extends Widget {
		protected $job_id;
		protected $user_id;
		protected $member_id;

		protected $bActiveSubscriber;
		protected $bPostLinkAvailable;
		protected $bQuoteAvailable;
		protected $bOwnedByUser;
		protected $bRevokable;

		protected $bFullDetails;

		protected $jm;
		protected $mmm;

		public function __construct( $id = "JobPane" , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );
			$this->oJobDetails = new JobDetails( "job_details" , $this );
			$this->jm = new JobModel();
			$this->mmm = new MyMemberModel();
			$this->oJobHandler = new JobHandler( "JobHandler" , $this );
		}

		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			$this->job_id = (isset( $this->aG["job_id"] )) ? $this->aG["job_id"] : 0;
		}
		
		protected function isOwnedByUser() {
			// nem valid job
			if (!$this->job_id) return false;
			// ha nincs belepve, akkor nem lehet az ove
			if (!$this->user_id) return false;
			return $this->jm->isJobOwnedByUser( $this->job_id , $this->user_id );
		}

		protected function isRevokable() {
			// nem valid job
			if (!$this->job_id) return false;
			// nincs belepve
			if (!$this->user_id) return false;
			// ha nem az ove, akkor nincs ra lehetoseg
			if (!$this->bOwnedByUser) return false;

			// mar vissa van vonva
			if ($this->job["is_revoked"]=="t") return false;
			// vagy le van zarva
			if ($this->job["is_closed"]=="t") return false;

			return true;
		}
		
		protected function isCloseable() {
			// nem valid job
			if (!$this->job_id) return false;
			// nincs belepve
			if (!$this->user_id) return false;
			// ha nem az ove, akkor nincs ra lehetoseg
			if (!$this->bOwnedByUser) return false;

			// mar vissa van vonva
			if ($this->job["is_revoked"]=="t") return false;
			// vagy le van zarva
			if ($this->job["is_closed"]=="t") return false;

			return true;
		}

		protected function isPostAvailable() {
			// nem valid job
			if (!$this->job_id) return false;
			// ha nincs belepve, akkor lehet (furcsa, de igy jo)
			if (!$this->user_id) return true;
			// ha be van, de az ove, akkor nem
			if ($this->bOwnedByUser) return false;
			// ha nem az ove, de visszavont, akkor sem
			if ($this->job["is_revoked"]=="t") return false;
			// ha nem visszavont, hanem lezart, akkor sem
			if ($this->job["is_closed"]=="t") return false;
			// ha lejart, akkor sem
			if ($this->job["job_quote_deadline"]<fo3_date( "Y-m-d H:i:s" )) return false;
			// egyebkent mehet
			return true;
		}

		
		protected function initializeDatas() {
			// all we have is job_id
			$this->job = $this->jm->getJobFromId( $this->job_id , true );
			$this->user_id = $this->mmm->getLoggedinUserId();
			$this->member_id = $this->mmm->getLoggedinMemberId();
			$this->bActiveSubscriber = $this->mmm->isLoggedinMemberActiveSubscriber();
			$this->bRevokable = false;
			$this->bCloseable = false;
			$this->bOwnedByUser = $this->isOwnedByUser();	// fontos a sorrend
			if ($this->bOwnedByUser) {
				$this->bRevokable = $this->isRevokable();
				$this->bCloseable = $this->isCloseable();
			}
			$this->bPostLinkAvailable = $this->isPostAvailable();
			$this->bQuoteAvailable = $this->jm->isJobQuoteAvailableForUser( $this->job_id , $this->user_id );

			$this->bFullDetails = $this->jm->isJobAvailableForUser( $this->job_id , $this->user_id , $this->job );

			$this->bExtendedDetails = $this->jm->isJobContactAvailableForUser( $this->job_id , $this->user_id , $this->job );
			// setting job details
			if ($this->job) {
				$this->oJobDetails->setJobData( $this->job );
				$this->oJobDetails->setDetails( $this->bFullDetails );

				$this->oJobDetails->setExtendedDetails( $this->bExtendedDetails );
				$this->oJobDetails->setOwnedByUser( $this->bOwnedByUser );
			}
		}

		protected function renderTeasingJobQuoteType() {
			// nincs belepve
			if (!$this->user_id) return "notloggedin";
			// be van lepve, de nem member
			if (!$this->member_id) return "notmember";
			// member, de sajat
			if ($this->bOwnedByUser) return "myjob";

			// member, de nem aktiv subscriber, viszont neki szol a melo
			if (!$this->bActiveSubscriber && $this->bQuoteAvailable) return "ok";

			// member, de nem aktiv subscriber
			if (!$this->bActiveSubscriber) return "notsubscriber";
			if (!$this->bQuoteAvailable) return "notavailable";
			return "ok";
		}
		
		protected function renderTeasingJobQuoteLink() {
			// nincs belepve
			if (!$this->user_id) return "";
			// be van lepve, de nem member
			if (!$this->member_id) return "";
			// member, de sajat
			if ($this->bOwnedByUser) return "";
			// member, de nem aktiv subscriber, viszont neki szol a melo
			if (!$this->bActiveSubscriber && $this->bQuoteAvailable) return JobURL::renderJobQuoteURL( $this->job_id );

			// member, de nem aktiv subscriber
			if (!$this->bActiveSubscriber) return MyMemberURL::renderMyStatusURL();
			if (!$this->bQuoteAvailable) return "";
			return JobURL::renderJobQuoteURL( $this->job_id );
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "jobquote_No_login" );
			fo3_add_js_langdef( "jobquote_Not_member" );
			fo3_add_js_langdef( "jobquote_My_job" );
			fo3_add_js_langdef( "jobquote_Not_active_subscriber" );
			fo3_add_js_langdef( "jobquote_Not_available" );

			fo3_add_js_langdef( "Cancel" );
			fo3_add_js_langdef( "Revoke" );
			fo3_add_js_langdef( "Revoke_job" );
		}


		public function render() {
			if ($this->sRendered) return $this->sRendered;
			if (!$this->job_id) {
				$this->setParam( "bValidJob" , false );
				return parent::render();
			}
			$this->initializeDatas();
			$this->generateLangDefs();

			if (!$this->job) {
				$this->setParam( "bValidJob" , false );
				return parent::render();
			}

			$title = sprintf( "<h1 class=\"dispin\">%s</h1>" , __("Jobs") );
			$this->setParam( "bOwnedByUser" , $this->bOwnedByUser );
			$this->setParam( "bCloseable" , $this->bCloseable );
			$this->setParam( "bRevokable" , $this->bRevokable );
			$this->setParam( "job" , $this->job );

			// setting params
			$p = array();
			foreach (array( "ord" , "ordd" , "pn" ) as $f)
				if (isset( $this->aG[$f] )) $p[$f]=$this->aG[$f];

			if ($this->bOwnedByUser) {
				$title.=sprintf( " <h2 class=\"dispin\"> - %s</h2>", __("jobpage_Job_owned_by_user" ) );
				$this->setParam( "url_my_jobs" , MyMemberURL::renderMyQuoteRequestsURL( $p ) );
			} else {
				$this->setParam( "url_jobs" , TranslatorJobsURL::renderURL( $p ) );
			}
			$this->oCommunityToolbar->setRawTitle( true );
			$this->oCommunityToolbar->setTitle( $title );

			$this->oCommunityToolbar->setSubTitle2( sprintf( "<h2>%s</h2>" , $this->job["job_title"] ) );
			$this->oCommunityToolbar->setCommunityLinks( false );
			$this->oCommunityToolbar->setIsSendPrint( false );

			$this->setParam( "bValidJob" , true );
			$this->setParam( "bPostLinkAvailable" , $this->bPostLinkAvailable );
			$this->setParam( "teasing_job_quote_type" , $this->renderTeasingJobQuoteType() );
			$this->setParam( "teasing_job_quote_link" , $this->renderTeasingJobQuoteLink() );
			return parent::render();
		}
	}
?>
