1: <?php
2: namespace Opencart\Admin\Model\Setting;
3: /**
4: * Class Event
5: *
6: * @package Opencart\Admin\Model\Setting
7: */
8: class Event extends \Opencart\System\Engine\Model {
9: /**
10: * Add Event
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
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: * Delete Event
24: *
25: * @param int $event_id
26: *
27: * @return void
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: * Delete Event By Code
35: *
36: * @param string $code
37: *
38: * @return void
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: * Edit Status
46: *
47: * @param int $event_id
48: * @param bool $status
49: *
50: * @return void
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: * Edit Status By Code
58: *
59: * @param string $code
60: * @param bool $status
61: *
62: * @return void
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: * Get Event
70: *
71: * @param int $event_id
72: *
73: * @return array<string, mixed>
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: * Get Event By Code
83: *
84: * @param string $code
85: *
86: * @return array<string, mixed>
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: * Get Events
96: *
97: * @param array<string, mixed> $data
98: *
99: * @return array<int, array<string, mixed>>
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: * Get Total Events
144: *
145: * @return int
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: