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


	class ArticlesSearchHitsGrid extends FrontendSuperGrid {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTitle = __("Freetext_search_in_articles" );
			$this->sTableName = "larticles,articles";
			$this->sIdField = "article_id";
			$this->setAttribute( "class" , "ArticlesSearchHitsGrid FrontendSuperGrid" );
			$this->sNoRows=__("No_articles_with_the_phrase_given");
		}


		public function constructColumns( ) {
			$this->addColumn( "article" , "<b>" . __("Articles")  . "</b>" , false );
		}

		protected function freetextFilter() {
			$q = $this->aG["q"];
			$retval = array();
			$retval["lead"]=sprintf( "larticles.larticle_lead ilike '%%%s%%'" , FNTH_db_addslashes( $q ) );
			$retval["title"]=sprintf( "larticles.larticle_title ilike '%%%s%%'" , FNTH_db_addslashes( $q ) );
			$retval["body"]=sprintf( "larticles.larticle_body ilike '%%%s%%'" , FNTH_db_addslashes( $q ) );
			$retval = sprintf( "(%s)" , implode( " or " , $retval ) );
			return $retval;
		}

		public function whereClause() {
			$filters = array();
			$filters[]=sprintf( "articles.is_active" );
			$filters[]=sprintf( "larticles.is_active" );
			$filters[]=sprintf( "articles.article_category_id not in (1,5)" );
			$filters[]=sprintf( "articles.article_id=larticles.article_id" );
			$filters[]=sprintf( "site_language_id=%d" , $GLOBALS["langcode"] );
			$filters[]=$this->freetextFilter();

			return " where " . implode( " and " , $filters );
		}

		public function constructOrderingHeader() {}


		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "article":
				$field->setAttribute( "class" , "article" );
				$date = $rd["article_ctstamp"];
				if (isset( $rd["article_publication_tstamp"] )) $date = $rd["article_publication_tstamp"];
				if (isset( $rd["larticle_publication_tstamp"] )) $date = $rd["larticle_publication_tstamp"];

				$val = sprintf( "<a style=\"color: black; font-size: 14px;\" href=\"%s\"><b>%s</b></a> - %s<br/>",
					$rd["larticle_url"],
					$rd["larticle_title"],
					fo3_format_textual_date_to_screen( $date ) );
				$val.=sprintf( "%s" , $rd["larticle_lead"] );
				$field->setValue( $val );
				break;
			}
			return $field;
		}
	}

?>
