<?php 



class ControllerRegister extends Controller {



	var $error = array();  



  	function index() {



$cache    =& $this->locator->get('cache');



		$cart     =& $this->locator->get('cart');



		$config   =& $this->locator->get('config');



		$customer =& $this->locator->get('customer');



		$database =& $this->locator->get('database');



		$language =& $this->locator->get('language');



		$module   =& $this->locator->get('module');



		$request  =& $this->locator->get('request');



		$response =& $this->locator->get('response');



		$session  =& $this->locator->get('session');



		$template =& $this->locator->get('template');



		$url      =& $this->locator->get('url');



    	$language->load('controller/register.php');



    	$template->set('title', $language->get('heading_title'));	

		

		if ($request->isPost()) {

		$post_invite_code=trim($_REQUEST['invite_code']);

        $post_email=trim($request->get('email', 'post'));

        $post_firstname=trim($request->get('firstname', 'post'));

	    $post_lastname=trim($request->get('lastname', 'post'));

		$post_md5_password=md5(trim($request->get('password', 'post')));      	  

		$full_name = $post_firstname.' '.$post_lastname;

		

		$search_result=	$database->getRow($database->parse("select customer_id  from customer where email = '?'", $post_email));

		if ($search_result) {

      	    echo "<script>alert('Error: E-mail address is already registered, if you forgot password, please input your first name or last name instead of password!');history.back();</script>";

			return;

    	 }	 

		

		$sql_country="select country_id, name from country where country_id='?'";		

		

		$country_id=(int)$request->get('country_id', 'post');

		

		$country_row=$database->getRow($database->parse($sql_country, $country_id));   

		

		$country_name = $country_row['name'];

		

		$customer_manage = $database->getRow("select register_verify from login_setting");			

		

		$sql = "insert into customer set firstname = '?', lastname = '?', email = '?', password = '?',status = '?', vip_rank = 'General Buyer', current_points = '?', invitation_code = '?',country_id = '?',ip = '".$_SERVER['REMOTE_ADDR']."',date_added ='?',remark ='?'";

		

		$registr_status=2;

		$remark=isset($_POST['hello_page'])?'beststoreitems_login':'beststoreitems_gate';

		$init_points=1;

		

		date_default_timezone_set('Asia/Hong_Kong');

		$database->query($database->parse($sql, $post_firstname, $post_lastname, $post_email, $post_md5_password, $registr_status, $init_points, md5(time()),$country_id, date('Y-m-d H:i:s',time()),$remark));

		

		$code = date('YmdHis');

		

		$start_time = date("Y-m-d").' 00:00:00';



		$end_time =date("Y-m-d 00:00:00",strtotime("+1 Year"));



		$database->query("insert into coupon set code = '$code', discount = 10.00, prefix = '-', shipping = 0, date_start = '$start_time', date_end = '$end_time', status = 1, assign_to = '$full_name', email = '$post_email',usable_times = 1, date_added = '".date('Y-m-d H:i:s',time())."'");

		

		$insert_id = $database->getLastId();

		

		$database->query("insert into coupon_description set coupon_id = $insert_id, language_id = 1, name = '$code', description = 'Registration Rewards'");				

		

		if (!empty($post_invite_code)){

			$search_customer  =	$database->getRow($database->parse("select customer_id,	email, firstname, lastname from customer where invitation_codes = '?'", $post_invite_code));

			if ($search_customer) {

				$code1 = date('YmdHi');

				$email1 = $search_customer['email'];

				$full_name1 = $search_customer['firstname'].' '.$search_customer['lastname'];

$database->query("insert into coupon set code = '$code1', discount = 10.00, prefix = '-', shipping = 0, date_start = '$start_time', date_end = '$end_time', status = 1, assign_to = '$full_name1', email = '$email1',usable_times = 1, date_added = '".date('Y-m-d H:i:s',time())."'");

		

			$insert_id = $database->getLastId();

		

			$database->query("insert into coupon_description set coupon_id = $insert_id, language_id = 1, name = '$code1', description = 'Affilate Rewards'");	

    	 	}	 

		}

		

		$login_result=$customer->login($post_email, $post_md5_password); 

		

		if ($registr_status && $login_result)

		{

//			 header('Location: https://www.beststoreitems.com'); 

	echo "<script>alert('Thanks for your registeration, there is a $10 coupon in your account, enjoy shopping.');top.location='https://www.beststoreitems.com';</script>";   

		}

		else

		{

			echo "<script>alert('Thanks for your registeration at beststoreitems.com, We need to review your account within 24 hours. Please try to login later.');top.location='https://www.beststoreitems.com';</script>";                    

	    

		}

	  } 

  }



}







?>