<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Blueboard.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/urlgen/Blueboard.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/widgets/BlueboardFilterForm.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/CommunityToolbar.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/BlueboardGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/widgets/BlueboardDetails.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/BlueboardCommentForm.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/widgets/BlueboardPostNewUser.inc.php" );
	
	class MyBlueBoardPane extends Widget {
		public $oDetails;
		public $oComment;
		public $oFilterForm;
		public $oGrid;
	
		protected $bValid = false;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->mm = new MemberModel();
			$this->mmm = new MyMemberModel();
			$this->bbm = new BlueboardModel();
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );
			$this->oCommunityToolbar->setCommunityLinks( false );
		}

		protected function getOwnFeedbackUrl() {
			$p = array();
			if (isset( $p["lang"] )) unset( $p["lang"] );
			$p["user_id"]=$this->user_id;
			return BlueboardURL::renderURL( $p );
		}

		protected function redirectToOwnFeedback() {
			$url = $this->getOwnFeedbackUrl();
			FNTH_redirect( $url );
		}

		protected function getFeedbackGridUrl() {
			$p = $this->aG;
			if (isset( $p["lang"] )) unset( $p["lang"] );
			unset( $p["user_id"] );
			return BlueboardURL::renderURL( $p );
		}

		protected function redirectToFeedbackGrid() {
			$url = $this->getFeedbackGridUrl();
			FNTH_redirect( $url );
		}

		public function modifyGetPost( &$g , &$p ) {
			$this->mmm->hashLogin( $g );
			$this->bValid = false;
			$this->user_id = $this->mmm->getLoggedinUserId();
			$this->setParam( "user_id" , $this->user_id );
			// check validity
			// -- check if logged in
			if (!$this->user_id) {
				$this->bValid = false;
				$this->oSystemArticle = new SystemArticle( "system_article" , $this , "sys_error_user_has_to_login" );
				return parent::modifyGetPost( $g , $p );
			}

			$this->bCanViewSearch = $this->bbm->canViewSearch( $this->user_id );
			$this->setParam( "bCanViewSearch" , $this->bCanViewSearch );
			// case 1: details oldal
			if (isset( $g["user_id"] )) {
				$this->bCanViewComments = $this->bbm->canViewComments( $this->user_id , $g["user_id"] );
				$this->bHasAbilityToViewComments = $this->bbm->hasAbilityToViewComments( $this->user_id );
				$this->bActiveSubscriber = $this->mmm->isLoggedinMemberActiveSubscriber();

				$this->setParam( "bCanViewComments" , $this->bCanViewComments );
				$this->setParam( "bHasAbilityToViewComments" , $this->bHasAbilityToViewComments );
				$this->setParam( "bActiveSubscriber" , $this->bActiveSubscriber );
				if ($this->bCanViewSearch) {
					$this->setParam( "url_feedbackgrid" , $this->getFeedbackGridUrl() );
				}

				if ($this->bCanViewComments) {
					// ha mase, vagy mondjuk a sajatja, es meg tudja nezni
					$this->bValid = true;
					$this->oDetails = new BlueboardDetails( "blueboard_details" , $this );
					$this->oCommentForm = new BlueboardCommentForm( "blueboard_commentform" , $this );

					// tud searchet nezni, akkor nincs baj
					if ($this->bCanViewSearch) return parent::modifyGetPost( $g , $p );
					// nem tud, de azert mert amugy sem tudna
					if (!$this->bHasAbilityToViewComments) return parent::modifyGetPost( $g , $p );
					$this->oSystemArticle = new SystemArticle( "system_article" , $this ,
						($this->bActiveSubscriber) ? "sys_blueboard_permitted_for_pros_only" : "sys_blueboard_permitted_for_active_subscribers_only" );
					$this->oSystemArticle->setOnlyBody( true );
				} else {
					// mert ha nem, jogosult, akkor bizony lukrafutas
					$this->bValid = false;
					$this->oSystemArticle = new SystemArticle( "system_article" , $this , "sys_content_not_permitted" );
				}
				return parent::modifyGetPost( $g , $p );
			}

			/*
			// -- check if member is loggedin
			$this->member = $this->mm->getMemberFromUserId( $this->user_id );
			if (!$this->member) {
				$this->redirectToOwnFeedback();
				$this->bValid = false;
				$this->oSystemArticle = new SystemArticle( "system_article" , $this , "sys_error_user_has_to_be_a_member" );
				return parent::modifyGetPost( $g , $p );
			}

			if ($this->member["expires_tstamp"]<fo3_date( "Y-m-d H:i:s" )) {
				$this->redirectToOwnFeedback();
				$this->bValid = false;
				$this->oSystemArticle = new SystemArticle( "system_article" , $this , "sys_error_user_has_to_be_an_active_subscriber" );
				return parent::modifyGetPost( $g , $p );
			}

			if ($this->member["member_type"]=="parttime") {
				$this->redirectToOwnFeedback();
				$this->bValid = false;
				$this->oSystemArticle = new SystemArticle( "system_article" , $this , "sys_error_parttimer_not_permitted_content" );
				return parent::modifyGetPost( $g , $p );
			}
			*/

			// case 2: grid oldal
			$this->bValid = true;
			$this->oFilterForm = new BlueboardFilterForm( "blueboard_filterform" , $this );
			$this->oGrid = new BlueboardGrid( "blueboard_grid" , $this );
			if ($this->bCanViewSearch) {
				$this->oBlueboardPostNewUser = new BlueboardPostNewUser( "blueboard_post_new_user" , $this );
			}
			return parent::modifyGetPost( $g , $p );
		}

		public function render() {
			$fp = array();
			$fp["title"]=__("menu_My_member_Blueboard");
			$cnt = $this->featureLockRender( "blueboard" , $fp );if ($cnt) return $cnt;

			if ($this->sRendered) return $this->sRendered;
			$title = __("blueboardpage_Blueboard");

			$this->setParam( "bValid" , $this->bValid );

			if (isset( $this->aG["user_id"] )) {
				$title.=": ";
				$um = new UserModel();
				$user = $um->getUserFromUserId( $this->aG["user_id"] );
				$title.=$user["user_name"] . " " . __("usertype_about_" . $um->getUserType( $this->aG["user_id"] ) );

				$mm = new MemberModel();
				$member = $mm->getMemberFromUserId( $user["user_id"] );
				$blueboard_url = fo3_break_url( $user["url"] );
				$blueboard_url_valid = false;
				if ($member) {
					$this->setParam( "member" , $member );
					$blueboard_url = sprintf( "http://%s%s" , $_SERVER["SERVER_NAME"] , $member["member_url"] );
					$blueboard_url_valid = true;
				}

				$this->setParam( "user" , $user );
				$this->setParam( "blueboard_email" , fo3_break_email( $user["email"] ) );
				$this->setParam( "blueboard_url" , $blueboard_url );
				$this->setParam( "blueboard_url_valid" , $blueboard_url_valid );
				$this->setParam( "blueboard_phone" , fo3_break_phone( $user["contact_phone"] ) );
			} else {
				$fields = array( "job_bgcolor_permitted" , "job_bgcolor_disabled" , "job_bgcolor_special" , "job_bgcolor_owned" );
				foreach ($fields as $f )
					$this->setParam( $f , SystemParametersModel::getVar( $f ) );
			}

			$user = new MyMemberModel();
			$this->setParam( "user_id" , (isset( $this->aG["user_id"] )) ? $this->aG["user_id"] : 0 );
			$this->oCommunityToolbar->setTitle( $title );
			$this->oCommunityToolbar->setIsSendPrint( false );
			$this->oCommunityToolbar->setSendPrint(
				sprintf( "<br/><a class=\"nrm\" target=\"_blank\" href=\"%s\">%s &raquo;</a>",
					ArticleURL::renderURLFromArticleName( "sys_feedback_terms_and_conditions" ),
					__("feedback_Terms_and_conditions") ) );
			return parent::render();
		}

	}

?>
