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

	class ForumSearchHitsPosts extends FrontendSuperGrid {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTitle = __("forum_Search_in_posts");
			$this->sTableName = "forum_posts";
			$this->sNoRows = __("forum_search_No_post_found" );
			$this->setCSSAttribute( "width" , "100%" );
			$this->sFilterField = "forum_search_text";
			$this->forum_model = new ForumModel();

			$this->_pagenum = "fppn";
			$this->_pagesize = "fpps";
			$this->_ordering = "fpo";
			$this->_orderingdir = "fpod";
		}

		public function constructColumns() {
			$w = 688;
			$w_poster_name = 140;$w-=$w_poster_name;
			$w_post = 270;$w-=$w_post;
			$w_ctstamp = 80;$w-=$w_ctstamp;
			$w_topic = $w;
			$this->addColumn( "poster_name" , "<b>".__("forumposts_Poster","attr")."</b>" , true , array( "width" => $w_poster_name ) );
			$this->addColumn( "post" , "<b>".__("forumposts_Post","attr")."</b>" , true , array( "width" => $w_post ) );
			$this->addColumn( "ctstamp" , "<b>".__("forumposts_Created_at","attr")."</b>" , true , array( "width" => $w_ctstamp ) );
			$this->addColumn( "topic" , "<b>".__("forumtopics_Title","attr")."</b>" , true , array( "width" => $w_topic ) );
		}

		protected function fieldsClause() {
			return
				"forum_posts.forum_post_id as id,
				forum_posts.forum_post_id,
				forum_posts.meta_ordering,
				forum_posts.forum_topic_id,
				forum_posts.poster_name,
				forum_posts.post,
				forum_posts.ctstamp";
		} 

		protected function whereClause() {
			$filters = array();
			$filters["moderated"]="not is_admin_moderated";

			$f = (isset( $this->aG[$this->sFilterField] )) ? $this->aG[$this->sFilterField] : "";
			if (strlen( $f )) {
				$f = FNTH_db_addslashes( $f );
				$ft = array();
				$ft[]=sprintf( "post ilike '%%%s%%'" , $f );
				$ft[]=sprintf( "poster_name ilike '%%%s%%'" , $f );
				$filters["post"]=sprintf( "(%s)" , implode( " or " , $ft ) );
			}

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

		protected function highlightRelevance( $original , $text ) {
			$real_original = $original;

			$pos = stripos( $original , $text );
			if ($pos===false) return FNTH_cut_string( $original , 32 , true );

			$startpos = $pos - 16;
			if ($startpos<0) $startpos = 0;

			$startpos = strrpos( substr( $original , 0 , $startpos ) , " " );
			if ($startpos<0) $startpos = 0;

			$o="";
			if ($startpos) $o.="... ";
			$o.=substr( $original , $startpos , 500 );
			$original = FNTH_cut_string( $o , 200 , true );
			$pos = stripos( $original , $text );

			$orig_pos = $pos;
			$offset = 0;
			$new = "";
			$len = strlen( $text );
			while ($pos!==false) {
				$new.=substr( $original , $offset , $pos - $offset );
				$new.="<b>";
				$new.=substr( $original , $pos , $len );
				$new.="</b>";
				$offset=$pos+$len;
				$pos = stripos( $original , $text , $offset );
			}
			$new.=substr( $original , $offset );
			return $new;
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];

			switch ($colname) {
			case "topic":
				$p = array();
				$topic = $this->forum_model->getTopicFromId( $rd["forum_topic_id"] );
				$url = ForumURL::renderURLFromTopicPostId( $rd["forum_post_id"] , $rd , $topic );
				$topic_name = $topic["forum_topic_title"];
				$val = sprintf( "<a class=\"nrm\" href=\"%s\">%s</a>",
					$url , $topic_name );
				$field->setValue( $val );
				break;
			case "poster_name":
			case "post":
				$field->setValue( $this->highlightRelevance( $rd[$colname] , $this->aG[$this->sFilterField] ) );
				break;
			case "ctstamp":
				$field->setAttribute( "align" , "center" );
			}
			return $field;
		}

		protected function orderClause() {
			$retval = "";
			switch ($this->sOrdering) {
			case "poster_name":
			case "post":
			case "ctstamp":
				$retval = $this->sOrdering;
				break;
			default:
				return " order by forum_post_id desc";break;
			}	
			if (empty( $retval )) return "";
			if (!$this->bAscending) $retval.=" desc";
			return " order by ".$retval;
		}
	}

?>
