<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSuperGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/UserNameTag.inc.php" );

	class QuotesOfJobGrid extends FrontendSuperGrid {
		protected $job_id;
		protected $bIsJobOwner = false;
		
		protected function addSuperGridHeaderFactory( $id , $parent ) { return null; }
		
		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "job_quotes,members";
			$this->setCSSAttribute( "width" , "100%" );
			$this->_pagenum = "qpn";
			$this->_pagesize = "qps";
			$this->_ordering = "qord";
			$this->_orderingdir = "qordd";
			$this->job_model = new JobModel();
			$this->job_url = new JobURL();
			$this->mymember_model = new MyMemberModel();
		}

		public function setIsJobOwner( $v ) { $this->bIsJobOwner = $v; }


		protected function isJobOwnedByUser() {
			if (!$this->mymember_model->isUserLoggedIn()) return false;
			$this->job = $this->job_model->getJobFromId( $this->job_id , true );
			if ($this->mymember_model->getLoggedInUserId()==$this->job["poster_user_id"]) return true;
			return false;
		}

		public function modifyGetPost( &$g , &$p ) {
			$retval = parent::modifyGetPost( $g , $p );
			$this->bIsJobOwnedByUser = $this->isJobOwnedByUser();
			return $retval;
		}

		public function whereClause() {
			$filters = array();
			$filters[] = sprintf( "job_quotes.member_id=members.member_id" );
			$filters[] = sprintf( "job_quotes.job_id=%d" , $this->job_id );
			if ($this->bIsJobOwner) {
				// a sajatja
				$this->sNoRows = __("qojgrid_No_quotes");
			} else if ($this->mymember_model->isLoggedinMemberActiveSubscriber()) {
				// ha aktiv elofizeto, akkor szurjuk a sajatjaira
				$this->sNoRows = __("qojgrid_No_quotes_submitted_by_myself");
				$member_id = $this->mymember_model->getLoggedinMemberId();
				$filters[] = sprintf( "job_quotes.member_id=%d" , $member_id );
			} else {
				// nem aktiv elofizeto, es nem is a sajatja
				$this->sNoRows = __("qojgrid_No_quotes_submitted_by_myself");
				$member_id = $this->mymember_model->getLoggedinMemberId();
				$filters[] = sprintf( "job_quotes.member_id=%d" , $member_id );
			}
			return sprintf( " where %s" , implode( " and " , $filters ) );
		}

		public function fieldsClause() {
			return "job_quotes.job_quote_id as id,
				job_quotes.member_id,
				members.user_id,
				job_quotes.quote_pricing,
				job_quotes.quote,
				job_quotes.ctstamp,
				job_quotes.is_accepted,
				job_quotes.accepted_at";
		}

		public function setJobId( $job_id ) { $this->job_id = $job_id; }

		public function constructColumns() {
			if ($this->bIsJobOwnedByUser) {
				$this->addColumn( "poster_user" , "<b>" . __("qojgrid_Poster_user","attr") . "</b>" , false );
				$this->addColumn( "ctstamp" , "<b>" . __("qojgrid_Posted_at","attr")."</b>" , false );
			} else {
				$this->addColumn( "ctstamp" , "<b>" . __("qojgrid_Posted_at","attr")."</b>" , false );
			}
			$this->addColumn( "quote_pricing" , "<b>" . __("qojgrid_Price","attr") . "</b>" , false );
			$this->addColumn( "quote" , "<b>" . __("qojgrid_Details","attr") . "</b>" , false );
			if ($this->bIsJobOwnedByUser) 
				$this->addColumn( "admin" , "<b>" . __("qojgrid_Admin","attr")."</b>" , false );
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "ctstamp":
				$field->setValue( fo3_format_datetime_to_screen( $rd["ctstamp"] ) );
				break;
			case "poster_user":
				$field->setValue( "" );	// clear for child widget acceptance
				$member = new UsernameTag( sprintf( "member_%d",$rd["member_id"] ) , $field );
				$member->setId( $rd["user_id"] );
				break;
			case "quote":
				$val = FNTH_cut_string( $rd["quote"] , 40 , true );
				$val.="<br/>";
				$p = array();
				foreach (array( "ord" , "ordd" , "pn" , "qord" , "qordd" , "qpn" ) as $f)
					if (isset( $this->aG[$f] )) $p[$f]=$this->aG[$f];

				$url =$this->job_url->renderQuoteOfJobURL( $rd["id"] , $p );
				$val.=sprintf( "<a class=\"nrm\" href=\"%s\">%s &raquo;</a>",
					$url , __("details") );
				$field->setValue( $val );
				break;
			case "admin":
				$field->setAttribute( "align" , "center" );	
				if (isset( $rd["is_accepted"] )) {
					$field->setValue( ($rd["is_accepted"]=="t") ? 
						__("qojpage_accepted_job_quote") :
						__("qojpage_declined_job_quote") );
					break;
				}
				$prefix = $this->getId() . "_" . $rd["id"];

				$buttons = array();
				$buttons[]=$this->renderMiniButton(
					$prefix,
					"accept",
					__("qojpage_Accept_job_quote","attr"),
					"page_accept",
					$this->renderStandardAction(
						"accept" , array( "job_quote_id" => $rd["id"] )
					)
				);
				$buttons[]=$this->renderMiniButton(
					$prefix,
					"decline",
					__("qojpage_Decline_job_quote","attr"),
					"page_remove",
					$this->renderStandardAction(
						"decline" , array( "job_quote_id" => $rd["id"] )
					)
				);
				$field->setValue( implode( " " , $buttons ) );
				break;
			}
			return $field;
		}
	}
?>
