<?php
class Currency {
  	var $code;
  	var $currencies = array();
  
  	function __construct(&$locator) {
		$this->config   =& $locator->get('config');
		$this->database =& $locator->get('database');
		$this->request  =& $locator->get('request');
		$this->session  =& $locator->get('session');
		$this->language =& $locator->get('language');
		$this->customer =& $locator->get('customer');
		
   		$results = $this->database->cache('currency', "select * from currency");

    	foreach ($results as $result) {
      		$this->currencies[$result['code']] = array(
        		'currency_id'   => $result['currency_id'],
        		'title'         => $result['title'],
        		'symbol_left'   => $result['symbol_left'],
        		'symbol_right'  => $result['symbol_right'],
        		'decimal_place' => $result['decimal_place'],
        		'value'         => $result['value']
      		); 
    	}

		$default_currency="USD";
		$counry_id=$this->customer->getCountryId();
		if ($counry_id)	{
			$country = $this->database->getRow("select currency_code from country where country_id = ".$counry_id);
			$default_currency=$country['currency_code'];
		} 

    	if (array_key_exists($this->session->get('currency'), $this->currencies)) {
      		$this->set($this->session->get('currency'));
			$cur=$this->session->get('currency');
/*			echo "<script language=\"JavaScript\">alert('session=$cur');</script>"; */
			
    	}else {
			$this->set($default_currency);
			
/*			echo "<script language=\"JavaScript\">alert('default_currency=$default_currency');</script>"; */
		}
		/*elseif (array_key_exists($this->request->get('currency', 'cookie'), $this->currencies)) {
      		$this->set($this->request->get('currency', 'cookie'));

    	} else {
      		$this->set($this->config->get('config_currency'));
    	} */
		
  	}
	
  	function set($currency) {
  		
    	$this->code = $currency;

    	if ((!$this->session->has('currency')) || ($this->session->get('currency') != $currency)) {
      		$this->session->set('currency', $currency);			
    	}

    	if ((!$this->request->get('currency', 'cookie')) || ($this->request->get('currency', 'cookie') != $currency)) {
	  		setcookie('currency', $currency, time() + 3600 * 24 * 30, '/');			
    	}
  	}

  	/*
  	@ $number: USD amount
  	@ $currency: current currency
  	@ $value: USD/current currency
  	@ $format: TRUE: addpend symbol_left or symbol_right
  	@ return: example AU$130.93
  	*/
  	function format($number, $currency = NULL, $value = NULL, $format = TRUE) {
		if ($currency) {
      		$symbol_left   = $this->currencies[$currency]['symbol_left'];
      		$symbol_right  = $this->currencies[$currency]['symbol_right'];
      		$decimal_place = $this->currencies[$currency]['decimal_place'];
    	} else {
      		$symbol_left   = $this->currencies[$this->code]['symbol_left'];
      		$symbol_right  = $this->currencies[$this->code]['symbol_right'];
      		$decimal_place = $this->currencies[$this->code]['decimal_place'];
    	}

    	if ($value) {
      		$value = $value;
    	} else {
      		$value = $this->currencies[$this->code]['value'];
    	}

    	if ($value) {
      		$value = $number * $value;
    	} else {
      		$value = $number;
    	}

    	$string = '';

    	if (($symbol_left) && ($format)) {
      		$string .= $symbol_left;
    	}

//    	$string .= number_format(round($value, $decimal_place), $decimal_place, (($format) ? $this->language->get('decimal_point') : '.'), (($format) ? $this->language->get('thousand_point') : NULL));
$string .= number_format(round($value, 2), 2, '.', '');
    	if (($symbol_right) && ($format)) {
      		$string .= $symbol_right;
    	}

    	return $string;
  	}

  	function getCode() {
    	return $this->code;
  	}
  
  	function getValue($currency) {
    	return (isset($this->currencies[$currency]) ? $this->currencies[$currency]['value'] : NULL);
  	}
    
  	function has($currency) {
    	return isset($this->currencies[$currency]);
  	}
}
?>
