<?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/QuoteDetails.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.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/JobQuote.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/QuoteOfJobHandler.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/urlgen/Blueboard.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/urlgen/TranslatorJobs.inc.php" );

	class QuoteOfJobPane extends Widget {
		protected $oCommunityToolbar;
		protected $bCanViewQuote;
		protected $bJobOwnedByUser;
		protected $bJobQuoteOwnedByMember;
		protected $bJobQuoteDecided;

		protected $model_mymember;
		protected $model_job;
		protected $job_quote_id = 0;
		protected $job_quote = array();
		protected $job_id = 0;
		protected $job = false;

		protected function canViewQuote() {
			$this->job_quote_id = $this->aG["job_quote_id"];
			$this->job_quote = $this->model_jobquote->getJobQuoteFromJobQuoteId( $this->job_quote_id );
			if (!$this->model_mymember->isUserLoggedIn()) return false;
			$user_id = $this->model_mymember->getLoggedinUserId();
			if (!$this->job_quote) return;

			$this->job_id = $this->job_quote["job_id"];
			$this->job = $this->model_job->getJobFromId( $this->job_id , true );

			// init
			$this->bJobOwnedByUser = false;
			$this->bJobQuoteOwnedByMember = false;

			// a tulaj siman megnezheti
			$this->bJobOwnedByUser = $this->model_job->isJobOwnedByUser( $this->job_id , $user_id , $this->job );
			if ($this->bJobOwnedByUser) return true;

			// elofizeto es o is a quote tulajdonosa
			$this->bJobQuoteOwnedByMember = ($this->model_mymember->getLoggedInMemberId()==$this->job_quote["member_id"]);
			if ($this->bJobQuoteOwnedByMember) return true;

			// ha nem aktiv elofizeto nem nezheti meg
			if (!$this->model_mymember->isLoggedInMemberActiveSubscriber()) return false;
			// minden mas esetben elhajtjuk, mint a nem kivant gyereket
			return false;
		}

		protected function isJobQuoteDecided() {
			return isset( $this->job_quote["is_accepted"] );
		}

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->model_mymember = new MyMemberModel();
			$this->model_jobquote = new JobQuoteModel();
			$this->model_job = new JobModel();
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );
		}

		public function modifyGetPost( &$g , &$p ) {
			$this->model_mymember->hashLogin( $g );

			$this->bCanViewQuote = $this->canViewQuote();
			$this->bJobQuoteDecided = $this->isJobQuoteDecided();
			if ($this->bCanViewQuote) {
				$this->oJobDetails = new JobDetails( "JobDetails" , $this );
				$this->oJobDetails->setJobData( $this->job );
				$this->oQuoteDetails = new QuoteDetails( "QuoteDetails" , $this );
				$this->oQuoteDetails->setJobQuoteData( $this->job_quote );
				$this->oQuoteOfJobHandler = new QuoteOfJobHandler( "QuoteOfJobHandler" , $this );
			} else {
				$this->oSystemArticle = new SystemArticle( "ErrorArticle" , $this , "sys_content_not_permitted" );
				$this->oSystemArticle->setOnlyBody( true );
			}
		}

		protected function initializeWidgets() {
			$this->oCommunityToolbar->setRawTitle( true );
			$title = "<h1 class=\"dispin\">".__("qojpage_Quote_of_job" )."</h1>";
			if ($this->bJobQuoteOwnedByMember)
				$title.=sprintf( "<h2 class=\"dispin\"> - %s</h2>", __("qojpage_My_quote") );
			$this->oCommunityToolbar->setTitle( $title );
			$this->oCommunityToolbar->setIsSend( false );
			$this->oCommunityToolbar->setCommunityLinks( false );
			if ($this->job && $this->bCanViewQuote)
				$this->oCommunityToolbar->setSubTitle2(
					sprintf( "<h2>%s</h2><a class=\"nrm\" href=\"javascript:QOJToggleJobDetails();\">%s &raquo;</a>",
					$this->job["job_title"],
					__("qojpage_Job_details") ) );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->initializeWidgets();
			$this->setParam( "bCanViewQuote" , $this->bCanViewQuote );
			$this->setParam( "bJobOwnedByUser" , $this->bJobOwnedByUser );
			$this->setParam( "bJobQuoteDecided" , $this->bJobQuoteDecided );
			$this->setParam( "bJobQuoteOwnedByMember" , $this->bJobQuoteOwnedByMember );
			$this->job_url = new JobURL();
			// url_quotes_of_job
			if (isset( $this->oQuoteDetails ))
				$this->oQuoteDetails->setJobQuoteOwnedByMember( $this->bJobQuoteOwnedByMember );
			$p = array();
			foreach (array( "ord" , "ordd" , "pn" , "qord" , "qordd" , "qpn" ) as $f)
				if (isset( $this->aG[$f] )) $p[$f]=$this->aG[$f];
			$url_quotes_of_job = $this->job_url->renderQuotesOfJobURL( $this->job_id , $p );
			$this->setParam( "url_quotes_of_job" , $url_quotes_of_job );
			if (!$this->bJobOwnedByUser) {
				$this->setParam( "url_job" , TranslatorJobsURL::renderURL( array( "job_id" => $this->job_id ) ) );
				$um = new UserModel();
				$user = $um->getUserFromUserId( $this->job["poster_user_id"] );
				$this->setParam( "job_poster_name",  $user["user_name"] );
				$this->setParam( "url_job_poster_feedback" , BlueboardURL::renderURL( array( "user_id" => $user["user_id"] ) ) );
			}

			$this->setParam( "job_quote_id" , $this->job_quote["job_quote_id"] );
			return parent::render();
		}

	}

?>
