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

	class AdminCountriesGridHeader extends AdminSuperGridHeader {
		protected $oKeywordFilter;

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

	class AdminCountriesGrid extends BackendGrid {
		protected $country_filter;
		protected $country_iso_filter;
		protected $langfilters = array();

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->bIdNumeric = false;
			$this->sTableName = "countries";
			$this->sTitle = __("countries_Countries");
			$this->setSelectable( true );
			$this->iPagesize = 20;

			$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 AdminCountriesGridHeader( $id , $parent );
		}

		protected function prepareDataset() {
			$n = array();
			$all = array();
			$lm = new CountriesModel();
			foreach ($GLOBALS["languages"] as $langcode) {
				$all[$langcode]=$lm->getCountryOptions( false , $langcode );
			}
			foreach ($this->aData as $row) {
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					$l =(isset( $all[$langcode][$row["id"]] )) ? $all[$langcode][$row["id"]] : "-";
					$row["country_".$langiso]=$l;
				}
				$n[]=$row;
			}
			$this->aData = $n;
		}

		protected function whereClause() {
			$retval="";
			// country_filter
			$f = $this->country_filter->getId();
			if (!empty( $this->aG[$f] )) {
				$retval.=(!empty( $retval )) ? " and " : "";
				$retval.=sprintf( "country_name ilike '%%%s%%'", $this->aG[$f] );
			}
			// country_iso_filter
			$f = $this->country_iso_filter->getId();
			if (!empty( $this->aG[$f] )) {
				$retval.=(!empty( $retval )) ? " and " : "";
				$retval.=sprintf( "country_id ilike '%%%s%%'", $this->aG[$f] );
			}
			// langfilters
			foreach ($this->langfilters as $wdg) {
				$f = $wdg->getId();
				if (!empty( $this->aG[$f] )) {
					$langiso = str_replace( "lang_" , "" , str_replace( "_filter" , "" , $f ) );
					$langcode = $GLOBALS["languages"][$langiso];
					$retval.=(!empty( $retval )) ? " and " : "";
					$retval.=sprintf( "country_id in (select country_id from country_langdefs where language_id=%d and langdef_name ilike '%%%s%%')", $langcode , FNTH_db_addslashes( $wdg->getValue() ) );
				}
			}

			if (empty( $retval )) return "";
			return " where " . $retval;
		}

		protected function orderClause() {
			$retval="";
			switch ($this->sOrdering) {
			default:
			case "country_name":
				$retval="country_name";
				break;
			case "country_id":
				$retval="country_id";
				break;
			/*
			default:
				return " order by country_id";
			*/
			}
			if (!strlen( $retval )) return "";
			if (!$this->bAscending) $retval.=" desc";
			return " order by ".$retval;
		}

		protected function fieldsClause() {
			return "country_id as id,country_id,country_name";
		}

		public function constructColumns() {
			$this->addColumn( "country_name" , __("countries_Official","attr") , true );
			$this->addColumn( "country_id" , __("countries_Iso","attr") , true );
			foreach ($GLOBALS["languages"] as $langiso => $langcode ) {
				$this->addColumn( "country_".$langiso , $langiso , false );
			}
		}

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

			$field = new FNTHGridField( "country_name" , $headerrow );
			$this->country_filter = new AdminTextbox( "country_filter" , $field );

			$field = new FNTHGridField( "country_iso" , $headerrow );
			$this->country_iso_filter = new AdminTextbox( "country_iso_filter" , $field );
			$this->country_iso_filter->setWidth( 50 );

			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				$field = new FNTHGridField( "lang_".$langiso , $headerrow );
				$f = new AdminTextbox( "lang_".$langiso."_filter" , $field );
				$f->setWidth( 80 );
				$this->langfilters[] = $f;
			}
			
		}

		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();
			}
			switch ($this->aP["action"]) {
			case "delete":
				$lm = new CountriesModel();
				foreach ($this->aP["ids"] as $id) $lm->deleteCountry( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			switch ($colname) {
			case "country_name":
				$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminCountriesGridModify( '%s' );\">%s</a>" , $this->aData[$rownum]["id"] , $this->aData[$rownum]["country_name"] );
				$field->setValue( $val );
				break;
			case "country_id":
				$field->setAttribute( "align" , "center" );
			default:
			}
			return $field;
		}
		protected function generateLangDefs() {
			fo3_add_js_langdef( "languages_confirm_Delete_language" );
		}

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

	}


?>
