/
GroupRole.php
242 lines (199 loc) · 5.78 KB
/
GroupRole.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php /** @noinspection PhpUnused */
/** @noinspection SqlDialectInspection */
/** @noinspection SqlNoDataSourceInspection */
namespace Concrete\Core\User\Group;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Foundation\ConcreteObject;
use Concrete\Core\Support\Facade\Application;
use Doctrine\DBAL\Exception;
use JsonSerializable;
class GroupRole extends ConcreteObject implements JsonSerializable
{
/** @var int */
protected $grID;
/** @var string */
protected $grName;
/** @var bool */
protected $grIsManager;
/**
* @return string
*/
public function getId()
{
return $this->grID;
}
/**
* @return string
*/
public function getName()
{
return $this->grName;
}
/**
* @param string $grName
* @return bool
*/
public function setName($grName)
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
$this->grName = $grName;
try {
$db->executeQuery("update GroupRoles set grName = ? where grID = ?", [$grName, $this->grID]);
return true;
} catch (Exception $e) {
return false;
}
}
/**
* @return bool
*/
public function isManager()
{
return (bool)$this->grIsManager;
}
/**
* @param bool $grIsManager
* @return bool
*/
public function setIsManager($grIsManager)
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
$this->grIsManager = $grIsManager;
try {
$db->executeQuery("update GroupRoles set grIsManager = ? where grID = ?", [(int)$grIsManager, $this->grID]);
return true;
} catch (Exception $e) {
return false;
}
}
/**
* @param $grID
* @return static|bool
*/
public static function getByID($grID)
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
try {
$row = $db->fetchAssoc('select grID, grName, grIsManager from GroupRoles where grID = ?', [$grID]);
} catch (Exception $e) {
return false;
}
if (isset($row['grID'])) {
$gs = new static();
$gs->setPropertiesFromArray($row);
return $gs;
} else {
return false;
}
}
/**
* @param GroupType $groupType
* @return static[]
*/
public static function getListByGroupType(GroupType $groupType)
{
$list = [];
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
try {
$rows = $db->fetchAll('select grID from GroupTypeSelectedRoles where gtID = ?', [$groupType->getId()]);
foreach ($rows as $row) {
$list[] = static::getByID($row['grID']);
}
} catch (Exception $e) {
}
return $list;
}
/**
* @param Group $group
* @return array
*/
public static function getListByGroup(Group $group)
{
$list = [];
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
try {
$rows = $db->fetchAll('select grID from GroupSelectedRoles where gID = ?', [$group->getGroupID()]);
foreach ($rows as $row) {
$list[] = static::getByID($row['grID']);
}
} catch (Exception $e) {
}
return $list;
}
/**
* @return array
*/
public static function getList()
{
$list = [];
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
try {
$rows = $db->fetchAll('select grID from GroupRoles order by grName asc');
foreach ($rows as $row) {
$list[] = static::getByID($row['grID']);
}
} catch (Exception $e) {
}
return $list;
}
/**
* @param string $grName
* @param bool $grIsManager
* @return bool|static
*/
public static function add($grName, $grIsManager)
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
try {
$db->executeQuery('insert into GroupRoles (grName, grIsManager) values (?,?)', [$grName, (int)$grIsManager]);
} catch (Exception $e) {
return false;
}
$id = $db->lastInsertId();
return self::getByID($id);
}
/**
* @return bool
* @throws Exception
*/
public function delete()
{
if ($this->getId() == DEFAULT_GROUP_ROLE_ID) {
throw new \Exception(t("You can't delete the default role."));
}
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
try {
$db->executeQuery('delete from GroupRoles where grID = ?', [$this->getId()]);
$db->executeQuery('delete from GroupSelectedRoles where grID = ?', [$this->getId()]);
$db->executeQuery('delete from GroupTypeSelectedRoles where grID = ?', [$this->getId()]);
// update Group Type or Group => set new default role
} catch (Exception $e) {
return false;
}
return true;
}
public function jsonSerialize()
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"manager" => $this->isManager(),
];
}
}