shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

weight.php (1689B)


      1 <?php
      2 namespace Cart;
      3 class Weight {
      4 	private $weights = array();
      5 
      6 	public function __construct($registry) {
      7 		$this->db = $registry->get('db');
      8 		$this->config = $registry->get('config');
      9 
     10 		$weight_class_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
     11 
     12 		foreach ($weight_class_query->rows as $result) {
     13 			$this->weights[$result['weight_class_id']] = array(
     14 				'weight_class_id' => $result['weight_class_id'],
     15 				'title'           => $result['title'],
     16 				'unit'            => $result['unit'],
     17 				'value'           => $result['value']
     18 			);
     19 		}
     20 	}
     21 
     22 	public function convert($value, $from, $to) {
     23 		if ($from == $to) {
     24 			return $value;
     25 		}
     26 
     27 		if (isset($this->weights[$from])) {
     28 			$from = $this->weights[$from]['value'];
     29 		} else {
     30 			$from = 1;
     31 		}
     32 
     33 		if (isset($this->weights[$to])) {
     34 			$to = $this->weights[$to]['value'];
     35 		} else {
     36 			$to = 1;
     37 		}
     38 
     39 		return $value * ($to / $from);
     40 	}
     41 
     42 	public function format($value, $weight_class_id, $decimal_point = '.', $thousand_point = ',') {
     43 		if (isset($this->weights[$weight_class_id])) {
     44 			return number_format($value, 2, $decimal_point, $thousand_point) . $this->weights[$weight_class_id]['unit'];
     45 		} else {
     46 			return number_format($value, 2, $decimal_point, $thousand_point);
     47 		}
     48 	}
     49 
     50 	public function getUnit($weight_class_id) {
     51 		if (isset($this->weights[$weight_class_id])) {
     52 			return $this->weights[$weight_class_id]['unit'];
     53 		} else {
     54 			return '';
     55 		}
     56 	}
     57 }