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

	public function index() {
		$language = $this->load->language('module/fields_register_brazil');
		$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->request->post['fields_register_brazil_status'] = '1';
			$this->model_setting_setting->editSetting('fields_register_brazil', $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');

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

    $this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['link'] = $this->url->link('catalog/product/update', 'token=' . $this->session->data['token'], 'SSL');

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

		$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('text_module'),
			'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/fields_register_brazil', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => ' :: '
		);

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


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

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

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

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

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

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

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

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

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

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

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

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

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

    public function install() {
		$query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "customer`
								   ADD `cpf` VARCHAR( 14 ) NOT NULL AFTER `fax` ,
								   add `celular` VARCHAR( 32 )NOT NULL after `cpf` ,
								   ADD `cnpj` VARCHAR( 18 ) NOT NULL AFTER `celular` ,
								   ADD `rg` VARCHAR( 9 ) NOT NULL AFTER `cnpj` ,
								   ADD `inscricao_estadual` VARCHAR( 200 ) NOT NULL AFTER `rg` ,
								   ADD `contribuinte` CHAR( 1 ) NOT NULL AFTER `inscricao_estadual` ,
								   ADD `perfil` int(1) NOT NULL AFTER `contribuinte` ,
								   ADD `razao_social` VARCHAR( 200 ) NOT NULL AFTER `perfil` ,
								   ADD `data_nascimento` DATE NOT NULL AFTER `inscricao_estadual`,
								   ADD `sexo` CHAR( 1 ) NOT NULL AFTER `data_nascimento`;
		");
		$query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "address`
								   ADD `apelido` VARCHAR( 128 ) NOT NULL AFTER `lastname`,
								   ADD `numero` VARCHAR( 14 ) NOT NULL AFTER `address_1`,
								   ADD `complemento` VARCHAR( 18 ) NOT NULL AFTER `address_2`;
		");
		$query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "order`
								   ADD `cpf` VARCHAR( 14 ) NOT NULL AFTER `fax` ,
								   ADD `cnpj` VARCHAR( 18 ) NOT NULL AFTER `cpf` ,
								   ADD `rg` VARCHAR( 9 ) NOT NULL AFTER `cnpj` ,
								   ADD `inscricao_estadual` VARCHAR( 200 ) NOT NULL AFTER `rg` ,
								   ADD `razao_social` VARCHAR( 200 ) NOT NULL AFTER `inscricao_estadual` ,
								   ADD `data_nascimento` DATE NOT NULL AFTER `inscricao_estadual`,
								   ADD `sexo` CHAR( 1 ) NOT NULL AFTER `data_nascimento`,
								   ADD `payment_apelido` VARCHAR( 128 ) NOT NULL AFTER `sexo`,
								   ADD `payment_numero` VARCHAR( 14 ) NOT NULL AFTER `payment_address_1`,
								   ADD `payment_complemento` VARCHAR( 18 ) NOT NULL AFTER `payment_address_2`,
								   ADD `shipping_apelido` VARCHAR( 128 ) NOT NULL AFTER `payment_code`,
								   ADD `shipping_numero` VARCHAR( 14 ) NOT NULL AFTER `shipping_address_1`,
								   ADD `shipping_complemento` VARCHAR( 18 ) NOT NULL AFTER `shipping_address_2`;
		");
	}

    public function uninstall() {
		/*$query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "customer`
								   DROP `cpf`,
								   DROP `celular`,
								   DROP `cnpj`,
								   DROP `rg`,
								   DROP `inscricao_estadual`,
								   DROP `razao_social`,
								   DROP `data_nascimento`,
								   DROP `sexo`;
		");
		$query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "address`
								   DROP `apelido`,
								   DROP `numero`,
								   DROP `complemento`;
		");
		$query = $this->db->query("ALTER TABLE `" . DB_PREFIX . "order`
								   DROP `cpf`,
								   DROP `cnpj`,
								   DROP `rg`,
								   DROP `inscricao_estadual`,
								   DROP `razao_social`,
								   DROP `data_nascimento`,
								   DROP `sexo`,
								   DROP `payment_apelido`,
								   DROP `payment_numero`,
								   DROP `payment_complemento`,
								   DROP `shipping_apelido`,
								   DROP `shipping_numero`,
								   DROP `shipping_complemento`;
		");
		por segurança, ao desinstalar, nao perde os dados... byMatheus
		*/
		$this->load->model('setting/setting');
		$this->model_setting_setting->editSetting('fields_register_brazil', array('fields_register_brazil_status' => '0'));
    }
}
?>
