<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/BackendGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSuperGridHeader.inc.php" );

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

	class AdminForumtopicsGridHeader extends AdminSuperGridHeader {

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
		}
	}

	class AdminForumtopicsGrid extends BackendGrid {

		protected function constructTable() {
			parent::constructTable();
			foreach ($this->aData as $rownum => $datarow) {
				$id = $this->oGridRows[$rownum]->getParentalId( 1 );
				FNTHPage::addUniquePageFullInit(
					sprintf( "AdminForumtopicsGridRowInit( '%s',%d,%d,aJSLangDefs );",
						$id,
						$rownum,
						$datarow["id"] )
				);
			}
		}

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "forum_topics";
			$this->sTitle = __("forumtopics_Topics");
			$this->setSelectable( true );
			$this->iPagesize = 20000;

			// $this->addToolbarButton( "filter" , __("Filter","attr") , "search",$this->renderStandardAction( "filter" ) );
			$this->addToolbarButton( "new" , __("New","attr") , "page_add" , $this->renderStandardAction( "new" ) );
			$this->addToolbarButton( "modify" , __("Modify","attr") , "page_edit" , $this->renderStandardAction( "modify" ) , false );
			$this->addToolbarButton( "delete" , __("Delete","attr") , "page_remove",$this->renderStandardAction( "delete" ) , false );
			FNTH_include_3rdparty( "contextmenu" );
			FNTH_include_3rdparty( "tooltip" );
		}

		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return new AdminForumtopicsGridHeader( $id , $parent );
		}

		protected function whereClause() {
			return "";
		}

		protected function orderClause() {
			return "order by forum_topic_meta_l";
		}

		protected function fieldsClause() {
			return "forum_topic_id as id,forum_topic_title,forum_topic_level,cache_number_of_posts,ctstamp,mtstamp";
		}

		public function constructColumns() {
			$this->addColumn( "forum_topic_title" , __("forumtopics_Title","attr") , true );
			$this->addColumn( "cache_number_of_posts" , __("forumtopics_Number_of_posts_sh","attr") , true );
			$this->addColumn( "ctstamp" , __("forumtopics_Created_at","attr") , true );
			$this->addColumn( "mtstamp" , __("forumtopics_Modified_at","attr") , true );
		}

		public function constructFilterHeader() {
			$headerrow = new FNTHGridRow( "filterrow" , $this , "header" );
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures

			$field = new FNTHGridField( "forum_topic_title" , $headerrow );
			// $this->forum_topic_title_filter = new AdminTextbox( "forum_topic_title_filter" , $field );
			$field = new FNTHGridField( "cache_number_of_posts" , $headerrow );
			$field = new FNTHGridField( "ctstamp" , $headerrow );
			$field = new FNTHGridField( "mtstamp" , $headerrow );
		}

		public function handleEvents() {
			// simple POST tasks
			if (isset( $this->aP["task"] )) {
				switch ($this->aP["task"]) {
				case "filter":
					$u = $this->aG;
					foreach ($this->aP as $k => $v) {
						if ($k=="task") continue;
						$u[$k]=$v;
					}
					unset( $u[ $this->getId()."_pagenum"] );
					FNTH_redirect( FNTH_render_url( $_SERVER["PHP_SELF"] , $u ) );
					fo_exit();
				}
			}

			// ajax events
			if (!isset( $this->aP["ajaxevent"] )) {
				return parent::handleEvents();
			}
			if (!isset( $this->aP["tbl"] )) {
				return parent::handleEvents();
			}
			if ($this->aP["tbl"]!=$this->getId()) {
				return parent::handleEvents();
			}
			switch ($this->aP["action"]) {
			case "delete":
				$lm = new ForumModel();
				foreach ($this->aP["ids"] as $id) $lm->deleteTopic( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "forum_topic_title":
				$padnum = 1 + ($this->aData[$rownum]["forum_topic_level"]-1)*2;
				$padding = str_repeat( "&raquo;" , $padnum ) . " ";
				
				$val =  sprintf( "%s<a class=\"green\" href=\"javascript:AdminForumtopicsGridModify( %d );\">%s</a>" ,
					$padding,
					$this->aData[$rownum]["id"],
					$this->aData[$rownum]["forum_topic_title"]
				);
				$field->setValue( $val );
				break;
			case "cache_number_of_posts":
				$field->setAttribute( "align" , "right" );
				break;
			case "ctstamp":
			case "mtstamp":
				$field->setValue(
					(isset($rd[$colname])) ? fo3_format_tstamp_to_screen( $rd[$colname] ) : "---"
				);
				$field->setAttribute( "align" , "center" );
				break;
			default:
			}
			return $field;
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "forumtopics_New_topic" );
			fo3_add_js_langdef( "forumtopics_Modify_topic" );
			fo3_add_js_langdef( "forumtopics_Delete_topic" );
			fo3_add_js_langdef( "forumtopics_confirm_Delete_topic" );
		}

		public function render() {
			$this->generateLangDefs();
			return parent::render();
		}
	}
?>
