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

	class BlueboardPostNewUser extends Widget {
		protected $unreg_user_name = null;
		protected $unreg_user_email = null;
		protected $unreg_phone = null;
		protected $unreg_url = null;
		protected $unreg_ncomment = null;
		protected $oSubmit = null;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->unreg_user_name = new FrontendTextbox( "unreg_user_name" , $this );
			$this->unreg_user_email = new FrontendTextbox( "unreg_email" , $this );
			$this->unreg_phone = new FrontendTextbox( "unreg_phone" , $this );
			$this->unreg_url = new FrontendTextbox( "unreg_url" , $this );
			$this->unreg_ncomment = new FrontendTextarea( "unreg_ncomment" , $this );

			$this->oSubmit = new FrontendSubmit( "smb_unreg" , $this );
			$this->oSubmit->setValue( __("blueboardpage_Post_unregistered_user" , "attr" ) );
		}

		public function handleEvents() {
			$this->refreshChildValuesFromArray();
			if (!$this->oSubmit->isSubmitted()) return parent::handleEvents();



			$u = array();
			$u["is_admin_acknowledged"]=false;
			$u["is_registered"]=false;
			$u["user_name"]=$this->unreg_user_name->getValue();
			$u["email"]=$this->unreg_user_email->getValue();
			$u["contact_phone"]=$this->unreg_phone->getValue();
			$u["npublic_comment"]=$this->unreg_ncomment->getValue();
			$u["url"]=$this->unreg_url->getValue();
			$u["pwd"]=fo3_pwgen();

			$this->bbmodel = new BlueboardModel();
			$um = new UserModel();
			$user_id = $um->addUser( $u );

			$p = array();
			$p["user_id"]=$user_id;
			$url = BlueboardURL::renderURL( $p );
			FNTH_redirect( $url );
		}

		protected function initializeWidgets() {
			$this->unreg_user_name->setWidth( 300 );
			$this->unreg_user_email->setWidth( 300 );
			$this->unreg_phone->setWidth( 300 );
			$this->unreg_url->setWidth( 300 );
			$this->unreg_ncomment->setWidth( 300 );
			$this->unreg_ncomment->setRows( 4 );
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "blueboardpage_No_unreg_user_name_filled" );
			fo3_add_js_langdef( "blueboardpage_No_comment_filled" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "redask" , "<span class=\"red\">*</span>" );
			$this->generateLangDefs();
			$this->initializeWidgets();
			return parent::render();
		}
	}

?>
