<?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/AdminTristate.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSuperGridHeader.inc.php" );

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

	class AdminForumpostsGridHeader extends AdminSuperGridHeader {
		protected $oKeywordFilter;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oForumTopicFilter = new AdminSelect( "forum_topic_filter" , $this );
			$this->oForumTopicFilter->setTitle( __("forumtopics_Title") );
			$this->oForumTopicFilter->setShowTitle( true );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$fm = new ForumModel();
			$this->oForumTopicFilter->setOptions( $fm->getForumSubtopicOptions() );
			return parent::render();
		}
	}

	class AdminForumpostsGrid extends BackendGrid {
		protected $username_filter;
		protected $fullname_filter;
		protected $current_status_filter;

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

			$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 AdminForumpostsGridHeader( $id , $parent );
		}

		protected function whereClause() {
			$filters = array();
			$filters[]="forum_posts.forum_topic_id=forum_topics.forum_topic_id";

			// topic filter
			$forum_topic_id = (isset( $this->aG["forum_topic_filter"] ) && $this->aG["forum_topic_filter"]>0) ? $this->aG["forum_topic_filter"] : 0;
			if ($forum_topic_id) $filters[]=sprintf( "forum_posts.forum_topic_id=%d" , $forum_topic_id );
			// poster name filter
			$poster_name_filter = $this->poster_name_filter->getValue();
			if (!empty( $poster_name_filter )) {
				$filters[]=sprintf( "forum_posts.poster_name ilike '%%%s%%'" , FNTH_db_addslashes( $poster_name_filter ) );
			}
			
			// poster_name filter
			$f = $this->poster_name_filter->getValue();
			if (strlen( $f ))
				$filters[]=sprintf( "forum_posts.poster_name ilike '%%%s%%'" , FNTH_db_addslashes( $f ) );

			// post filter
			$f = $this->post_filter->getValue();
			if (strlen( $f ))
				$filters[]=sprintf( "forum_posts.post ilike '%%%s%%'" , FNTH_db_addslashes( $f ) );

			// is_admin_acknowledged
			$is_admin_acknowledged_filter = $this->is_admin_acknowledged_filter->getValue();
			if (strlen( $is_admin_acknowledged_filter )) {
				$is_admin_acknowledged_filter = (int)$is_admin_acknowledged_filter;
				$filters[]=sprintf( "%sforum_posts.is_admin_acknowledged",
					($is_admin_acknowledged_filter) ? "" : "not " );
			}
			// is_admin_moderated
			$is_admin_moderated_filter = $this->is_admin_moderated_filter->getValue();
			if (strlen( $is_admin_moderated_filter )) {
				$is_admin_moderated_filter = (int)$is_admin_moderated_filter;
				$filters[]=sprintf( "%sforum_posts.is_admin_moderated",
					($is_admin_moderated_filter) ? "" : "not " );
			}
			if (empty( $filters )) return "";
			return " where " . implode( " and " , $filters );
		}
		
		protected function orderClause() {
			if (isset( $this->aP["inajax"] )) return "";	// ajaxban minek rendezzunk
			$retval = "";
			if (empty( $this->sOrdering)) return " order by forum_post_id desc";
			switch ($this->sOrdering) {
			case "ctstamp":
			case "poster_name":
			case "post":
				$retval = $this->sOrdering;
				break;
			default:
				break;
			}
			if (empty( $retval )) return $retval;
			if (!$this->bAscending) $retval.=" desc";
			return " order by " . $retval;
		}

		protected function fieldsClause() {
			return "forum_posts.forum_post_id as id,
				forum_posts.meta_post_number,
				forum_topics.forum_topic_title,
				forum_posts.poster_name,
				forum_posts.poster_email,
				forum_posts.ctstamp,
				forum_posts.post,
				forum_posts.ipaddress,
				forum_posts.hostname,
				forum_posts.is_admin_acknowledged,
				forum_posts.is_admin_moderated";
		}

		public function constructColumns() {
			$forum_topic_id = (isset( $this->aG["forum_topic_filter"] ) && $this->aG["forum_topic_filter"]>0) ? $this->aG["forum_topic_filter"] : 0;
			$this->addColumn( "meta_post_number" , "#" , true , array( "width" => 50 ) );
			$this->addColumn( "poster_name" , __("forumposts_Poster","attr") , true );
			$this->addColumn( "ctstamp" , __("forumposts_Created_at","attr") , true );
			$this->addColumn( "post" , __("forumposts_Post","attr") ,true );
			if (!$forum_topic_id)
				$this->addColumn( "forum_topic_title" , __("forumtopics_Title","attr") , true );
			$this->addColumn( "is_admin_acknowledged" , __("forumposts_Admin_acknowledged","attr") , true );
			$this->addColumn( "is_admin_moderated" , __("forumposts_Admin_moderated","attr") , true );
			$this->addColumn( "admin" , __("Admin","attr") , false );
		}

		public function constructFilterHeader() {
			$forum_topic_id = (isset( $this->aG["forum_topic_filter"] ) && $this->aG["forum_topic_filter"]>0) ? $this->aG["forum_topic_filter"] : 0;

			$headerrow = new FNTHGridRow( "filterrow" , $this , "header" );
			foreach ($this->aColumns as $colname => $column) {
				$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
				switch ($colname) {
				case "poster_name":
					$this->poster_name_filter = new AdminTextbox( "poster_name_filter" , $field );
					$this->addOnEnterCallbackToWidget( $this->poster_name_filter );
					break;
				case "post":
					$this->post_filter = new AdminTextbox( "post_filter" , $field );
					$this->addOnEnterCallbackToWidget( $this->post_filter );
					break;
				case "forum_topic_title":
					if (!$forum_topic_id) {
						$this->forum_topic_title_filter = new AdminTextbox( "forum_topic_title_filter" , $field );
						$this->addOnEnterCallbackToWidget( $this->forum_topic_title_filter );
					}
					break;
				case "is_admin_acknowledged":
					$field->setAttribute( "align" , "center" );
					$this->is_admin_acknowledged_filter = new AdminTristate( "is_admin_acknowledged" , $field );
					break;
				case "is_admin_moderated":
					$field->setAttribute( "align" , "center" );
					$this->is_admin_moderated_filter = new AdminTristate( "is_admin_moderated_filter" , $field );
					break;
				}
			}
			return $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 "moderate":
				$fm = new ForumModel();
				$fm->moderateTopicPost( $this->aP["forum_post_id"] , true );
				$this->refreshGrid();
				break;
			case "unmoderate":
				$fm = new ForumModel();
				$fm->moderateTopicPost( $this->aP["forum_post_id"] , false );
				$this->refreshGrid();
				break;
			case "delete":
				// FIXME: delete the user
				FNTH_direct_echo( FNTH_embed_js_script( 
					"alert( 'deleted' );" .
					"document.location.href=document.location.href;" ) );
				break;
			}
			fo_exit();
		}

		protected function renderPostField( $field , $rd ) {
			$val = sprintf( "<div id=\"post_%d\" class=\"post\">%s</div>",
				$rd["id"],
				nl2br( $rd["post"] ) );
			// FNTHPage::addUniquePageFullInit( sprintf( "InitForumpost( 'post_%d' );" , $rd["id"] ) );
			return $val;
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "poster_name":
				$val="";
				$val.=  sprintf( "<a class=\"green\" href=\"javascript:AdminForumpostsGridModify( %d );\">%s</a>" , $rd["id"] , $rd["poster_name"] );
				$field->setValue( $val );
				break;
			case "ctstamp":
				$field->setValue( (isset($rd[$colname])) ? fo3_format_tstamp_to_screen( $rd[$colname] ) : "---" );
				$field->setAttribute( "align" , "center" );
				break;
			case "post":
				$field->setValue( $this->renderPostField( $field , $rd ) );
				break;
			case "is_admin_acknowledged":
			case "is_admin_moderated":
				$field->setAttribute( "align" , "center" );
				$field->setValue( fo3_render_boolean_yesno( $rd[$colname] ) );
				break;
			case "username":
				$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminForumpostsGridModify( %d );\">%s</a>" , $this->aData[$rownum]["id"] , $this->aData[$rownum]["username"] );
				$field->setValue( $val );
				break;
			case "meta_post_number":
				$field->setAttribute( "align" , "right" );
				break;
			case "admin":
				$buttons = array();
				$prefix = $this->getId() . "_" . $rd["id"];
				switch ($rd["is_admin_moderated"]) {
				case "f":
					$buttons[]=$this->renderMiniButton(
						$prefix,
						"moderate",
						__("Moderate_forum_post","attr"),
						"page_remove",
						$this->renderStandardAction(
							"moderate" , array( "forum_post_id" => $rd["id"] )
						)
					);
					break;
				case "t":
					$buttons[]=$this->renderMiniButton(
						$prefix,
						"unmoderate",
						__("Unmoderate_forum_post","attr"),
						"page_add",
						$this->renderStandardAction(
							"unmoderate" , array( "forum_post_id" => $rd["id"] )
						)
					);
					break;
				}
				$field->setAttribute( "align" , "center" );
				$field->setValue( implode( " " , $buttons ) );
				break;
			default:
			}
			return $field;
		}

	}

?>
