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

	class ForumTopicPostsPager extends FrontendSuperGridPager {
		protected function renderPagerUrl( $pagenum , $actual = false ) {
			if ($actual) {
				return sprintf( "<b>[%d]</b>" , $pagenum+1 );
			}
			$grid = $this->oParent->oParent->oParent;
			$u = $this->aG;
			unset( $u["uri"] );
			$u[$grid->_pagenum]=$pagenum;
			return sprintf( "<a href=\"%s\">[%d]</a>",
				FNTH_render_url( FNTH_strip_url_params( $_SERVER["REQUEST_URI"] ) , $u , true ),
				$pagenum+1 );
		}

	}

	class ForumTopicPosts extends FrontendSuperGrid {
		public $forum_topic_id = 0;
		protected $set_title = true;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "forum_posts";
			$this->iPagesize = 10;
			$this->setCSSAttribute( "width" , "100%" );
			$this->_pagenum = "pn";
			$this->_pagesize = "ps";
			$this->forum_model = new ForumModel();
			$this->seltrans = new SelectedTranslators( "selected_translators" , $this );
		}

		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() );
			}
			if ($this->set_title) fo3_set_page_title( $page_title );

			$ds = $this->aData;
			$parentids = array();
			$parentposts = array();
			foreach ($ds as $row) if (isset( $row["forum_post_parentid"] )) $parentids[]=$row["forum_post_parentid"];
			if (!empty( $parentids )) {
				$query = sprintf( "select forum_post_id,meta_ordering from forum_posts where forum_post_id in (%s)" , implode( "," , $parentids ) );
				$pds = FNTH_db_get_resultset( $query );
				foreach ($pds as $row) $parentposts[$row["forum_post_id"]]=$row;
			}
			
			// double dataset
			$retval = array();
			foreach ($ds as $row) {
				if (isset( $row["forum_post_parentid"] )) {
					if (isset( $parentposts[$row["forum_post_parentid"]] )) {
						$row["parent_post_meta_ordering"]=$parentposts[$row["forum_post_parentid"]]["meta_ordering"];
					}
				}
				$retval[]=$row;
				$retval[]=$row;
			}
			$this->aData = $retval;
		}
		
		protected function addSuperGridPagerFactory( $id , $parent ) {
			if ($this->oSuperGridPager) return $this->oSuperGridPager;
			$this->oSuperGridPager = new ForumTopicPostsPager( $id , $parent );
			$this->oSuperGridPager->setPageSize( $this->iPagesize );
			return $this->oSuperGridPager;
		}

		protected function whereClause() {
			// forum topic id
			$retval = sprintf( "forum_topic_id=%d" , $this->forum_topic_id );
			// not moderated
			$retval.= " and not is_admin_moderated";

			if (empty( $retval )) return "";
			return " where " . $retval;
		}

		protected function fieldsClause() {
			return "forum_post_id as id,ctstamp,forum_post_parentid,poster_name,owner_user_id,post,meta_ordering,meta_post_number,'forum_left' as forum_left,'forum_right' as forum_right";
		}

		protected function orderClause() {
			return " order by meta_ordering desc";
		}
		
		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return null;
		}
		
		public function constructColumns() {
			$this->addColumn( "forum_left" , __("forumspage_Left","attr") , true );
			$this->addColumn( "forum_right" , __("forumspage_Right","attr") , true );
		}

		public function constructOrderingHeader() {

		}

		protected function constructRow( $datarow , $rownum ) {
			$realrownum = $rownum / 2;
			$header = !($rownum%2);
			$tablerow = $this->addRowFactory( sprintf( "row%d%s" , $datarow["id"] , ($header) ? "_hdr" : "_data" ) , $this );
			$tablerow->setAttribute( "class" , ($header) ? "row0" : "data0" );
			return $tablerow;
		}
		
		protected function generateParentPostUrl( $rd ) {
			$current_postnum = $this->iNumofrecords - $rd["meta_ordering"];
			$current_pagenum = (int)($current_postnum/$this->iPagesize);
			$parent_postnum = $this->iNumofrecords - $rd["parent_post_meta_ordering"];
			$parent_pagenum = (int)($parent_postnum/$this->iPagesize);
			/*
			FNTH_da( sprintf( "id:%d pid:%d cn:%d cp:%d pn:%d pp:%d",
				$rd["id"],
				$rd["forum_post_parentid"],
				$current_postnum , $current_pagenum , $parent_postnum , $parent_pagenum ) , false );
			*/
			if ($current_pagenum==$parent_pagenum)
				return sprintf( "#forum_post_%d" , $rd["forum_post_parentid"] );
			$u = $this->aG;
			unset( $u["uri"] );
			$u["pn"]=$parent_pagenum;
			$url = FNTH_render_url( $_SERVER["REQUEST_URI"] , $u );
			return $url.sprintf( "#forum_post_%d" , $rd["forum_post_parentid"] );
		}

		public function constructField( $rownum , $row , $colname ) {
			$header = !($rownum%2);
			$realrownum = (int)($rownum/2);
			// prefilter
			switch ($colname) {
			case "forum_left":
				break;
			case "forum_right":
				if (!$header) return false;
				break;	
			}
			$field = parent::constructField( $rownum , $row , $colname );
			$field->setAttribute( "class" , "forumcell" );
			$rd = $this->aData[$rownum];

			switch ($colname) {
			case "forum_left":
				if (!$header) {
					$field->setAttribute( "colspan" , "2" );
					$field->setValue( nl2br( $rd["post"] ) . "<br/><br/>" );
					break;
				}
				if (isset( $rd["owner_user_id"] )) {
					$user = new UserNameTag( "poster_user_".$rd["id"] , $field );
					$user->setId( $rd["owner_user_id"] );
					$field->setValue( "" );
				} else {
					$field->setValue( $rd["poster_name"] );
				}
				break;
			case "forum_right":
				// post number
				$val = sprintf( "<a id=\"forum_post_%d\">#%d</a>" , $rd["id"] , $rd["meta_post_number"] );
				// start timestamp
				$val = substr( $rd["ctstamp"] , 0 , 19 ) . " " . $val;
				// reply ha van
				if (isset( $rd["parent_post_meta_ordering"] )) {
					$hljs=sprintf( "HighlightTopicPost( '%s' , %d );" , $this->getId() , $rd["forum_post_parentid"] );
					$val = sprintf( "<a class=\"nrm\" href=\"%s\" onclick=\"%s\">%s</a> %s" ,
						$this->generateParentPostUrl( $rd ),
						$hljs,
						__("forumposts_Parent_post") , $val );
				}

				$val = sprintf( "<a class=\"nrm\" href=\"#lnk_forum_post_form\" onclick=\"ForumPostReply( %d , %d );\">%s</a> ",
					$rd["id"], $rd["meta_post_number"],
					__("forum_post_reply") ) . $val;

				$field->setAttribute( "class" , "forumlinks" );
				$field->setValue( $val );
				break;
			}
			return $field;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$vp = ForumModel::canViewPosts( $this->forum_topic_id );
			if ($vp) {
				FNTH_include_3rdparty( "localscroll" );
				FNTHPage::addUniquePageFullInit( sprintf( "InitForumTopicPosts( '%s' );" , $this->getId() ) );
				return parent::render();
			}

			$mm = new MyMemberModel();
			if (!$mm->isMemberLoggedin()) {
				$this->setParam( "message" , __("forumposts_Have_to_login") );
			} else if (!$mm->isloggedinMemberActiveSubscriber()) {
				$this->setParam( "message" , __("forumposts_Have_to_be_an_active_subscriber") );
			}

			$filename = $this->getTemplateFilename( ".empty" );
			$this->sRendered = $this->renderFromTemplate( $filename );
			return $this->sRendered;
		}

	}
?>
