<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/BlueboardScore.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextarea.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSubmit.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/models/Blueboard.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.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/Constants.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/urlgen/Blueboard.inc.php" );

	class BlueboardCommentForm extends Widget {
		protected $bbmodel;
		protected $oScore;
		protected $oComment;
		protected $oSubmit;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->bbmodel = new BlueboardModel();

			$this->oScore = new BlueboardScore( "score" , $this );
			$this->oComment = new FrontendTextarea( "comment" , $this );
			$this->oComment->setMaxLength( 300 );
			$this->oSubmit = new FrontendSubmit( "submit" , $this );
			$this->oSubmit->setValue( __("Post_a_feedback","attr") );
		}

		public function handleEvents() {
			$this->refreshChildValuesFromArray( $this->aP );
			if ($this->oSubmit->isSubmitted()) {
				$mum = new MyUserModel();
				$this->bbmodel->postUserComment(
					null,
					$mum->getLoggedinUserId(),
					$this->aG["user_id"],
					$this->oComment->getValue(),
					$this->oScore->getValue() );
				$g = $this->aG;
				unset( $g["lang"] );
				unset( $g["user_id"] );


				$um = new UserModel();
				$poster_user = $um->getUserFromUserId( $mum->getLoggedinUserId() );
				$receiver_user = $um->getUserFromUserId( $this->aG["user_id"] );

				$um->touchFeedbackOrderingTstamp( $receiver_user["user_id"] );
				if ($um->isUserGetsFeedbackEmail( $receiver_user["user_id"] , $receiver_user )) {
					$mail = new TemplateMailModel( "sys_feedback_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"] );
				}
				$new_url = BlueboardURL::renderURL( $g );
				FNTH_redirect( $new_url );
			}
			return parent::handleEvents();
		}

		protected function initializeWidgets() {
			fo3_add_js_langdef( "blueboardpage_Fill_the_feedback" );
			fo3_add_js_langdef( "blueboardpage_Fill_the_score" );
			fo3_add_js_langdef( "blueboardpage_Post_feedback_confirmation" );
			$this->oComment->setColumns( 60 );
		}
		
		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$mym = new MyMemberModel();
			if (!$mym->isUserLoggedIn()) return "";		// ha nincs belepve user
			if ($mym->getLoggedinUserId()==$this->aG["user_id"]) return "";		// ha onmaga

			$onlytranslators = false;
			if (!$mym->isMemberLoggedIn()) {
				// nem is member
				$onlytranslators = true;
			} else {
				// member
				if (!$mym->isLoggedinMemberActiveSubscriber()) $onlytranslators = true;
				// else if ($mym->getLoggedinMemberType()=="parttime") $onlytranslators = true;
			}

			if ($onlytranslators) {
				$mm = new MemberModel();
				$member = $mm->getMemberFromUserId( $this->aG["user_id"] );
				if (!$member) return "";
			}

			$bbm = new BlueboardModel();
			if ($bbm->alreadyCommented(
				$mym->getLoggedInUserId() , $this->aG["user_id"] )) return "";	// ha mar commentezett ra
			$this->initializeWidgets();
			return parent::render();
		}
	}

?>
