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

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

	class AdminMailtemplatesGridHeader extends AdminSuperGridHeader {
		protected $oKeywordFilter;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oKeywordsFilter = new AdminTextbox( "keyword_filter" , $this );
		}
	}

	class AdminMailtemplatesGrid extends BackendGrid {
		protected $template_name_filter;
		protected $template_title_filter;
		protected $template_subject_filter;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "mail_templates";
			$this->sTitle = __("mail_templates_Mail_templates");
			$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 );
			FNTH_include_3rdparty( "contextmenu" );
			FNTH_include_3rdparty( "tooltip" );
		}

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

		protected function whereClause() {
			$filters = array();
			// template_name filter
			$f = $this->template_name_filter->getId();
			if (!empty( $this->aG[$f] )) $filters[]=sprintf( "mail_templates.template_name like '%%%s%%'", $this->aG[$f] );

			// template_title filter
			$f = $this->template_title_filter->getId();
			if (!empty( $this->aG[$f] )) $filters[]=sprintf( "mail_templates.template_title like '%%%s%%'", $this->aG[$f] );
			// template_subject filter
			$f = $this->template_subject_filter->getId();
			if (!empty( $this->aG[$f] )) $filters[]=sprintf( "mail_templates.template_subject like '%%%s%%'", $this->aG[$f] );

			// template_language filter
			$f = $this->template_language_filter->getId();
			if (!empty( $this->aG[$f] )) $filters[]=sprintf( "mail_templates.site_language_id=%d" , $this->aG[$f] );
			
			// template_type filter
			$f = $this->template_type_filter->getId();
			if (isset( $this->aG[$f] ) && strlen( $this->aG[$f] ) ) $filters[]=sprintf( "mail_templates.mail_template_type_id=%d" , $this->aG[$f] );
			
			if (empty( $filters )) return "";
			$retval =  " where ".implode( " and " , $filters );
			return $retval;
		}

		public function orderClause() {
			switch ($this->sOrdering) {
			default:$retval = "";break;
			case "template_name": $retval="template_name";break;
			case "template_title": $retval="template_title";break;
			default:
			case "template_subject": $retval="template_subject";break;
			case "template_language": $retval="site_language_id";break;
			case "template_type": $retval="mail_template_type_id";break;
			}
			if (empty($retval)) return "";
			return sprintf( " order by %s%s",
				$retval,($this->bAscending) ? "" : " desc" );
		}

		protected function fieldsClause() {
			return "mail_templates.mail_template_id as id,
				mail_templates.template_name as template_name,
				mail_templates.template_title as template_title,
				mail_templates.template_subject as template_subject,
				mail_templates.site_language_id as template_language,
				mail_templates.mail_template_type_id as template_type";
		}

		public function constructColumns() {
			$this->addColumn( "template_name" , __("mail_templates_Template_name","attr") , true );
			$this->addColumn( "template_title" , __("mail_templates_Template_title","attr") , true );
			$this->addColumn( "template_subject" , __("mail_templates_Template_subject","attr") , true );
			$this->addColumn( "template_language" , __("mail_templates_Template_language","attr") , true );
			$this->addColumn( "template_type" , __("mail_templates_Template_type","attr") , true );
		}

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

			$field = new FNTHGridField( "template_name" , $headerrow );
			$this->template_name_filter = new AdminTextbox( "template_name_filter" , $field );
			$this->addOnEnterCallbackToWidget( $this->template_name_filter );
			
			$field = new FNTHGridField( "template_title" , $headerrow );
			$this->template_title_filter = new AdminTextbox( "template_title_filter" , $field );
			$this->addOnEnterCallbackToWidget( $this->template_title_filter );

			$field = new FNTHGridField( "template_subject" , $headerrow );
			$this->template_subject_filter  = new AdminTextbox( "template_subject_filter" , $field );
			$this->addOnEnterCallbackToWidget( $this->template_subject_filter );

			$field = new FNTHGridField( "template_language" , $headerrow );
			$this->template_language_filter = new AdminSelect( "template_language_filter" , $field );
			$langmodel = new LanguagesModel();
			$this->template_language_filter->setOptions( $langmodel->getSiteLanguages() );

			$field = new FNTHGridField( "template_type" , $headerrow );
			$this->template_type_filter = new AdminSelect( "template_type_filter" , $field );
			$mailtemplatemodel = new MailtemplatesModel();
			$this->template_type_filter->setOptions( $mailtemplatemodel->getMailTemplateTypes() );
		}

		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"] )) {
				parent::handleEvents();
				return;
			}
			if (!isset( $this->aP["tbl"] )) {
				parent::handleEvents();
				return;
			}
			if ($this->aP["tbl"]!=$this->getId()) {
				parent::handleEvents();
				return;
			}
			switch ($this->aP["action"]) {
			case "delete":
				$mtm = new MailtemplatesModel();
				foreach ($this->aP["ids"] as $id) $mtm->deleteMailTemplate( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			switch ($colname) {
			case "template_name":
				$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminMailtemplatesGridModify( %d );\">%s</a>" , $this->aData[$rownum]["id"] , $this->aData[$rownum]["template_name"] );
				$field->setValue( $val );
				break;
			case "template_language":
				$field->setAttribute( "align" , "center" );
				$field->setValue(
					$this->template_language_filter->getOption( $this->aData[$rownum]["template_language"] ) );
				break;
			case "template_type":
				$field->setAttribute( "align" , "center" );
				$field->setValue(
					$this->template_type_filter->getOption( $this->aData[$rownum]["template_type"] ) );
				break;
			}
			return $field;
		}

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

		public function render() {
			$this->generateLangdefs();
			return parent::render();
		}
	}

?>
