1: | <?php
2: | namespace Opencart\Admin\Model\Tool;
3: | /**
4: | * Class Upload
5: | *
6: | * @package Opencart\Admin\Model\Tool
7: | */
8: | class Upload extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Upload
11: | *
12: | * @param string $name
13: | * @param string $filename
14: | *
15: | * @return string
16: | */
17: | public function addUpload(string $name, string $filename): string {
18: | $code = oc_token(32);
19: |
20: | $this->db->query("INSERT INTO `" . DB_PREFIX . "upload` SET `name` = '" . $this->db->escape($name) . "', `filename` = '" . $this->db->escape($filename) . "', `code` = '" . $this->db->escape($code) . "', `date_added` = NOW()");
21: |
22: | return $code;
23: | }
24: |
25: | /**
26: | * Delete Upload
27: | *
28: | * @param int $upload_id
29: | *
30: | * @return void
31: | */
32: | public function deleteUpload(int $upload_id): void {
33: | $this->db->query("DELETE FROM `" . DB_PREFIX . "upload` WHERE `upload_id` = '" . (int)$upload_id . "'");
34: | }
35: |
36: | /**
37: | * Get Upload
38: | *
39: | * @param int $upload_id
40: | *
41: | * @return array<string, mixed>
42: | */
43: | public function getUpload(int $upload_id): array {
44: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "upload` WHERE `upload_id` = '" . (int)$upload_id . "'");
45: |
46: | return $query->row;
47: | }
48: |
49: | /**
50: | * Get Upload By Code
51: | *
52: | * @param string $code
53: | *
54: | * @return array<string, mixed>
55: | */
56: | public function getUploadByCode(string $code): array {
57: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "upload` WHERE `code` = '" . $this->db->escape($code) . "'");
58: |
59: | return $query->row;
60: | }
61: |
62: | /**
63: | * Get Uploads
64: | *
65: | * @param array<string, mixed> $data
66: | *
67: | * @return array<int, array<string, mixed>>
68: | */
69: | public function getUploads(array $data = []): array {
70: | $sql = "SELECT * FROM `" . DB_PREFIX . "upload`";
71: |
72: | $implode = [];
73: |
74: | if (!empty($data['filter_name'])) {
75: | $implode[] = "LCASE(`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
76: | }
77: |
78: | if (!empty($data['filter_code'])) {
79: | $implode[] = "LCASE(`code`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_code']) . '%') . "'";
80: | }
81: |
82: | if (!empty($data['filter_date_from'])) {
83: | $implode[] = "DATE(`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_from']) . "')";
84: | }
85: |
86: | if (!empty($data['filter_date_to'])) {
87: | $implode[] = "DATE(`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_to']) . "')";
88: | }
89: |
90: | if ($implode) {
91: | $sql .= " WHERE " . implode(" AND ", $implode);
92: | }
93: |
94: | $sort_data = [
95: | 'name',
96: | 'code',
97: | 'date_added'
98: | ];
99: |
100: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
101: | $sql .= " ORDER BY " . $data['sort'];
102: | } else {
103: | $sql .= " ORDER BY `date_added`";
104: | }
105: |
106: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
107: | $sql .= " DESC";
108: | } else {
109: | $sql .= " ASC";
110: | }
111: |
112: | if (isset($data['start']) || isset($data['limit'])) {
113: | if ($data['start'] < 0) {
114: | $data['start'] = 0;
115: | }
116: |
117: | if ($data['limit'] < 1) {
118: | $data['limit'] = 20;
119: | }
120: |
121: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
122: | }
123: |
124: | $query = $this->db->query($sql);
125: |
126: | return $query->rows;
127: | }
128: |
129: | /**
130: | * Get Total Uploads
131: | *
132: | * @param array<string, mixed> $data
133: | *
134: | * @return int
135: | */
136: | public function getTotalUploads(array $data = []): int {
137: | $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "upload`";
138: |
139: | $implode = [];
140: |
141: | if (!empty($data['filter_name'])) {
142: | $implode[] = "LCASE(`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
143: | }
144: |
145: | if (!empty($data['filter_code'])) {
146: | $implode[] = "LCASE(`code`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_code']) . '%') . "'";
147: | }
148: |
149: | if (!empty($data['filter_date_from'])) {
150: | $implode[] = "DATE(`date_added`) >= DATE('" . $this->db->escape((string)$data['filter_date_from']) . "')";
151: | }
152: |
153: | if (!empty($data['filter_date_to'])) {
154: | $implode[] = "DATE(`date_added`) <= DATE('" . $this->db->escape((string)$data['filter_date_to']) . "')";
155: | }
156: |
157: | if ($implode) {
158: | $sql .= " WHERE " . implode(" AND ", $implode);
159: | }
160: |
161: | $query = $this->db->query($sql);
162: |
163: | return (int)$query->row['total'];
164: | }
165: | }
166: |

OpenCart API API documentation generated by ApiGen dev-master