1: <?php
2: namespace Opencart\Admin\Model\Setting;
3: /**
4: * Class Store
5: *
6: * @package Opencart\Admin\Model\Setting
7: */
8: class Store extends \Opencart\System\Engine\Model {
9: /**
10: * Add Store
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addStore(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "store` SET `name` = '" . $this->db->escape((string)$data['config_name']) . "', `url` = '" . $this->db->escape((string)$data['config_url']) . "'");
18:
19: $store_id = $this->db->getLastId();
20:
21: // Layout Route
22: $this->load->model('design/layout');
23:
24: $results = $this->model_design_layout->getRoutesByStoreId(0);
25:
26: foreach ($results as $result) {
27: $this->model_design_layout->addRoute($result['layout_id'], $result + ['store_id' => $store_id]);
28: }
29:
30: // SEO URL
31: $this->load->model('design/seo_url');
32:
33: $results = $this->model_design_seo_url->getSeoUrlsByStoreId(0);
34:
35: foreach ($results as $result) {
36: $this->model_design_seo_url->addSeoUrl($result['key'], $result['value'], $result['keyword'], $store_id, $result['language_id'], $result['sort_order']);
37: }
38:
39: $this->cache->delete('store');
40:
41: return $store_id;
42: }
43:
44: /**
45: * Edit Store
46: *
47: * @param int $store_id
48: * @param array<string, mixed> $data
49: *
50: * @return void
51: */
52: public function editStore(int $store_id, array $data): void {
53: $this->db->query("UPDATE `" . DB_PREFIX . "store` SET `name` = '" . $this->db->escape((string)$data['config_name']) . "', `url` = '" . $this->db->escape((string)$data['config_url']) . "' WHERE `store_id` = '" . (int)$store_id . "'");
54:
55: $this->cache->delete('store');
56: }
57:
58: /**
59: * Delete Store
60: *
61: * @param int $store_id
62: *
63: * @return void
64: */
65: public function deleteStore(int $store_id): void {
66: $this->db->query("DELETE FROM `" . DB_PREFIX . "store` WHERE `store_id` = '" . (int)$store_id . "'");
67:
68: // Category
69: $this->load->model('catalog/category');
70:
71: $this->model_catalog_category->deleteLayoutsByStoreId($store_id);
72: $this->model_catalog_category->deleteStoresByStoreId($store_id);
73:
74: $this->load->model('catalog/information');
75:
76: $this->model_catalog_information->deleteLayoutsByStoreId($store_id);
77: $this->model_catalog_information->deleteStoresByStoreId($store_id);
78:
79: $this->load->model('catalog/manufacturer');
80:
81: $this->model_catalog_manufacturer->deleteLayoutsByStoreId($store_id);
82: $this->model_catalog_manufacturer->deleteStoresByStoreId($store_id);
83:
84: $this->load->model('catalog/product');
85:
86: $this->model_catalog_product->deleteLayoutsByStoreId($store_id);
87: $this->model_catalog_product->deleteStoresByStoreId($store_id);
88:
89: $this->load->model('customer/gdpr');
90:
91: $this->model_customer_gdpr->deleteGdprsByStoreId($store_id);
92:
93: $this->load->model('design/theme');
94:
95: $this->model_design_theme->deleteThemesByStoreId($store_id);
96:
97: $this->load->model('design/translation');
98:
99: $this->model_design_translation->deleteTranslationsByStoreId($store_id);
100:
101: $this->load->model('design/seo_url');
102:
103: $this->model_design_seo_url->deleteSeoUrlsByStoreId($store_id);
104:
105: $this->load->model('setting/setting');
106:
107: $this->model_setting_setting->deleteSettingsByStoreId($store_id);
108:
109: $this->cache->delete('store');
110: }
111:
112: /**
113: * Get Store
114: *
115: * @param int $store_id
116: *
117: * @return array<string, mixed>
118: */
119: public function getStore(int $store_id): array {
120: $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "store` WHERE `store_id` = '" . (int)$store_id . "'");
121:
122: return $query->row;
123: }
124:
125: /**
126: * Get Stores
127: *
128: * @param array<string, mixed> $data
129: *
130: * @return array<int, array<string, mixed>>
131: */
132: public function getStores(array $data = []): array {
133: $sql = "SELECT * FROM `" . DB_PREFIX . "store` ORDER BY `url`";
134:
135: $key = md5($sql);
136:
137: $store_data = $this->cache->get('store.' . $key);
138:
139: if (!$store_data) {
140: $query = $this->db->query($sql);
141:
142: $store_data = $query->rows;
143:
144: $this->cache->set('store.' . $key, $store_data);
145: }
146:
147: return $store_data;
148: }
149:
150: /**
151: * Create Store Instance
152: *
153: * @param int $store_id
154: * @param string $language
155: * @param string $session_id
156: *
157: * @throws \Exception
158: *
159: * @return \Opencart\System\Engine\Registry
160: */
161: public function createStoreInstance(int $store_id = 0, string $language = '', string $session_id = ''): \Opencart\System\Engine\Registry {
162: // Autoloader
163: $this->autoloader->register('Opencart\Catalog', DIR_CATALOG);
164:
165: // Registry
166: $registry = new \Opencart\System\Engine\Registry();
167: $registry->set('autoloader', $this->autoloader);
168:
169: $config = new \Opencart\System\Engine\Config();
170: $registry->set('config', $config);
171:
172: // Load the default config
173: $config->addPath(DIR_CONFIG);
174: $config->load('default');
175: $config->load('catalog');
176: $config->set('application', 'Catalog');
177:
178: // Store
179: $config->set('config_store_id', $store_id);
180:
181: // Logging
182: $registry->set('log', $this->log);
183:
184: // Event
185: $event = new \Opencart\System\Engine\Event($registry);
186: $registry->set('event', $event);
187:
188: // Event Register
189: if ($config->has('action_event')) {
190: foreach ($config->get('action_event') as $key => $value) {
191: foreach ($value as $priority => $action) {
192: $event->register($key, new \Opencart\System\Engine\Action($action), $priority);
193: }
194: }
195: }
196:
197: // Factory
198: $registry->set('factory', new \Opencart\System\Engine\Factory($registry));
199:
200: // Loader
201: $loader = new \Opencart\System\Engine\Loader($registry);
202: $registry->set('load', $loader);
203:
204: // Create a dummy request class, so we can feed the data to the order editor
205: $request = new \stdClass();
206: $request->get = [];
207: $request->post = [];
208: $request->server = $this->request->server;
209: $request->cookie = [];
210:
211: // Request
212: $registry->set('request', $request);
213:
214: // Response
215: $response = new \Opencart\System\Library\Response();
216: $registry->set('response', $response);
217:
218: // Database
219: $registry->set('db', $this->db);
220:
221: // Cache
222: $registry->set('cache', $this->cache);
223:
224: // Session
225: $session = new \Opencart\System\Library\Session($config->get('session_engine'), $registry);
226: $registry->set('session', $session);
227:
228: // Start session
229: $session->start($session_id);
230:
231: // Template
232: $template = new \Opencart\System\Library\Template($config->get('template_engine'));
233: $template->addPath(DIR_CATALOG . 'view/template/');
234: $registry->set('template', $template);
235:
236: // Adding language var to the GET variable so there is a default language
237: $request->get['language'] = $language;
238:
239: // Language
240: $language = new \Opencart\System\Library\Language($config->get('language_code'));
241: $language->addPath(DIR_CATALOG . 'language/');
242: $language->load('default');
243: $registry->set('language', $language);
244:
245: // Url
246: $registry->set('url', new \Opencart\System\Library\Url($config->get('site_url')));
247:
248: // Document
249: $registry->set('document', new \Opencart\System\Library\Document());
250:
251: // Run pre actions to load key settings and classes.
252: $pre_actions = [
253: 'startup/setting',
254: 'startup/language',
255: 'startup/extension',
256: 'startup/customer',
257: 'startup/tax',
258: 'startup/currency',
259: 'startup/application',
260: 'startup/startup',
261: 'startup/event'
262: ];
263:
264: // Pre Actions
265: foreach ($pre_actions as $pre_action) {
266: $loader->controller($pre_action);
267: }
268:
269: return $registry;
270: }
271:
272: /**
273: * Get Total Stores
274: *
275: * @return int
276: */
277: public function getTotalStores(): int {
278: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "store`");
279:
280: return (int)$query->row['total'];
281: }
282:
283: /**
284: * Get Total Stores By Layout ID
285: *
286: * @param int $layout_id
287: *
288: * @return int
289: */
290: public function getTotalStoresByLayoutId(int $layout_id): int {
291: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_layout_id' AND `value` = '" . (int)$layout_id . "' AND `store_id` != '0'");
292:
293: return (int)$query->row['total'];
294: }
295:
296: /**
297: * Get Total Stores By Language
298: *
299: * @param string $language
300: *
301: * @return int
302: */
303: public function getTotalStoresByLanguage(string $language): int {
304: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_language' AND `value` = '" . $this->db->escape($language) . "' AND `store_id` != '0'");
305:
306: return (int)$query->row['total'];
307: }
308:
309: /**
310: * Get Total Stores By Currency
311: *
312: * @param string $currency
313: *
314: * @return int
315: */
316: public function getTotalStoresByCurrency(string $currency): int {
317: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_currency' AND `value` = '" . $this->db->escape($currency) . "' AND `store_id` != '0'");
318:
319: return (int)$query->row['total'];
320: }
321:
322: /**
323: * Get Total Stores By Country ID
324: *
325: * @param int $country_id
326: *
327: * @return int
328: */
329: public function getTotalStoresByCountryId(int $country_id): int {
330: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_country_id' AND `value` = '" . (int)$country_id . "' AND `store_id` != '0'");
331:
332: return (int)$query->row['total'];
333: }
334:
335: /**
336: * Get Total Stores By Zone ID
337: *
338: * @param int $zone_id
339: *
340: * @return int
341: */
342: public function getTotalStoresByZoneId(int $zone_id): int {
343: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_zone_id' AND `value` = '" . (int)$zone_id . "' AND `store_id` != '0'");
344:
345: return (int)$query->row['total'];
346: }
347:
348: /**
349: * Get Total Stores By Customer Group ID
350: *
351: * @param int $customer_group_id
352: *
353: * @return int
354: */
355: public function getTotalStoresByCustomerGroupId(int $customer_group_id): int {
356: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_customer_group_id' AND `value` = '" . (int)$customer_group_id . "' AND `store_id` != '0'");
357:
358: return (int)$query->row['total'];
359: }
360:
361: /**
362: * Get Total Stores By Information ID
363: *
364: * @param int $information_id
365: *
366: * @return int
367: */
368: public function getTotalStoresByInformationId(int $information_id): int {
369: $account_query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_account_id' AND `value` = '" . (int)$information_id . "' AND `store_id` != '0'");
370:
371: $checkout_query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_checkout_id' AND `value` = '" . (int)$information_id . "' AND `store_id` != '0'");
372:
373: return $account_query->row['total'] + $checkout_query->row['total'];
374: }
375:
376: /**
377: * Get Total Stores By Order Status ID
378: *
379: * @param int $order_status_id
380: *
381: * @return int
382: */
383: public function getTotalStoresByOrderStatusId(int $order_status_id): int {
384: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "setting` WHERE `key` = 'config_order_status_id' AND `value` = '" . (int)$order_status_id . "' AND `store_id` != '0'");
385:
386: return (int)$query->row['total'];
387: }
388: }
389: