|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Project: RocketCMS |
|
22
|
|
|
* License: Apache 2.0 license |
|
23
|
|
|
* User: Justin |
|
24
|
|
|
* Date: 20.03.2018 |
|
25
|
|
|
* Time: 14:36 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class Groups { |
|
29
|
|
|
|
|
30
|
|
|
protected $my_groups = array(); |
|
31
|
|
|
|
|
32
|
|
|
public function __construct() { |
|
33
|
|
|
// |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function loadMyGroups (int $userID) { |
|
37
|
|
|
if (Cache::contains("groups", "own-groups-" . $userID)) { |
|
38
|
|
|
$this->my_groups = Cache::get("groups", "own-groups-" . $userID); |
|
39
|
|
|
} else { |
|
40
|
|
|
$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}group_members` LEFT JOIN `{praefix}groups` ON `{praefix}group_members`.`groupID` = `{praefix}groups`.`groupID` WHERE `{praefix}group_members`.`userID` = :userID AND `{praefix}group_members`.`activated` = '1'; ", array( |
|
41
|
|
|
'userID' => array( |
|
42
|
|
|
'type' => PDO::PARAM_INT, |
|
43
|
|
|
'value' => $userID |
|
44
|
|
|
) |
|
45
|
|
|
)); |
|
46
|
|
|
|
|
47
|
|
|
$this->my_groups = $rows; |
|
48
|
|
|
|
|
49
|
|
|
//cache rows |
|
50
|
|
|
Cache::put("groups", "own-groups-" . $userID, $this->my_groups); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function listGroupIDs () : array { |
|
55
|
|
|
$array = array(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($this->my_groups as $group_row) { |
|
58
|
|
|
$array[] = $group_row['groupID']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $array; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function listMyGroups () : array { |
|
65
|
|
|
$array = array(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($this->my_groups as $row) { |
|
68
|
|
|
$group = new Group(); |
|
69
|
|
|
$group->loadByRow($row); |
|
70
|
|
|
|
|
71
|
|
|
$array[] = $group; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $array; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public static function createGroupIfIdAbsent (int $groupID, string $name, string $description, string $color = "#000000", bool $show = true, bool $system_group = false, bool $auto_assign_regist = false) { |
|
78
|
|
|
//check, if color is valide |
|
79
|
|
|
$validator = new Validator_Color(); |
|
80
|
|
|
|
|
81
|
|
|
if (!$validator->isValide($color)) { |
|
82
|
|
|
throw new IllegalArgumentException("color '" . $color . "' isnt a valide hex color."); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
Database::getInstance()->execute("INSERT INTO `{praefix}groups` ( |
|
86
|
|
|
`groupID`, `name`, `description`, `color`, `auto_assign_regist`, `system_group`, `show`, `activated` |
|
87
|
|
|
) VALUES ( |
|
88
|
|
|
:groupID, :name, :description, :color, :auto_assign_regist, :system_group, :show, '1' |
|
89
|
|
|
) ON DUPLICATE KEY UPDATE `groupID` = :groupID; ", array( |
|
90
|
|
|
'groupID' => $groupID, |
|
91
|
|
|
'name' => Validator_String::get($name), |
|
92
|
|
|
'description' => Validator_String::get($description), |
|
93
|
|
|
'color' => $color, |
|
94
|
|
|
'auto_assign_regist' => ($auto_assign_regist ? 1 : 0), |
|
95
|
|
|
'system_group' => ($system_group ? 1 : 0), |
|
96
|
|
|
'show' => ($show ? 1 : 0) |
|
97
|
|
|
)); |
|
98
|
|
|
|
|
99
|
|
|
//clear complete cache for all groups, so membership cache is also cleared |
|
100
|
|
|
Cache::clear("groups"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function deleteGroup (int $groupID) { |
|
104
|
|
|
$group = new Group(); |
|
105
|
|
|
|
|
106
|
|
|
try { |
|
107
|
|
|
$group->loadById($groupID); |
|
108
|
|
|
} catch (IllegalStateException $e) { |
|
109
|
|
|
//group doesnt exists, we dont have to do anything |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$group->delete(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public static function addGroupToUser (int $groupID, int $userID, bool $group_leader = false) { |
|
117
|
|
|
Database::getInstance()->execute("INSERT INTO `{praefix}group_members` ( |
|
118
|
|
|
`groupID`, `userID`, `group_leader`, `activated` |
|
119
|
|
|
) VALUES ( |
|
120
|
|
|
:groupID, :userID, :group_leader, '1' |
|
121
|
|
|
) ON DUPLICATE KEY UPDATE `group_leader` = :group_leader; ", array( |
|
122
|
|
|
'groupID' => array( |
|
123
|
|
|
'type' => PDO::PARAM_INT, |
|
124
|
|
|
'value' => $groupID |
|
125
|
|
|
), |
|
126
|
|
|
'userID' => array( |
|
127
|
|
|
'type' => PDO::PARAM_INT, |
|
128
|
|
|
'value' => $userID |
|
129
|
|
|
), |
|
130
|
|
|
'group_leader' => ($group_leader ? 1 : 0) |
|
131
|
|
|
)); |
|
132
|
|
|
|
|
133
|
|
|
//clear cache |
|
134
|
|
|
Cache::clear("groups", "own-groups-" . $userID); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public static function removeGroupFromUser (int $groupID, int $userID) { |
|
138
|
|
|
Database::getInstance()->execute("DELETE FROM `{praefix}group_members` WHERE `groupID` = :groupID AND `userID` = :userID; ", array( |
|
139
|
|
|
'groupID' => array( |
|
140
|
|
|
'type' => PDO::PARAM_INT, |
|
141
|
|
|
'value' => $groupID |
|
142
|
|
|
), |
|
143
|
|
|
'userID' => array( |
|
144
|
|
|
'type' => PDO::PARAM_INT, |
|
145
|
|
|
'value' => $userID |
|
146
|
|
|
) |
|
147
|
|
|
)); |
|
148
|
|
|
|
|
149
|
|
|
//clear cache |
|
150
|
|
|
Cache::clear("groups", "own-groups-" . $userID); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
?> |
|
|
|
|
|
|
156
|
|
|
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.