<?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 CSettings extends CPlugin{
	
	var $tplvars; 

	function CSettings() {
		//$this->CPlugin($db, $tables , $templates);
	}

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

		parent::DoEvents();

		if ($_GET["mod"] == "settings") {
			$sub = $_GET["sub"];
			$action = $_GET["action"];

			//$this->__install();

			$this->tpl_module = array("mod_name" => "Global Settings" );


			switch ($sub) {

				case "settings":
				case "landing":

					$_GET["sub"] = $sub = "generic";
				case "google":
				case "facebook":
				case "bing":
				case "twitter":
				case "generic":

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

						if ($data->Done()) {

							$_POST["show_settings"] = "0";

							if (!$_POST["set_logo"]) {
								unset($_POST["set_logo"]);
								unset($data->form["fields"]["box"][0]["fields"]["set_logo"]);
							}

							if (!$_POST["set_logo_small"]) {
								unset($_POST["set_logo_small"]);
								unset($data->form["fields"]["box"][0]["fields"]["set_logo_small"]);
							}

							if (!$_POST["set_favicon"]) {
								unset($_POST["set_favicon"]);
								unset($data->form["fields"]["box"][0]["fields"]["set_favicon"]);
							}

							$fields = CForm::KeyFields($data->form);
							$fields[] = "set_logo_alt";
							$fields[] = "set_logo_small_alt";
							$fields[] = "set_favicon_file";

							$this->SavePageConf();


							$this->vars->SetVars(
								$this->vars->ExtractVals(
									$fields,
									$_POST
								)
							);
						}

						$fields = CForm::KeyFields($data->form);
						$fields[] = "set_logo_alt";
						
						return $data->Show(							
							$this->vars->GetVars(
								$fields
							)
						
						);
				break;

				case "vars":					
					$data = new CSQLAdmin($sub, $this->__parent_templates,$this->db,$this->tables,$extra);

					$this->PrepareFields($data->forms["forms"]);
					return $data->DoEvents();
				break;

			}
		}
	}


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

		$path = "../upload/conf/";

		$js = explode("," , $_POST["set_page_js"]);

		if (in_array("1" , $js))
			CFile::Save($path . "js_minify" , 1);
		else 
			CFile::Remove($path . "js_minify");

		if (in_array("2",$js))
			CFile::Save($path . "js_concatenate" , 1);
		else 
			CFile::Remove($path . "js_concatenate");

		if (in_array("3" ,  $js)) {
			CFile::Save($path . "js_cache" , 1);

			$css = new CDir("../upload/_cache/" , ".js");

			if (is_array($css->_files) && count($css->_files)) {
				foreach ($css->_files as $key => $val) {
					CFile::Remove($val);
				}				
			}

		} else {
			CFile::Remove($path . "js_cache");
		}


		$css = explode("," , $_POST["set_page_css"]);

		if (in_array("1" , $css))
			CFile::Save($path . "css_minify" , 1);
		else 
			CFile::Remove($path . "css_minify");

		if (in_array("2",$css))
			CFile::Save($path . "css_concatenate" , 1);
		else 
			CFile::Remove($path . "css_concatenate");

		if (in_array("3" ,  $css)) {
			CFile::Save($path . "css_cache" , 1);

			$css = new CDir("../upload/_cache/" , ".css");

			if (is_array($css->_files) && count($css->_files)) {
				foreach ($css->_files as $key => $val) {
					CFile::Remove($val);
				}				
			}
						
		} else  {
			CFile::Remove($path . "css_cache");
		}

	}
	

}

?>