<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

include_once './core/logger.class.php';
include_once './core/main.class.php';

#[\AllowDynamicProperties]
class Config extends Main {

	protected $base_object;
	protected $db;

	public $list_table_config = array (
		'title' => 'Systemeinstellungen',
		'db_table' => 'configuration',
		'list_fields' => array(
			array(
				'db_field' => 'group_id',
				'name' => 'Einstellungen',
				'sortable' => 1
			)
		),
		'list_group_by' => 'group_id',
		'permanent_filter' => array(
			array(
				'db_field' => 'group_id',
				'value' => '',
				'relational_operator' => '!='
			)
		),
		'search_fields' => array('group_id', 'name', 'text'),
		'db_id_field' => 'group_id',
		'edit_link' => 'index.php?admin_modul=admin_object_edit&object=Config&object_id=',
		'toolbar' => array(
			'delete' => 0,
			'new' => 0,
			'copy' => 0,
			'select_all' => 1,
			'edit' => 0,
			'actions' => 0,
			'filter' => 0,
			'search' => 1
		),
		'edit_title' => 'Systemeinstellung',
		'edit_fields' => array (),
		'edit_mandatory_fields' => array(),
		'edit_toolbar' => array(
			'close' => 'index.php?admin_modul=admin_object_list&object=Config',
			'copy' => 0,
			'undo' => 0,
			'redo' => 0,
			'save' => 1,
			'delete' => 0
		)
	);

	/*$resulttba = $mysqli->query("SELECT * FROM configuration", MYSQLI_USE_RESULT);

	$pdo = new PDO('mysql:host=newmail.intelectra.de;dbname=webshop-sql2022', 'webshopadm', 'Je=53qi2Jey53qi2');
	$statement = $pdo->query("SELECT * FROM configuration");
	$row = $statement->fetch(PDO::FETCH_ASSOC);
	echo htmlentities($row['configuration']);

	print $resulttba;*/

	function __construct($base_object) {

		$this->db = $base_object->db;
		$this->config = $base_object->config;

		// Config
		$sql = "SELECT * FROM configuration";

		$result = $this->db->query($sql);

		$text_values = array();


		while ($obj = $result->fetch_object()) {
			if ($obj->text == 'formated' || $obj->text == 'multi') {
				$text_values[] = $obj;
			} else if ($obj->type == 2) {
				$data = array();
				eval($obj->setting);
				$this->shopConfiguration[$obj->name] = $data;
			} else {
				$this->shopConfiguration[$obj->name] = $obj->setting;
			}
		}

		foreach ($text_values as $obj) {
			$this->shopConfiguration[$obj->name] = str_replace('{$THEME_DIR}', STANDARD_DIR.'web/'.SHOP_SYSTEM.'/themes/'.$this->shopConfiguration['THEME'], $obj->setting);
		}

		// VAT
		$result = $this->db->query("
			SELECT id, steuersatz FROM tax WHERE standard = '1'
		");
		$obj = $result->fetch_object();

		$this->shopConfiguration['commonVAT'] = $obj->steuersatz;
		$this->shopConfiguration['commonVAT_Id'] = $obj->id;

		$this->shopConfiguration['SHOP_SYSTEM'] = SHOP_SYSTEM;

		// Countries
		$result = $this->db->query("SELECT * FROM countries ORDER BY name ASC");
		while ($obj = $result->fetch_object()) {
			$countries[$obj->id] = $obj;
		}

		$this->shopConfiguration['countries'] = $countries;
		$this->setup_config();
	} // end __construct


	public function init_edit_fields($object_id) {
		// include config file
		if (file_exists('./core/config/'.$object_id.'.inc.php')) {
			include './core/config/'.$object_id.'.inc.php';
			foreach ($config_group_settings['edit_fields'] as $setting) {
				$this->list_table_config['edit_fields'][] = $setting;
			}
		}
		else {
			// get edit fields
			$sql = "
			SELECT *
			FROM configuration
			WHERE group_id='".$this->db->real_escape_string($object_id)."'
			";
			$result = $this->db->query($sql);
			while ($obj = $result->fetch_object()) {
				$type = 'text';
				if ($obj->text == 'multi' ) {
					$type = 'multitext';
				}
				else if ($obj->text == 'formated') {
					$type = 'formatedtext';
				}
				$this->list_table_config['edit_fields'][] = array(
					'db_field' => $obj->name,
					'name' => $obj->description,
					'type' => $type
				);
			}
		}

		return;
	}

	public function load($object_id) {
		$sql = "
			SELECT *
			FROM configuration
			WHERE group_id='".$this->db->real_escape_string($object_id)."'
		";
		$result = $this->db->query($sql);
		if ($this->db->error) {
			$log_object = Logger::get_instance();
			$log_object->info('Error in /core/main_class.php->load()', $this->db->error.', sql:'.$sql);
			return false;
		}
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[$obj->name] = $obj->setting;
		}
		return $data;
	}

