<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPopup.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextarea.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTimestamp.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminHtmlEdit2.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminCheckbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminNewsletterTable.inc.php" );

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

	FNTH_import( "xmlprocess" );

	
	class AdminNewsletterXmlToAbsolute extends FNTHXmlProcess {
		protected function makeAbsoluteUrl( $tag , $attr ) {
			if (!isset( $tag[$attr] )) return $tag;
			$url = FNTH_strip_quotes( $tag[$attr] );
			$url = FNTH_strip_quotes( $url , "\"" );
			if (substr( $url , 0 , 8 )!="https://" &&
				substr( $url , 0 , 7 )!="http://" &&
				substr( $url , 0 , 6 )!="ftp://" &&
				substr( $url , 0 , 7 )!="mailto:") {
				$nurl = sprintf( "http://%s" , $_SERVER["SERVER_NAME"] );
				if (substr( $url , 0 , 1)!="/" && substr( $url , 0 , 1)!="[") $nurl.="/";
				$nurl.=$url;
				$url = $nurl;
			}
			$tag[$attr]=sprintf( "\"%s\"" , $url );
			return $tag;
		}
		public function parseHtmlTag( $tag ) {
			switch ($tag["tagtype"]) {
			case "a":
			case "link":
				$tag = $this->makeAbsoluteUrl( $tag , "href" );
				break;
			case "img":
			case "iframe":
			case "script":
				$tag = $this->makeAbsoluteUrl( $tag , "src" );
				break;
			default:
				break;	
			}
			return $tag;
		}	

	}

	class AdminNewsletterNewsletterEditPage extends BackendPopup {
		protected $nl_title;
		protected $nl_subject;
		protected $nl_submit;


		public function __construct( $id = "AdminNewsletterNewsletterEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;

			$this->newsletter_group_id = new AdminSelect( "newsletter_group_id" , $this );

			$this->nl_title = new AdminTextbox( "nl_title" , $this );
			$this->nl_is_html = new AdminCheckbox( "nl_is_html" , $this );
			$this->nl_is_html->setChecked( true );
			$this->nl_subject = new AdminTextbox( "nl_subject" , $this );
			$this->nl_head = new AdminTextarea( "nl_head" , $this );
			$this->nl_body = new AdminHtmlEdit2( "nl_body" , $this );
			$this->nl_submit = new AdminSubmit( "nl_submit" , $this );
			$this->newsletter_table = new AdminNewsletterTable( "AdminNewsletterTable" , $this );
		}

		protected function initializeWidgets() {
			$ctrlw = 760;

			$nm = new NewsletterModel();
			$this->newsletter_group_id->setTitle( __("newsletters_Newsletter_group" ));
			$this->newsletter_group_id->setShowTitle( true );
			$this->newsletter_group_id->setOptions( $nm->getNewsletterGroupOptions() );
			if (isset( $this->aG["newsletter_group_id"] ))
				$this->newsletter_group_id->setValue( $this->aG["newsletter_group_id"] );

			$this->nl_title->setTitle( __("newsletters_Newsletter_title") );
			$this->nl_title->setShowTitle( true );
			$this->nl_title->setWidth( 550 );
			$this->nl_title->setAttribute( "maxlength" , "128" );
		
			$this->nl_is_html->setTitle( __("newsletters_Newsletter_is_html") );
			$this->nl_is_html->setShowTitle( true );
			
			$this->nl_subject->setTitle( __("newsletters_Newsletter_subject") );
			$this->nl_subject->setShowTitle( true );
			$this->nl_subject->setWidth( $ctrlw );
			$this->nl_subject->setAttribute( "maxlength" , "128" );
			
			$this->nl_head->setTitle( __("newsletters_Newsletter_head") );
			$this->nl_head->setShowTitle( true );
			$this->nl_head->setWidth( $ctrlw );
			$this->nl_head->setRows( 4 );
			$this->nl_head->setAttribute( "maxlength" , "128" );
			
			$this->nl_body->setTitle( __("newsletters_Newsletter_body") );
			$this->nl_body->setShowTitle( true );
			$this->nl_body->setWidth( $ctrlw );
			$this->nl_body->setHeight( 160 );

			switch ($this->aG["mode"]) {
			case "new":
				$this->nl_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("newsletters_New_newsletter") );
				break;
			case "modify":
				$this->nl_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("newsletters_Modify_newsletter" ) );
				break;
			case "delete":
				$this->nl_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("newsletters_Delete_newsletter" ) );
				break;
			}
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
			}
		}

		protected function loadDatas( $uid ) {
			$nm = new NewsletterModel();
			$nl = $nm->getNewsletterFromId( $uid );

			$this->newsletter_group_id->setValue( $nl["newsletter_group_id"] );
			$this->nl_title->setValue( $nl["newsletter_title"] );
			$this->nl_subject->setValue( $nl["newsletter_subject"] );
			$this->nl_head->setValue( $nl["newsletter_head"] );
			$this->nl_body->setValue( $this->prepareBodyForLoad( $nl["newsletter_body"] ) );
			$this->nl_is_html->setChecked( $nl["newsletter_is_html"] );
		}

		protected function prepareBodyForLoad( $val ) {
			return $val;
		}

		protected function prepareBodyForSave( $val ) {
			$xmlp = new AdminNewsletterXmlToAbsolute();
			$retval =$xmlp->processHtmlTags( $val );
			return $retval;
		}

		protected function saveDatas( $uid ) {
			$this->refreshChildValuesFromArray();
			$p = array();
			$p["newsletter_group_id"]=$this->newsletter_group_id->getValue();
			$p["newsletter_title"]=$this->nl_title->getValue();
			$p["newsletter_subject"]=$this->nl_subject->getValue();
			if (empty( $p["newsletter_title"] )) $p["newsletter_title"]=$p["newsletter_subject"];
			if (empty( $p["newsletter_subject"] )) $p["newsletter_subject"]=$p["newsletter_title"];

			$p["newsletter_head"]=$this->nl_head->getValue();
			$p["newsletter_body"]=$this->prepareBodyForSave( $this->nl_body->getValue() );
			$p["newsletter_is_html"]=$this->nl_is_html->isChecked();
			$nm = new NewsletterModel();
			if ($uid)
				$nm->updateNewsletter( $uid , $p );
			else
				$nm->addNewsletter( $p );
		}
		
		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->nl_submit->getId()] )) return parent::handleEvents();
			switch ($this->aG["mode"]) {
			case "new":$this->saveDatas( 0 );break;
			case "modify":$this->saveDatas( $this->aG["uid"] );break;
			case "delete":$this->deleteDatas( $this->aG["uid"] );break;
			}
			$this->closeAndRefreshParent();
		}

		public function render() {
			if (!$this->isLoggedIn()) return parent::render();
			$this->initializeWidgets();
			if (isset( $this->aG["uid"] ))
				$this->setParam( "newsletter_id" , $this->aG["uid"] );
			return parent::render();
		}


	}

?>
