<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/BackendGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminCheckbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSuperGridHeader.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTristate.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminDeleted.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminDateRange.inc.php" );

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

	class AdminNewsletterGroupsGridHeader extends AdminSuperGridHeader {
		public $oKeywordFilter;
		public $oDeletedFilter;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oKeywordFilter = new AdminTextbox( "keyword" , $this );
			$this->oKeywordFilter->setTitle( __("newsletters_Keyword" ));
			$this->oKeywordFilter->setShowTitle( true );

			$this->oDeletedFilter = new AdminDeleted( "deleted" , $this );
		}
	}

	class AdminNewsletterGroupsGrid extends BackendGrid {
		protected $ng_name_filter;
		protected $ng_title_filter;
		protected $ng_from_email_filter;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "newsletter_groups";
			$this->sTitle = __("newsletters_Newsletter_groups");
			$this->setSelectable( true );

			$this->addToolbarButton( "filter" , __("Filter","attr") , "search",$this->renderStandardAction( "filter" ) );
			$this->addToolbarButton( "new" , __("New","attr") , "page_add" , $this->renderStandardAction( "new" ) );
			$this->addToolbarButton( "modify" , __("Modify","attr") , "page_edit" , $this->renderStandardAction( "modify" ) , false );
			$this->addToolbarButton( "delete" , __("Delete","attr") , "page_remove",$this->renderStandardAction( "delete" ) , false );
			$this->addToolbarButton( "users" , __("newsletters_Newsletter_users","attr") , "users",$this->renderStandardAction( "allusers" ) );
			FNTH_include_3rdparty( "contextmenu" );
			FNTH_include_3rdparty( "tooltip" );
		}

		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return new AdminNewsletterGroupsGridHeader( $id , $parent );
		}

		protected function whereClause() {
			$filters = array();

			// ng_name_filter
			$val = $this->ng_name_filter->getValue();
			if (!empty($val))
				$filters[]=sprintf( "newsletter_groups.ng_name like '%%%s%%'", FNTH_db_addslashes( $val ));
			
			// ng_title_filter
			$val = $this->ng_title_filter->getValue();
			if (!empty($val))
				$filters[]=sprintf( "newsletter_groups.ng_title like '%%%s%%'", FNTH_db_addslashes( $val ));
			
			// ng_from_email_filter
			$val = $this->ng_from_email_filter->getValue();
			if (!empty($val))
				$filters[]=sprintf( "newsletter_groups.ng_from_email like '%%%s%%'", FNTH_db_addslashes( $val ));
		
			if (empty( $filters )) return "";
			return sprintf( " where %s" , implode( " and " , $filters ) );
		}

		protected function fieldsClause() {
			return "newsletter_groups.newsletter_group_id as id,
				newsletter_groups.ng_name,
				newsletter_groups.ng_title,
				newsletter_groups.ng_from_email";
		}

		public function constructColumns() {
			$this->addColumn( "ng_name" , __("newsletters_Newsletter_group_name","attr") , true );
			$this->addColumn( "ng_title" , __("newsletters_Newsletter_group_title","attr") , true );
			$this->addColumn( "ng_from_email" , __("newsletters_Newsletter_group_from_email","attr") , true );
			$this->addColumn( "admin" , __("newsletters_Admin","attr") , false );
		}

		public function constructFilterHeader() {
			$headerrow = new FNTHGridRow( "filterrow" , $this , "header" );
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures

			$field = new FNTHGridField( "ng_name" , $headerrow );
			$this->ng_name_filter = new AdminTextbox( "ng_name" , $field );

			$field = new FNTHGridField( "ng_title" , $headerrow );
			$this->ng_title_filter = new AdminTextbox( "ng_title" , $field );
			
			$field = new FNTHGridField( "ng_from_email" , $headerrow );
			$this->ng_from_email_filter = new AdminTextbox( "ng_from_email" , $field );
			
			$field = new FNTHGridField( "admin" , $headerrow );
			return $headerrow;
		}

		public function handleEvents() {
			// simple POST tasks
			if (isset( $this->aP["task"] )) {
				switch ($this->aP["task"]) {
				case "filter":
					$u = $this->aG;
					foreach ($this->aP as $k => $v) {
						if ($k=="task") continue;
						$u[$k]=$v;
					}
					unset( $u[ $this->getId()."_pagenum"] );
					FNTH_redirect( FNTH_render_url( $_SERVER["PHP_SELF"] , $u ) );
					fo_exit();
				}
			}

			// ajax events
			if (!isset( $this->aP["ajaxevent"] )) {
				return parent::handleEvents();
			}
			if (!isset( $this->aP["tbl"] )) {
				return parent::handleEvents();
			}
			if ($this->aP["tbl"]!=$this->getId()) {
				return parent::handleEvents();
			}
			$action_reply = "";
			switch ($this->aP["action"]) {
			case "delete":
				if (!empty( $this->aP["ids"])) {
					$nm = new NewsletterModel();
					foreach ($this->aP["ids"] as $ng_id)
						$nm->deleteNewsletterGroup( $ng_id );
				}	
				$action_reply = "document.location.href=document.location.href;";
				break;
			}
			FNTH_direct_echo( $action_reply );
			fo_exit();
		}

		public function constructRow( $datarow , $rownum ) {
			$retval = parent::constructRow( $datarow , $rownum );
			// $retval->setSelectable( !isset( $datarow["status_senttstamp"] ));
			return $retval;
		}

		public function constructField( $rownum , $row , $colname ) {
			$rd = $this->aData[$rownum];
			$field = parent::constructField( $rownum , $row , $colname );

			switch ($colname) {
			case "ng_name":
				$val=sprintf( "<a class=\"green\" href=\"javascript:AdminNewsletterGroupsGridModify( %d );\">%s</a>",
					$rd["id"],$rd["ng_name"] );
				$field->setValue( $val );
				break;
			case "admin":
				$field->setAttribute( "align" , "center" );
				$prefix = $this->getId()."_".$rd["id"];
				$buttons=array();
				$buttons[]=$this->renderMiniButton(
					$prefix,
					"newsletters",
					__("newsletters_Newsletters","attr"),
					"applications",
					$this->renderStandardAction(
						"newsletters",
						array( "newsletter_group_id" => $rd["id"] )
					)
				);
				$buttons[]=$this->renderMiniButton(
					$prefix,
					"users",
					__("newsletters_Newsletter_users","attr"),
					"users",
					$this->renderStandardAction(
						"users",
						array( "newsletter_group_id" => $rd["id"] )
					)
				);
				$field->setValue( implode( " " , $buttons ) );
				break;
			}
			return $field;
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "newsletters_Confirm_delete_newsletter_group" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();
			return parent::render();
		}

	}

?>
