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

	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Job.inc.php" );
	
	class MyQuotesGrid extends FrontendSuperGrid {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "jobs,job_quotes";
			$this->iPagesize = 10;
			$this->setCSSAttribute( "width" , "100%" );
			$this->_pagenum = "pn";
			$this->_pagesize = "ps";
			$this->_ordering = "ord";
			$this->_orderingdir = "ordd";
			$this->job_url = new JobURL();
		}

		public function modifyGetPost( &$g , &$p ) {
			if (!isset( $g[$this->_ordering] )) {
				$g[$this->_ordering]="quote_tstamp";
				$g[$this->_orderingdir]="desc";
			}
			return parent::modifyGetPost( $g , $p );
		}
		
		protected function orderClause() {
			$retval = "";
			switch ($this->sOrdering) {
			case "job_title":$retval="jobs.job_title";break;
			case "job_quote_deadline":$retval="jobs.job_quote_deadline";break;
			case "job_deadline":$retval="jobs.job_deadline";break;
			case "quote_tstamp":$retval="job_quotes.ctstamp";break;
			}
			if (empty( $retval )) return "";
			if (!$this->bAscending) $retval.=" desc";
			return " order by ".$retval;
		}


		protected function whereClause() {
			$mymember = new MyMemberModel();
			$retval = " where ";
			$retval.= sprintf( "jobs.job_id=job_quotes.job_id and ");
			$retval.= sprintf( "job_quotes.member_id=%d" , $mymember->getLoggedinMemberId() );
			return $retval;
		}


		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return null;
		}
		
		public function constructColumns() {
			$this->addColumn( "job_title" , __("myquotespage_Title","attr") , true );
			$this->addColumn( "job_quote_deadline" , __("myquotespage_Quote_deadline","attr") , true );
			$this->addColumn( "job_deadline" , __("myquotespage_Deadline","attr") , true );
			$this->addColumn( "result" , __("myquotespage_Result","attr") , false );
			$this->addColumn( "quote_tstamp" , __("myquotespage_Quote_timestamp","attr") , true );
			$this->addColumn( "quoteinfo" , __("myquotespage_Quote_information","attr") , false );
		}

		protected function fieldsClause() {
			return "jobs.job_id as id,
				jobs.job_title,
				jobs.job_quote_deadline,
				jobs.job_deadline,
				job_quotes.ctstamp as quote_tstamp,
				job_quotes.job_quote_id,
				job_quotes.quote,
				job_quotes.is_accepted,
				job_quotes.accepted_at";
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "job_quote_deadline":
			case "job_deadline":
				$field->setCSSAttribute( "text-align" , "center" );
				break;
			case "quote_tstamp":
				$field->setValue( fo3_format_datetime_to_screen( $rd["quote_tstamp"] ) );
				break;
			case "result":
				$field->setAttribute( "align" , "center" );
				if (!isset( $rd["is_accepted"] )) {
					$field->setValue( __("myquotespage_response_not_arrived") );
				} else {
					$field->setValue( ($rd["is_accepted"]=="t") ?
						__("myquotespage_response_accepted") :
						__("myquotespage_response_declined") );
				}
				break;
			case "quoteinfo":
				$val = FNTH_cut_string( $rd["quote"] , 40 , true );
				$val.="<br/>";
				$url =$this->job_url->renderQuoteOfJobURL( $rd["job_quote_id"] );
				$val.=sprintf( "<a class=\"nrm\" href=\"%s\">%s &raquo;</a>",
					$url , __("details") );
				$field->setValue( $val );
				break;
			}
			return $field;
		}
	}

?>
