<?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/models/Job.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/TranslatorJobs.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Job.inc.php" );
	
	class JobsGrid extends FrontendSuperGrid {
		protected $job_model;
		protected $mymember_model;

		protected $aFilters = array();

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "jobs";
			$this->setCSSAttribute( "width" , "100%" );
			$this->_pagenum = "pn";
			$this->_pagesize = "ps";
			$this->_ordering = "ord";
			$this->_orderingdir = "ordd";
			$this->job_model = new JobModel();
			$this->job_url = new JobURL();
			$this->mymember_model = new MyMemberModel();
			$this->sNoRows = __("jobsgrid_No_active_job_exists");

			$this->setFilter( "currents" , $this->job_model->getJobFilter( "currents" ) );
		}
		
		public function modifyGetPost( &$g , &$p ) {
			return parent::modifyGetPost( $g , $p );
		}

		public function setFilter( $filter_name , $filter ) {
			$this->aFilters[$filter_name]=$filter;
		}

		public function unsetFilter( $filter_name ) {
			unset( $this->aFilters[$filter_name] );
		}

		protected function prepareDataset() {
			// job ids
			$job_ids=array();
			if (!empty( $this->aData )) {
				foreach ($this->aData as $row) $job_ids[] = $row["id"];
				$query = sprintf( "select job_id,count(*) as num from job_quotes where job_id in (%s) group by job_id",implode( ",",$job_ids ) );
				$job_quotes = FNTH_db_get_associative( $query , "job_id" , "num" );
			}

			$user_id = $this->mymember_model->getLoggedinUserId();
			$member_id = $this->mymember_model->getLoggedinMemberId();

			$d = array();
			foreach ($this->aData as $row) {
				$item=$this->job_model->getJobFromId( $row["id"] , true );
				$item["id"]=$item["job_id"];
				$item["job_details"]="";
				$item["quoteinfo"]="";
				$item["owned"]=$this->job_model->isJobOwnedByUser( $item["job_id"] , $user_id , $item );
				if ($member_id)
					$item["is_member_type"]=$this->job_model->isJobMemberType( $item["job_id"] , $member_id , $item );
				$item["numofquotes"]=(isset($job_quotes[$row["id"]])) ? $job_quotes[$row["id"]] : 0;
				$d[]=$item;
			}
			$this->aData = $d;
		}
		
		protected function orderClause() {
			$retval = "";
			if (empty( $this->sOrdering )) return " order by id desc";
			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;
			}
			if (empty( $retval )) return "";
			if (!$this->bAscending) $retval.=" desc";
			return " order by ".$retval;
		}
		
		protected function whereClause() {
			if (empty( $this->aFilters )) return "";
			return " where " . implode( " and " , $this->aFilters );
		}
		
		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return null;
		}
		
		public function constructColumns() {
			$this->addColumn( "job_title" , "<b>" . __("jobspage_Title","attr") . "</b>" , true );
			$this->addColumn( "job_details" , "<b>" . __("jobspage_Details","attr") . "</b>" , false );
			$this->addColumn( "quoteinfo" , "<b>". __("jobspage_Quotes","attr") ."</b>" , false );
		}
		
		protected function fieldsClause() {
			return "jobs.job_id as id";
		}

		public function constructRow( $datarow , $rownum ) {
			$retval = parent::constructRow( $datarow , $rownum );
			$col = SystemParametersModel::getVar( "job_bgcolor_permitted" );
			if ($datarow["owned"]) {
				// $retval->setAttribute( "class" , "rowhl" );
				$col = SystemParametersModel::getVar( "job_bgcolor_owned" );
			} else if (
				(isset( $datarow["is_member_type"] ) && !$datarow["is_member_type"]) ||
				$datarow["is_closed"]=="t" ||
				$datarow["is_revoked"]=="t" ||
				$datarow["job_quote_deadline"]<fo3_date("Y-m-d H:i:s")


				) {
				$col = SystemParametersModel::getVar( "job_bgcolor_disabled" );
				$retval->setAttribute( "class" , "rowdisabled" );
			} else {
				if ($datarow["job_recipients"]["type"]=="custom")
					$col = SystemParametersModel::getVar( "job_bgcolor_special" );
			}
			$retval->setAttribute( "style" , sprintf( "background-color: #%s;" , $col ) );
			return $retval;
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$field->setAttribute( "class", "tdtop" );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "job_title":
				$turl = new TranslatorJobsURL();
				$p = array( "job_id" => $rd["id"] );
				foreach (array( "ord" , "ordd" , "pn" ) as $f)
					if (isset( $this->aG[$f] )) $p[$f]=$this->aG[$f];
				$url = $turl->renderURL( $p );

				$val = sprintf( "<a class=\"nrm\" href=\"%s\">%s</a>", $url , $rd["job_title"] );
				$val.="<br/>";
				$val.=sprintf( "<span class=\"posted_at\">%s:</span> %s",
					__("jobspage_Posted_at"),
					fo3_format_datetime_to_screen( $rd["ctstamp"] ) );

				$status = $this->job_model->getJobStatusString( $rd );
				$val.=sprintf( " | <b>%s</b>" , $status );
				$field->setValue( $val );
				break;
			case "job_description":
				// default
				break;
			case "job_details":
				$field->setAttribute( "class" , "at" );
				$val = sprintf( "<span class=\"deadline\">%s: %s</span>",
					__("jobspage_Deadline"),
					fo3_format_datetime_to_screen( $this->aData[$rownum]["job_deadline"] ) );
				$val.="<br/>";
				$job = $this->aData[$rownum];
				$val.=sprintf( "<span class=\"recipients\">%s:</span> %s",
					__("jobspage_Recipients"),
					fo3_render_job_recipients( $job["job_recipients"] , false ) );
				$field->setValue( $val );
				break;
			case "quoteinfo":
				$numofquotes = $this->aData[$rownum]["numofquotes"];
				$val = sprintf( "<span class=\"numofquotes\">%s:</span> %d",
					__("jobspage_Number_of_quotes"),
					$numofquotes );
				$p = array();
				foreach (array( "ord" , "ordd" , "pn" ) as $f)
					if (isset( $this->aG[$f] )) $p[$f]=$this->aG[$f];
				$url = $this->job_url->renderQuotesOfJobURL( $this->aData[$rownum]["job_id"] , $p );
				if ($numofquotes) {
					$val.="<br/>";
					$val.=sprintf( "<a href=\"%s\" class=\"nrm\">%s &raquo;",
						$url,
						__( "details" ) );
				}
				$field->setValue( $val );
				break;
			}
			return $field;
		}

	}

?>
