<?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" );

	class AdminLanguagesGridHeader extends AdminSuperGridHeader {
		protected $oKeywordFilter;

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

	class AdminLanguagesGrid extends BackendGrid {
		protected $language_filter;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "languages";
			$this->sTitle = __("languages_Languages");
			$this->setSelectable( true );
			$this->iPagesize = 200;

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

		protected function prepareDataset() {
			$n = array();
			$all = array();
			$lm = new LanguagesModel();
			foreach ($GLOBALS["languages"] as $langcode) {
				$all[$langcode]=$lm->getLanguageOptions( false , $langcode );
			}
			foreach ($this->aData as $row) {
				foreach ($GLOBALS["languages"] as $langiso => $langcode)
					$row["language_".$langiso]=
						(isset( $all[$langcode][$row["id"]] )) ? $all[$langcode][$row["id"]] : "-";
				$n[]=$row;
			}
			$this->aData = $n;
			if ($this->sOrdering=="") $this->defaultSorting();
		}

		protected function lorder( $a , $b ) {
			$va = $this->lordering[ $a["id"] ];
			$vb = $this->lordering[ $b["id"] ];
			if ($va==$vb) return 0;
			return ($va>$vb) ? 1 : -1;
		}

		protected function defaultSorting() {
			$query = sprintf( "select language_id,langdef_ordering from language_langdefs where site_language_id=%d" , $GLOBALS["langcode"] );
			$this->lordering = FNTH_db_get_associative( $query , "language_id" , "langdef_ordering" );
			usort( $this->aData , array( $this , "lorder" ) );
		}

		protected function whereClause() {
			$retval="";
			// language_filter
			$f = $this->language_filter->getId();
			if (!empty( $this->aG[$f] )) {
				$retval.=(!empty( $retval )) ? " and " : "";
				$retval.=sprintf( "language_name like '%%%s%%'", $this->aG[$f] );
			}
			if (empty( $retval )) return "";
			return " where " . $retval;
		}

		protected function orderClause() {
			$field = "";
			$ascending = $this->bAscending;

			switch ($this->sOrdering) {
			default:
				$field = "language_id";
				break;
			case "language_name":
				$field = "language_name";
				break;
			}
			$retval = " order by ".$field;
			if (!$ascending) $retval.=" desc";
			return $retval;
		}

		protected function fieldsClause() {
			return "language_id as id,language_name";
		}

		public function constructColumns() {
			$this->addColumn( "language_name" , __("languages_Name","attr") , true );
			foreach ($GLOBALS["languages"] as $langiso => $langcode ) {
				// if ($langcode==1) continue;
				$val = $langiso;
				$val.=$this->renderMiniButton(
					$langiso,
					"sort",
					__("Sort","attr"),
					"process",
					$this->renderStandardAction(
						"sort" , array(
							"language_id" => $langcode,
							"langiso" => $langiso )
					)
				);
					
				$this->addColumn( "language_".$langiso , $val , false );
			}
		}

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

			$field = new FNTHGridField( "language_name" , $headerrow );
			$this->language_filter = new AdminTextbox( "language_filter" , $field );
			
		}

		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 LanguagesModel();
				foreach ($this->aP["ids"] as $id) $lm->deleteLanguage( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

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

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

	}


?>
