<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/ForumTopicsGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/ForumTopicPosts.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/ForumTopicBreadcrumb.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/ForumPostForm.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/ForumNewSubtopicForm.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/ForumSearchForm.inc.php" );

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

	class ForumPane extends Widget {
		protected $forum_model;
		protected $forum_topic_id =0;
		protected $forum_topic;
		protected $forum_topics_grid;
		protected $forum_topic_posts;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->forum_model = new ForumModel();
			$this->my_member_model = new MyMemberModel();
		}

		public function handleEvents() {
			if ($this->featureLockHandleEvents( "forum" )) return;
			return parent::handleEvents();
		}

		public function modifyGetPost( &$g , &$p ) {
			$this->forum_topic_id = $this->forum_model->getForumTopicIdFromURI( $g["uri"] );
			$can_view_topics = ForumModel::canViewTopics( $this->forum_topic_id );
			$can_view_posts = ForumModel::canViewPosts( $this->forum_topic_id );
			$can_create_topic = ForumModel::canCreateTopic( $this->forum_topic_id );
			$can_post = ForumModel::canPost( $this->forum_topic_id );

			$this->bLoggedIn = $this->my_member_model->isUserLoggedIn();
			$this->setParam( "bLoggedIn" , $this->bLoggedIn );
			$this->bMemberLoggedIn = $this->my_member_model->isMemberLoggedIn();
			$this->setParam( "bMemberLoggedIn" , $this->bMemberLoggedIn );
			$this->bActiveSubscriber = $this->my_member_model->isLoggedinMemberActiveSubscriber();
			$this->setParam( "bActiveSubscriber" , $this->bActiveSubscriber );

			$this->setParam( "can_view_posts" , $can_view_posts );
			$this->setParam( "can_view_topics" , $can_view_topics );
			$this->setParam( "can_create_topic" , $can_create_topic );
			$this->setParam( "can_post" , $can_post );

			$this->forum_search_form = new ForumSearchForm( "forum_search_form" , $this );

			$this->forum_topic = $this->forum_model->getForumTopicFromId( $this->forum_topic_id );
			$this->forum_topic_breadcrumb = new ForumTopicBreadCrumb( "forum_topic_breadcrumb" , $this );
			$this->forum_topic_breadcrumb->forum_topic_id = $this->forum_topic_id;
			$am = new ArticlesModel();
			$this->setParam( "url_forum_terms_and_conditions" ,
				$am->getLanguageArticleUrlFromArticleName("sys_forum_terms_and_conditions") );
			if ($this->forum_topic["is_topic_post_thread"]) {
				if ($this->bMemberLoggedIn && $this->bActiveSubscriber) {
					// teljesen franko csoka
					$this->forum_topic_posts = new ForumTopicPosts( "forum_topic_posts" , $this );
					$this->forum_topic_posts->forum_topic_id = $this->forum_topic_id;

					if ($can_post) {
						$this->forum_post_form = new ForumPostForm( "forum_post_form" , $this );
						$this->forum_post_form->forum_topic_id = $this->forum_topic_id;
					}
				} else if ($this->bMemberLoggedIn) {
					// nem aktiv elofizeto
					$this->sa_error = new SystemArticle( "sa_error" , $this , "sys_error_user_has_to_be_an_active_subscriber" );
				} else if ($this->bLoggedIn) {
					// bejelentkezett ajanlatkero
					$this->sa_error = new SystemArticle( "sa_error" , $this , "sys_error_user_has_to_be_a_member" );
				} else {
					// nincs is bejelentkezve
					$this->sa_error = new SystemArticle( "sa_error" , $this , "sys_error_user_has_to_login" );
				}
			} else {
				if ($can_create_topic) {
					$this->forum_new_subtopic = new ForumNewSubtopicForm( "forum_new_subtopic" , $this );
					$this->forum_new_subtopic->forum_topic_id = $this->forum_topic_id;
				}
				if ($can_view_topics) {
					$this->forum_topics_grid = new ForumTopicsGrid( "forum_topics_grid" , $this );
					$this->forum_topics_grid->forum_topic_id = $this->forum_topic_id;
				}
			}
			return parent::modifyGetPost( $g , $p );
		}

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

			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "forum_topic_thread" , !$this->forum_topic["is_topic_post_thread"] );
			// TODO: topics / posts (poster form is, )
			// $this->setParam( "pagetype" , "topics" );
			return parent::render();
		}

	}

?>
