<?php
/*
	Stempora Web Framework
	Copyright (c) 2002-2014 Stempora. 
	All rights reserved.
		web:  www.stempora.com
		mail: support@stempora.com		
*/

// dependencies

/**
* description
*
* @library	
* @author	
* @since	
*/
class CLanguages extends CPlugin{
	
	var $tplvars; 

	function CLanguages() {
		//$this->CPlugin($db, $tables , $templates);
		$this->name = "languages";
	}

	function DoEvents(){
		global $base, $_CONF, $_TSM , $_VARS , $_USER , $_BASE , $_SESS;

		parent::DoEvents();

		if ($_GET["mod"] == "languages") {

			$sub = $_GET["sub"];
			$action = $_GET["action"];

			$this->tpl_module = array(
				"module_name"	=> "Languages"
			);

		
			//$lang = $this->DefaultLang();
			//$_GET["lang_id"] = $lang["lang_id"];

			switch ($sub) {
				case "landing":					
					$data = new CSQLAdmin($sub, $this->__parent_templates,$this->db,$this->tables,$extra);
					$this->PrepareFields($data->forms["forms"]);
					return $data->DoEvents();
				break;

				case "settings":
		
					$data = new CFormSettings($this->forms_path  . $sub . ".xml" ,$_CONF["forms"]["admintemplate"] , $this->db,$this->tables);

					$this->PrepareDashboard($data->form);
					$this->LangPrepareFields($data->form);

					if ($data->Done()) {
						$this->vars->SetVars(
							$this->vars->ExtractVals(
								CForm::KeyFields($data->form),
								$_POST
							)
						);
					}
					
					return $data->Show(
						$this->vars->GetVars(
							CForm::KeyFields($data->form)
						)
					);
				break;


			}
		}
	}


	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function UpdateVars() {
		global $_AJAX , $base;

			//debug($_POST,1);

		if (is_array($_POST["lang_var"])) {



			foreach ($_POST["lang_var"] as $key => $val) {

				$tmp = array();

				foreach ($val as $k => $v) {
					$tmp["lang_val_{$k}"] = $v;
				}


				
				$this->db->QueryUpdate(
					$this->tables['core:languages_vars'],
					$tmp , 
					"lang_var LIKE '{$key}'"
				);

			}			
		}

		if ($_AJAX) {

			return $base->htmlElements->AlertSmall(
				"neutral",
				"Autosaved..."
			);
		}
	

		if ($_GET["returnurl"]) {
			urlredirect(urldecode($_GET["returnurl"]));
		} else {
			urlredirect("index.php?mod=languages&sub=vars");
		}
		
		
	}
	

	function DefaultLang($noupdate = false) {
		global $_LANG_ID;

		if (isModSub("languages" , "landing"))  {
			return "";
		}
		

		$lang = $this->db->QFetchArray("SELECT * FROM {$this->tables['core:languages']} WHERE lang_default=1 LIMIT 1");

		if (!$_GET["lang_id"]) {
			$_GET["lang_id"] = $lang["lang_id"];
		}

		if (!$_LANG_ID) {
			$_LANG_ID = $_GET["lang_id"];
		}
		
		
						
		return $lang;
	}

	/**
	* description
	*
	* @param
	*
	* @return
	*
	* @access
	*/
	function GetLanguages() {

		return $this->db->QFetchRowArray("SELECT * FROM {$this->tables['core:languages']} ");

	}
	
	
}

?>