/
GroupSet.php
173 lines (149 loc) · 4.86 KB
/
GroupSet.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
<?php
namespace Concrete\Core\User\Group;
use Concrete\Core\Foundation\ConcreteObject;
use Gettext\Translations;
use Loader;
class GroupSet extends ConcreteObject
{
public static function getList()
{
$db = Loader::db();
$r = $db->Execute('select gsID from GroupSets order by gsName asc');
$list = array();
while ($row = $r->fetch()) {
$list[] = static::getByID($row['gsID']);
}
return $list;
}
public static function getByID($gsID)
{
$db = Loader::db();
$row = $db->GetRow('select gsID, pkgID, gsName from GroupSets where gsID = ?', array($gsID));
if (isset($row['gsID'])) {
$gs = new static();
$gs->setPropertiesFromArray($row);
return $gs;
}
}
public static function getByName($gsName)
{
$db = Loader::db();
$row = $db->GetRow('select gsID, pkgID, gsName from GroupSets where gsName = ?', array($gsName));
if (isset($row['gsID'])) {
$gs = new static();
$gs->setPropertiesFromArray($row);
return $gs;
}
}
public static function getListByPackage($pkg)
{
$db = Loader::db();
$list = array();
$r = $db->Execute('select gsID from GroupSets where pkgID = ? order by gsID asc', array($pkg->getPackageID()));
while ($row = $r->fetch()) {
$list[] = static::getByID($row['gsID']);
}
$r->free();
return $list;
}
public function getGroupSetID()
{
return $this->gsID;
}
public function getGroupSetName()
{
return $this->gsName;
}
public function getPackageID()
{
return $this->pkgID;
}
/** Returns the display name for this group set (localized and escaped accordingly to $format)
* @param string $format = 'html'
* Escape the result in html format (if $format is 'html').
* If $format is 'text' or any other value, the display name won't be escaped.
*
* @return string
*/
public function getGroupSetDisplayName($format = 'html')
{
$value = tc('GroupSetName', $this->getGroupSetName());
switch ($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}
}
public function updateGroupSetName($gsName)
{
$this->gsName = $gsName;
$db = Loader::db();
$db->Execute("update GroupSets set gsName = ? where gsID = ?", array($gsName, $this->gsID));
}
public function addGroup(Group $g)
{
$db = Loader::db();
$no = $db->GetOne("select count(gID) from GroupSetGroups where gID = ? and gsID = ?", array($g->getGroupID(), $this->getGroupSetID()));
if ($no < 1) {
$db->Execute('insert into GroupSetGroups (gsID, gID) values (?, ?)', array($this->getGroupSetID(), $g->getGroupID()));
}
}
public static function add($gsName, $pkg = false)
{
$db = Loader::db();
$pkgID = 0;
if (is_object($pkg)) {
$pkgID = $pkg->getPackageID();
}
$db->Execute('insert into GroupSets (gsName, pkgID) values (?,?)', array($gsName, $pkgID));
$id = $db->Insert_ID();
$gs = self::getByID($id);
return $gs;
}
public function clearGroups()
{
$db = Loader::db();
$db->Execute('delete from GroupSetGroups where gsID = ?', array($this->gsID));
}
public function getGroups()
{
$db = Loader::db();
$r = $db->Execute('select gID from GroupSetGroups where gsID = ? order by gID asc', $this->getGroupSetId());
$groups = array();
while ($row = $r->fetch()) {
$g = Group::getByID($row['gID']);
if (is_object($g)) {
$groups[] = $g;
}
}
return $groups;
}
public function contains(Group $g)
{
$db = Loader::db();
$r = $db->GetOne('select count(gID) from GroupSetGroups where gsID = ? and gID = ?', array($this->getGroupSetID(), $g->getGroupID()));
return $r > 0;
}
public function delete()
{
$db = Loader::db();
$db->Execute('delete from GroupSets where gsID = ?', array($this->getGroupSetID()));
$db->Execute('delete from GroupSetGroups where gsID = ?', array($this->getGroupSetID()));
}
public function removeGroup(Group $g)
{
$db = Loader::db();
$db->Execute('delete from GroupSetGroups where gsID = ? and gID = ?', array($this->getGroupSetID(), $g->getGroupID()));
}
public static function exportTranslations()
{
$translations = new Translations();
$sets = static::getList();
foreach ($sets as $set) {
$translations->insert('GroupSetName', $set->getGroupSetName());
}
return $translations;
}
}