<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/ArticleTemplate.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/RMPVideo.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Openads.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/CommunityToolbar.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/ArticleComments.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/ArticleBreadcrumb.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/widgets/ArticleConnecteds.inc.php" );

	require_once( $GLOBALS["site_root"] . "/includes/widgets/ForumPostForm.inc.php" );

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

	class ArticlePanel extends Widget {
		protected $article_model = null;
		protected $article_id = 0;
		protected $larticle_id = 0;
		protected $oCommunityToolbar = null;
		protected $oArticleBreadcrumb = null;
		protected $oArticleConnecteds = null;
		protected $pagenum = 0;
		protected $article = array();
		protected $larticle = array();

		public function setArticleId( $v ) {
			$this->article_id = $v;
			$this->oArticleBreadcrumb->setArticleId( $v );
			$this->oArticleConnecteds->setArticleId( $v );
		}
		public function setLarticleId( $v ) { $this->larticle_id = $v; $this->oArticleBreadcrumb->setLarticleId( $v ); }
		public function setPagenum( $v ) { $this->pagenum = $v; }

		public function __construct( $id = "ArticlePanel" , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->article_model = new ArticlesModel();
			$this->oArticleComments = new ArticleComments( "article_comments" , $this );
			$this->oArticleBreadcrumb = new ArticleBreadcrumb( "article_breadcrumb" , $this );
			$this->oArticleConnecteds = new ArticleConnecteds( "connecteds" , $this );

			$this->mmm = new MyMemberModel();
			if ($this->mmm->isMemberLoggedIn())
				$this->oForumPostForm = new ForumPostForm( "forum_post_form" , $this );
		}

		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );

			if (isset( $g["article_id"] )) {
				$forum_topic_id = $this->article_model->getForumTopicIdForArticleId( $g["article_id"] , true );
				$this->oArticleComments->forum_topic_id = $forum_topic_id;
				if ($this->mmm->isMemberLoggedIn())
					$this->oForumPostForm->forum_topic_id = $forum_topic_id;
			}

			if (!isset( $g["prnc"] )) {
				switch ($GLOBALS["language"]) {
				default:
				case "hu":$top = 163;$bottom = 165; break;
				case "en":$top = 168;$bottom = 167; break;
				case "de":$top = 183;$bottom = 182; break;
				}
				$this->oTopBanner = new Openads( "adserver_163" , $this , $top );
				$this->oBottomBanner = new Openads( "adserver_165" , $this , $bottom );
			} else {
				$this->oParent->setFullscreen( true );
				$this->oParent->setPrint( true );
				$this->oCommunityToolbar->setIsSendPrint( false );
				$this->oCommunityToolbar->setCommunityLinks( false );
			}
		}

		protected function prepareArticle( $body ) {
			// load templater & constants
			$tmpl = new ArticleTemplate( "_article_template" , null );
			$cm = new ConstantsModel( $tmpl );

			// -- custom parameters: users
			$mmm = new MyMemberModel();
			if ($mmm->isUserLoggedIn()) {
				$cm->addUserConstantsToTemplate( $mmm->getLoggedinUserId() );
			}

			// -- custom parameters: members
			if ($mmm->isMemberLoggedIn()) {
				$cm->addMemberConstantsToTemplate( $mmm->getLoggedinMemberId() );
			}

			// -- custom parameters: system parameters
			$cm->addSystemParametersToTemplate();

			$body = $tmpl->renderFromString( $body , $this->larticle["larticle_tech_mtstamp"] );
			return $body;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			if (!$this->larticle_id) {
				$this->article = $this->article_model->getArticleFromId( $this->article_id );
				$this->larticle = $this->article_model->getLanguageArticle(
					$this->article_id , $GLOBALS["langcode"] );
			} else {
				$this->larticle = $this->article_model->getLanguageArticleFromLarticleId( $this->larticle_id );
			}
			$this->setParam( "article" , $this->article );
			$this->setParam( "larticle" , $this->larticle );

			if (isset( $this->oCommunityToolbar )) $this->oCommunityToolbar->setTitle( $this->larticle["larticle_title"] );
			$author = "fordit.hu";

			$used_date = $this->article["article_ctstamp"];
			if (isset( $this->larticle["larticle_ctstamp"] )) $used_date = $this->larticle["larticle_ctstamp"];
			if (isset( $this->article["article_publication_tstamp"] )) $used_date = $this->article["article_publication_tstamp"];
			if (isset( $this->larticle["larticle_publication_tstamp"] )) $used_date = $this->larticle["larticle_publication_tstamp"];

			// set page title
			$page_title = sprintf( "%s | %s" , $this->larticle["larticle_title"], GlobalsModel::getSiteName() );
			fo3_set_page_title( $page_title );

			// set page description
			if (isset( $this->larticle["larticle_lead"] ) && !empty( $this->larticle["larticle_lead"] ))
				fo3_set_meta_description( $this->larticle["larticle_lead"] );

			if (isset( $this->oCommunityToolbar )) $this->oCommunityToolbar->setSubTitle2(
				sprintf( "%s | %s",
					fo3_format_date_to_screen( $used_date ),
					$author ) );

			$body = $this->larticle["larticle_body"];
			$body = $this->prepareArticle( $body );
			$bodies = $this->article_model->explodeBody( $body );

			$this->setParam( "pagenum" , $this->pagenum );
			$this->setParam( "numofpages" , count( $bodies ) );
			$this->setParam( "bodies" , $bodies );

			$errormsg = "";
			$show = true;
			if ($this->article["is_memberonly"]) {
				$mmm = new MyMemberModel();
				if ($mmm->isMemberLoggedin()) {
					// be van jelentkezve
					if ($this->article["is_subscriberonly"]) {
						if (!$mmm->isLoggedinMemberActiveSubscriber()) {
							$show = false;
							$errormsg = __("globalerror_User_has_to_be_an_active_member_to_access_page" );
						}
					}
				} else {
					$show = false;
					$errormsg = __("globalerror_User_has_to_login_to_access_page");
				}
			}
			$this->setParam( "bShowContents" , $show );
			$this->setParam( "errormsg" , $errormsg );
			$this->setParam( "print" , isset( $this->aG["prnc"] ) );
			return parent::render();
		}
	}
?>
