1: | <?php
2: | namespace Opencart\Admin\Model\Setting;
3: | /**
4: | * Class Extension
5: | *
6: | * @package Opencart\Admin\Model\Setting
7: | */
8: | class Extension extends \Opencart\System\Engine\Model {
9: | /**
10: | * Get Extensions
11: | *
12: | * @return array<int, array<string, mixed>>
13: | */
14: | public function getExtensions(): array {
15: | $query = $this->db->query("SELECT DISTINCT `extension` FROM `" . DB_PREFIX . "extension`");
16: |
17: | return $query->rows;
18: | }
19: |
20: | /**
21: | * Get Extensions By Type
22: | *
23: | * @param string $type
24: | *
25: | * @return array<int, array<string, mixed>>
26: | */
27: | public function getExtensionsByType(string $type): array {
28: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension` WHERE `type` = '" . $this->db->escape($type) . "' ORDER BY `code` ASC");
29: |
30: | return $query->rows;
31: | }
32: |
33: | /**
34: | * Get Extension By Code
35: | *
36: | * @param string $type
37: | * @param string $code
38: | *
39: | * @return array<string, mixed>
40: | */
41: | public function getExtensionByCode(string $type, string $code): array {
42: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension` WHERE `type` = '" . $this->db->escape($type) . "' AND `code` = '" . $this->db->escape($code) . "'");
43: |
44: | return $query->row;
45: | }
46: |
47: | /**
48: | * Get Total Extensions By Extension
49: | *
50: | * @param string $extension
51: | *
52: | * @return int
53: | */
54: | public function getTotalExtensionsByExtension(string $extension): int {
55: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "extension` WHERE `extension` = '" . $this->db->escape($extension) . "'");
56: |
57: | return (int)$query->row['total'];
58: | }
59: |
60: | /**
61: | * Install
62: | *
63: | * @param string $type
64: | * @param string $extension
65: | * @param string $code
66: | *
67: | * @return void
68: | */
69: | public function install(string $type, string $extension, string $code): void {
70: | $extensions = $this->getExtensionsByType($type);
71: |
72: | $codes = array_column($extensions, 'code');
73: |
74: | if (!in_array($code, $codes)) {
75: | $this->db->query("INSERT INTO `" . DB_PREFIX . "extension` SET `extension` = '" . $this->db->escape($extension) . "', `type` = '" . $this->db->escape($type) . "', `code` = '" . $this->db->escape($code) . "'");
76: | }
77: | }
78: |
79: | /**
80: | * Uninstall
81: | *
82: | * @param string $type
83: | * @param string $code
84: | *
85: | * @return void
86: | */
87: | public function uninstall(string $type, string $code): void {
88: | $this->db->query("DELETE FROM `" . DB_PREFIX . "extension` WHERE `type` = '" . $this->db->escape($type) . "' AND `code` = '" . $this->db->escape($code) . "'");
89: |
90: | $this->load->model('setting/setting');
91: |
92: | $this->model_setting_setting->deleteSettingByCode($type . '_' . $code);
93: | }
94: |
95: | /**
96: | * Add Install
97: | *
98: | * @param array<string, mixed> $data
99: | *
100: | * @return int
101: | */
102: | public function addInstall(array $data): int {
103: | $this->db->query("INSERT INTO `" . DB_PREFIX . "extension_install` SET `extension_id` = '" . (int)$data['extension_id'] . "', `extension_download_id` = '" . (int)$data['extension_download_id'] . "', `name` = '" . $this->db->escape($data['name']) . "', `description` = '" . $this->db->escape($data['description']) . "', `code` = '" . $this->db->escape($data['code']) . "', `version` = '" . $this->db->escape($data['version']) . "', `author` = '" . $this->db->escape($data['author']) . "', `link` = '" . $this->db->escape($data['link']) . "', `status` = '0', `date_added` = NOW()");
104: |
105: | return $this->db->getLastId();
106: | }
107: |
108: | /**
109: | * Delete Install
110: | *
111: | * @param int $extension_install_id
112: | *
113: | * @return void
114: | */
115: | public function deleteInstall(int $extension_install_id): void {
116: | $this->db->query("DELETE FROM `" . DB_PREFIX . "extension_install` WHERE `extension_install_id` = '" . (int)$extension_install_id . "'");
117: | }
118: |
119: | /**
120: | * Edit Status
121: | *
122: | * @param int $extension_install_id
123: | * @param bool $status
124: | *
125: | * @return void
126: | */
127: | public function editStatus(int $extension_install_id, bool $status): void {
128: | $this->db->query("UPDATE `" . DB_PREFIX . "extension_install` SET `status` = '" . (bool)$status . "' WHERE `extension_install_id` = '" . (int)$extension_install_id . "'");
129: | }
130: |
131: | /**
132: | * Get Install
133: | *
134: | * @param int $extension_install_id
135: | *
136: | * @return array<string, mixed>
137: | */
138: | public function getInstall(int $extension_install_id): array {
139: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension_install` WHERE `extension_install_id` = '" . (int)$extension_install_id . "'");
140: |
141: | return $query->row;
142: | }
143: |
144: | /**
145: | * Get Install By Extension Download ID
146: | *
147: | * @param int $extension_download_id
148: | *
149: | * @return array<string, mixed>
150: | */
151: | public function getInstallByExtensionDownloadId(int $extension_download_id): array {
152: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension_install` WHERE `extension_download_id` = '" . (int)$extension_download_id . "'");
153: |
154: | return $query->row;
155: | }
156: |
157: | /**
158: | * Get Install By Code
159: | *
160: | * @param string $code
161: | *
162: | * @return array<string, mixed>
163: | */
164: | public function getInstallByCode(string $code): array {
165: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension_install` WHERE `code` = '" . $this->db->escape($code) . "'");
166: |
167: | return $query->row;
168: | }
169: |
170: | /**
171: | * Get Installs
172: | *
173: | * @param array<string, mixed> $data
174: | *
175: | * @return array<int, array<string, mixed>>
176: | */
177: | public function getInstalls(array $data = []): array {
178: | $sql = "SELECT * FROM `" . DB_PREFIX . "extension_install`";
179: |
180: | if (!empty($data['filter_extension_download_id'])) {
181: | $sql .= " WHERE `extension_download_id` = '" . (int)$data['filter_extension_download_id'] . "'";
182: | }
183: |
184: | $sort_data = [
185: | 'name',
186: | 'version',
187: | 'date_added'
188: | ];
189: |
190: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
191: | $sql .= " ORDER BY " . $data['sort'];
192: | } else {
193: | $sql .= " ORDER BY `date_added`";
194: | }
195: |
196: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
197: | $sql .= " DESC";
198: | } else {
199: | $sql .= " ASC";
200: | }
201: |
202: | if (isset($data['start']) || isset($data['limit'])) {
203: | if ($data['start'] < 0) {
204: | $data['start'] = 0;
205: | }
206: |
207: | if ($data['limit'] < 1) {
208: | $data['limit'] = 20;
209: | }
210: |
211: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
212: | }
213: |
214: | $query = $this->db->query($sql);
215: |
216: | return $query->rows;
217: | }
218: |
219: | /**
220: | * Get Total Installs
221: | *
222: | * @param array<string, mixed> $data
223: | *
224: | * @return int
225: | */
226: | public function getTotalInstalls(array $data = []): int {
227: | $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "extension_install`";
228: |
229: | if (!empty($data['filter_extension_download_id'])) {
230: | $sql .= " WHERE `extension_download_id` = '" . (int)$data['filter_extension_download_id'] . "'";
231: | }
232: |
233: | $query = $this->db->query($sql);
234: |
235: | return (int)$query->row['total'];
236: | }
237: |
238: | /**
239: | * Add Path
240: | *
241: | * @param int $extension_install_id
242: | * @param string $path
243: | *
244: | * @return void
245: | */
246: | public function addPath(int $extension_install_id, string $path): void {
247: | $this->db->query("INSERT INTO `" . DB_PREFIX . "extension_path` SET `extension_install_id` = '" . (int)$extension_install_id . "', `path` = '" . $this->db->escape($path) . "'");
248: | }
249: |
250: | /**
251: | * Delete Path
252: | *
253: | * @param int $extension_path_id
254: | *
255: | * @return void
256: | */
257: | public function deletePath(int $extension_path_id): void {
258: | $this->db->query("DELETE FROM `" . DB_PREFIX . "extension_path` WHERE `extension_path_id` = '" . (int)$extension_path_id . "'");
259: | }
260: |
261: | /**
262: | * Get Paths By Extension Install ID
263: | *
264: | * @param int $extension_install_id
265: | *
266: | * @return array<int, array<string, mixed>>
267: | */
268: | public function getPathsByExtensionInstallId(int $extension_install_id): array {
269: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension_path` WHERE `extension_install_id` = '" . (int)$extension_install_id . "' ORDER BY `extension_path_id` ASC");
270: |
271: | return $query->rows;
272: | }
273: |
274: | /**
275: | * Get Paths
276: | *
277: | * @param string $path
278: | *
279: | * @return array<int, array<string, mixed>>
280: | */
281: | public function getPaths(string $path): array {
282: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension_path` WHERE `path` LIKE '" . $this->db->escape($path) . "' ORDER BY `path` ASC");
283: |
284: | return $query->rows;
285: | }
286: |
287: | /**
288: | * Get Total Paths
289: | *
290: | * @param string $path
291: | *
292: | * @return int
293: | */
294: | public function getTotalPaths(string $path): int {
295: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "extension_path` WHERE `path` LIKE '" . $this->db->escape($path) . "'");
296: |
297: | return (int)$query->row['total'];
298: | }
299: | }
300: |

OpenCart API API documentation generated by ApiGen dev-master