1: | <?php
2: | namespace Opencart\Admin\Model\Catalog;
3: | /**
4: | * Class Download
5: | *
6: | * @package Opencart\Admin\Model\Catalog
7: | */
8: | class Download extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Download
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addDownload(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "download` SET `filename` = '" . $this->db->escape((string)$data['filename']) . "', `mask` = '" . $this->db->escape((string)$data['mask']) . "', `date_added` = NOW()");
18: |
19: | $download_id = $this->db->getLastId();
20: |
21: | foreach ($data['download_description'] as $language_id => $download_description) {
22: | $this->model_catalog_download->addDescription($download_id, $language_id, $download_description);
23: | }
24: |
25: | return $download_id;
26: | }
27: |
28: | /**
29: | * Edit Download
30: | *
31: | * @param int $download_id
32: | * @param array<string, mixed> $data
33: | *
34: | * @return void
35: | */
36: | public function editDownload(int $download_id, array $data): void {
37: | $this->db->query("UPDATE `" . DB_PREFIX . "download` SET `filename` = '" . $this->db->escape((string)$data['filename']) . "', `mask` = '" . $this->db->escape((string)$data['mask']) . "' WHERE `download_id` = '" . (int)$download_id . "'");
38: |
39: | $this->model_catalog_download->deleteDescriptions($download_id);
40: |
41: | foreach ($data['download_description'] as $language_id => $download_description) {
42: | $this->model_catalog_download->addDescription($download_id, $language_id, $download_description);
43: | }
44: | }
45: |
46: | /**
47: | * Delete Download
48: | *
49: | * @param int $download_id
50: | *
51: | * @return void
52: | */
53: | public function deleteDownload(int $download_id): void {
54: | $this->db->query("DELETE FROM `" . DB_PREFIX . "download` WHERE `download_id` = '" . (int)$download_id . "'");
55: |
56: | $this->model_catalog_download->deleteDescriptions($download_id);
57: | $this->model_catalog_download->deleteReports($download_id);
58: | }
59: |
60: | /**
61: | * Get Download
62: | *
63: | * @param int $download_id
64: | *
65: | * @return array<string, mixed>
66: | */
67: | public function getDownload(int $download_id): array {
68: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "download` `d` LEFT JOIN `" . DB_PREFIX . "download_description` `dd` ON (`d`.`download_id` = `dd`.`download_id`) WHERE `d`.`download_id` = '" . (int)$download_id . "' AND `dd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
69: |
70: | return $query->row;
71: | }
72: |
73: | /**
74: | * Get Downloads
75: | *
76: | * @param array<string, mixed> $data
77: | *
78: | * @return array<int, array<string, mixed>>
79: | */
80: | public function getDownloads(array $data = []): array {
81: | $sql = "SELECT * FROM `" . DB_PREFIX . "download` `d` LEFT JOIN `" . DB_PREFIX . "download_description` `dd` ON (`d`.`download_id` = `dd`.`download_id`) WHERE `dd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
82: |
83: | if (!empty($data['filter_name'])) {
84: | $sql .= " AND LCASE(`dd`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
85: | }
86: |
87: | $sort_data = [
88: | 'dd.name',
89: | 'd.date_added'
90: | ];
91: |
92: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
93: | $sql .= " ORDER BY " . $data['sort'];
94: | } else {
95: | $sql .= " ORDER BY `dd`.`name`";
96: | }
97: |
98: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
99: | $sql .= " DESC";
100: | } else {
101: | $sql .= " ASC";
102: | }
103: |
104: | if (isset($data['start']) || isset($data['limit'])) {
105: | if ($data['start'] < 0) {
106: | $data['start'] = 0;
107: | }
108: |
109: | if ($data['limit'] < 1) {
110: | $data['limit'] = 20;
111: | }
112: |
113: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
114: | }
115: |
116: | $query = $this->db->query($sql);
117: |
118: | return $query->rows;
119: | }
120: |
121: | /**
122: | * Add Description
123: | *
124: | * @param int $download_id primary key of the attribute record to be fetched
125: | * @param int $language_id
126: | * @param array<string, mixed> $data
127: | *
128: | * @return void
129: | */
130: | public function addDescription(int $download_id, int $language_id, array $data): void {
131: | $this->db->query("INSERT INTO `" . DB_PREFIX . "download_description` SET `download_id` = '" . (int)$download_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
132: | }
133: |
134: | /**
135: | * Delete Descriptions
136: | *
137: | * @param int $download_id primary key of the download record to be fetched
138: | *
139: | * @return void
140: | */
141: | public function deleteDescriptions(int $download_id): void {
142: | $this->db->query("DELETE FROM `" . DB_PREFIX . "download_description` WHERE `download_id` = '" . (int)$download_id . "'");
143: | }
144: |
145: | /**
146: | * Delete Descriptions By Language ID
147: | *
148: | * @param int $language_id
149: | *
150: | * @return void
151: | */
152: | public function deleteDescriptionsByLanguageId(int $language_id): void {
153: | $this->db->query("DELETE FROM `" . DB_PREFIX . "download_description` WHERE `language_id` = '" . (int)$language_id . "'");
154: | }
155: |
156: | /**
157: | * Get Descriptions
158: | *
159: | * @param int $download_id
160: | *
161: | * @return array<int, array<string, string>>
162: | */
163: | public function getDescriptions(int $download_id): array {
164: | $download_description_data = [];
165: |
166: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "download_description` WHERE `download_id` = '" . (int)$download_id . "'");
167: |
168: | foreach ($query->rows as $result) {
169: | $download_description_data[$result['language_id']] = ['name' => $result['name']];
170: | }
171: |
172: | return $download_description_data;
173: | }
174: |
175: | /**
176: | * Get Descriptions By Language ID
177: | *
178: | * @param int $language_id
179: | *
180: | * @return array<int, array<string, string>>
181: | */
182: | public function getDescriptionsByLanguageId(int $language_id): array {
183: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "download_description` WHERE `language_id` = '" . (int)$language_id . "'");
184: |
185: | return $query->rows;
186: | }
187: |
188: | /**
189: | * Get Total Downloads
190: | *
191: | * @return int
192: | */
193: | public function getTotalDownloads(): int {
194: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "download`");
195: |
196: | return (int)$query->row['total'];
197: | }
198: |
199: | /**
200: | * Get Reports
201: | *
202: | * @param int $download_id
203: | * @param int $start
204: | * @param int $limit
205: | *
206: | * @return array<int, array<string, mixed>>
207: | */
208: | public function getReports(int $download_id, int $start = 0, int $limit = 10): array {
209: | if ($start < 0) {
210: | $start = 0;
211: | }
212: |
213: | if ($limit < 1) {
214: | $limit = 10;
215: | }
216: |
217: | $query = $this->db->query("SELECT `ip`, `store_id`, `country`, `date_added` FROM `" . DB_PREFIX . "download_report` WHERE `download_id` = '" . (int)$download_id . "' ORDER BY `date_added` ASC LIMIT " . (int)$start . "," . (int)$limit);
218: |
219: | return $query->rows;
220: | }
221: |
222: | /**
223: | * Delete Reports
224: | *
225: | * @param int $download_id
226: | *
227: | * @return void
228: | */
229: | public function deleteReports(int $download_id): void {
230: | $this->db->query("DELETE FROM `" . DB_PREFIX . "download_report` WHERE `download_id` = '" . (int)$download_id . "'");
231: | }
232: |
233: | /**
234: | * Get Total Reports
235: | *
236: | * @param int $download_id
237: | *
238: | * @return int
239: | */
240: | public function getTotalReports(int $download_id): int {
241: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "download_report` WHERE `download_id` = '" . (int)$download_id . "'");
242: |
243: | return (int)$query->row['total'];
244: | }
245: | }
246: |

OpenCart API API documentation generated by ApiGen dev-master