	private function setup_config() {
		include_once './web/'.SHOP_SYSTEM.'/config/theme_widget.inc.php';

		if (isset($parent_theme)) {
			$this->shopConfiguration['parent_theme'] = $parent_theme;
		}

		if (isset($customized_templates)) {
			$this->shopConfiguration['customized_templates'] = $customized_templates;
		}

		$this->shopConfiguration['images'] = array();

		// there is already code for this and there might be a better way to do this
		//$this->shopConfiguration['images'][] = array('', $default_image_size);


		$default_image_size = $this->shopConfiguration['item_image_size_zoom'];

		$default_generatable_images = array(
				'overview_' => $this->shopConfiguration['item_image_size_overview'],
				'thumb_' => $this->shopConfiguration['item_image_size_thumb'],
				'details_' => $this->shopConfiguration['item_image_size_details'],
				'detaildefault_' => $this->shopConfiguration['item_image_size_detaildefault'],
		);


		if (isset($default_generatable_images)) {
			foreach ($default_generatable_images as $key => $value) {
				$this->shopConfiguration['images'][] = array($key, $value);
			}
		}

		if (isset($widget_regions)) {
			$this->shopConfiguration['widget_regions'] = $widget_regions;
			foreach ($widget_regions as $region) {
				$this->shopConfiguration['default_widget_list_'.$region] = ${'default_widget_list_'.$region};
			}
		}
	}

	public function set_config_item($item_key, $item_value) {
		if (is_array($item_value)) {
			$type = 2;
			$value = '$data = '.$this->php_2_string($item_value).';';
		} else {
			$type = 1;
			$value = $item_value;
		}

		if (isset($this->shopConfiguration[$item_key])) {
			$sql = '
					UPDATE
						configuration
					SET
						type = '.$type.',
						setting = "'.$this->db->real_escape_string($value).'"
					WHERE
						name = "'.$this->db->real_escape_string($item_key).'"';
		} else {
			$sql = '
					INSERT INTO	configuration (
						type,
						setting,
						name
					) values (
						'.$type.',
						"'.$this->db->real_escape_string($value).'",
						"'.$this->db->real_escape_string($item_key).'"
					)';
		}
		$this->db->query($sql);
		if ($this->db->error) {
			$log = Logger::get_instance();
			$log->error(__FILE__, $mysqli->error);
		}
		$this->shopConfiguration[$item_key] = $item_value;
		return;
	}

	private function php_2_string($data) {
		$string = '';
		if (is_array($data)) {
			$string .= 'array(';
			foreach ($data as $key => $value) {
				if (is_array($value)) {
					$string .= "'$key' => ".$this->php_2_string($value).", ";
				} else {
					$string .= "'$key' => '$value', ";
				}
			}
			$string .= ")";
		} else {
			$string .= "'".$data."'";
		};
		return $string;
	}



	function get_config_elements($group_id) {
		$sql = "
			SELECT *
			FROM configuration
			WHERE group_id='".$group_id."'
		";

		$result = $this->db->query($sql);
		$data = array();
		while ($obj = $result->fetch_object()) {
			$data[] = $obj;
		}
		return $data;
	}

	public function save($data, $object_id = false) {
		if ($data && $object_id) {
			$this->update($object_id, $data);
			// already saved
			return true;
		}
		else {
			// no data to save
			$log_object = Logger::get_instance();
			$log_object->info('Error in /core/config_class.php->save()', 'No data to save!');
			return false;
		}
	}

	public function update($group_id, $form_data) {
		$config_elemente = $this->get_config_elements($group_id);
		foreach ($config_elemente as $config_element) {
			$sql = "
				UPDATE configuration
				SET setting='".$form_data[$config_element->name]."'
				WHERE name='".$config_element->name."'
			";
			$this->db->query($sql);
		}
		return;
	} // end update

	public function update_config_element($name, $setting) {
		$sql = "
			UPDATE configuration
			SET setting='".$this->db->real_escape_string($setting)."'
			WHERE name='".$this->db->real_escape_string($name)."'
		";
		$this->db->query($sql);

		return;
	} // end update

	public static function has_key($key) {
		$db = Registry::get('base')->db;

		$sql = "SELECT *
				FROM configuration
				WHERE name = '$key'";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			return true;
		}

		return false;
	} // end has_key

	public static function is_set($key) {
		$db = Registry::get('base')->db;

		$sql = "SELECT *
				FROM configuration
				WHERE name = '$key'";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			$return = $result->fetch_object();

			if ($return->setting != '') {
				return true;
			}
		}

		return false;
	} // end has_key

	public static function get_value($key) {
		$db = Registry::get('base')->db;

		$sql = "SELECT setting
				FROM configuration
				WHERE name = '$key'";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			$return = $result->fetch_object();

			return $return->setting;
		}

		return false;
	} // end get_key

	public static function set_key($key, $value) {
		$db = Registry::get('base')->db;

		if (is_array($value)) {
			$type = 2;
			$value = '$data = '.$this->php_2_string($value).';';
		} else {
			$type = 1;
			$value = $value;
		}

		if (Config::has_key($key)) {
			$sql = '
				UPDATE
					configuration
				SET
					type = '.$type.',
					setting = "'.$db->real_escape_string($value).'"
				WHERE
					name = "'.$db->real_escape_string($key).'"';
		} else {
			$sql = '
				INSERT INTO	configuration (
					type,
					setting,
					name
				) values (
					'.$type.',
					"'.$db->real_escape_string($value).'",
					"'.$db->real_escape_string($key).'"
				)';
		}

		$db->query($sql);

		if ($db->error) {
			$log = Logger::get_instance();
			$log->error(__FILE__, $mysqli->error);
		}

		return;
	}
}

?>