<?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/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextarea.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminHtmlEdit2.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );

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

	class AdminMailtemplateEditPage extends BackendPopup {
		protected $site_language_id;
		protected $mail_template_type_id;
		protected $template_name;
		protected $template_title;
		protected $template_subject;
		protected $template_body;
		protected $template_submit;

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

			$this->site_language_id = new AdminSelect( "site_language_id" , $this );
			$this->mail_template_type_id = new AdminSelect( "mail_template_type_id" , $this );
			$this->template_name = new AdminTextbox( "template_name" , $this );
			$this->template_title = new AdminTextbox( "template_title" , $this );
			$this->template_subject = new AdminTextbox( "template_subject" , $this );
			$this->template_body = new AdminHtmlEdit2( "template_body" , $this );
			$this->template_submit = new AdminSubmit( "template_submit" , $this );
		}

		protected function initializeWidgets() {
			$lang = new LanguagesModel();
			$mt = new MailtemplatesModel();

			$this->site_language_id->setTitle( __("mail_templates_Template_language" ) );
			$this->site_language_id->setShowTitle( true );
			$this->site_language_id->setCSSAttribute( "width" , "220px" );
			$this->site_language_id->setOptions( $lang->getSiteLanguages( false ) );

			$this->mail_template_type_id->setTitle( __("mail_templates_Template_type" ) );
			$this->mail_template_type_id->setShowTitle( true );
			$this->mail_template_type_id->setCSSAttribute( "width" , "220px" );
			$this->mail_template_type_id->setOptions( $mt->getMailTemplateTypes( false ) );

			$this->template_name->setTitle( __("mail_templates_Template_name") );
			$this->template_name->setCSSAttribute( "width" , "300px" );
			$this->template_name->setShowTitle( true );
			$this->template_name->setAttribute( "tabindex" , "1" );
			
			$this->template_title->setTitle( __("mail_templates_Template_title" ) );
			$this->template_title->setCSSAttribute( "width" , "500px" );
			$this->template_title->setShowTitle( true );
			$this->template_title->setAttribute( "tabindex" , "2" );
			
			$this->template_subject->setTitle( __("mail_templates_Template_subject" ) );
			$this->template_subject->setCSSAttribute( "width" , "700px" );
			$this->template_subject->setShowTitle( true );
			$this->template_subject->setAttribute( "tabindex" , "3" );

			$this->template_body->setWidth( 740 );
			$this->template_body->setHeight( 370 );
			$this->template_body->setTitle( __("mail_templates_Template_body"), $this );
			$this->template_body->setShowTitle( true );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new": $this->template_submit->setValue( __( "New","attr" ) );break;
			case "modify": $this->template_submit->setValue( __( "Modify","attr" ) );break;
			case "delete": $this->template_submit->setValue( __( "Delete","attr") );break;
			}
			// load datas
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
				break;
			}
		}

		protected function loadDatas( $uid ) {
			$this->setParam( "uid" , $uid );
			$mt = new MailTemplatesModel();
			$datas = $mt->getMailTemplateFromId( $uid );
			$this->site_language_id->setValue( $datas["site_language_id"] );
			$this->mail_template_type_id->setValue( $datas["mail_template_type_id"] );
			$this->template_name->setValue( $datas["template_name"] );
			$this->template_title->setValue( $datas["template_title"] );
			$this->template_subject->setValue( $datas["template_subject"] );
			$this->template_body->setValue( $datas["template_content"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$mt = new MailTemplatesModel();
			$datas = array(
				"site_language_id" => $this->aP[$this->site_language_id->getId()],
				"mail_template_type_id" => $this->aP[$this->mail_template_type_id->getId()],
				"template_name" => $this->aP[$this->template_name->getId()],
				"template_title" => $this->aP[$this->template_title->getId()],
				"template_subject" => $this->aP[$this->template_subject->getId()],
				"template_content" => $this->aP[$this->template_body->getId()]
			);
			if (!$uid)
				$mt->addMailTemplate( $datas );
			else
				$mt->updateMailTemplate( $uid , $datas );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->template_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();
			return parent::render();
		}
	}
?>
