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

	class AdminLangdefsGridHeader extends AdminSuperGridHeader {
		public $oKeywordFilter;
		public $oDeveloperFilter = null;
		public $oFreetextFilter = null;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oKeywordsFilter = new AdminTextbox( "keyword_filter" , $this );
		}
		
		public function modifyGetPost( &$g , &$p ) {
			$retval = parent::modifyGetPost( $g , $p );
			if (isset( $this->aG["dev"] )) {
				$this->oDeveloperFilter = new AdminTextbox( "developer" , $this );
				$this->oDeveloperFilter->setTitle( "Developer" );
				$this->oDeveloperFilter->setShowTitle( true );
				$this->oDeveloperFilter->setWidth( 500 );
				$this->oParent->oParent->oParent->addOnEnterCallbackToWidget( $this->oDeveloperFilter );
				
				$this->oFreetextFilter = new AdminTextbox( "freetext" , $this );
				$this->oFreetextFilter->setTitle( "Freetext!!" );
				$this->oFreetextFilter->setShowTitle( true );
				$this->oFreetextFilter->setWidth( 500 );
				$this->oParent->oParent->oParent->addOnEnterCallbackToWidget( $this->oFreetextFilter );
			}	
			return $retval;
		}


	}

	class AdminLangdefsGrid extends BackendGrid {
		protected $ldef_namefilter;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "ldefs";
			$this->sIdField = "ldef_name";
			$this->bIdNumeric = false;

			$this->sTitle = __("langdefs_Langdefs");
			$this->setSelectable( true );

			$this->addToolbarButton( "filter" , __("Filter","attr") , "search",$this->renderStandardAction( "filter" ) );
			$this->addToolbarButton( "import" , __("Import","attr") , "database_add",$this->renderStandardAction( "import" ) );
			$this->addToolbarButton( "export" , __("Export","attr") , "database_next",$this->renderStandardAction( "export" ) );

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

		protected function prepareDataset() {
			if (empty( $this->aData )) return;

			// get ids
			$ids = array();
			foreach ($this->aData as $row) $ids[]=FNTH_db_addslashes( $row["ldef_name"] );

			$query = sprintf( "select * from ldef_defs where ldef_name in ('%s')",implode( "','" , $ids ) );
			$ds = FNTH_db_get_resultset( $query );
			// load languages
			$langdefs = array();
			foreach ($ds as $row) {
				$lang_id = $row["site_language_id"];
				$ldef_name = $row["ldef_name"];
				$def = $row["def"];

				if (!isset( $langdefs[$lang_id] )) $langdefs[$lang_id]=array();
				if (!isset( $langdefs[$lang_id][$ldef_name] )) $langdefs[$lang_id][$ldef_name]=$def;
			}
			// build it up
			$r = array();
			foreach ($this->aData as $row) {
				foreach ($GLOBALS["languages"] as $langcode => $langid ) {
					if (!isset( $langdefs[$langid][$row["ldef_name"]] )) continue;
					$row[$langcode]=$langdefs[$langid][$row["ldef_name"]];
				}
				$r[]=$row;
			}
			$this->aData = $r;
		}

		protected function whereClause() {
			$filters = array();
			// ldef_namefilter
			$f = $this->ldef_namefilter->getId();
			if (!empty( $this->aG[$f] )) {
				$filters[]=sprintf( "ldef_name ilike '%%%s%%'", $this->aG[$f] );
			}
			if (isset( $this->oSuperGridHeader->oDeveloperFilter )) {
				$val = $this->oSuperGridHeader->oDeveloperFilter->getValue();
				if (strlen( $val )) $filters[]=$val;
			}
			if (isset( $this->oSuperGridHeader->oFreetextFilter )) {
				$val = $this->oSuperGridHeader->oFreetextFilter->getValue();
				if (strlen( $val )) $filters[]=sprintf( "ldef_name in (select ldef_name from ldef_defs where def like '%%%s%%')" , FNTH_db_addslashes( $val ) );
			}
			if (empty( $filters )) return "";
			return sprintf( " where %s" , implode( " and " , $filters ) );
		}

		protected function orderClause() {
			return " order by ldef_name";
		}

		public function constructColumns() {
			$this->addColumn( "ldef_name" , __("langdefs_Name","attr") , true );
			foreach ($GLOBALS["fo3_langdefs"] as $langiso => $langcode ) {
				$this->addColumn( "language_".$langiso , $langiso , false );
			}
		}

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

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

		protected function handleImportEvent() {
			if (!isset( $_FILES["importfile"] )) return;
			$file = $_FILES["importfile"];
			if (!isset( $file["error"] )) return;
			if ($file["error"]) return;

			$lm = new LangdefsModel();
			$p = pathinfo( $file["name"] );
			switch ($p["extension"]) {
			case "ini":
				$parsed = parse_ini_file( $file["tmp_name"] , true );
				foreach ($parsed as $langcode => $langdefs) {
					$langid = $GLOBALS["languages"][$langcode];
					foreach ($langdefs as $def => $val ) {
						$lm->addSetLangdefDef( $langid , $def , $val );
					}
				}
			}
			FNTH_refresh();
		}

		public function handleEvents() {
			// simple POST tasks
			if (isset( $this->aP["task"] )) {
				switch ($this->aP["task"]) {
				case "import":
					$this->handleImportEvent();
					break;
				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();
				}
				return parent::handleEvents();
			}

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

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

			switch ($colname) {
			case "ldef_name":
				$ldef_name = $this->aData[$rownum]["ldef_name"];
				$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminLangdefsGridModify( '%s' );\">%s</a>",
					$ldef_name,
					FNTH_cut_string( $ldef_name ) );
				$field->setValue( $val );
				break;
			case "chk":
				break;
			default:
				$c = str_replace( "language_" , "" , $colname );
				$val = (isset( $rd[$c] )) ? $rd[$c] : "-";
				$field->setValue( $val );
			}
			return $field;
		}

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

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

	}


?>
