<?php
class ModelLocalisationZone extends Model {
	public function getZone($zone_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone WHERE zone_id = '" . (int)$zone_id . "' AND status = '1'");

		return $query->row;
	}

	public function getZonesByCountryId($country_id) {
		$zone_data = $this->cache->get('zone.' . (int)$country_id);

		if (!$zone_data) {
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone WHERE country_id = '" . (int)$country_id . "' AND status = '1' ORDER BY name");

			$zone_data = $query->rows;

			$this->cache->set('zone.' . (int)$country_id, $zone_data);
		}

		return $zone_data;
	}

	public function getZoneBySlug($slug, $country_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone WHERE slug = '" . $this->db->escape($slug) . "' AND country_id = '" . (int)$country_id . "' AND status = '1' LIMIT 1");

		return $query->row;
	}

	public function getContacts($zone_id = 0) {
		if (!$zone_id) {
			$zone_id = isset($this->session->data['city_id']) ? (int)$this->session->data['city_id'] : (int)$this->config->get('config_zone_id');
		}

		$zone = $this->getZone($zone_id);
		$contacts = array();

		foreach (array('telephone', 'email', 'address', 'geocode', 'fax', 'open', 'comment') as $field) {
			$key = 'contact_' . $field;

			if (isset($zone[$key]) && trim((string)$zone[$key]) !== '') {
				$contacts[$field] = $zone[$key];
			} else {
				$contacts[$field] = $this->config->get('config_' . $field);
			}
		}

		return $contacts;
	}

	public function getReplacements($zone_id = 0) {
		if (!$zone_id) {
			$zone_id = isset($this->session->data['city_id']) ? (int)$this->session->data['city_id'] : (int)$this->config->get('config_zone_id');
		}

		$zone = $this->getZone($zone_id);

		if (!$zone) {
			$zone = $this->getZone((int)$this->config->get('config_zone_id'));
		}

		if (!$zone) {
			return array();
		}

		$country_query = $this->db->query("SELECT name FROM " . DB_PREFIX . "country WHERE country_id = '" . (int)$zone['country_id'] . "' LIMIT 1");
		$country = $country_query->num_rows ? $country_query->row['name'] : '';
		$replacements = array(
			'{_REGION_}'  => $zone['name'],
			'{_GOROD_}'   => $zone['name'],
			'{_GORODU_}'  => $zone['name'],
			'{_GORODOM_}' => $zone['name'],
			'{_STRANA_}'  => $country,
			'{_STRANU_}'  => $country,
			'{_STRANOI_}' => $country,
			'{_STRANE_}'  => $country,
			'{_STRANY_}'  => $country
		);

		foreach (array('gorod', 'gorodu', 'gorodom', 'strana', 'stranu', 'stranoi', 'strane', 'strany') as $field) {
			$key = 'replacement_' . $field;

			if (isset($zone[$key]) && trim((string)$zone[$key]) !== '') {
				$replacements['{_' . strtoupper($field) . '_}'] = $zone[$key];
			}
		}

		return $replacements;
	}

	public function getZoneUrl($zone_id, $url) {
		$zone = $this->getZone($zone_id);

		return $this->getZoneUrlByZone($zone, $url);
	}

	public function getZoneUrlByZone($zone, $url) {
		if (!$zone) {
			return $url;
		}

		$url_info = parse_url(str_replace('&amp;', '&', $url));
		$secure = isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] === 'on') || ($this->request->server['HTTPS'] === '1'));
		$store_url = $secure ? $this->config->get('config_ssl') : $this->config->get('config_url');
		$store_info = parse_url($store_url);

		if (!$url_info || !$store_info || !isset($store_info['scheme']) || !isset($store_info['host'])) {
			return $store_url;
		}

		$parts = array_values(array_filter(explode('/', trim(isset($url_info['path']) ? $url_info['path'] : '', '/')), 'strlen'));
		$base_parts = array_values(array_filter(explode('/', trim(isset($store_info['path']) ? $store_info['path'] : '', '/')), 'strlen'));

		if ($base_parts && array_slice($parts, 0, count($base_parts)) === $base_parts) {
			$parts = array_slice($parts, count($base_parts));
		}

		$zones = $this->getZonesByCountryId($zone['country_id']);

		foreach ($zones as $item) {
			if (!empty($item['slug']) && isset($parts[0]) && $parts[0] === $item['slug']) {
				array_shift($parts);
				break;
			}
		}

		if ((int)$zone['zone_id'] !== (int)$this->config->get('config_zone_id') && $zone['slug'] !== '') {
			array_unshift($parts, $zone['slug']);
		}

		$redirect = $store_info['scheme'] . '://' . $store_info['host'] . (isset($store_info['port']) ? ':' . $store_info['port'] : '') . '/' . implode('/', array_merge($base_parts, $parts));

		if (isset($url_info['query'])) {
			$redirect .= '?' . $url_info['query'];
		}

		return $redirect;
	}
}
