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

	function index() {
		$response =& $this->locator->get('response');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$module   =& $this->locator->get('module');

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

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

		$template->set('content', $this->getList());

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

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

	function insert() {
		$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');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

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

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

		if (($request->isPost()) && ($this->validateForm())) {
			$sql = "insert into country set name = '?', iso_code_2 = '?', iso_code_3 = '?', address_format = '?'";
			$database->query($database->parse($sql, $request->get('name', 'post'), $request->get('iso_code_2', 'post'), $request->get('iso_code_3', 'post'), $request->get('address_format', 'post')));
			
			$cache->delete('country');
			
			$session->set('message', $language->get('text_message'));

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

		$template->set('content', $this->getForm());

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

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

	function update() {
		$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');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

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

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

		if (($request->isPost()) && ($this->validateForm())) {
			$sql = "update country set name = '?', iso_code_2 = '?', iso_code_3 = '?', address_format = '?' where country_id = '?'";
			$database->query($database->parse($sql, $request->get('name', 'post'), $request->get('iso_code_2', 'post'), $request->get('iso_code_3', 'post'), $request->get('address_format', 'post'), $request->get('country_id')));

			$cache->delete('country');
			
			$session->set('message', $language->get('text_message'));

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

		$template->set('content', $this->getForm());

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

		$response->set($template->fetch('layout.tpl'));
	}
 
	function delete() {
		$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');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

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

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

		if (($request->get('country_id')) && ($this->validateDelete())) {
			$database->query("delete from country where country_id = '" . (int)$request->get('country_id') . "'");

			$cache->delete('country');
			
			$session->set('message', $language->get('text_message'));

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

		$template->set('content', $this->getList());

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

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

	function getList() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$config   =& $this->locator->get('config');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$session  =& $this->locator->get('session');

		$cols = array();

		$cols[] = array(
			'name'  => $language->get('column_name'),
			'sort'  => 'name',
			'align' => 'left'
		);

		$cols[] = array(
			'name'  => $language->get('column_iso_code_2'),
			'sort'  => 'iso_code_2',
			'align' => 'left'
		);

		$cols[] = array(
			'name'  => $language->get('column_iso_code_3'),
			'sort'  => 'iso_code_3',
			'align' => 'left'
		);

    	$cols[] = array(
      		'name'  => $language->get('column_action'),
      		'align' => 'right'
    	);
				
		if (!$session->get('country.search')) {
			$sql = "select country_id, name, iso_code_2, iso_code_3 from country";
		} else {
			$sql = "select country_id, name, iso_code_2, iso_code_3 from country where name like '?' or iso_code_2 like '?' or iso_code_3 like '?'";
		}

		$sort = array(
			'name',
			'iso_code_2',
			'iso_code_3'
		);

		if (in_array($session->get('country.sort'), $sort)) {
			$sql .= " order by " . $session->get('country.sort') . " " . (($session->get('country.order') == 'desc') ? 'desc' : 'asc');
		} else {
			$sql .= " order by name asc";
		}

		$results = $database->getRows($database->splitQuery($database->parse($sql, '%' . $session->get('country.search') . '%', $session->get('country.search'), $session->get('country.search')), $session->get('country.page'), $config->get('config_max_rows')));

		$rows = array();

		foreach ($results as $result) {
			$cell = array();

			$cell[] = array(
				'value'   => $result['name'],
				'align'   => 'left',
				'default' => ($result['country_id'] == $config->get('config_country_id'))
			);

			$cell[] = array(
				'value' => $result['iso_code_2'],
				'align' => 'left'
			);

			$cell[] = array(
				'value' => $result['iso_code_3'],
				'align' => 'left'
			);

			$action = array();
      		
			$action[] = array(
        		'icon' => 'update.png',
				'text' => $language->get('button_update'),
				'href' => $url->ssl('country', 'update', array('country_id' => $result['country_id']))
      		);
						
			$action[] = array(
        		'icon' => 'delete.png',
				'text' => $language->get('button_delete'),
				'href' => $url->ssl('country', 'delete', array('country_id' => $result['country_id']))
      		);

      		$cell[] = array(
        		'action' => $action,
        		'align'  => 'right'
      		);
						
			$rows[] = array('cell' => $cell);
		}

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

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

		$view->set('text_default', $language->get('text_default'));
		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

		$view->set('entry_page', $language->get('entry_page'));
		$view->set('entry_search', $language->get('entry_search'));

		$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('error', @$this->error['message']);

		$view->set('message', $session->get('message'));
		
		$session->delete('message');
		
		$view->set('action', $url->ssl('country', 'page'));

		$view->set('search', $session->get('country.search'));
		$view->set('sort', $session->get('country.sort'));
		$view->set('order', $session->get('country.order'));
		$view->set('page', $session->get('country.page'));

		$view->set('cols', $cols);
		$view->set('rows', $rows);

		$view->set('list', $url->ssl('country'));

		$view->set('insert', $url->ssl('country', 'insert'));

		$page_data = array();

		for ($i = 1; $i <= $database->getPages(); $i++) {
			$page_data[] = array(
				'text'  => $language->get('text_pages', $i, $database->getPages()),
				'value' => $i
			);
		}

		$view->set('pages', $page_data);

		return $view->fetch('content/list.tpl');
	}

	function getForm() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');

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

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

		$view->set('entry_name', $language->get('entry_name'));
		$view->set('entry_iso_code_2', $language->get('entry_iso_code_2'));
		$view->set('entry_iso_code_3', $language->get('entry_iso_code_3'));
		$view->set('entry_address_format', $language->get('entry_address_format'));

		$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_general', $language->get('tab_general'));

		$view->set('error', @$this->error['message']);
		$view->set('error_name', @$this->error['name']);

		$view->set('action', $url->ssl('country', $request->get('action'), array('country_id' => $request->get('country_id'))));

		$view->set('list', $url->ssl('country'));

		$view->set('insert', $url->ssl('country', 'insert'));

		if ($request->get('country_id')) {
			$view->set('update', $url->ssl('country', 'update', array('country_id' => $request->get('country_id'))));
			$view->set('delete', $url->ssl('country', 'delete', array('country_id' => $request->get('country_id'))));
		}

		$view->set('cancel', $url->ssl('country'));

		if (($request->get('country_id')) && (!$request->isPost())) {
			$country_info = $database->getRow("select distinct * from country where country_id = '" . (int)$request->get('country_id') . "'");
		}

		if ($request->has('name', 'post')) {
			$view->set('name', $request->get('name', 'post'));
		} else {
			$view->set('name', @$country_info['name']);
		}

		if ($request->has('iso_code_2', 'post')) {
			$view->set('iso_code_2', $request->get('iso_code_2', 'post'));
		} else {
			$view->set('iso_code_2', @$country_info['iso_code_2']);
		}

		if ($request->has('iso_code_3', 'post')) {
			$view->set('iso_code_3', $request->get('iso_code_3', 'post'));
		} else {
			$view->set('iso_code_3', @$country_info['iso_code_3']);
		}

		if ($request->has('address_format', 'post')) {
			$view->set('address_format', $request->get('address_format', 'post'));
		} else {
			$view->set('address_format', @$country_info['address_format']);
		}

		return $view->fetch('content/country.tpl');
	}

	function validateForm() {
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');

		if (!$user->hasPermission('modify', 'country')) {
			$this->error['message'] = $language->get('error_permission');
		}

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

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

	function validateDelete() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$config   =& $this->locator->get('config');
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');

		if (!$user->hasPermission('modify', 'country')) {
			$this->error['message'] = $language->get('error_permission');
		}

		if ($config->get('config_country_id') == $request->get('country_id')) {
			$this->error['message'] = $language->get('error_default');
		}

		$address_info = $database->getRow("select count(*) as total from address where country_id = '" . (int)$request->get('country_id') . "'");

		if ($address_info['total']) {
			$this->error['message'] = $language->get('error_address', $address_info['total']);
		}

		$address_info = $database->getRow("select count(*) as total from address where country_id = '" . (int)$request->get('country_id') . "'");

		if ($address_info['total']) {
			$this->error['message'] = $language->get('error_address', $address_info['total']);
		}

		$zone_info = $database->getRow("select count(*) as total from zone where country_id = '" . (int)$request->get('country_id') . "'");

		if ($zone_info['total']) {
			$this->error['message'] = $language->get('error_zone', $zone_info['total']);
		}

		$zone_to_geo_zone_info = $database->getRow("select count(*) as total from zone_to_geo_zone where country_id = '" . (int)$request->get('country_id') . "'");

		if ($zone_to_geo_zone_info['total']) {
			$this->error['message'] = $language->get('error_zone_to_geo_zone', $zone_to_geo_zone_info['total']);
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$url      =& $this->locator->get('url');
		$session  =& $this->locator->get('session');

		if ($request->has('search', 'post')) {
			$session->set('country.search', $request->get('search', 'post'));
		}

		if (($request->has('page', 'post')) || ($request->has('search', 'post'))) {
			$session->set('country.page', $request->get('page', 'post'));
		}

		if ($request->has('sort', 'post')) {
			$session->set('country.order', (($session->get('country.sort') == $request->get('sort', 'post')) && ($session->get('country.order') == 'asc')) ? 'desc' : 'asc');
		}

		if ($request->has('sort', 'post')) {
			$session->set('country.sort', $request->get('sort', 'post'));
		}

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