currency.php (5898B)
1 <?php 2 class ModelLocalisationCurrency extends Model { 3 public function addCurrency($data) { 4 $this->db->query("INSERT INTO " . DB_PREFIX . "currency SET title = '" . $this->db->escape($data['title']) . "', code = '" . $this->db->escape($data['code']) . "', symbol_left = '" . $this->db->escape($data['symbol_left']) . "', symbol_right = '" . $this->db->escape($data['symbol_right']) . "', decimal_place = '" . $this->db->escape($data['decimal_place']) . "', value = '" . $this->db->escape($data['value']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW()"); 5 6 $currency_id = $this->db->getLastId(); 7 8 if ($this->config->get('config_currency_auto')) { 9 $this->refresh(true); 10 } 11 12 $this->cache->delete('currency'); 13 14 return $currency_id; 15 } 16 17 public function editCurrency($currency_id, $data) { 18 $this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape($data['title']) . "', code = '" . $this->db->escape($data['code']) . "', symbol_left = '" . $this->db->escape($data['symbol_left']) . "', symbol_right = '" . $this->db->escape($data['symbol_right']) . "', decimal_place = '" . $this->db->escape($data['decimal_place']) . "', value = '" . $this->db->escape($data['value']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'"); 19 20 $this->cache->delete('currency'); 21 } 22 23 public function deleteCurrency($currency_id) { 24 $this->db->query("DELETE FROM " . DB_PREFIX . "currency WHERE currency_id = '" . (int)$currency_id . "'"); 25 26 $this->cache->delete('currency'); 27 } 28 29 public function getCurrency($currency_id) { 30 $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "currency WHERE currency_id = '" . (int)$currency_id . "'"); 31 32 return $query->row; 33 } 34 35 public function getCurrencyByCode($currency) { 36 $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "currency WHERE code = '" . $this->db->escape($currency) . "'"); 37 38 return $query->row; 39 } 40 41 public function getCurrencies($data = array()) { 42 if ($data) { 43 $sql = "SELECT * FROM " . DB_PREFIX . "currency"; 44 45 $sort_data = array( 46 'title', 47 'code', 48 'value', 49 'date_modified' 50 ); 51 52 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 53 $sql .= " ORDER BY " . $data['sort']; 54 } else { 55 $sql .= " ORDER BY title"; 56 } 57 58 if (isset($data['order']) && ($data['order'] == 'DESC')) { 59 $sql .= " DESC"; 60 } else { 61 $sql .= " ASC"; 62 } 63 64 if (isset($data['start']) || isset($data['limit'])) { 65 if ($data['start'] < 0) { 66 $data['start'] = 0; 67 } 68 69 if ($data['limit'] < 1) { 70 $data['limit'] = 20; 71 } 72 73 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 74 } 75 76 $query = $this->db->query($sql); 77 78 return $query->rows; 79 } else { 80 $currency_data = $this->cache->get('currency'); 81 82 if (!$currency_data) { 83 $currency_data = array(); 84 85 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency ORDER BY title ASC"); 86 87 foreach ($query->rows as $result) { 88 $currency_data[$result['code']] = array( 89 'currency_id' => $result['currency_id'], 90 'title' => $result['title'], 91 'code' => $result['code'], 92 'symbol_left' => $result['symbol_left'], 93 'symbol_right' => $result['symbol_right'], 94 'decimal_place' => $result['decimal_place'], 95 'value' => $result['value'], 96 'status' => $result['status'], 97 'date_modified' => $result['date_modified'] 98 ); 99 } 100 101 $this->cache->set('currency', $currency_data); 102 } 103 104 return $currency_data; 105 } 106 } 107 108 public function refresh($force = false) { 109 $currency_data = array(); 110 111 if ($force) { 112 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'"); 113 } else { 114 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' AND date_modified < '" . $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "'"); 115 } 116 117 foreach ($query->rows as $result) { 118 $currency_data[] = $this->config->get('config_currency') . $result['code'] . '=X'; 119 $currency_data[] = $result['code'] . $this->config->get('config_currency') . '=X'; 120 } 121 122 $curl = curl_init(); 123 124 curl_setopt($curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $currency_data) . '&f=sl1&e=.json'); 125 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 126 curl_setopt($curl, CURLOPT_HEADER, false); 127 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); 128 curl_setopt($curl, CURLOPT_TIMEOUT, 30); 129 130 $content = curl_exec($curl); 131 132 curl_close($curl); 133 134 $line = explode("\n", trim($content)); 135 136 for ($i = 0; $i < count($line); $i = $i + 2) { 137 $currency = utf8_substr($line[$i], 4, 3); 138 $value = utf8_substr($line[$i], 11, 6); 139 140 if ((float)$value < 1 && isset($line[$i + 1])) { 141 $value = (1 / utf8_substr($line[$i + 1], 11, 6)); 142 } 143 144 if ((float)$value) { 145 $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($currency) . "'"); 146 } 147 } 148 149 $this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '1.00000', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "'"); 150 151 $this->cache->delete('currency'); 152 } 153 154 public function getTotalCurrencies() { 155 $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "currency"); 156 157 return $query->row['total']; 158 } 159 }