<?php
class ControllerModulefuturaseo extends Controller {
	private $error = array();

	public function index() {
		
		$language = $this->language->load('module/futura_seo');
		$this->data = array_merge($this->data, $language);		

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

			$this->model_setting_setting->editSettingFuturaSeo('configSeo', $this->request->post);

			if ($this->config->get('config_currency_auto')) {
				$this->load->model('localisation/currency');

				$this->model_localisation_currency->updateCurrencies();
			}
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
					'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
					'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/futura_seo', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->data['action'] = $this->url->link('module/futura_seo', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['token'] = $this->session->data['token'];


		if (isset($this->request->post['config_entry_body'])) {
			$this->data['config_entry_body'] = $this->request->post['config_entry_body'];
		} else {
			$this->data['config_entry_body'] = $this->config->get('config_entry_body');
		}

    if (isset($this->request->post['config_google_footer'])) {
			$this->data['config_google_footer'] = $this->request->post['config_google_footer'];
		} else {
			$this->data['config_google_footer'] = $this->config->get('config_google_footer');
		}

    if (isset($this->request->post['config_pixel_facebook_status'])) {
			$this->data['config_pixel_facebook_status'] = $this->request->post['config_pixel_facebook_status'];
		} else {
			$this->data['config_pixel_facebook_status'] = $this->config->get('config_pixel_facebook_status');
		}
		
		if (isset($this->request->post['config_status_analytics_avancado'])) {
			$this->data['config_status_analytics_avancado'] = $this->request->post['config_status_analytics_avancado'];
		} else {
			$this->data['config_status_analytics_avancado'] = $this->config->get('config_status_analytics_avancado');
		}		

    if (isset($this->request->post['config_pixel_facebook'])) {
			$this->data['config_pixel_facebook'] = $this->request->post['config_pixel_facebook'];
		} else {
			$this->data['config_pixel_facebook'] = $this->config->get('config_pixel_facebook');
		}
		
		if (isset($this->request->post['config_google_analytics'])) {
			$this->data['config_google_analytics'] = $this->request->post['config_google_analytics'];
		} else {
			$this->data['config_google_analytics'] = $this->config->get('config_google_analytics');
		}		

		$this->template = 'module/futura_seo.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	protected function validate() {
		/*if (!$this->user->hasPermission('modify', 'module/futura_seo')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}*/

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	public function template() {
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$server = HTTPS_CATALOG;
		} else {
			$server = HTTP_CATALOG;
		}

		if (file_exists(DIR_IMAGE . 'templates/' . basename($this->request->get['template']) . '.png')) {
			$image = $server . 'image/templates/' . basename($this->request->get['template']) . '.png';
		} else {
			$image = $server . 'image/no_image.jpg';
		}

		$this->response->setOutput('<img src="' . $image . '" alt="" title="" style="border: 1px solid #EEEEEE;" />');
	}

	public function country() {
		$json = array();

		$this->load->model('localisation/country');

    	$country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);

		if ($country_info) {
			$this->load->model('localisation/zone');

			$json = array(
				'country_id'        => $country_info['country_id'],
				'name'              => $country_info['name'],
				'iso_code_2'        => $country_info['iso_code_2'],
				'iso_code_3'        => $country_info['iso_code_3'],
				'address_format'    => $country_info['address_format'],
				'postcode_required' => $country_info['postcode_required'],
				'zone'              => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),
				'status'            => $country_info['status']
			);
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>
