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

	class AdminNewsletterGroupEditPage extends BackendPopup {
		protected $ng_name;
		protected $ng_title;
		protected $ng_from_email;
		protected $ng_submit;


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

			$this->ng_name = new AdminTextbox( "ng_name" , $this );
			$this->ng_title = new AdminTextbox( "ng_title" , $this );
			$this->ng_from_email = new AdminTextbox( "ng_from_email" , $this );
			$this->ng_submit = new AdminSubmit( "ng_submit" , $this );
			$this->newsletter_table = new AdminNewsletterTable( "AdminNewsletterTable" , $this );
		}

		protected function initializeWidgets() {
			$this->ng_name->setTitle( __("newsletters_Newsletter_group_name") );
			$this->ng_name->setShowTitle( true );
			$this->ng_name->setWidth( 200 );
			$this->ng_name->setAttribute( "maxlength" , "128" );

			$this->ng_title->setTitle( __("newsletters_Newsletter_group_title") );
			$this->ng_title->setShowTitle( true );
			$this->ng_title->setWidth( 200 );
			$this->ng_title->setAttribute( "maxlength" , "128" );

			$this->ng_from_email->setTitle( __("newsletters_Newsletter_group_from_email") );
			$this->ng_from_email->setShowTitle( true );
			$this->ng_from_email->setWidth( 200 );
			$this->ng_from_email->setAttribute( "maxlength" , "128" );

			switch ($this->aG["mode"]) {
			case "new":
				$this->ng_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("newsletters_New_newsletter_group") );
				break;
			case "modify":
				$this->ng_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("newsletters_Modify_newsletter_group" ) );
				break;
			case "delete":
				$this->ng_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("newsletters_Delete_newsletter_group" ) );
				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();
			$group = $nm->getNewsletterGroupFromId( $uid );
			$this->ng_name->setValue( $group["newsletter_group_name"] );
			$this->ng_title->setValue( $group["newsletter_group_title"] );
			$this->ng_from_email->setValue( $group["newsletter_group_from_email"] );
		}

		protected function saveDatas( $uid ) {
			$this->refreshChildValuesFromArray();
			$p = array();
			$p["newsletter_group_name"]=$this->ng_name->getValue();
			$p["newsletter_group_title"]=$this->ng_title->getValue();
			$p["newsletter_group_from_email"]=$this->ng_from_email->getValue();
			$nm = new NewsletterModel();
			if ($uid)
				$nm->updateNewsletterGroup( $uid , $p );
			else
				$nm->addNewsletterGroup( $p );
		}
		
		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->ng_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( "user_id" , $this->aG["uid"] );
			return parent::render();
		}


	}

?>
