<?php
	require_once( $GLOBALS["site_root"]."/includes/models/Blueboard.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.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/BlueboardReplybox.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/models/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Blueboard.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );

	class BlueboardDetails extends FrontendSuperGrid {
		protected $bbmodel;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->bbmodel = new BlueboardModel();
			$this->bRenderCols = true;
			$this->sIdField = "user_comment_detail_id";
			$this->setAttribute( "class" , "FrontendSuperGrid BlueboardDetails" );
			$this->sNoRows = __( "blueboard_No_feedback" );
		}

		public function prepareDataset() {
			$user_id = $this->aG["user_id"];
			$p = array();
			$p["reply_tree"]=true;
			$mym = new MyUserModel();
			$p["viewer_user_id"]=$mym->getLoggedInUserId();

			$this->aData = $this->bbmodel->getUserCommentDetails( $user_id , $p );
			$r = array();
			$i = 0;
			foreach ($this->aData as $row) {
				$r[]=$row;
				$r[]=$row;
				$i++;
			}
			$this->aData = $r;
			$this->iNumofrecords = $i*2;
		}

		public function constructColumns() {
			$this->addColumn( "score" , __("blueboardpage_Score" ,"attr") , false , array( "width" => 50 ) );
			$this->addColumn( "user" , __("blueboardpage_User","attr") , false , array( "width" => 500 ) );
			$this->addColumn( "timestamp" , __("blueboardpage_Timestamp","attr") , false , array( "width" => 130 ) );
		}

		// kill default functionality
		public function constructOrderingHeader() {}
		public function buildQuery() { return false; }
		public function buildCountQuery() { return false; }

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

		public function handleEvents() {
			if (!isset( $this->aP["smb_post_reply"] )) return parent::handleEvents();
			$mum = new MyUserModel();
	
			$uc = $this->bbmodel->getUserCommentDetail( $this->aP["user_comment_detail_id"] );
			if (!$uc) return parent::handleEvents();

			$this->bbmodel->postUserComment(
				$this->aP["user_comment_detail_id"],
				$mum->getLoggedinUserId(),
				$uc["poster_user_id"],
				$this->aP["comment_reply"],
				0.0 );

			$um = new UserModel();
			$poster_user = $um->getUserFromUserId( $mum->getLoggedinUserId() );
			$receiver_user = $um->getUserFromUserId( $uc["poster_user_id"] );
			if ($um->isUserGetsFeedbackEmail( $receiver_user["user_id"] , $receiver_user )) {
				$mail = new TemplateMailModel( "sys_feedback_reply_received" );
				$cm = new ConstantsModel( $mail );
				$cm->addSystemParametersToTemplate();
				$cm->addUserConstantsToTemplate( $poster_user["user_id"] , "poster_user" , $poster_user );
				$cm->addUserConstantsToTemplate( $receiver_user["user_id"] , "receiver_user" , $receiver_user );
				$mail->sendMail( $receiver_user["email"] );
			}
			$url = BlueboardUrl::renderURL( $this->aG );
			FNTH_redirect( $url );
			return parent::handleEvents();
		}

		public function constructRow( $datarow , $rownum ) {
			$retval = parent::constructRow( $datarow , $rownum );
			if (!$datarow["visible"])  $retval->setAttribute( "class" , "rowdisabled" );
			return $retval;
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = null;
			$rd = $this->aData[$rownum];
			if ($rownum%2) {
				// a comment row
				switch ($colname) {
				case "score":
					$field = parent::constructField( $rownum , $row , $colname );
					$field->setAttribute( "class" , "bbscore" );
					if (!$rd["visible"]) $field->setValue( "?" );
					break;
				case "user":	// REALLY comment
					$field = parent::constructField( $rownum , $row , $colname );
					// create field
					$field->setValue( "" );
					$field->setAttribute( "colspan" , "2" );
					$cls = (isset( $rd["parent_user_comment_detail_id"] )) ? "bbreplydetails" : "bbdetails";
					$field->setAttribute( "class" , "bbdetails" );

					$reply = new BlueboardReplybox( sprintf( "reply%d" , $rownum ) , $field );
					$reply->setVisible( $rd["visible"] );
					$reply->iUserCommentId = $rd["user_comment_detail_id"];
					$reply->sComment = sprintf( "<b>%s</b>: %s" , __("blueboardpage_Rating"), $rd["user_comment_detail"] );

					if (isset( $rd["reply"] )) {
						$reply->sComment.=sprintf( "<br/><b>%s:</b> %s" , __("blueboardpage_Reply"), $rd["reply"]["user_comment_detail"] );
					}
					$mym = new MyMemberModel();
					if ($mym->isUserLoggedIn() && $rd["target_user_id"]==$mym->getLoggedInUserId()) {
						if (!isset( $rd["reply"] )) {
							$reply->bCanReply = true;
						}
					}
					break;
				case "timestamp":
					return false;
				}
			} else {
				// header row
				switch ($colname) {
				case "score":
					return false;
				case "user":
					$field = parent::constructField( $rownum , $row , $colname );
					$field->setAttribute( "colspan" , 2 );
					$unametag = new UserNameTag( sprintf( "username%d" , $rownum ) , $field );
					$unametag->setPrefix( sprintf( "%s: " , __("blueboardpage_Poster") ) );
					$unametag->setId( $rd["poster_user_id"] );
					$field->setValue( "" );
					$cls = (isset( $rd["parent_user_comment_detail_id"] )) ? "bbreplyusername" : "bbusername";
					$field->setAttribute( "class" , $cls );
					break;
				case "timestamp":
					$field = parent::constructField( $rownum , $row , $colname );
					$val = $rd["ctstamp"];
					// if there is no comment already then available
					$field->setValue( $val );
					$field->setAttribute( "class" , "bbtstamp" );
					break;
				}	
			}
			return $field;
		}

		/*
		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$mym = new MyMemberModel();
			$bMemberLoggedIn = $mym->isMemberLoggedIn();
			$bSubscribed = $mym->isLoggedinMemberActiveSubscriber();
			$this->setParam( "bMember" , $bMemberLoggedIn );
			$this->setParam( "bSubscribed" , $bSubscribed );
			if ($bSubscribed) {
				$this->setParam( "datas" , $datas = $this->getDatas() );
			} 
			return parent::render();
		}
		*/
	}

?>
