<?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/widgets/AdminNewsletterUserGroups.inc.php" );

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

	class AdminNewsletterUserEditPage extends BackendPopup {
		protected $nu_name;
		protected $nu_email;
		protected $nu_submit;
		protected $newsletter_table;
		protected $newsletter_groups;


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

			$this->nu_name = new AdminTextbox( "nu_name" , $this );
			$this->nu_email = new AdminTextbox( "nu_email" , $this );
			$this->nu_submit = new AdminSubmit( "nu_submit" , $this );
			$this->newsletter_table = new AdminNewsletterTable( "AdminNewsletterTable" , $this );
			$this->newsletter_groups = new AdminNewsletterUserGroups( "AdminNewsletterGroups" , $this );
		}

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

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

			switch ($this->aG["mode"]) {
			case "new":
				$this->nu_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("newsletters_New_newsletter_user") );
				break;
			case "modify":
				$this->nu_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("newsletters_Modify_newsletter_user" ) );
				break;
			case "delete":
				$this->nu_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("newsletters_Delete_newsletter_user" ) );
				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->getNewsletterUserFromId( $uid );
			$this->nu_name->setValue( $group["newsletter_user_name"] );
			$this->nu_email->setValue( $group["newsletter_user_email"] );

			$ng_ids = $nm->getNewsletterUsersGroups( $uid );
			foreach ($ng_ids as $ng_id)
				$this->newsletter_groups->setChecked( $ng_id );
		}

		protected function saveDatas( $uid ) {
			$this->refreshChildValuesFromArray();
			$p = array();
			$p["newsletter_user_name"]=$this->nu_name->getValue();
			$p["newsletter_user_email"]=$this->nu_email->getValue();
			$nm = new NewsletterModel();
			if ($uid)
				$nm->updateNewsletterUser( $uid , $p );
			else
				$uid = $nm->addNewsletterUser( $p );
			$nm->setNewsletterUsersGroups( $uid , $this->newsletter_groups->getCheckeds() );
		}
		
		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->nu_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();
		}


	}

?>
