<?php
class ControllerSetting extends Controller {
	var $error = array();

	function index() { 
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$module   =& $this->locator->get('module');
		$session  =& $this->locator->get('session');
		$cache    =& $this->locator->get('cache');

		$language->load('controller/setting.php');

		$template->set('title', $language->get('heading_title'));

		if (($request->isPost()) && ($this->validate())) {
			$database->query("delete from setting where `group` = 'config'");

			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_store', `value` = '?'", $request->get('global_config_store', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_owner', `value` = '?'", $request->get('global_config_owner', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_address', `value` = '?'", $request->get('global_config_address', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_telephone', `value` = '?'", $request->get('global_config_telephone', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_fax', `value` = '?'", $request->get('global_config_fax', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_template', `value` = '?'", $request->get('catalog_config_template', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_max_rows', `value` = '?'", $request->get('catalog_config_max_rows', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_url_alias', `value` = '?'", $request->get('catalog_config_url_alias', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_parse_time', `value` = '?'", $request->get('catalog_config_parse_time', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_ssl', `value` = '?'", $request->get('catalog_config_ssl', 'post')));
			$database->query($database->parse("insert into setting set type = 'admin', `group` = 'config', `key` = 'config_template', `value` = '?'", $request->get('admin_config_template', 'post')));
			$database->query($database->parse("insert into setting set type = 'admin', `group` = 'config', `key` = 'config_max_rows', `value` = '?'", $request->get('admin_config_max_rows', 'post')));
			$database->query($database->parse("insert into setting set type = 'admin', `group` = 'config', `key` = 'config_parse_time', `value` = '?'", $request->get('admin_config_parse_time', 'post')));
			$database->query($database->parse("insert into setting set type = 'admin', `group` = 'config', `key` = 'config_ssl', `value` = '?'", $request->get('admin_config_ssl', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_country_id', `value` = '?'", $request->get('global_config_country_id', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_zone_id', `value` = '?'", $request->get('global_config_zone_id', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_language', `value` = '?'", $request->get('global_config_language', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_currency', `value` = '?'", $request->get('global_config_currency', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_weight_class_id', `value` = '?'", $request->get('global_config_weight_class_id', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_tax', `value` = '?'", $request->get('global_config_tax', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_order_status_id', `value` = '?'", $request->get('global_config_order_status_id', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_stock_check', `value` = '?'", $request->get('catalog_config_stock_check', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_stock_checkout', `value` = '?'", $request->get('catalog_config_stock_checkout', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_stock_subtract', `value` = '?'", $request->get('catalog_config_stock_subtract', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_vat', `value` = '?'", $request->get('catalog_config_vat', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_account_id', `value` = '?'", $request->get('catalog_config_account_id', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_checkout_id', `value` = '?'", $request->get('catalog_config_checkout_id', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_email', `value` = '?'", $request->get('global_config_email', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_email_send', `value` = '?'", $request->get('global_config_email_send', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_cache_query', `value` = '?'", $request->get('global_config_cache_query', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_compress_output', `value` = '?'", $request->get('global_config_compress_output', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_compress_level', `value` = '?'", $request->get('global_config_compress_level', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_image_resize', `value` = '?'", $request->get('global_config_image_resize', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_image_width', `value` = '?'", $request->get('global_config_image_width', 'post')));
			$database->query($database->parse("insert into setting set type = 'global', `group` = 'config', `key` = 'config_image_height', `value` = '?'", $request->get('global_config_image_height', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_download', `value` = '?'", $request->get('catalog_config_download', 'post')));
			$database->query($database->parse("insert into setting set type = 'catalog', `group` = 'config', `key` = 'config_download_status', `value` = '?'", $request->get('catalog_config_download_status', 'post')));
 
			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('setting'));
		}

		$view = $this->locator->create('template');

		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));

		$view->set('text_yes', $language->get('text_yes'));
		$view->set('text_no', $language->get('text_no'));
		$view->set('text_none', $language->get('text_none'));

		$view->set('entry_store', $language->get('entry_store'));
		$view->set('entry_owner', $language->get('entry_owner'));
		$view->set('entry_address', $language->get('entry_address'));
		$view->set('entry_telephone', $language->get('entry_telephone'));
		$view->set('entry_fax', $language->get('entry_fax'));
		$view->set('entry_template', $language->get('entry_template'));
		$view->set('entry_url_alias', $language->get('entry_url_alias'));
		$view->set('entry_parse_time', $language->get('entry_parse_time'));
		$view->set('entry_ssl', $language->get('entry_ssl'));
		$view->set('entry_rows_per_page', $language->get('entry_rows_per_page'));
		$view->set('entry_country', $language->get('entry_country'));
		$view->set('entry_zone', $language->get('entry_zone'));
		$view->set('entry_language', $language->get('entry_language'));
		$view->set('entry_currency', $language->get('entry_currency'));
		$view->set('entry_weight', $language->get('entry_weight'));
		$view->set('entry_tax', $language->get('entry_tax'));
		$view->set('entry_order_status', $language->get('entry_order_status'));
		$view->set('entry_stock_check', $language->get('entry_stock_check'));
		$view->set('entry_stock_checkout', $language->get('entry_stock_checkout'));
		$view->set('entry_stock_subtract', $language->get('entry_stock_subtract'));
		$view->set('entry_vat', $language->get('entry_vat'));
		$view->set('entry_account', $language->get('entry_account'));
		$view->set('entry_checkout', $language->get('entry_checkout'));
		$view->set('entry_email', $language->get('entry_email'));
		$view->set('entry_email_send', $language->get('entry_email_send'));
		$view->set('entry_cache_query', $language->get('entry_cache_query'));
		$view->set('entry_compress_output', $language->get('entry_compress_output'));
		$view->set('entry_compress_level', $language->get('entry_compress_level'));
		$view->set('entry_image_resize', $language->get('entry_image_resize'));
		$view->set('entry_image_width', $language->get('entry_image_width'));
		$view->set('entry_image_height', $language->get('entry_image_height'));
		$view->set('entry_download', $language->get('entry_download'));
		$view->set('entry_download_status', $language->get('entry_download_status'));
 
		$view->set('button_list', $language->get('button_list'));
		$view->set('button_insert', $language->get('button_insert'));
		$view->set('button_update', $language->get('button_update'));
		$view->set('button_delete', $language->get('button_delete'));
		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('tab_shop', $language->get('tab_shop'));
		$view->set('tab_admin', $language->get('tab_admin'));
		$view->set('tab_local', $language->get('tab_local'));
		$view->set('tab_stock', $language->get('tab_stock'));
		$view->set('tab_option', $language->get('tab_option'));
		$view->set('tab_mail', $language->get('tab_mail'));
		$view->set('tab_cache', $language->get('tab_cache'));
		$view->set('tab_image', $language->get('tab_image'));
		$view->set('tab_download', $language->get('tab_download'));

		$view->set('error', @$this->error['message']);
		$view->set('error_store', @$this->error['store']);
		$view->set('error_owner', @$this->error['owner']);
		$view->set('error_address', @$this->error['address']);
		$view->set('error_telephone', @$this->error['telephone']);
		
		$view->set('message', $session->get('message'));
		
		$session->delete('message');
		
		$view->set('action', $url->ssl('setting'));
		
		$view->set('cancel', $url->ssl('setting'));

		$results = $database->getRows("select * from setting where `group` = 'config'");

		foreach ($results as $result) {
			$setting_info[$result['type']][$result['key']] = $result['value'];
		}

		if ($request->has('global_config_store', 'post')) {
			$view->set('global_config_store', $request->get('global_config_store', 'post'));
		} else {
			$view->set('global_config_store', @$setting_info['global']['config_store']);
		}

		if ($request->has('global_config_owner', 'post')) {
			$view->set('global_config_owner', $request->get('global_config_owner', 'post'));
		} else {
			$view->set('global_config_owner', @$setting_info['global']['config_owner']);
		}

		if ($request->has('global_config_address', 'post')) {
			$view->set('global_config_address', $request->get('global_config_address', 'post'));
		} else {
			$view->set('global_config_address', @$setting_info['global']['config_address']);
		}

		if ($request->has('global_config_telephone', 'post')) {
			$view->set('global_config_telephone', $request->get('global_config_telephone', 'post'));
		} else {
			$view->set('global_config_telephone', @$setting_info['global']['config_telephone']);
		}

		if ($request->has('global_config_fax', 'post')) {
			$view->set('global_config_fax', $request->get('global_config_fax', 'post'));
		} else {
			$view->set('global_config_fax', @$setting_info['global']['config_fax']);
		}

		if ($request->has('catalog_config_url_alias', 'post')) {
			$view->set('catalog_config_url_alias', $request->get('catalog_config_url_alias', 'post'));
		} else {
			$view->set('catalog_config_url_alias', @$setting_info['catalog']['config_url_alias']);
		}

		if ($request->has('catalog_config_template', 'post')) {
			$view->set('catalog_config_template', $request->get('catalog_config_template', 'post'));
		} else {
			$view->set('catalog_config_template', @$setting_info['catalog']['config_template']); 
		}

		$template_data = array();

		foreach (glob(DIR_CATALOG_TEMPLATE . '*', GLOB_ONLYDIR) as $dirctory) {
			$template_data[] = basename($dirctory);
		}

		$view->set('catalog_templates', $template_data);

		if ($request->has('admin_config_template', 'post')) {
			$view->set('admin_config_template', $request->get('admin_config_template', 'post'));
		} else {
			$view->set('admin_config_template', @$setting_info['admin']['config_template']);
		}

		$template_data = array();

		foreach (glob(DIR_TEMPLATE . '*', GLOB_ONLYDIR) as $dirctory) {
			$template_data[] = basename($dirctory);
		}

		$view->set('admin_templates', $template_data);

		if ($request->has('catalog_config_max_rows', 'post')) {
			$view->set('catalog_config_max_rows', $request->get('catalog_config_max_rows', 'post'));
		} else {
			$view->set('catalog_config_max_rows', @$setting_info['catalog']['config_max_rows']);
		}

		if ($request->has('admin_config_max_rows', 'post')) {
			$view->set('admin_config_max_rows', $request->get('admin_config_max_rows', 'post'));
		} else {
			$view->set('admin_config_max_rows', @$setting_info['admin']['config_max_rows']);
		}

		if ($request->has('global_config_tax', 'post')) {
			$view->set('global_config_tax', $request->get('global_config_tax', 'post'));
		} else {
			$view->set('global_config_tax', @$setting_info['global']['config_tax']);
		}

		if ($request->has('global_config_email', 'post')) {
			$view->set('global_config_email', $request->get('global_config_email', 'post'));
		} else {
			$view->set('global_config_email', @$setting_info['global']['config_email']);
		}

		if ($request->has('global_config_email_send', 'post')) {
			$view->set('global_config_email_send', $request->get('global_config_email_send', 'post'));
		} else {
			$view->set('global_config_email_send', @$setting_info['global']['config_email_send']);
		}

		if ($request->has('catalog_config_parse_time', 'post')) {
			$view->set('catalog_config_parse_time', $request->get('catalog_config_parse_time', 'post'));
		} else {
			$view->set('catalog_config_parse_time', @$setting_info['catalog']['config_parse_time']);
		}

		if ($request->has('admin_config_parse_time', 'post')) {
			$view->set('admin_config_parse_time', $request->get('admin_config_parse_time', 'post'));
		} else {
			$view->set('admin_config_parse_time', @$setting_info['admin']['config_parse_time']);
		}

		if ($request->has('catalog_config_ssl', 'post')) {
			$view->set('catalog_config_ssl', $request->get('catalog_config_ssl', 'post'));
		} else {
			$view->set('catalog_config_ssl', @$setting_info['catalog']['config_ssl']);
		}

		if ($request->has('admin_config_ssl', 'post')) {
			$view->set('admin_config_ssl', $request->get('admin_config_ssl', 'post'));
		} else {
			$view->set('admin_config_ssl', @$setting_info['admin']['config_ssl']);
		}

		if ($request->has('catalog_config_stock_check', 'post')) {
			$view->set('catalog_config_stock_check', $request->get('catalog_config_stock_check', 'post'));
		} else {
			$view->set('catalog_config_stock_check', @$setting_info['catalog']['config_stock_check']);
		}

		if ($request->has('catalog_config_stock_checkout')) {
			$view->set('catalog_config_stock_checkout', $request->get('catalog_config_stock_checkout'));
		} else {
			$view->set('catalog_config_stock_checkout', @$setting_info['catalog']['config_stock_checkout']);
		}

		if ($request->has('catalog_config_stock_subtract')) {
			$view->set('catalog_config_stock_subtract', $request->get('catalog_config_stock_subtract'));
		} else {
			$view->set('catalog_config_stock_subtract', @$setting_info['catalog']['config_stock_subtract']);
		}

		if ($request->has('catalog_config_vat')) {
			$view->set('catalog_config_vat', $request->get('catalog_config_vat'));
		} else {
			$view->set('catalog_config_vat', @$setting_info['catalog']['config_vat']);
		}

		if ($request->has('catalog_config_account_id')) {
			$view->set('catalog_config_account_id', $request->get('catalog_config_account_id'));
		} else {
			$view->set('catalog_config_account_id', @$setting_info['catalog']['config_account_id']);
		}

		if ($request->has('catalog_config_checkout_id')) {
			$view->set('catalog_config_checkout_id', $request->get('catalog_config_checkout_id'));
		} else {
			$view->set('catalog_config_checkout_id', @$setting_info['catalog']['config_checkout_id']);
		}
						
		$view->set('informations', $database->cache('information-' . (int)$language->getId(), "select * from information i left join information_description id on (i.information_id = id.information_id) where id.language_id = '" . (int)$language->getId() . "' order by i.sort_order"));

		if ($request->has('global_config_cache_query')) {
			$view->set('global_config_cache_query', $request->get('global_config_cache_query'));
		} else {
			$view->set('global_config_cache_query', @$setting_info['global']['config_cache_query']);
		}

		if ($request->has('global_config_compress_output')) {
			$view->set('global_config_compress_output', $request->get('global_config_compress_output'));
		} else {
			$view->set('global_config_compress_output', @$setting_info['global']['config_compress_output']);
		}

		if ($request->has('global_config_compress_level')) {
			$view->set('global_config_compress_level', $request->get('global_config_compress_level'));
		} else {
			$view->set('global_config_compress_level', @$setting_info['global']['config_compress_level']);
		}

		if ($request->has('global_config_image_resize')) {
			$view->set('global_config_image_resize', $request->get('global_config_image_resize'));
		} else {
			$view->set('global_config_image_resize', @$setting_info['global']['config_image_resize']);
		}

		if ($request->has('global_config_image_width')) {
			$view->set('global_config_image_width', $request->get('global_config_image_width'));
		} else {
			$view->set('global_config_image_width', @$setting_info['global']['config_image_width']);
		}

		if ($request->has('global_config_image_height')) {
			$view->set('global_config_image_height', $request->get('global_config_image_height'));
		} else {
			$view->set('global_config_image_height', @$setting_info['global']['config_image_height']);
		}

		if ($request->has('global_config_country_id')) {
			$view->set('global_config_country_id', $request->get('global_config_country_id'));
		} else {
			$view->set('global_config_country_id', @$setting_info['global']['config_country_id']);
		}

		$view->set('countries', $database->cache('country', "select country_id, name from country order by name"));

		if ($request->has('global_config_zone_id')) {
			$view->set('global_config_zone_id', $request->get('global_config_zone_id'));
		} else {
			$view->set('global_config_zone_id', @$setting_info['global']['config_zone_id']);
		}

		$view->set('zones', $database->cache('zone', "select * from zone order by country_id, name"));

		if ($request->has('global_config_language')) {
			$view->set('global_config_language', $request->get('global_config_language'));
		} else {
			$view->set('global_config_language', @$setting_info['global']['config_language']);
		}

		$view->set('languages', $database->cache('language', "select * from language order by sort_order"));

		if ($request->has('global_config_currency')) {
			$view->set('global_config_currency', $request->get('global_config_currency'));
		} else {
			$view->set('global_config_currency', @$setting_info['global']['config_currency']);
		}

		$view->set('currencies', $database->cache('currency', "select * from currency"));

		if ($request->has('global_config_weight_class_id')) {
			$view->set('global_config_weight_class_id', $request->get('global_config_weight_class_id'));
		} else {
			$view->set('global_config_weight_class_id', @$setting_info['global']['config_weight_class_id']);
		}

		$view->set('weight_classes', $database->cache('weight_class-' . (int)$language->getId(), "select weight_class_id, title from weight_class where language_id = '" . (int)$language->getId() . "'"));

		if ($request->has('global_config_order_status_id')) {
			$view->set('global_config_order_status_id', $request->get('global_config_order_status_id'));
		} else {
			$view->set('global_config_order_status_id', @$setting_info['global']['config_order_status_id']);
		}

		$view->set('order_statuses', $database->cache('order_status-' . (int)$language->getId(), "select order_status_id, name from order_status where language_id = '" . (int)$language->getId() . "' order by name"));
 
		if ($request->has('catalog_config_download')) {
			$view->set('catalog_config_download', $request->get('catalog_config_download'));
		} else {
			$view->set('catalog_config_download', @$setting_info['catalog']['config_download']);
		}

		if ($request->has('catalog_config_download_status')) {
			$view->set('catalog_config_download_status', $request->get('catalog_config_download_status'));
		} else {
			$view->set('catalog_config_download_status', @$setting_info['catalog']['config_download_status']);
		}

		$template->set('content', $view->fetch('content/setting.tpl'));

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function validate() {
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$module   =& $this->locator->get('module');
		$user     =& $this->locator->get('user');
			
		if (!$user->hasPermission('modify', 'setting')) {
			$this->error['message'] = $language->get('error_permission');
		}
				
		if ((strlen($request->get('global_config_store', 'post')) < 3) || (strlen($request->get('global_config_store', 'post')) > 32)) {
			$this->error['store'] = $language->get('error_store');
		}

		if ((strlen($request->get('global_config_owner', 'post')) < 3) || (strlen($request->get('global_config_owner', 'post')) > 32)) {
			$this->error['owner'] = $language->get('error_owner');
		}

		if ((strlen($request->get('global_config_address', 'post')) < 3) || (strlen($request->get('global_config_address', 'post')) > 128)) {
			$this->error['address'] = $language->get('error_address');
		}

		if ((strlen($request->get('global_config_telephone', 'post')) < 5) || (strlen($request->get('global_config_telephone', 'post')) > 32)) {
			$this->error['telephone'] = $language->get('error_telephone');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function zone() {
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		
		$output = '<select name="global_config_zone_id">';

		$results = $database->cache('zone-' . (int)$request->get('country_id'), "select zone_id, name from zone where country_id = '" . (int)$request->get('country_id') . "' order by name");

		foreach ($results as $result) {
			$output .= '<option value="' . $result['zone_id'] . '"';

			if ($request->get('zone_id') == $result['zone_id']) {
				$output .= ' SELECTED';
			}

			$output .= '>' . $result['name'] . '</option>';
		}

		if (!$results) {
			$output .= '<option value="0">' . $language->get('text_none') . '</option>';
		}

		$output .= '</select>';

		$response->set($output);
	}
}
?>