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

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

		$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('config_futura', $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'));
		}

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_owner'] = $this->language->get('entry_owner');
		$this->data['entry_address'] = $this->language->get('entry_address');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_exibir'] = $this->language->get('entry_exibir');
		$this->data['entry_telephone'] = $this->language->get('entry_telephone');
		$this->data['entry_fax'] = $this->language->get('entry_fax');
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['entry_telephone_extra'] = $this->language->get('entry_telephone_extra');
		$this->data['entry_contato_status'] = $this->language->get('entry_contato_status');
		$this->data['entry_celular'] = $this->language->get('entry_celular');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_telephone_title'] = $this->language->get('entry_telephone_title');
		$this->data['entry_facebook'] = $this->language->get('entry_facebook');
		$this->data['entry_image_facebook'] = $this->language->get('entry_image_facebook');
		$this->data['entry_instagram'] = $this->language->get('entry_instagram');
		$this->data['entry_image_instagram'] = $this->language->get('entry_image_instagram');
		$this->data['entry_twitter'] = $this->language->get('entry_twitter');
		$this->data['entry_image_twitter'] = $this->language->get('entry_image_twitter');
		$this->data['entry_title_column'] = $this->language->get('entry_title_column');
		$this->data['entry_mapa'] = $this->language->get('entry_mapa');
		$this->data['entry_status_mapa'] = $this->language->get('entry_status_mapa');
		$this->data['entry_width_mapa'] = $this->language->get('entry_width_mapa');
		$this->data['entry_height_mapa'] = $this->language->get('entry_height_mapa');
		$this->data['entry_telephone_status'] = $this->language->get('entry_telephone_status');

		$this->data['tab_seo'] = $this->language->get('tab_seo');
		$this->data['tab_setting'] = $this->language->get('tab_setting');
		$this->data['tab_image'] = $this->language->get('tab_image');
		$this->data['tab_social'] = $this->language->get('tab_social');
		$this->data['tab_contato'] = $this->language->get('tab_contato');

		$this->data['text_opcao_exibir'] = explode(',',$this->language->get('text_opcao_exibir'));
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_enabled']       = $this->language->get('text_enabled');
		$this->data['text_disabled']      = $this->language->get('text_disabled');

 		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'] = '';
		}

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

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

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

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

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

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

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

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

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

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

  		$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_name'])) {
			$this->data['config_name'] = $this->request->post['config_name'];
		} else {
			$this->data['config_name'] = $this->config->get('config_name');
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		$this->load->model('tool/image');

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

		if ($this->config->get('config_futura_img_padrao') && file_exists(DIR_IMAGE . $this->config->get('config_futura_img_padrao')) && is_file(DIR_IMAGE . $this->config->get('config_futura_img_padrao'))) {
			$this->data['img_padrao'] = $this->model_tool_image->resize($this->config->get('config_futura_img_padrao'), 100, 100);
		} else {
			$this->data['img_padrao'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}

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

		if ($this->config->get('config_futura_img_facebook') && file_exists(DIR_IMAGE . $this->config->get('config_futura_img_facebook')) && is_file(DIR_IMAGE . $this->config->get('config_futura_img_facebook'))) {
		    $this->data['img_facebook'] = $this->model_tool_image->resize($this->config->get('config_futura_img_facebook'), 100, 100);
		} else {
		    $this->data['img_facebook'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}

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

		if ($this->config->get('config_futura_img_instagram') && file_exists(DIR_IMAGE . $this->config->get('config_futura_img_instagram')) && is_file(DIR_IMAGE . $this->config->get('config_futura_img_instagram'))) {
		    $this->data['img_instagram'] = $this->model_tool_image->resize($this->config->get('config_futura_img_instagram'), 100, 100);
		} else {
		    $this->data['img_instagram'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}

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

		if ($this->config->get('config_futura_img_twitter') && file_exists(DIR_IMAGE . $this->config->get('config_futura_img_twitter')) && is_file(DIR_IMAGE . $this->config->get('config_futura_img_twitter'))) {
		    $this->data['img_twitter'] = $this->model_tool_image->resize($this->config->get('config_futura_img_twitter'), 100, 100);
		} else {
		    $this->data['img_twitter'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}

		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);


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

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

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

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

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

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

	protected function validate() {

		if (!$this->request->post['config_name']) {
			$this->error['name'] = $this->language->get('error_name');
		}

		if ((utf8_strlen($this->request->post['config_owner']) < 3) || (utf8_strlen($this->request->post['config_owner']) > 64)) {
			$this->error['owner'] = $this->language->get('error_owner');
		}

		if($this->request->post['config_futura_contato_status']==1){
		    if ((utf8_strlen($this->request->post['config_futura_address']) < 3) || (utf8_strlen($this->request->post['config_futura_address']) > 256)) {
		        $this->error['address'] = $this->language->get('error_address');
		    }
		}

		if($this->request->post['config_futura_mapa_status']==1){

		    if (!$this->request->post['config_futura_mapa']) {
		        $this->error['mapa'] = $this->language->get('error_mapa');
		    }

		    if ($this->request->post['config_futura_width_mapa']>1200) {
		        $this->error['width_mapa'] = $this->language->get('error_width_mapa');
		    }

		    if ($this->request->post['config_futura_height_mapa']>880) {
		        $this->error['height_mapa'] = $this->language->get('error_height_mapa');
		    }
		}

		if (trim($this->request->post['config_futura_email'])){
			if ((substr_count($this->request->post['config_futura_email'],';') > 2)||(utf8_strlen($this->request->post['config_futura_email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z, ,;,]{2,6}$/i', $this->request->post['config_futura_email'])) {
				$this->error['email'] = $this->language->get('error_email');
			}
			if($this->request->post['config_futura_exibe_email']==0){
			    $this->error['exibe_email'] = $this->language->get('error_exibe_mail');
			}
		}

		if($this->request->post['config_futura_telefone_status']==1){
		    if ((utf8_strlen($this->request->post['config_futura_telephone_title']) < 3) || (utf8_strlen($this->request->post['config_futura_telephone_title']) > 32)) {
		        $this->error['title_telephone'] = $this->language->get('error_title_telephone');
		    }

		    if ((utf8_strlen($this->request->post['config_futura_telephone']) < 3) || (utf8_strlen($this->request->post['config_futura_telephone']) > 32)) {
		        $this->error['telephone'] = $this->language->get('error_telephone');
		    }
		}

		if (!$this->request->post['config_title']) {
			$this->error['title'] = $this->language->get('error_title');
		}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

		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));
	}
}
?>
