Skip to content

Commit

Permalink
Currency code models updated like in oc 4
Browse files Browse the repository at this point in the history
  • Loading branch information
condor2 committed Mar 1, 2024
1 parent 3563465 commit c7a7c03
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 63 deletions.
112 changes: 53 additions & 59 deletions upload/admin/model/localisation/currency.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<?php
class ModelLocalisationCurrency extends Model {
public function addCurrency($data) {
$this->db->query("INSERT INTO " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . $this->db->escape((string)$data['decimal_place']) . "', value = '" . (float)$data['value'] . "', status = '" . (bool)$data['status'] . "', date_modified = NOW()");

$currency_id = $this->db->getLastId();
$this->db->query("INSERT INTO " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . (int)$data['decimal_place'] . "', value = '" . (float)$data['value'] . "', status = '" . (bool)($data['status'] ?? 0) . "', date_modified = NOW()");

$this->cache->delete('currency');

return $currency_id;
return $this->db->getLastId();
}

public function editCurrency($currency_id, $data) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . $this->db->escape((string)$data['decimal_place']) . "', value = '" . (float)$data['value'] . "', status = '" . (bool)$data['status'] . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . (int)$data['decimal_place'] . "', value = '" . (float)$data['value'] . "', status = '" . (bool)($data['status'] ?? 0) . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'");

$this->cache->delete('currency');
}

public function editValueByCode($code, $value) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = NOW() WHERE code = '" . $this->db->escape((string)$code) . "'");
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = NOW() WHERE code = '" . $this->db->escape($code) . "'");

$this->cache->delete('currency');
}
Expand All @@ -41,70 +39,66 @@ public function getCurrencyByCode($currency) {
}

public function getCurrencies(array $data = []) {
if ($data) {
$sql = "SELECT * FROM " . DB_PREFIX . "currency";

$sort_data = [
'title',
'code',
'value',
'date_modified'
];
$sql = "SELECT * FROM " . DB_PREFIX . "currency";

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY title";
}
$sort_data = [
'title',
'code',
'value',
'date_modified'
];

if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY title";
}

if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}

if ($data['limit'] < 1) {
$data['limit'] = 20;
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}

$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
if ($data['limit'] < 1) {
$data['limit'] = 20;
}

$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}

$results = $this->cache->get('currency.' . md5($sql));

if (!$results) {
$query = $this->db->query($sql);

return $query->rows;
} else {
$currency_data = $this->cache->get('currency');

if (!$currency_data) {
$currency_data = [];

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency ORDER BY title ASC");

foreach ($query->rows as $result) {
$currency_data[$result['code']] = [
'currency_id' => $result['currency_id'],
'title' => $result['title'],
'code' => $result['code'],
'symbol_left' => $result['symbol_left'],
'symbol_right' => $result['symbol_right'],
'decimal_place' => $result['decimal_place'],
'value' => $result['value'],
'status' => $result['status'],
'date_modified' => $result['date_modified']
];
}

$this->cache->set('currency', $currency_data);
}
$results = $query->rows;

return $currency_data;
$this->cache->set('currency.' . md5($sql), $results);
}

$currency_data = [];

foreach ($results as $result) {
$currency_data[$result['code']] = [
'currency_id' => $result['currency_id'],
'title' => $result['title'],
'code' => $result['code'],
'symbol_left' => $result['symbol_left'],
'symbol_right' => $result['symbol_right'],
'decimal_place' => $result['decimal_place'],
'value' => $result['value'],
'status' => $result['status'],
'date_modified' => $result['date_modified']
];
}

return $currency_data;
}

public function getTotalCurrencies() {
Expand Down
10 changes: 6 additions & 4 deletions upload/catalog/model/localisation/currency.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php
class ModelLocalisationCurrency extends Model {
public function getCurrencyByCode(string $currency): array {
$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "currency` WHERE `code` = '" . $this->db->escape($currency) . "'");
$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "currency` WHERE `code` = '" . $this->db->escape($currency) . "' AND `status` = '1'");

return $query->row;
}

public function getCurrencies(): array {
$currency_data = $this->cache->get('currency');
$sql = "SELECT * FROM `" . DB_PREFIX . "currency` WHERE `status` = '1' ORDER BY `title` ASC";

$currency_data = $this->cache->get('currency.' . md5($sql));

if (!$currency_data) {
$currency_data = [];

$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "currency` WHERE `status` = '1' ORDER BY `title` ASC");
$query = $this->db->query($sql);

foreach ($query->rows as $result) {
$currency_data[$result['code']] = [
Expand All @@ -28,7 +30,7 @@ public function getCurrencies(): array {
];
}

$this->cache->set('currency', $currency_data);
$this->cache->set('currency.' . md5($sql), $currency_data);
}

return $currency_data;
Expand Down

0 comments on commit c7a7c03

Please sign in to comment.