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

	require_once( $GLOBALS["site_root"] . "/includes/models/Forum.inc.php" );

	class ForumTopicsGrid extends FrontendSuperGrid {
		public $forum_topic_id = 0;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "forum_topics";
			$this->iPagesize = 50;
			$this->setCSSAttribute( "width" , "100%" );
			$this->_pagenum = "pn";
			$this->_pagesize = "ps";
			$this->_ordering = "ord";
			$this->_orderingdir = "ordd";
			$this->forum_model = new ForumModel();
		}
		
		public function modifyGetPost( &$g , &$p ) {
			return parent::modifyGetPost( $g , $p );
		}

		protected function prepareDataset() {
			$page_title = "";
			if ($this->forum_topic_id) {
				// valami subtema
				$topic = $this->forum_model->getTopicFromId( $this->forum_topic_id );
				$page_title = sprintf( "%s - %s | %s",__("menu_Forum"),$topic["forum_topic_title"],GlobalsModel::getSiteName() );
			} else {
				$page_title = sprintf( "%s | %s" , __("menu_Forum") , GlobalsModel::getSiteName() );
			}
			fo3_set_page_title( $page_title );

			$ndata = array();
			foreach ($this->aData as $row) {
				// if (!isset( $row["last_poster_user_id"] )) {
					$row["last_poster_user_name"] = $this->forum_model->getForumTopicLastPosterName( $row["id"] );
				// }
				$ndata[]=$row;
			}
			$this->aData = $ndata;
		}

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

		protected function prepareUriArray( $input ) {
			$output = $input;
			unset( $output["uri"] );
			return $output;
		}

		protected function whereClause() {
			$retval = "";
			// forum subtopic
			$retval.=(!empty( $retval )) ? " and " : "";
			if ($this->forum_topic_id) {
				$retval.=sprintf( "forum_topic_parentid = %d" , $this->forum_topic_id );
			} else {
				$retval.=sprintf( "forum_topic_parentid is null" );
			}
			if (empty( $retval )) return "";
			return " where " . $retval;
		}
		
		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return null;
		}

		protected function fieldsClause() {
			return
				"forum_topic_id as id,
				forum_topic_title,
				forum_topic_aliasname,
				ctstamp,
				mtstamp,
				is_topic_post_thread,
				cache_number_of_posts,
				last_poster_user_id";
		}
		
		public function constructColumns() {
			$this->addColumn( "forum_topic_title" , "<b>".__("forumtopics_Title","attr"). "</b>", true );
			$this->addColumn( "cache_number_of_posts" , "<b>" .__("forumtopics_Number_of_posts","attr") ."</b>" , true , array( "width" => 50 , "align" => "center" ) );
			// $this->addColumn( "ctstamp" , "<b>" .__("forumtopics_Created_at","attr") . "</b>" , true );
			$this->addColumn( "mtstamp" , "<b>" .__("forumtopics_Modified_at","attr") . "</b>" , true , array( "width" => 140 , "align" => "center" ) );
			$this->addColumn( "muser" , "<b>" . __("forumtopics_Last_poster_user" , "attr" ) . "</b>" , false , array( "width" => 140, "align" => "center" ) );
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "ctstamp":
			case "mtstamp":
				if (!isset( $rd[$colname] )) {
					$field->setValue( "" );
					break;
				}
				$field->setValue( fo3_format_datetime_to_screen( $rd[$colname] ) );
				$field->setAttribute( "align" , "center" );	
				break;
			case "forum_topic_title":
				$uf = new ForumURL();
				$p = array();
				// FIXME = ep full breadcrumb path
				$uri = (isset( $this->aG["uri"] )) ? $this->aG["uri"] : "";
				$ep = $uri . $rd["forum_topic_aliasname"] . "/";
				$url = $uf->renderURL( $p , $ep );
				$field->setValue( sprintf( "<a class=\"nrm\" href=\"%s\" title=\"%s\">%s</a>",
					$url ,
					htmlspecialchars( $rd["forum_topic_title"] ),
					$rd["forum_topic_title"] ) );
				break;
			case "cache_number_of_posts":
				if ($rd["is_topic_post_thread"]=="f") {
					$field->setCSSAttribute( "text-align" , "center" );
					$field->setValue( sprintf( "%s: %d",
						__("forumtopics_Number_of_subtopics"),
						$this->forum_model->getNumberofSubtopics( $rd["id"] ) ) );
				} else {
					$field->setCSSAttribute( "text-align" , "right" );
				}
				break;
			case "muser":
				/*
				if (isset( $rd["last_poster_user_id"] ))
					$field->setValue( $rd["last_poster_user_id"] );
				else
				*/
					$field->setValue( $rd["last_poster_user_name"] );
				$field->setAttribute( "align" , "center" );
				break;
			}
			return $field;
		}
	}
?>
