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

	class QuotesOfJobPane extends Widget {
		protected $oCommunityToolbar;
		protected $bCanViewQuotes;
		protected $bJobOwnedByUser;

		protected $model_mymember;
		protected $model_job;
		protected $job_id;
		protected $job;

		protected function canViewQuotes() {
			// ha o a tulaj, vagy belepett aktiv elofizeto, akkor igen, egyebkent nem
			if (!$this->model_mymember->isUserLoggedIn()) return false;
			$user_id = $this->model_mymember->getLoggedinUserId();
			$this->job_id = $this->aG["job_id"];
			$this->job = $this->model_job->getJobFromId( $this->job_id , true );
			$this->bJobOwnedByUser = $this->model_job->isJobOwnedByUser( $this->job_id , $user_id , $this->job );
			if ($this->bJobOwnedByUser) return true;	// ha az ove, akkor egyertelmu

			return $this->model_job->isJobQuoteAvailableForUser( $this->job_id , $user_id );
		}

		protected function isJobOwner() {
			if (!$this->model_mymember->isUserLoggedIn()) return false;
			$user_id = $this->model_mymember->getLoggedinUserId();
			if ($this->model_job->isJobOwnedByUser( $this->job_id , $user_id , $this->job )) return true;	// ha az ove, akkor egyertelmu
			
		}

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

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

			$this->bCanViewQuotes = $this->canViewQuotes();
			$this->bIsJobOwner = $this->isJobOwner();

			if ($this->bCanViewQuotes) {
				$this->oQuotesOfJobGrid = new QuotesOfJobGrid( "QuotesOfJobGrid" , $this );
				$this->oQuotesOfJobGrid->setJobId( $this->job_id );
				if ($this->bIsJobOwner) {
					$this->oQuotesOfJobGrid->setIsJobOwner( true );
					$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_Quotes_of_job" )."</h1>";
			// if (false) $title.="<h2 class=\"dispin\"> - ide ki tudunk irni valamit valamikor</h2>";
			$this->oCommunityToolbar->setTitle( $title );
			$this->oCommunityToolbar->setIsSendPrint( false );
			$this->oCommunityToolbar->setCommunityLinks( false );
			$this->oCommunityToolbar->setSubTitle2(
				sprintf( "<h2>%s</h2>" , $this->job["job_title"] ) );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->initializeWidgets();
			$this->setParam( "bCanViewQuotes" , $this->bCanViewQuotes );
			$this->setParam( "bIsJobOwner" , $this->bIsJobOwner );
			if ($this->bIsJobOwner) {
				$turl = new TranslatorJobsURL();
				$p = array( "job_id" => $this->aG["job_id"] );
				foreach (array( "ord" , "ordd" , "pn" ) as $f)
					if (isset( $this->aG[$f] )) $p[$f]=$this->aG[$f];
				$url = $turl->renderURL( $p );
				$this->setParam( "url_job" , $url );
			}
			return parent::render();
		}

	}

?>
