<?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/AdminUserSelector.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/widgets/AdminUserNameTag.inc.php" );

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

	class AdminFeedbacksGridHeader extends AdminSuperGridHeader {
		protected $oKeywordFilter;

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


		protected function generateLangDefs() {
			fo3_add_js_langdef( "blueboardpage_confirm_Delete_feedback" );
		}

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

	class AdminFeedbacksGrid extends BackendGrid {
		protected $is_admin_acknowledged_filter;
		protected $is_proofread_filter;

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

			$this->addToolbarButton( "filter" , __("Filter","attr") , "search",$this->renderStandardAction( "filter" ) );
			$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 AdminFeedbacksGridHeader( $id , $parent );
		}

		protected function whereClause() {
			$filters = array();
			
			// is_reply
			$is_reply_filter = $this->is_reply_filter->getValue();
			if (strlen( $is_reply_filter )) {
				switch ($is_reply_filter) {
				case 0:$filters[]="parent_user_comment_detail_id is null";break;
				case 1:$filters[]="parent_user_comment_detail_id is not null";break;
				}
			}

			$poster_user = (strlen( $this->poster_user_filter->getValue() )) ? $this->poster_user_filter->getValue() : false;
			$target_user = (strlen( $this->target_user_filter->getValue() )) ? $this->target_user_filter->getValue() : false;

			if ($poster_user || $target_user) {
				$vals = array();
				if ($poster_user) $vals[]=sprintf( "poster_user_id=%d" , $poster_user );
				if ($target_user) $vals[]=sprintf( "target_user_id=%d" , $target_user );
				$filters[]=sprintf( "(%s)",implode( " or " , $vals ) );
			}

			// user_comment_detail
			$user_comment_detail_filter = $this->user_comment_detail_filter->getValue();
			if (strlen( $user_comment_detail_filter ))
				$filters[]=sprintf( "user_comment_detail like '%%%s%%'" , FNTH_db_addslashes( $user_comment_detail_filter ) );

			// 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( "%suser_comment_details.is_admin_acknowledged",
					($is_admin_acknowledged_filter) ? "" : "not " );
			}
			// is_proofread_filter
			$is_proofread_filter = $this->is_proofread_filter->getValue();
			if (strlen( $is_proofread_filter )) {
				$is_proofread_filter = (int)$is_proofread_filter;
				$filters[]=sprintf( "%suser_comment_details.is_proofread",
					($is_proofread_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 user_comment_detail_id desc";
			switch ($this->sOrdering) {
			case "ctstamp":
			case "user_comment_detail":
				$retval = $this->sOrdering;
				break;
			default:
				break;
			}
			if (empty( $retval )) return $retval;
			if (!$this->bAscending) $retval.=" desc";
			return " order by " . $retval;
		}

		protected function fieldsClause() {
			return "user_comment_details.user_comment_detail_id as id,
				user_comment_details.parent_user_comment_detail_id,
				user_comment_details.score,
				user_comment_details.ctstamp,
				user_comment_details.poster_user_id,
				user_comment_details.target_user_id,
				user_comment_details.user_comment_detail,
				user_comment_details.is_admin_acknowledged,
				user_comment_details.is_proofread
				";
		}

		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( "ctstamp" , __("blueboardpage_Comment_created_at" , "attr" ) , true );
			$this->addColumn( "poster_user" , __("blueboardpage_Poster_user" , "attr" ) , false );
			$this->addColumn( "target_user" , __("blueboardpage_Target_user" , "attr" ) , false );
			$this->addColumn( "user_comment_detail" , __("blueboardpage_Comment" , "attr" ) , true , array( "width" => 200 ) );
			$this->addColumn( "score" , __("blueboardpage_Score" , "attr" ) , false );
			$this->addColumn( "is_reply" , __("blueboardpage_Is_reply" , "attr" ) , false );

			$this->addColumn( "is_proofread" , __("blueboardpage_Proofread","attr") , true );
			$this->addColumn( "is_admin_acknowledged" , __("blueboardpage_Admin_acknowledged","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_user":
					$this->poster_user_filter = new AdminUserSelector( "poster_user" , $field );
					break;
				case "target_user":
					$this->target_user_filter = new AdminUserSelector( "target_user" , $field );
					break;
				case "user_comment_detail":
					$this->user_comment_detail_filter = new AdminTextbox( "user_comment_detail" , $field );
					break;
				case "is_reply":
					$field->setAttribute( "align" , "center" );
					$this->is_reply_filter = new AdminTristate( "is_reply" , $field );
					break;
				case "is_admin_acknowledged":
					$field->setAttribute( "align" , "center" );
					$this->is_admin_acknowledged_filter = new AdminTristate( "is_admin_acknowledged" , $field );
					break;
				case "is_proofread":
					$field->setAttribute( "align" , "center" );
					$this->is_proofread_filter = new AdminTristate( "is_proofread" , $field );
					break;
				}
			}
		}

		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 "proofread":
				$bbm = new BlueboardModel();
				$bbm->proofreadUserComment( $this->aP["user_comment_detail_id"] , true );
				$this->refreshGrid();
				break;
			case "unproofread":
				$bbm = new BlueboardModel();
				$bbm->proofreadUserComment( $this->aP["user_comment_detail_id"] , false );
				$this->refreshGrid();
				break;
			case "moderate":
				$bbm = new BlueboardModel();
				$bbm->moderateUserComment( $this->aP["user_comment_detail_id"] , false );
				$this->refreshGrid();
				break;
			case "unmoderate":
				$bbm = new BlueboardModel();
				$bbm->moderateUserComment( $this->aP["user_comment_detail_id"] , true );
				$this->refreshGrid();
				break;
			case "delete":
				if (!empty( $this->aP["ids"])) {
					$bbm = new BlueboardModel();
					foreach ($this->aP["ids"] as $user_comment_detail_id)
						$bbm->deleteUserComment( $user_comment_detail_id );
				}	
				
				FNTH_direct_echo( "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["user_comment_detail"] ) );
			// 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_user":
			case "target_user":
				$w = new AdminUserNameTag( sprintf( "%s_%d" , $colname , $rd["id"] ) , $field );
				$w->setId( $rd[$colname."_id"] );
				$field->setValue( "" );
				break;
			case "ctstamp":
				$field->setValue( (isset($rd[$colname])) ? fo3_format_tstamp_to_screen( $rd[$colname] ) : "---" );
				$field->setAttribute( "align" , "center" );
				break;
			case "user_comment_detail":
				$field->setValue( $this->renderPostField( $field , $rd ) );
				break;
			case "score":
				$field->setAttribute( "align" , "right" );
				if (!isset( $rd[$colname] )) $field->setValue( "-" );
				break;
			case "is_reply":
				$field->setAttribute( "align" , "center" );
				$field->setValue( fo3_render_boolean_yesno( isset( $rd["parent_user_comment_detail_id"] ) ) );
				break;
			case "is_admin_acknowledged":
			case "is_proofread":
				$field->setAttribute( "align" , "center" );
				$field->setValue( fo3_render_boolean_yesno( $rd[$colname] ) );
				break;
			case "admin":
				$buttons = array();
				$prefix = $this->getId() . "_" . $rd["id"];
				switch ($rd["is_admin_acknowledged"]) {
				case "t":
					$buttons[]=$this->renderMiniButton(
						$prefix,
						"moderate",
						__("Moderate_feedback","attr"),
						"page_remove",
						$this->renderStandardAction(
							"moderate" , array( "user_comment_detail_id" => $rd["id"] )
						)
					);
					break;
				case "f":
					$buttons[]=$this->renderMiniButton(
						$prefix,
						"unmoderate",
						__("Unmoderate_feedback","attr"),
						"page_add",
						$this->renderStandardAction(
							"unmoderate" , array( "user_comment_detail_id" => $rd["id"] )
						)
					);
					break;
				}
				switch ($rd["is_proofread"]) {
				case "t":
					$buttons[]=$this->renderMiniButton(
						$prefix,
						"unproofread",
						__("Unproofread","attr"),
						"page_remove",
						$this->renderStandardAction(
							"unproofread" , array( "user_comment_detail_id" => $rd["id"] )
						)
					);
					break;
				case "f":
					$buttons[]=$this->renderMiniButton(
						$prefix,
						"proofread",
						__("Proofread","attr"),
						"page_add",
						$this->renderStandardAction(
							"proofread" , array( "user_comment_detail_id" => $rd["id"] )
						)
					);
					break;
				}
				$field->setAttribute( "align" , "center" );
				$field->setValue( implode( " " , $buttons ) );
				break;
			default:
			}
			return $field;
		}
		
	}

?>
