1: | <?php
2: | namespace Opencart\Admin\Model\User;
3: | /**
4: | * Class User Group
5: | *
6: | * @package Opencart\Admin\Model\User
7: | */
8: | class UserGroup extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add User Group
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addUserGroup(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "user_group` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `permission` = '" . (isset($data['permission']) ? $this->db->escape(json_encode($data['permission'])) : '') . "'");
18: |
19: | return $this->db->getLastId();
20: | }
21: |
22: | /**
23: | * Edit User Group
24: | *
25: | * @param int $user_group_id
26: | * @param array<string, mixed> $data
27: | *
28: | * @return void
29: | */
30: | public function editUserGroup(int $user_group_id, array $data): void {
31: | $this->db->query("UPDATE `" . DB_PREFIX . "user_group` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `permission` = '" . (isset($data['permission']) ? $this->db->escape(json_encode($data['permission'])) : '') . "' WHERE `user_group_id` = '" . (int)$user_group_id . "'");
32: | }
33: |
34: | /**
35: | * Delete User Group
36: | *
37: | * @param int $user_group_id
38: | *
39: | * @return void
40: | */
41: | public function deleteUserGroup(int $user_group_id): void {
42: | $this->db->query("DELETE FROM `" . DB_PREFIX . "user_group` WHERE `user_group_id` = '" . (int)$user_group_id . "'");
43: | }
44: |
45: | /**
46: | * Get User Group
47: | *
48: | * @param int $user_group_id
49: | *
50: | * @return array<string, mixed>
51: | */
52: | public function getUserGroup(int $user_group_id): array {
53: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "user_group` WHERE `user_group_id` = '" . (int)$user_group_id . "'");
54: |
55: | return [
56: | 'name' => $query->row['name'],
57: | 'permission' => json_decode($query->row['permission'], true)
58: | ];
59: | }
60: |
61: | /**
62: | * Get User Groups
63: | *
64: | * @param array<string, mixed> $data
65: | *
66: | * @return array<int, array<string, mixed>>
67: | */
68: | public function getUserGroups(array $data = []): array {
69: | $sql = "SELECT * FROM `" . DB_PREFIX . "user_group` ORDER BY `name`";
70: |
71: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
72: | $sql .= " DESC";
73: | } else {
74: | $sql .= " ASC";
75: | }
76: |
77: | if (isset($data['start']) || isset($data['limit'])) {
78: | if ($data['start'] < 0) {
79: | $data['start'] = 0;
80: | }
81: |
82: | if ($data['limit'] < 1) {
83: | $data['limit'] = 20;
84: | }
85: |
86: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
87: | }
88: |
89: | $query = $this->db->query($sql);
90: |
91: | return $query->rows;
92: | }
93: |
94: | /**
95: | * Get Total User Groups
96: | *
97: | * @return int
98: | */
99: | public function getTotalUserGroups(): int {
100: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "user_group`");
101: |
102: | return (int)$query->row['total'];
103: | }
104: |
105: | /**
106: | * Add Permission
107: | *
108: | * @param int $user_group_id
109: | * @param string $type
110: | * @param string $route
111: | *
112: | * @return void
113: | */
114: | public function addPermission(int $user_group_id, string $type, string $route): void {
115: | $user_group_query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "user_group` WHERE `user_group_id` = '" . (int)$user_group_id . "'");
116: |
117: | if ($user_group_query->num_rows) {
118: | $data = json_decode($user_group_query->row['permission'], true);
119: |
120: | $data[$type][] = $route;
121: |
122: | $this->db->query("UPDATE `" . DB_PREFIX . "user_group` SET `permission` = '" . $this->db->escape(json_encode($data)) . "' WHERE `user_group_id` = '" . (int)$user_group_id . "'");
123: | }
124: | }
125: |
126: | /**
127: | * Remove Permission
128: | *
129: | * @param int $user_group_id
130: | * @param string $type
131: | * @param string $route
132: | *
133: | * @return void
134: | */
135: | public function removePermission(int $user_group_id, string $type, string $route): void {
136: | $user_group_query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "user_group` WHERE `user_group_id` = '" . (int)$user_group_id . "'");
137: |
138: | if ($user_group_query->num_rows) {
139: | $data = json_decode($user_group_query->row['permission'], true);
140: |
141: | if (isset($data[$type])) {
142: | $data[$type] = array_diff($data[$type], [$route]);
143: | }
144: |
145: | $this->db->query("UPDATE `" . DB_PREFIX . "user_group` SET `permission` = '" . $this->db->escape(json_encode($data)) . "' WHERE `user_group_id` = '" . (int)$user_group_id . "'");
146: | }
147: | }
148: | }
149: |

OpenCart API API documentation generated by ApiGen dev-master