<?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');
			
   		$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']
      		); 
    	}
  
    	if (array_key_exists($this->session->get('currency'), $this->currencies)) {
      		$this->set($this->session->get('currency'));
    	} 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() + 60 * 60 * 24 * 30, '/', $_SERVER['HTTP_HOST']);
    	}
  	}

  	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));

    	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]);
  	}
}
?>
