<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.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 ForumPostForm extends Widget {
		public $forum_topic_id;

		protected $forum_post;
		protected $forum_submit;

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

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

			if (is_numeric( $this->aP["parent_post_id"] ) && $this->aP["parent_post_id"]>0) {
				$p["forum_post_parentid"]=sprintf( "%d" , $this->aP["parent_post_id"] );
			}
			$mym = new MyUserModel();
			$user_id = $mym->getLoggedinUserId();
			$p["owner_user_id"] = $user_id;

			/*
			$um = new UserModel();
			$user = $um->getUserFromUserid( $user_id );
			// $p["poster_name"]= 
			*/
			
			$this->fm->addTopicPost( $this->forum_topic_id , $p );
		
			FNTH_refresh();
			parent::handleEvents();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "forumspage_This_is_reply_to" );
		}

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->forum_post = new FrontendTextarea( "forum_post" , $this );
			$this->forum_post->setColumns( 64 );
			$this->forum_post->setRows( 6 );
			$this->forum_submit = new FrontendSubmit( "forum_submit" , $this );
			$this->forum_submit->setValue( __("forumspage_Post","attr") );
		}


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

?>
