<?php
class ControllerQuickCheckoutRegister extends Controller {
  	public function index() {
		$data = $this->load->language('checkout/checkout');
		$data = array_merge($data, $this->load->language('quickcheckout/checkout'));

		$data['entry_newsletter'] = sprintf($this->language->get('entry_newsletter'), $this->config->get('config_name'));

		if ($this->config->get('config_account_id')) {
			$this->load->model('catalog/information');

			$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));

			if ($information_info) {
				$data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/agree', 'information_id=' . $this->config->get('config_account_id'), true), $information_info['title'], $information_info['title']);
			} else {
				$data['text_agree'] = '';
			}
		} else {
			$data['text_agree'] = '';
		}

		// All variables
		$data['field_newsletter'] = $this->config->get('quickcheckout_field_newsletter');

		if(file_exists(DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') .'/template/quickcheckout/register.tpl')){
		    return $this->load->view( DIR_LOCAL_TEMPLATE . $this->config->get('theme_default_directory') .'/template/quickcheckout/register', $data);
		}else{ 
		    return $this->load->view(DIR_TEMPLATE . 'default/template/quickcheckout/register', $data);
		}

		
  	}

	public function validate() {
		$this->load->language('checkout/checkout');
		$this->load->language('quickcheckout/checkout');

		$this->load->model('account/customer');

		$json = array();

		// Validate if customer is already logged out.
		if ($this->customer->isLogged()) {
			$json['redirect'] = $this->url->link('quickcheckout/checkout', '', true);
		}

		if (!$json) {
			// Customer Group
			if (isset($this->request->post['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
				$customer_group_id = $this->request->post['customer_group_id'];
			} else {
				$customer_group_id = $this->config->get('config_customer_group_id');
			}
			
			if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
				$json['error']['warning'] = $this->language->get('error_exists');
			}

			if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
				$json['error']['password'] = $this->language->get('error_password');
			}

			if ($this->request->post['confirm'] != $this->request->post['password']) {
				$json['error']['confirm'] = $this->language->get('error_confirm');
			}

			if ($this->config->get('config_account_id')) {
				$this->load->model('catalog/information');

				$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));

				if ($information_info && !isset($this->request->post['agree'])) {
					$json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
				}
			}
		}

		if (!$json) {
			$customer_id = $this->model_account_customer->addCustomer($this->request->post);
			
			// Clear any previous login attempts for unregistered accounts.
			$this->model_account_customer->deleteLoginAttempts($this->request->post['email']);

			$this->session->data['account'] = 'register';

			$this->load->model('account/customer_group');

			$customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);

			if ($customer_group_info && !$customer_group_info['approval']) {
				$this->customer->login($this->request->post['email'], $this->request->post['password']);

                #NetGSM 2020/06/08 BILAL

                if($this->config->get('netgsm_status')) {

                    $this->load->model('setting/setting');
                    $netgsm_ayarlari = $this->model_setting_setting->getSetting('netgsm');


                    if ($netgsm_ayarlari['netgsm_status'] == 1) {
                        if (isset($netgsm_ayarlari['netgsm_newuser_to_customer_control']) and $netgsm_ayarlari['netgsm_newuser_to_customer_control'] == 1 and $netgsm_ayarlari['netgsm_newuser_to_customer_text'] != '') {
                            $mesaj = str_replace(array('[uye_adi]', '[uye_soyadi]', '[uye_telefonu]', '[uye_epostasi]', '[uye_sifresi]'), array($this->request->post['firstname'], $this->request->post['lastname'], $this->request->post['telephone'], $this->request->post['email'], $this->request->post['password']), $netgsm_ayarlari['netgsm_newuser_to_customer_text']);
                            $netgsmsms = new Netgsmsms($netgsm_ayarlari['netgsm_user'], $netgsm_ayarlari['netgsm_pass'], $netgsm_ayarlari['netgsm_input_smstitle'], $netgsm_ayarlari['netgsm_turkishChar']);
                            $smsgonder = $netgsmsms->sendSMS($this->request->post['telephone'], $mesaj);
                        }
                    }
                    if (isset($netgsm_ayarlari['netgsm_newuser_to_admin_control']) and $netgsm_ayarlari['netgsm_newuser_to_admin_control'] == 1 and $netgsm_ayarlari['netgsm_newuser_to_admin_text'] != '') {
                        $mesaj = str_replace(array('[uye_adi]', '[uye_soyadi]', '[uye_telefonu]', '[uye_epostasi]', '[uye_sifresi]'), array($this->request->post['firstname'], $this->request->post['lastname'], $this->request->post['telephone'], $this->request->post['email'], $this->request->post['password']), $netgsm_ayarlari['netgsm_newuser_to_admin_text']);
                        $netgsmsms = new Netgsmsms($netgsm_ayarlari['netgsm_user'], $netgsm_ayarlari['netgsm_pass'], $netgsm_ayarlari['netgsm_input_smstitle'], $netgsm_ayarlari['netgsm_turkishChar']);
                        $smsgonder = $netgsmsms->sendSMS($netgsm_ayarlari['netgsm_newuser_to_admin_no'], $mesaj);
                    }

                    if ($netgsm_ayarlari['netgsm_status'] == 1) {
                        if (isset($netgsm_ayarlari['netgsm_rehber_control']) and $netgsm_ayarlari['netgsm_rehber_control'] == 1 and $netgsm_ayarlari['netgsm_rehber_groupname'] != '') {
                            $netgsmsms = new Netgsmsms($netgsm_ayarlari['netgsm_user'], $netgsm_ayarlari['netgsm_pass'], $netgsm_ayarlari['netgsm_input_smstitle'], $netgsm_ayarlari['netgsm_turkishChar']);
                            $grubakaydet = $netgsmsms->addContact($this->request->post['firstname'], $this->request->post['lastname'], $this->request->post['telephone'], $netgsm_ayarlari['netgsm_rehber_groupname']);
                        }
                    }
                }
                #NetGSM 2020/06/08 BILAL

				// Default Payment Address
				$this->load->model('account/address');

				$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());

				if (!empty($this->request->post['shipping_address'])) {
					$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
				}
			} else {
				$json['redirect'] = $this->url->link('account/success');
			}

			unset($this->session->data['guest']);

			// Add to activity log
			if ($this->config->get('config_customer_activity')) {
				$this->load->model('account/activity');

				$activity_data = array(
					'customer_id' => $customer_id,
					'name'        => $this->request->post['firstname'] . ' ' . $this->request->post['lastname']
				);

				$this->model_account_activity->addActivity('register', $activity_data);
			}
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
}