<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextbox.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/Forum.inc.php" );

	class ForumNewSubtopicForm extends Widget {
		public $forum_topic_id;

		protected $forum_topic_title;
		protected $forum_post;
		protected $forum_submit;

		protected function validateForumNewTopic() {
			$errors = "";
			$forum_topic_title = $this->forum_topic_title->getValue();
			if (!strlen( $forum_topic_title )) {
				$errors.=__("Empty_forum_topic_title")."<br/>";
			}

			$forum_post = $this->forum_post->getValue();
			if (!strlen( $forum_post )) {
				$errors.=__("Empty_forum_post")."<br/>";
			}
			return $errors;
		}

		public function handleEvents() {
			$this->refreshChildValuesFromArray( $this->aP );
			if (!$this->forum_submit->isSubmitted()) return parent::handleEvents();
			if (!ForumModel::canCreateTopic( $this->forum_topic_id )) return parent::handleEvents();

			$errors = $this->validateForumNewTopic();
			if (!empty( $errors )) {
				FrontendPage::warningMessage( "forum_new_subtopic_errors" , $errors );
				return parent::handleEvents();
			}

			$this->fm = new ForumModel();
			$mym = new MyUserModel();
			$user_id = $mym->getLoggedinUserId();

			$topic = $this->fm->getForumTopicFromId( $this->forum_topic_id );
			// add the topic
			$p = array();
			if ($this->forum_topic_id)
				$p["forum_topic_parentid"]=$this->forum_topic_id;
			$p["owner_user_id"] = $user_id;
			$p["forum_topic_title"]=$this->forum_topic_title->getValue();
			$p["forum_topic_level"]=(isset( $topic["forum_topic_level"] )) ? $topic["forum_topic_level"]+1 : 1;
			$p["forum_topic_ordering"]=$this->fm->getNextTopicOrderingOnParent( $this->forum_topic_id );
			$subtopic_id = $this->fm->addTopic( $p );

			$p = array();
			$p["post"]=$this->forum_post->getValue();
			$p["owner_user_id"]=$user_id;

			$um = new UserModel();
			$user = $um->getUserFromUserid( $user_id );
		
			$this->fm->addTopicPost( $subtopic_id , $p );
			$url = ForumURL::renderURLFromTopicId( $subtopic_id );
			FNTH_redirect( $url );
		}

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->forum_topic_title = new FrontendTextbox( "forum_topic_title" , $this );
			$this->forum_topic_title->setWidth( 300 );
			$this->forum_topic_title->setAttribute( "maxlength" , 32 );

			$this->forum_post = new FrontendTextarea( "forum_post" , $this );
			$this->forum_post->setColumns( 64 );
			$this->forum_post->setRows( 6 );
			$this->forum_submit = new FrontendSubmit( "forum_newsubtopic_submit" , $this );
			$this->forum_submit->setValue( __("forumspage_Post","attr") );
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "Empty_forum_topic_title" );
			fo3_add_js_langdef( "Empty_forum_post" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();
			return parent::render();
		}
	}
?>
