<?php
/**
* @version      $Id: website_customer_registration.php
* @package      Easyshop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       Richard Kammermayer <rk@ta-edv.de>
* Easyshop is a web shop system
*/

include_once './core/country.class.php';
include_once './core/cs_ticket.class.php';
include_once './core/site_content.class.php';

class website_customer_registration {
	private $base_object;
	private $layout_object;
	
	public function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
	}
		
	function run() {
		$data = array(
			'state' => 0
		);

		$error_message = array();	
	
		if (isset($_POST['customer_field'])) {
			
			//$form_data = $this->base_object->customer->data_filter($_POST['customer_field']);
			$form_data = $_POST['customer_field'];
			$group_id = 1;
	
			$json = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=6LfR26UZAAAAACn88_M7nYay62q7NZL6v0oh4lK3&response='.$_POST['g-recaptcha-response']);
			$data_check = json_decode($json);
			//var_dump($data_check->success);
			
			if (
			      (
				($data_check->success && $form_data['honorific'] == 2) &&
				$form_data['company'] &&
				$form_data['street'] &&
				$form_data['house_number'] &&
				$form_data['zip_code'] &&
				$form_data['city'] &&
				$form_data['email'] &&
				$_POST['agreements_accepted'] &&
				$form_data['password1']
			      ) || (
				($form_data['honorific'] == '0' || $form_data['honorific'] == '1') && $data_check->success &&
				$form_data['firstname'] &&
				$form_data['surname'] &&
				$form_data['street'] &&
				$form_data['house_number'] &&
				$form_data['zip_code'] &&
				$form_data['city'] &&
				$form_data['email'] &&
				$_POST['agreements_accepted'] &&
				$form_data['password1']
			    )
			) {

				$customer_data = $form_data;
				
				if (isset($form_data['company'])) {
					$address_data['company'] = $form_data['company'];
				}
				
				if (isset($form_data['honorific'])) {
					$address_data['honorific'] = $form_data['honorific'];
				}
				
				if (isset($form_data['firstname'])) {
					$address_data['firstname'] = $form_data['firstname'];
				}
				
				if (isset($form_data['surname'])) {
					$address_data['surname'] = $form_data['surname'];
				}
				
				if (isset($form_data['street'])) {
					$address_data['street'] = $form_data['street'];
					unset($customer_data['street']);
				}
				
				if (isset($form_data['house_number'])) {
					$address_data['house_number'] = $form_data['house_number'];
					unset($customer_data['house_number']);
				}
				
				if (isset($form_data['zip_code'])) {
					$address_data['zip_code'] = $form_data['zip_code'];
					unset($customer_data['zip_code']);
				}
				
				if (isset($form_data['city'])) {
					$address_data['city'] = $form_data['city'];
					unset($customer_data['city']);
				}
				
				if (isset($form_data['country'])) {
					$address_data['country'] = $form_data['country'];
					unset($customer_data['country']);
				}
				
				if ($form_data['password1'] == $form_data['password2']) {
					$customer_data['pass'] = $form_data['password1'];
					unset($customer_data['password1']);
					unset($customer_data['password2']);
					unset($form_data['password1']);
					unset($form_data['password2']);
				} else {
					unset($form_data['password1']);
					unset($form_data['password2']);
					$data['state'] = 4;
					$error_message[] = 'Die Passwörter stimmen nicht überein!';
					$data['form_data'] = $form_data;
					
					$this->layout_object->assign('data', $data);
					// error message
					$this->layout_object->assign('error_message', $error_message);
					// get countries
					$country_object = new Country($this->base_object);
					$countries = $country_object->get_all(array(
						'isset_shipping_area' => 1
					));
					$this->layout_object->assign('countries', $countries);
					return $this->layout_object->_fetch('content_customer_registration.tpl');
				}

				$birthdate = $_POST['birthdate'];
				if ($birthdate['day'] != 0 && $birthdate['month'] != 0 && $birthdate['year'] != 0) {
					// there is a typo in the database
					$burth_date = $birthdate['year'] . '-' . $birthdate['month'] . '-' . $birthdate['day'];
					$customer_data['burth_date'] = $burth_date;
				}
				
				if (isset($form_data['merchant'])) {
					$customer_data['merchant'] = 1;
				} else {
					$customer_data['merchant'] = 0;
				}
				
				// new customer registration default customer group
				if ($this->base_object->config->shopConfiguration['customer_registration_group_id']) {
					$customer_data['group_id'] = $this->base_object->config->shopConfiguration['customer_registration_group_id'];
				}
				else {
					$customer_data['group_id'] = 100;
				}
				
				// get customer group
				$this->base_object->customer_group->id = $customer_data['group_id'];
				$customer_group_data = $this->base_object->customer_group->get_data();
				if ($customer_group_data->state_after_registration) {
					$customer_data['locked'] = 0;
				}
                                else {
                                        $customer_data['locked'] = 1;
                                }

				$result = $this->base_object->customer->create($customer_data);
				
				if ($result) {
					$address_data['customer_id'] = $result;
					$address_id = $this->base_object->customer->add_address($address_data);
					
					// Adresse als standard definieren
					Customer::set_standard_address($address_data['customer_id'], $address_id);
					
					// ticket event new customer registration
					$ticket_object = new Cs_ticket($this->base_object);

					if ($form_data['honorific'] == 2) {
						$ticket_object->event(13, $result, false); // company
					}
					else {					
						$ticket_object->event(2, $result, false); // person
					}
					
					// auto login
					if ($customer_group_data->state_after_registration == 2) {
						$_SESSION['easyshop']['login']['id'] = $result;
					}
				}
				
				$data['state'] = 1;
				
				if ($result) {
					if ($_SESSION['easyshop']['shopAction'] == 'cash_desk_customer_registration') {
						header('Location: /Kasse/');
					}
					else {
						header('Location: /Kundenregistrierung_abschluss/');
					}
				} else {
					$error = $this->base_object->customer->get_error();

					if ($error == 'email exisitiert bereits') {
						$data['state'] = 3;
						$error_message[] = 'Es ist bereits ein Kunde mit dieser E-Mail Adresse registriert!';
						$data['form_data'] = $form_data;
					} else {
						$data['state'] = 5;
						$data['form_data'] = $form_data;
						$error_message[] = 'Es ist ein Fehler Aufgetreten, bitte wenden Sie sich direkt an den Shopbetreiber!';
					}
				}
			}
			else {
				$data['state'] = 2;
				$error_message[] = 'Bitte füllen Sie alle Pflichtfelder aus und bestätigen Sie unsere AGB!';
				$data['form_data'] = $form_data;
			}
		}
		else {
			if (isset($_POST['shopAction']) && $_POST['shopAction'] == 'cash_desk_customer_registration') {
				$_SESSION['easyshop']['shopAction'] = 'cash_desk_customer_registration';
			}
			else {
				$_SESSION['easyshop']['shopAction'] = '';
			}
		}
		
		// information_privacy_statement
		$site_content_object = new SiteContent($this->base_object);

		$agb_data = array();
		$agb_data['datenschutz'] = $site_content_object->get_textbox_data_by_id(55);
		$agb_data['agbs'] = $site_content_object->get_textbox_data_by_id(56);
		$agb_data['widerruf'] = $site_content_object->get_textbox_data_by_id(60);
		$this->layout_object->assign('agb_data', $agb_data);

		// get countries
		$country_object = new Country($this->base_object);
		$countries = $country_object->get_all(array('isset_shipping_area' => 1));
		$this->layout_object->assign('countries', $countries);
		
		// error message
		$this->layout_object->assign('error_message', $error_message);
		
		// days
		$days = array();
		for($i=1;$i<32;$i++) {
			$days[] = $i;
		}
		$this->layout_object->assign('days', $days);
		// months
		$months = array();
		for($i=1;$i<13;$i++) {
			$months[] = $i;
		}
		$this->layout_object->assign('months', $months);
		// years
		$years = array();
		for($i=1944;$i<2012;$i++) {
			$years[] = $i;
		}
		$this->layout_object->assign('years', $years);

		$this->layout_object->assign('data', $data);

		return $this->layout_object->_fetch('content_customer_registration.tpl');
	}
}