1: | <?php
2: | namespace Opencart\Admin\Model\Catalog;
3: | /**
4: | * Class Option
5: | *
6: | * @package Opencart\Admin\Model\Catalog
7: | */
8: | class Option extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Option
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addOption(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "option` SET `type` = '" . $this->db->escape((string)$data['type']) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
18: |
19: | $option_id = $this->db->getLastId();
20: |
21: | foreach ($data['option_description'] as $language_id => $value) {
22: | $this->model_catalog_option->addDescription($option_id, $language_id, $value);
23: | }
24: |
25: | if (isset($data['option_value'])) {
26: | foreach ($data['option_value'] as $option_value) {
27: | $this->model_catalog_option->addValue($option_id, $option_value);
28: | }
29: | }
30: |
31: | return $option_id;
32: | }
33: |
34: | /**
35: | * Edit Option
36: | *
37: | * @param int $option_id
38: | * @param array<string, mixed> $data
39: | *
40: | * @return void
41: | */
42: | public function editOption(int $option_id, array $data): void {
43: | $this->db->query("UPDATE `" . DB_PREFIX . "option` SET `type` = '" . $this->db->escape((string)$data['type']) . "', `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `option_id` = '" . (int)$option_id . "'");
44: |
45: | $this->model_catalog_option->deleteDescriptions($option_id);
46: |
47: | foreach ($data['option_description'] as $language_id => $value) {
48: | $this->model_catalog_option->addDescription($option_id, $language_id, $value);
49: | }
50: |
51: | $this->model_catalog_option->deleteValues($option_id);
52: |
53: | if (isset($data['option_value'])) {
54: | foreach ($data['option_value'] as $option_value) {
55: | $this->model_catalog_option->addValue($option_id, $option_value);
56: | }
57: | }
58: | }
59: |
60: | /**
61: | * Delete Option
62: | *
63: | * @param int $option_id
64: | *
65: | * @return void
66: | */
67: | public function deleteOption(int $option_id): void {
68: | $this->db->query("DELETE FROM `" . DB_PREFIX . "option` WHERE `option_id` = '" . (int)$option_id . "'");
69: |
70: | $this->model_catalog_option->deleteDescriptions($option_id);
71: | $this->model_catalog_option->deleteValues($option_id);
72: | }
73: |
74: | /**
75: | * Get Option
76: | *
77: | * @param int $option_id
78: | *
79: | * @return array<string, mixed>
80: | */
81: | public function getOption(int $option_id): array {
82: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option` `o` LEFT JOIN `" . DB_PREFIX . "option_description` `od` ON (`o`.`option_id` = `od`.`option_id`) WHERE `o`.`option_id` = '" . (int)$option_id . "' AND `od`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
83: |
84: | return $query->row;
85: | }
86: |
87: | /**
88: | * Get Options
89: | *
90: | * @param array<string, mixed> $data
91: | *
92: | * @return array<int, array<string, mixed>>
93: | */
94: | public function getOptions(array $data = []): array {
95: | $sql = "SELECT * FROM `" . DB_PREFIX . "option` `o` LEFT JOIN `" . DB_PREFIX . "option_description` `od` ON (`o`.`option_id` = `od`.`option_id`) WHERE `od`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
96: |
97: | if (!empty($data['filter_name'])) {
98: | $sql .= " AND LCASE(`od`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
99: | }
100: |
101: | $sort_data = [
102: | 'od.name',
103: | 'o.type',
104: | 'o.sort_order'
105: | ];
106: |
107: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
108: | $sql .= " ORDER BY " . $data['sort'];
109: | } else {
110: | $sql .= " ORDER BY `od`.`name`";
111: | }
112: |
113: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
114: | $sql .= " DESC";
115: | } else {
116: | $sql .= " ASC";
117: | }
118: |
119: | if (isset($data['start']) || isset($data['limit'])) {
120: | if ($data['start'] < 0) {
121: | $data['start'] = 0;
122: | }
123: |
124: | if ($data['limit'] < 1) {
125: | $data['limit'] = 20;
126: | }
127: |
128: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
129: | }
130: |
131: | $query = $this->db->query($sql);
132: |
133: | return $query->rows;
134: | }
135: |
136: | /**
137: | * Get Total Options
138: | *
139: | * @return int
140: | */
141: | public function getTotalOptions(): int {
142: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "option`");
143: |
144: | return (int)$query->row['total'];
145: | }
146: |
147: | /**
148: | * Add Description
149: | *
150: | * @param int $option_id primary key
151: | * @param int $language_id
152: | * @param array<string, mixed> $data
153: | *
154: | * @return void
155: | */
156: | public function addDescription(int $option_id, int $language_id, array $data): void {
157: | $this->db->query("INSERT INTO `" . DB_PREFIX . "option_description` SET `option_id` = '" . (int)$option_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
158: | }
159: |
160: | /**
161: | * Delete Descriptions
162: | *
163: | * @param int $option_id
164: | *
165: | * @return void
166: | */
167: | public function deleteDescriptions(int $option_id): void {
168: | $this->db->query("DELETE FROM `" . DB_PREFIX . "option_description` WHERE `option_id` = '" . (int)$option_id . "'");
169: | }
170: |
171: | /**
172: | * Delete Descriptions By Language ID
173: | *
174: | * @param int $language_id
175: | *
176: | * @return void
177: | */
178: | public function deleteDescriptionsByLanguageId(int $language_id): void {
179: | $this->db->query("DELETE FROM `" . DB_PREFIX . "option_description` WHERE `language_id` = '" . (int)$language_id . "'");
180: | }
181: |
182: | /**
183: | * Get Descriptions
184: | *
185: | * @param int $option_id
186: | *
187: | * @return array<int, array<string, string>>
188: | */
189: | public function getDescriptions(int $option_id): array {
190: | $description_data = [];
191: |
192: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_description` WHERE `option_id` = '" . (int)$option_id . "'");
193: |
194: | foreach ($query->rows as $result) {
195: | $description_data[$result['language_id']] = ['name' => $result['name']];
196: | }
197: |
198: | return $description_data;
199: | }
200: |
201: | /**
202: | * Get Descriptions By Language ID
203: | *
204: | * @param int $language_id
205: | *
206: | * @return array<int, array<string, string>>
207: | */
208: | public function getDescriptionsByLanguageId(int $language_id): array {
209: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_description` WHERE `language_id` = '" . (int)$language_id . "'");
210: |
211: | return $query->rows;
212: | }
213: |
214: | /**
215: | * Add Value
216: | *
217: | * @param int $option_id
218: | * @param array<string, mixed> $data
219: | */
220: | public function addValue(int $option_id, array $data): int {
221: | if ($data['option_value_id']) {
222: | $this->db->query("INSERT INTO `" . DB_PREFIX . "option_value` SET `option_value_id` = '" . (int)$data['option_value_id'] . "', `option_id` = '" . (int)$option_id . "', `image` = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
223: | } else {
224: | $this->db->query("INSERT INTO `" . DB_PREFIX . "option_value` SET `option_id` = '" . (int)$option_id . "', `image` = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
225: | }
226: |
227: | $option_value_id = $this->db->getLastId();
228: |
229: | if ($data['option_value_id']) {
230: | if (isset($data['option_value_description'])) {
231: | foreach ($data['option_value_description'] as $language_id => $option_value_description) {
232: | $this->model_catalog_option->addValueDescription($option_value_id, $option_id, $language_id, $option_value_description);
233: | }
234: | }
235: | }
236: |
237: | return $option_value_id;
238: | }
239: |
240: | /**
241: | * Delete Values
242: | *
243: | * @param int $option_id
244: | *
245: | * @return void
246: | */
247: | public function deleteValues(int $option_id): void {
248: | $this->db->query("DELETE FROM `" . DB_PREFIX . "option_value` WHERE `option_id` = '" . (int)$option_id . "'");
249: |
250: | $this->model_catalog_option->deleteValueDescriptionsByOptionId($option_id);
251: | }
252: |
253: | /**
254: | * Get Value
255: | *
256: | * @param int $option_value_id
257: | *
258: | * @return array<string, mixed>
259: | */
260: | public function getValue(int $option_value_id): array {
261: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value` `ov` LEFT JOIN `" . DB_PREFIX . "option_value_description` `ovd` ON (`ov`.`option_value_id` = `ovd`.`option_value_id`) WHERE `ov`.`option_value_id` = '" . (int)$option_value_id . "' AND `ovd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
262: |
263: | return $query->row;
264: | }
265: |
266: | /**
267: | * Get Values
268: | *
269: | * @param int $option_id
270: | *
271: | * @return array<int, array<string, mixed>>
272: | */
273: | public function getValues(int $option_id): array {
274: | $option_value_data = [];
275: |
276: | $option_value_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value` `ov` LEFT JOIN `" . DB_PREFIX . "option_value_description` `ovd` ON (`ov`.`option_value_id` = `ovd`.`option_value_id`) WHERE `ov`.`option_id` = '" . (int)$option_id . "' AND `ovd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' ORDER BY `ov`.`sort_order`, `ovd`.`name`");
277: |
278: | foreach ($option_value_query->rows as $option_value) {
279: | $option_value_data[] = [
280: | 'option_value_id' => $option_value['option_value_id'],
281: | 'name' => $option_value['name'],
282: | 'image' => $option_value['image'],
283: | 'sort_order' => $option_value['sort_order']
284: | ];
285: | }
286: |
287: | return $option_value_data;
288: | }
289: |
290: | /**
291: | * Add Value Description
292: | *
293: | * @param int $option_value_id
294: | * @param int $option_id
295: | * @param int $language_id
296: | * @param array<string, mixed> $data
297: | *
298: | * @return void
299: | */
300: | public function addValueDescription(int $option_value_id, int $option_id, int $language_id, array $data): void {
301: | $this->db->query("INSERT INTO `" . DB_PREFIX . "option_value_description` SET `option_value_id` = '" . (int)$option_value_id . "', `language_id` = '" . (int)$language_id . "', `option_id` = '" . (int)$option_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
302: | }
303: |
304: | /**
305: | * Delete Value Descriptions By Option ID
306: | *
307: | * @param int $option_id
308: | *
309: | * @return void
310: | */
311: | public function deleteValueDescriptionsByOptionId(int $option_id): void {
312: | $this->db->query("DELETE FROM `" . DB_PREFIX . "option_value_description` WHERE `option_id` = '" . (int)$option_id . "'");
313: | }
314: |
315: | /**
316: | * Delete Value Descriptions By Language ID
317: | *
318: | * @param int $language_id
319: | *
320: | * @return void
321: | */
322: | public function deleteValueDescriptionsByLanguageId(int $language_id): void {
323: | $this->db->query("DELETE FROM `" . DB_PREFIX . "option_value_description` WHERE `language_id` = '" . (int)$language_id . "'");
324: | }
325: |
326: | /**
327: | * Get Value Descriptions
328: | *
329: | * @param int $option_id
330: | *
331: | * @return array<int, array<string, mixed>>
332: | */
333: | public function getValueDescriptions(int $option_id): array {
334: | $option_value_data = [];
335: |
336: | $option_value_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value` WHERE `option_id` = '" . (int)$option_id . "' ORDER BY `sort_order`");
337: |
338: | foreach ($option_value_query->rows as $option_value) {
339: | $option_value_description_data = [];
340: |
341: | $option_value_description_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value_description` WHERE `option_value_id` = '" . (int)$option_value['option_value_id'] . "'");
342: |
343: | foreach ($option_value_description_query->rows as $option_value_description) {
344: | $option_value_description_data[$option_value_description['language_id']] = ['name' => $option_value_description['name']];
345: | }
346: |
347: | $option_value_data[] = [
348: | 'option_value_id' => $option_value['option_value_id'],
349: | 'option_value_description' => $option_value_description_data,
350: | 'image' => $option_value['image'],
351: | 'sort_order' => $option_value['sort_order']
352: | ];
353: | }
354: |
355: | return $option_value_data;
356: | }
357: |
358: | /**
359: | * Get Value Descriptions By Language ID
360: | *
361: | * @param int $language_id
362: | *
363: | * @return array<int, array<string, string>>
364: | */
365: | public function getValueDescriptionsByLanguageId(int $language_id): array {
366: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option_value_description` WHERE `language_id` = '" . (int)$language_id . "'");
367: |
368: | return $query->rows;
369: | }
370: | }
371: |

OpenCart API API documentation generated by ApiGen dev-master