1: <?php
2: namespace Opencart\Admin\Model\Setting;
3: /**
4: * Class Setting
5: *
6: * @package Opencart\Admin\Model\Setting
7: */
8: class Setting extends \Opencart\System\Engine\Model {
9: /**
10: * Get Settings
11: *
12: * @param int $store_id
13: *
14: * @return array<int, array<string, mixed>>
15: */
16: public function getSettings(int $store_id = 0): array {
17: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '" . (int)$store_id . "' OR `store_id` = '0' ORDER BY `store_id` ASC");
18:
19: return $query->rows;
20: }
21:
22: /**
23: * Get Setting
24: *
25: * @param string $code
26: * @param int $store_id
27: *
28: * @return array<string, mixed>
29: */
30: public function getSetting(string $code, int $store_id = 0): array {
31: $setting_data = [];
32:
33: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '" . (int)$store_id . "' AND `code` = '" . $this->db->escape($code) . "'");
34:
35: foreach ($query->rows as $result) {
36: if (!$result['serialized']) {
37: $setting_data[$result['key']] = $result['value'];
38: } else {
39: $setting_data[$result['key']] = json_decode($result['value'], true);
40: }
41: }
42:
43: return $setting_data;
44: }
45:
46: /**
47: * Edit Setting
48: *
49: * @param string $code
50: * @param array<string, mixed> $data
51: * @param int $store_id
52: *
53: * @return void
54: */
55: public function editSetting(string $code, array $data, int $store_id = 0): void {
56: $this->deleteSetting($code, $store_id);
57:
58: foreach ($data as $key => $value) {
59: if (substr($key, 0, strlen($code)) == $code) {
60: $this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `store_id` = '" . (int)$store_id . "', `code` = '" . $this->db->escape($code) . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape(!is_array($value) ? $value : json_encode($value)) . "', `serialized` = '" . (bool)is_array($value) . "'");
61: }
62: }
63: }
64:
65: /**
66: * Delete Setting
67: *
68: * @param string $code
69: * @param int $store_id
70: *
71: * @return void
72: */
73: public function deleteSetting(string $code, int $store_id = 0): void {
74: $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '" . (int)$store_id . "' AND `code` = '" . $this->db->escape($code) . "'");
75: }
76:
77: /**
78: * Delete Settings By Code
79: *
80: * @param string $code
81: *
82: * @return void
83: */
84: public function deleteSettingsByCode(string $code): void {
85: $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE `code` = '" . $this->db->escape($code) . "'");
86: }
87:
88: /**
89: * Delete Settings By Store ID
90: *
91: * @param int $store_id
92: *
93: * @return void
94: */
95: public function deleteSettingsByStoreId(int $store_id): void {
96: $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '" . (int)$store_id . "'");
97: }
98:
99: /**
100: * Get Value
101: *
102: * @param string $key
103: * @param int $store_id
104: *
105: * @return string
106: */
107: public function getValue(string $key, int $store_id = 0): string {
108: $query = $this->db->query("SELECT `value` FROM `" . DB_PREFIX . "setting` WHERE `store_id` = '" . (int)$store_id . "' AND `key` = '" . $this->db->escape($key) . "'");
109:
110: if ($query->num_rows) {
111: return $query->row['value'];
112: } else {
113: return '';
114: }
115: }
116:
117: /**
118: * Edit Value
119: *
120: * @param string $code
121: * @param string $key
122: * @param array<mixed>|string $value
123: * @param int $store_id
124: *
125: * @return void
126: */
127: public function editValue(string $code = '', string $key = '', $value = '', int $store_id = 0): void {
128: $this->db->query("UPDATE `" . DB_PREFIX . "setting` SET `value` = '" . $this->db->escape(!is_array($value) ? $value : json_encode($value)) . "', `serialized` = '" . (bool)is_array($value) . "' WHERE `code` = '" . $this->db->escape($code) . "' AND `key` = '" . $this->db->escape($key) . "' AND `store_id` = '" . (int)$store_id . "'");
129: }
130: }
131: