<?php

class Customer {

	var $data = array();



  	function __construct(&$locator) {

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

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

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

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

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

		if ($this->session->has('customer_id')) { 

			$this->data = $this->database->getRow("select * from customer where customer_id = '" . (int)$this->session->get('customer_id') . "'");	

			if ($this->data) {

      			$sql = "update customer set cart = '?', ip = '?' where customer_id = '?'";

      			$this->database->query($this->database->parse($sql, serialize($this->cart->getData('cart')), $_SERVER['REMOTE_ADDR'], $this->session->get('customer_id')));

			} else {

				$this->logout();

			}

  		}

	}		

  	function login($email, $password) {		

	$sql = "select * from customer where email = '?' and password = '?' and status >= '1'";
  	$customer_info = $this->database->getRow($this->database->parse($sql, $email, (strlen($password)==32)?$password:md5($password)));

    	if ($customer_info) {			 
			
			$ip=$customer_info['ip'];
			$login_ip=$_SERVER['REMOTE_ADDR'];
			$remark=$customer_info['remark'];
			if (empty($remark))
			{
				$remark="[".$ip."]";				
			}				
			if ( strpos($remark, $login_ip)=== false )
			{				
				$remark .= "-[".$login_ip."]";
			}
//			echo $remark; exit;
			date_default_timezone_set('Asia/Hong_Kong');
$this->database->query("UPDATE customer SET ip='".$login_ip."', remark='".$remark."', lastlogin_time='".date('Y-m-d H:i:s',time())."' WHERE customer_id=".$customer_info['customer_id']);

      		$this->session->set('customer_id', $customer_info['customer_id']);

	  		$this->data = $customer_info;			

	  		if ($customer_info['cart']) {

        		$this->cart->restore(unserialize($customer_info['cart']));
      		}

	  		return TRUE;

    	} else {

      		return FALSE;

    	}

  	}  

  	function logout() {

		$this->session->delete('customer_id');

    	$this->data = array();
  	}  

  	function isLogged() {
    	return !empty($this->data);
  	}

  	function getId() {
    	return (isset($this->data['customer_id']) ? (int)$this->data['customer_id'] : NULL);

  	}      
  	function getPoints() {
    	return (isset($this->data['current_points']) ? $this->data['current_points'] : NULL);
  	}
  	function getFirstName() {
		return (isset($this->data['firstname']) ? $this->data['firstname'] : NULL);
  	} 

  	function getLastName() {
		return (isset($this->data['lastname']) ? $this->data['lastname'] : NULL);
  	}
  function getFullName() {
		return ($this->getFirstName().' '.$this->getLastName());
  	}

  	function getEmail() {
		return (isset($this->data['email']) ? $this->data['email'] : NULL);
  	}
  
  	function getTelephone() {
		return (isset($this->data['telephone']) ? $this->data['telephone'] : NULL);
  	}
  

  	function getAddressId() {

		return (isset($this->data['address_id']) ? $this->data['address_id'] : NULL);	

  	}

	function getCountryId() {
		return (isset($this->data['country_id']) ? $this->data['country_id'] : NULL);	
  	}
  	
  	function getCountryName() {  		
		$result=$this->database->getRow("select name from country where country_id = '".$this->getCountryId()."'");
//		exit($this->getCountryId());
		return (isset($result['name']) ? $result['name'] : NULL);	
  	}
  	
  	function getTimeZone (){
  		if(!isset($this->data['country_id'])){
  			return 'America/Los_Angeles';
  		}
  		$time_zone ="America/Los_Angeles";
  		switch($this->data['country_id']){  			
  			case 223: // USA
  			case 38: // CANADA
  				{
  					$time_zone='America/Los_Angeles';
  					break;
  				}
  			case 188: // SG
  			case 129: // MA
  			{ $time_zone='Asia/Singapore'; break;}  	 		
  			case 13: // AU
  			case 153: // NZ
  			{ $time_zone='Australia/Queensland'; break;}
  			case 222: // UK
  			case 56: // CZECH
  			{ $time_zone='Europe/London'; break;}
  			default:{ $time_zone='America/Los_Angeles'; break;}
  		}
  		return $time_zone;
  	}

  	function getNewsLetter() {
		return (isset($this->data['newsletter']) ? $this->data['newsletter'] : NULL);	
  	}

  	function getStatus() {
		return (isset($this->data['status']) ? $this->data['status'] : 0);	
  	}	

}

?>

