1: | <?php
|
2: | namespace Opencart\Admin\Model\Setting;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Event extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: |
|
16: | public function addEvent(array $data): int {
|
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "event` SET `code` = '" . $this->db->escape($data['code']) . "', `description` = '" . $this->db->escape($data['description']) . "', `trigger` = '" . $this->db->escape($data['trigger']) . "', `action` = '" . $this->db->escape($data['action']) . "', `status` = '" . (bool)$data['status'] . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
18: |
|
19: | return $this->db->getLastId();
|
20: | }
|
21: |
|
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: |
|
29: | public function deleteEvent(int $event_id): void {
|
30: | $this->db->query("DELETE FROM `" . DB_PREFIX . "event` WHERE `event_id` = '" . (int)$event_id . "'");
|
31: | }
|
32: |
|
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: |
|
40: | public function deleteEventByCode(string $code): void {
|
41: | $this->db->query("DELETE FROM `" . DB_PREFIX . "event` WHERE `code` = '" . $this->db->escape($code) . "'");
|
42: | }
|
43: |
|
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: |
|
52: | public function editStatus(int $event_id, bool $status): void {
|
53: | $this->db->query("UPDATE `" . DB_PREFIX . "event` SET `status` = '" . (bool)$status . "' WHERE `event_id` = '" . (int)$event_id . "'");
|
54: | }
|
55: |
|
56: | |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: |
|
64: | public function editStatusByCode(string $code, bool $status): void {
|
65: | $this->db->query("UPDATE `" . DB_PREFIX . "event` SET `status` = '" . (bool)$status . "' WHERE `code` = '" . $this->db->escape($code) . "'");
|
66: | }
|
67: |
|
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: |
|
75: | public function getEvent(int $event_id): array {
|
76: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "event` WHERE `event_id` = '" . (int)$event_id . "'");
|
77: |
|
78: | return $query->row;
|
79: | }
|
80: |
|
81: | |
82: | |
83: | |
84: | |
85: | |
86: | |
87: |
|
88: | public function getEventByCode(string $code): array {
|
89: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "event` WHERE `code` = '" . $this->db->escape($code) . "' LIMIT 1");
|
90: |
|
91: | return $query->row;
|
92: | }
|
93: |
|
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: |
|
101: | public function getEvents(array $data = []): array {
|
102: | $sql = "SELECT * FROM `" . DB_PREFIX . "event`";
|
103: |
|
104: | $sort_data = [
|
105: | 'code',
|
106: | 'trigger',
|
107: | 'action',
|
108: | 'sort_order',
|
109: | 'status',
|
110: | 'date_added'
|
111: | ];
|
112: |
|
113: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
114: | $sql .= " ORDER BY " . $data['sort'];
|
115: | } else {
|
116: | $sql .= " ORDER BY `sort_order`";
|
117: | }
|
118: |
|
119: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
120: | $sql .= " DESC";
|
121: | } else {
|
122: | $sql .= " ASC";
|
123: | }
|
124: |
|
125: | if (isset($data['start']) || isset($data['limit'])) {
|
126: | if ($data['start'] < 0) {
|
127: | $data['start'] = 0;
|
128: | }
|
129: |
|
130: | if ($data['limit'] < 1) {
|
131: | $data['limit'] = 20;
|
132: | }
|
133: |
|
134: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
135: | }
|
136: |
|
137: | $query = $this->db->query($sql);
|
138: |
|
139: | return $query->rows;
|
140: | }
|
141: |
|
142: | |
143: | |
144: | |
145: | |
146: |
|
147: | public function getTotalEvents(): int {
|
148: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "event`");
|
149: |
|
150: | return (int)$query->row['total'];
|
151: | }
|
152: | }
|
153: | |