<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php");
	require_once( $GLOBALS["site_root"]."/includes/widgets/CommunityLinks.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );

	class CommunityToolbar extends Widget {
		protected $oLinks;
		protected $sUrl = "";
		protected $sRSSUrl = "";
		protected $bRawTitle = false;
		protected $sTitle = "";
		protected $sSubtitle = "";
		protected $sSubtitle2 = "";

		protected $bCommunityLinks = true;
		
		protected $bSendPrint = true;
		protected $sSendPrint = "";

		protected $bSend = true;
		
		public function setIsSendPrint( $v ) { $this->bSendPrint = $v; }
		public function setIsSend( $v ) { $this->bSend = $v; }
		public function setSendPrint( $v ) { $this->sSendPrint = $v; }

		public function setRawTitle( $v ) { $this->bRawTitle = $v; }
		public function setTitle( $v ) { $this->sTitle = $v; }
		public function setCommunityLinks( $v ) { $this->bCommunityLinks = $v; }
		public function setRSSUrl( $v ) { $this->sRSSUrl = $v; }
		public function setUrl( $v ) { $this->sUrl = $v; }
		public function setSubtitle( $v ) { $this->sSubtitle = $v; }
		public function setSubtitle2( $v ) { $this->sSubtitle2 = $v; }
	

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->sUrl = sprintf( "http://%s%s" , $_SERVER["SERVER_NAME"] , $_SERVER["REQUEST_URI"] );
			$this->oLinks = new CommunityLinks( "community_links" , $this );
		}

		protected function prepareSendParams( ) {
			$uri = $_SERVER["REQUEST_URI"];
			$url = sprintf( "http://%s%s" , $_SERVER["SERVER_NAME"] , $uri );
			$retval = array();
			$retval["uri"]=$uri;
			$retval["url"]=$url;
			return $retval;
		}
		

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "send_content_email":
				if (!isset( $this->aP["ajaxtarget"] )) break;
				if ($this->aP["ajaxtarget"]!=$this->getId()) break;
				if (!isset( $this->aP["sender_name"] )) break;
				if (!isset( $this->aP["sender_email"] )) break;
				if (!isset( $this->aP["target_name"] )) break;
				if (!isset( $this->aP["target_email"] )) break;
				if (!isset( $this->aP["sender_message"] )) break;
				$error = "";
				// sender_name
				$sender_name = $this->aP["sender_name"];
				if (strlen( trim( $sender_name ))==0) {
					$error.=__( "No_sender_name_given" )."\n";
				}
				// sender_email
				$sender_email = $this->aP["sender_email"];
				if (!fo3_is_email_valid( $sender_email )) {
					$error.=__( "Invalid_sender_email" )."\n";
				}
				// target_name
				$target_name = $this->aP["target_name"];
				if (strlen( trim( $target_name ))==0) {
					$error.=__( "No_target_name_given" )."\n";
				}
				// target_email
				$target_email = $this->aP["target_email"];
				if (!fo3_is_email_valid( $target_email )) {
					$error.=__( "Invalid_target_email" )."\n";
				}
				$sender_message = $this->aP["sender_message"];

				$retval = array();
				if (!strlen( $error )) {
					$tm = new TemplateMailModel();
					$tm->setTemplateFromName( "sys_content_send" );
					$tm->setParam( "sender_name" , $sender_name );
					$tm->setParam( "sender_email" , $sender_email );
					$tm->setParam( "sender_message" , $sender_message );
					$tm->setParam( "target_name" , $target_name );
					$tm->setParam( "target_email" , $target_email );
					$url = sprintf( "http://%s%s" , $_SERVER["SERVER_NAME"] , $_SERVER["REQUEST_URI"] );
					$tm->setParam( "content_url" , $url );
					$tm->sendMail( $target_email );
					$retval["sent"]=1;
				} else {
					$retval["sent"]=0;
					$retval["error"]=sprintf( "alert('%s');" , FNTH_addslashes_js( $error ) );
				}
				FNTH_echo_json_response( $retval );
				exit();
			}
			return parent::handleEvents();
		}
		protected function preparePrintParams( ) {
			$retval = array();
			$retval["uri"] = $uri = $_SERVER["REQUEST_URI"];
			$retval["uri"]=$uri;
			$url = sprintf( "http://%s%s" , $_SERVER["SERVER_NAME"] , $uri );
			$eurl = FNTH_explode_url( $url );
			$page= sprintf( "%://%s%s" , $eurl["scheme"] , $eurl["host"] , $eurl["path"] );
			$params = (isset( $eurl["params"] )) ? $eurl["params"] : array();
			$params["prnc"]=1;
			$retval["url"]=FNTH_render_url( $page , $params );
			return $retval;
		}

		protected function generateLangdefs() {
			fo3_add_js_langdef( "sendinemail_Send_in_email" );
			fo3_add_js_langdef( "Print_content" );
			fo3_add_js_langdef( "Send" );
			fo3_add_js_langdef( "Cancel" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->oLinks->setUrl( $this->sUrl );
			$this->oLinks->setRSSUrl( $this->sRSSUrl );
			$this->setParam( "bRawTitle" , $this->bRawTitle );
			$this->setParam( "title" , $this->sTitle );
			$this->setParam( "subtitle" , $this->sSubtitle );
			$this->setParam( "subtitle2" , $this->sSubtitle2 );
			$this->setParam( "bCommunityLinks" , $this->bCommunityLinks );
			$this->setParam( "bSendPrint" , $this->bSendPrint );
			$this->setParam( "bSend" , $this->bSend );
			$this->setParam( "sSendPrint" , $this->sSendPrint );
			if ($this->bSend)
				FNTHPage::addUniquePageFullInit( sprintf( "InitSendParams( '%s' , %s );",
				$this->getId(),
				json_encode( $this->prepareSendParams() ) ) );
			
			FNTHPage::addUniquePageFullInit( sprintf( "InitPrintParams( '%s' , %s );",
				$this->getId(),
				json_encode( $this->preparePrintParams() ) ) );
			$this->generateLangdefs();
			return parent::render();
		}

	}

?>
