1: | <?php
2: | namespace Opencart\Admin\Model\Localisation;
3: | /**
4: | * Class Language
5: | *
6: | * @package Opencart\Admin\Model\Localisation
7: | */
8: | class Language extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Language
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addLanguage(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "language` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `locale` = '" . $this->db->escape((string)$data['locale']) . "', `extension` = '" . $this->db->escape((string)$data['extension']) . "', `sort_order` = '" . (int)$data['sort_order'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "'");
18: |
19: | $this->cache->delete('language');
20: |
21: | $language_id = $this->db->getLastId();
22: |
23: | // Attribute
24: | $this->load->model('catalog/attribute');
25: |
26: | $results = $this->model_catalog_attribute->getDescriptionsByLanguageId($this->config->get('config_language_id'));
27: |
28: | foreach ($results as $attribute) {
29: | $this->model_catalog_attribute->addDescription($attribute['attribute_id'], $language_id, $attribute);
30: | }
31: |
32: | // Attribute Group
33: | $this->load->model('catalog/attribute_group');
34: |
35: | $results = $this->model_catalog_attribute_group->getDescriptionsByLanguageId($this->config->get('config_language_id'));
36: |
37: | foreach ($results as $attribute_group) {
38: | $this->model_catalog_attribute_group->addDescription($attribute_group['attribute_group_id'], $language_id, $attribute_group);
39: | }
40: |
41: | // Banner
42: | $this->load->model('design/banner');
43: |
44: | $results = $this->model_design_banner->getImagesByLanguageId($this->config->get('config_language_id'));
45: |
46: | foreach ($results as $banner_image) {
47: | $this->model_design_banner->addImage($banner_image['banner_id'], $language_id, $banner_image);
48: | }
49: |
50: | // Category
51: | $this->load->model('catalog/category');
52: |
53: | $results = $this->model_catalog_category->getDescriptionsByLanguageId($this->config->get('config_language_id'));
54: |
55: | foreach ($results as $category) {
56: | $this->model_catalog_category->addDescription($category['category_id'], $language_id, $category);
57: | }
58: |
59: | // Customer Group
60: | $this->load->model('customer/customer_group');
61: |
62: | $results = $this->model_customer_customer_group->getDescriptionsByLanguageId($this->config->get('config_language_id'));
63: |
64: | foreach ($results as $customer_group) {
65: | $this->model_customer_customer_group->addDescription($customer_group['customer_group_id'], $language_id, $customer_group);
66: | }
67: |
68: | // Custom Field
69: | $this->load->model('customer/custom_field');
70: |
71: | $results = $this->model_customer_custom_field->getDescriptionsByLanguageId($this->config->get('config_language_id'));
72: |
73: | foreach ($results as $custom_field) {
74: | $this->model_customer_custom_field->addDescription($custom_field['custom_field_id'], $language_id, $custom_field);
75: | }
76: |
77: | // Custom Field Value
78: | $results = $this->model_customer_custom_field->getValueDescriptionsByLanguageId($this->config->get('config_language_id'));
79: |
80: | foreach ($results as $custom_field_value) {
81: | $this->model_customer_custom_field->addValueDescription($custom_field_value['custom_field_value_id'], $custom_field_value['custom_field_id'], $language_id, $custom_field_value);
82: | }
83: |
84: | // Download
85: | $this->load->model('catalog/download');
86: |
87: | $results = $this->model_catalog_download->getDescriptionsByLanguageId($this->config->get('config_language_id'));
88: |
89: | foreach ($results as $download) {
90: | $this->model_catalog_download->addDescription($download['download_id'], $language_id, $download);
91: | }
92: |
93: | // Filter
94: | $this->load->model('catalog/filter');
95: |
96: | $results = $this->model_catalog_filter->getDescriptionsByLanguageId($this->config->get('config_language_id'));
97: |
98: | foreach ($results as $filter) {
99: | $this->model_catalog_filter->addDescription($filter['filter_id'], $language_id, $filter);
100: | }
101: |
102: | // Filter Group
103: | $this->load->model('catalog/filter_group');
104: |
105: | $results = $this->model_catalog_filter_group->getDescriptionsByLanguageId($this->config->get('config_language_id'));
106: |
107: | foreach ($results as $filter_group) {
108: | $this->model_catalog_filter_group->addDescription($filter_group['filter_group_id'], $language_id, $filter_group);
109: | }
110: |
111: | // Information
112: | $this->load->model('catalog/information');
113: |
114: | $results = $this->model_catalog_information->getDescriptionsByLanguageId($this->config->get('config_language_id'));
115: |
116: | foreach ($results as $information) {
117: | $this->model_catalog_information->addDescription($information['information_id'], $language_id, $information);
118: | }
119: |
120: | // Length
121: | $this->load->model('localisation/length_class');
122: |
123: | $results = $this->model_localisation_length_class->getDescriptionsByLanguageId($this->config->get('config_language_id'));
124: |
125: | foreach ($results as $length) {
126: | $this->model_localisation_length_class->addDescription($length['length_class_id'], $language_id, $length);
127: | }
128: |
129: | // Option
130: | $this->load->model('catalog/option');
131: |
132: | $results = $this->model_catalog_option->getDescriptionsByLanguageId($this->config->get('config_language_id'));
133: |
134: | foreach ($results as $option) {
135: | $this->model_catalog_option->addDescription($option['option_id'], $language_id, $option);
136: | }
137: |
138: | // Option Value
139: | $results = $this->model_catalog_option->getValueDescriptionsByLanguageId($this->config->get('config_language_id'));
140: |
141: | foreach ($results as $option_value) {
142: | $this->model_catalog_option->addValueDescription($option_value['option_value_id'], $option_value['option_id'], $language_id, $option_value);
143: | }
144: |
145: | // Order Status
146: | $this->load->model('localisation/order_status');
147: |
148: | $results = $this->model_localisation_order_status->getDescriptionsByLanguageId($this->config->get('config_language_id'));
149: |
150: | foreach ($results as $order_status) {
151: | $this->model_localisation_order_status->addDescription($order_status['order_status_id'], $language_id, $order_status);
152: | }
153: |
154: | // Product
155: | $this->load->model('catalog/product');
156: |
157: | $results = $this->model_catalog_product->getDescriptionsByLanguageId($this->config->get('config_language_id'));
158: |
159: | foreach ($results as $product) {
160: | $this->model_catalog_product->addDescription($product['product_id'], $language_id, $product);
161: | }
162: |
163: | // Product Attribute
164: | $results = $this->model_catalog_product->getAttributesByLanguageId($this->config->get('config_language_id'));
165: |
166: | foreach ($results as $product_attribute) {
167: | $this->model_catalog_product->addAttribute($product_attribute['product_id'], $product_attribute['attribute_id'], $language_id, $product_attribute);
168: | }
169: |
170: | // Return Action
171: | $this->load->model('localisation/return_action');
172: |
173: | $results = $this->model_localisation_return_action->getDescriptionsByLanguageId($this->config->get('config_language_id'));
174: |
175: | foreach ($results as $return_action) {
176: | $this->model_localisation_return_action->addDescription($return_action['return_action_id'], $language_id, $return_action);
177: | }
178: |
179: | // Return Reason
180: | $this->load->model('localisation/return_reason');
181: |
182: | $results = $this->model_localisation_return_reason->getDescriptionsByLanguageId($this->config->get('config_language_id'));
183: |
184: | foreach ($results as $return_reason) {
185: | $this->model_localisation_return_reason->addDescription($return_reason['return_reason_id'], $language_id, $return_reason);
186: | }
187: |
188: | // Return Status
189: | $this->load->model('localisation/return_status');
190: |
191: | $results = $this->model_localisation_return_status->getDescriptionsByLanguageId($this->config->get('config_language_id'));
192: |
193: | foreach ($results as $return_status) {
194: | $this->model_localisation_return_status->addDescription($return_status['return_status_id'], $language_id, $return_status);
195: | }
196: |
197: | // Stock Status
198: | $this->load->model('localisation/stock_status');
199: |
200: | $results = $this->model_localisation_stock_status->getDescriptionsByLanguageId($this->config->get('config_language_id'));
201: |
202: | foreach ($results as $stock_status) {
203: | $this->model_localisation_stock_status->addDescription($stock_status['stock_status_id'], $language_id, $stock_status);
204: | }
205: |
206: | // Voucher Theme
207: | $this->load->model('sale/voucher_theme');
208: |
209: | $results = $this->model_sale_voucher_theme->getDescriptionsByLanguageId($this->config->get('config_language_id'));
210: |
211: | foreach ($results as $voucher_theme) {
212: | $this->model_sale_voucher_theme->addDescription($voucher_theme['voucher_theme_id'], $language_id, $voucher_theme);
213: | }
214: |
215: | // Weight Class
216: | $this->load->model('localisation/weight_class');
217: |
218: | $results = $this->model_localisation_weight_class->getDescriptionsByLanguageId($this->config->get('config_language_id'));
219: |
220: | foreach ($results as $weight_class) {
221: | $this->model_localisation_weight_class->addDescription($weight_class['weight_class_id'], $language_id, $weight_class);
222: | }
223: |
224: | // Subscription Plan
225: | $this->load->model('catalog/subscription_plan');
226: |
227: | $results = $this->model_catalog_subscription_plan->getDescriptionsByLanguageId($this->config->get('config_language_id'));
228: |
229: | foreach ($results as $subscription_plan) {
230: | $this->model_catalog_subscription_plan->addDescription($subscription_plan['subscription_plan_id'], $language_id, $subscription_plan);
231: | }
232: |
233: | // Subscription Status
234: | $this->load->model('localisation/subscription_status');
235: |
236: | $results = $this->model_localisation_subscription_status->getDescriptionsByLanguageId($this->config->get('config_language_id'));
237: |
238: | foreach ($results as $subscription) {
239: | $this->model_localisation_subscription_status->addDescription($subscription['subscription_status_id'], $language_id, $subscription);
240: | }
241: |
242: | // SEO URL
243: | $this->load->model('design/seo_url');
244: |
245: | $results = $this->model_design_seo_url->getSeoUrlsByLanguageId($this->config->get('config_language_id'));
246: |
247: | foreach ($results as $seo_url) {
248: | $this->model_design_seo_url->addSeoUrl($seo_url['key'], $seo_url['value'], $seo_url['keyword'], $seo_url['store_id'], $language_id, $seo_url['sort_order']);
249: | }
250: |
251: | return $language_id;
252: | }
253: |
254: | /**
255: | * Edit Language
256: | *
257: | * @param int $language_id
258: | * @param array<string, mixed> $data
259: | *
260: | * @return void
261: | */
262: | public function editLanguage(int $language_id, array $data): void {
263: | $this->db->query("UPDATE `" . DB_PREFIX . "language` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `locale` = '" . $this->db->escape((string)$data['locale']) . "', `extension` = '" . $this->db->escape((string)$data['extension']) . "', `sort_order` = '" . (int)$data['sort_order'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "' WHERE `language_id` = '" . (int)$language_id . "'");
264: |
265: | $this->cache->delete('language');
266: | }
267: |
268: | /**
269: | * Delete Language
270: | *
271: | * @param int $language_id
272: | *
273: | * @return void
274: | */
275: | public function deleteLanguage(int $language_id): void {
276: | $this->db->query("DELETE FROM `" . DB_PREFIX . "language` WHERE `language_id` = '" . (int)$language_id . "'");
277: |
278: | $this->cache->delete('language');
279: |
280: | // Attribute
281: | $this->load->model('catalog/attribute');
282: |
283: | $this->model_catalog_attribute->deleteDescriptionsByLanguageId($language_id);
284: |
285: | // Attribute Group
286: | $this->load->model('catalog/attribute_group');
287: |
288: | $this->model_catalog_attribute_group->deleteDescriptionsByLanguageId($language_id);
289: |
290: | // Banner
291: | $this->load->model('design/banner');
292: |
293: | $this->model_design_banner->deleteImagesByLanguageId($language_id);
294: |
295: | // Category
296: | $this->load->model('catalog/category');
297: |
298: | $this->model_catalog_category->deleteDescriptionsByLanguageId($language_id);
299: |
300: | // Customer Group
301: | $this->load->model('customer/customer_group');
302: |
303: | $this->model_customer_customer_group->deleteDescriptionsByLanguageId($language_id);
304: |
305: | // Custom Field
306: | $this->load->model('customer/custom_field');
307: |
308: | $this->model_customer_custom_field->deleteDescriptionsByLanguageId($language_id);
309: | $this->model_customer_custom_field->deleteValueDescriptionsByLanguageId($language_id);
310: |
311: | // Download
312: | $this->load->model('catalog/download');
313: |
314: | $this->model_catalog_download->deleteDescriptionsByLanguageId($language_id);
315: |
316: | // Filter
317: | $this->load->model('catalog/filter');
318: |
319: | $this->model_catalog_filter->deleteDescriptionsByLanguageId($language_id);
320: |
321: | // Filter Group
322: | $this->load->model('catalog/filter_group');
323: |
324: | $this->model_catalog_filter_group->deleteDescriptionsByLanguageId($language_id);
325: |
326: | // Information
327: | $this->load->model('catalog/information');
328: |
329: | $this->model_catalog_information->deleteDescriptionsByLanguageId($language_id);
330: |
331: | // Length
332: | $this->load->model('localisation/length_class');
333: |
334: | $this->model_localisation_length_class->deleteDescriptionsByLanguageId($language_id);
335: |
336: | // Option
337: | $this->load->model('catalog/option');
338: |
339: | $this->model_catalog_option->deleteDescriptionsByLanguageId($language_id);
340: | $this->model_catalog_option->deleteValueDescriptionsByLanguageId($language_id);
341: |
342: | // Order Status
343: | $this->load->model('localisation/order_status');
344: |
345: | $this->model_localisation_order_status->deleteOrderStatusesByLanguageId($language_id);
346: |
347: | // Product
348: | $this->load->model('catalog/product');
349: |
350: | $this->model_catalog_product->deleteDescriptionsByLanguageId($language_id);
351: | $this->model_catalog_product->deleteAttributesByLanguageId($language_id);
352: |
353: | // Return Action
354: | $this->load->model('localisation/return_action');
355: |
356: | $this->model_localisation_return_action->deleteReturnActionsByLanguageId($language_id);
357: |
358: | // Return Reason
359: | $this->load->model('localisation/return_reason');
360: |
361: | $this->model_localisation_return_reason->deleteReturnReasonsByLanguageId($language_id);
362: |
363: | // Return Status
364: | $this->load->model('localisation/return_status');
365: |
366: | $this->model_localisation_return_status->deleteReturnStatusesByLanguageId($language_id);
367: |
368: | // Stock Status
369: | $this->load->model('localisation/stock_status');
370: |
371: | $this->model_localisation_stock_status->deleteStockStatusesByLanguageId($language_id);
372: |
373: | // Voucher Theme
374: | $this->load->model('sale/voucher_theme');
375: |
376: | $this->model_sale_voucher_theme->deleteDescriptionsByLanguageId($language_id);
377: |
378: | // Weight Class
379: | $this->load->model('localisation/weight_class');
380: |
381: | $this->model_localisation_weight_class->deleteDescriptionsByLanguageId($language_id);
382: |
383: | // Subscription Status
384: | $this->load->model('localisation/subscription_status');
385: |
386: | $this->model_localisation_subscription_status->deleteStockStatusesByLanguageId($language_id);
387: |
388: | // SEO URL
389: | $this->load->model('design/seo_url');
390: |
391: | $this->model_design_seo_url->deleteSeoUrlsByLanguageId($language_id);
392: | }
393: |
394: | /**
395: | * Get Language
396: | *
397: | * @param int $language_id
398: | *
399: | * @return array<string, mixed>
400: | */
401: | public function getLanguage(int $language_id): array {
402: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "language` WHERE `language_id` = '" . (int)$language_id . "'");
403: |
404: | $language = $query->row;
405: |
406: | if ($language) {
407: | $language['image'] = HTTP_CATALOG;
408: |
409: | if (!$language['extension']) {
410: | $language['image'] .= 'catalog/';
411: | } else {
412: | $language['image'] .= 'extension/' . $language['extension'] . '/catalog/';
413: | }
414: |
415: | $language['image'] .= 'language/' . $language['code'] . '/' . $language['code'] . '.png';
416: | }
417: |
418: | return $language;
419: | }
420: |
421: | /**
422: | * Get Language By Code
423: | *
424: | * @param string $code
425: | *
426: | * @return array<string, mixed>
427: | */
428: | public function getLanguageByCode(string $code): array {
429: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "language` WHERE `code` = '" . $this->db->escape($code) . "'");
430: |
431: | $language = $query->row;
432: |
433: | if ($language) {
434: | $language['image'] = HTTP_CATALOG;
435: |
436: | if (!$language['extension']) {
437: | $language['image'] .= 'catalog/';
438: | } else {
439: | $language['image'] .= 'extension/' . $language['extension'] . '/catalog/';
440: | }
441: |
442: | $language['image'] .= 'language/' . $language['code'] . '/' . $language['code'] . '.png';
443: | }
444: |
445: | return $language;
446: | }
447: |
448: | /**
449: | * Get Languages
450: | *
451: | * @param array<string, mixed> $data
452: | *
453: | * @return array<string, array<string, mixed>>
454: | */
455: | public function getLanguages(array $data = []): array {
456: | $sql = "SELECT * FROM `" . DB_PREFIX . "language`";
457: |
458: | $sort_data = [
459: | 'name',
460: | 'code',
461: | 'sort_order'
462: | ];
463: |
464: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
465: | $sql .= " ORDER BY " . $data['sort'];
466: | } else {
467: | $sql .= " ORDER BY `sort_order`, `name`";
468: | }
469: |
470: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
471: | $sql .= " DESC";
472: | } else {
473: | $sql .= " ASC";
474: | }
475: |
476: | if (isset($data['start']) || isset($data['limit'])) {
477: | if ($data['start'] < 0) {
478: | $data['start'] = 0;
479: | }
480: |
481: | if ($data['limit'] < 1) {
482: | $data['limit'] = 20;
483: | }
484: |
485: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
486: | }
487: |
488: | $results = $this->cache->get('language.' . md5($sql));
489: |
490: | if (!$results) {
491: | $query = $this->db->query($sql);
492: |
493: | $results = $query->rows;
494: |
495: | $this->cache->set('language.' . md5($sql), $results);
496: | }
497: |
498: | $language_data = [];
499: |
500: | foreach ($results as $result) {
501: | $image = HTTP_CATALOG;
502: |
503: | if (!$result['extension']) {
504: | $image .= 'catalog/';
505: | } else {
506: | $image .= 'extension/' . $result['extension'] . '/catalog/';
507: | }
508: |
509: | $language_data[$result['code']] = $result + ['image' => $image . 'language/' . $result['code'] . '/' . $result['code'] . '.png'];
510: | }
511: |
512: | return $language_data;
513: | }
514: |
515: | /**
516: | * Get Languages By Extension
517: | *
518: | * @param string $extension
519: | *
520: | * @return array<int, array<string, mixed>>
521: | */
522: | public function getLanguagesByExtension(string $extension): array {
523: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "language` WHERE `extension` = '" . $this->db->escape($extension) . "'");
524: |
525: | return $query->rows;
526: | }
527: |
528: | /**
529: | * Get Total Languages
530: | *
531: | * @return int
532: | */
533: | public function getTotalLanguages(): int {
534: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "language`");
535: |
536: | return (int)$query->row['total'];
537: | }
538: | }
539: |

OpenCart API API documentation generated by ApiGen dev-master