-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.memberships.plugin.php
More file actions
344 lines (307 loc) · 12 KB
/
class.memberships.plugin.php
File metadata and controls
344 lines (307 loc) · 12 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2013 Alessandro Miliucci <lifeisfoo@gmail.com>
This file is part of Memberships plugin for vanillaforums
This plugin was initially based on the Memberships plugin by
johnathon released under GPL2. For more informations see this link
<http://vanillaforums.org/discussion/23298/membership-plugin-license>
Memberships is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Memberships is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Memberships. If not, see <http://www.gnu.org/licenses/>.
*/
$PluginInfo['Memberships'] = array(
'Name' => 'Memberships',
'Description' => 'This plugin allows users to be assigned to groups created by the Groups plugin.',
'Version' => '0.2',
'RequiredApplications' => FALSE,
'RequiredTheme' => FALSE,
'RequiredPlugins' => array('Groups' => '0.1'),
'SettingsUrl' => '/dashboard/plugin/memberships',
'SettingsPermission' => 'Garden.Settings.Manage',
'HasLocale' => TRUE,
'RegisterPermissions' => FALSE,
'Author' => "Alessandro Miliucci",
'AuthorEmail' => 'lifeisfoo@gmail.com',
'AuthorUrl' => 'http://forkwait.net',
'Repository' => 'https://github.com/lifeisfoo/Memberships',
'IssueTracker' => 'https://github.com/lifeisfoo/Memberships/issues',
'License' => 'GPL3+'
);
class MembershipsPlugin extends Gdn_Plugin {
public function Base_GetAppSettingsMenuItems_Handler($Sender) {
$LinkText = T('Memberships');
$Menu = $Sender->EventArguments['SideMenu'];
$Menu->AddItem('Users', T('Users'));
$Menu->AddLink('Users', $LinkText, 'plugin/memberships', 'Garden.Settings.Manage');
}
public function PluginController_Memberships_Create($Sender) {
$Sender->Permission('Garden.Settings.Manage');
$Sender->Title('Membership Management');
$Sender->AddSideMenu('plugin/memberships');
$Sender->Form = new Gdn_Form();
$this->Dispatch($Sender, $Sender->RequestArgs);
}
/**
* Show left panel with groups list
*/
public function CategoriesController_AfterRenderAsset_Handler($Sender) {
if(strcmp(GetValue("AssetName", $Sender->EventArguments), "Panel") == 0){
echo '<div class="Box BoxGroups">';
echo '<h4><a href="' . Url('groups') . '">' . T('Groups') . '</a></h4>';
echo '<ul class="PanelInfo GroupsPanel">';
$GroupList = Gdn::SQL()->Select('*')
->From('Group gr')
->Get();
while ($GroupItems = $GroupList->NextRow(DATASET_TYPE_ARRAY)) {
$Name = $GroupItems['Name'];
$ID = $GroupItems['GroupID'];
echo '<li>';
echo '<a href=' . Url('groups/' . Gdn_Format::Url($Name)) . '>' . $Name . '</a>';
echo '</li>';
}
echo "</div>";
}
}
public function PluginController_MembershipsList_Create($Sender) {
$Sender->ClearCssFiles();
$Sender->AddCssFile('style.css');
$Sender->MasterView = 'default';
$GroupNameArg = $Sender->RequestArgs[0];
if(!$GroupNameArg){
$Sender->GroupList = $this->GetAllGroups();
$Sender->Render(dirname(__FILE__) . DS . 'views' . DS . 'groups_list.php');
}else{
$Found = FALSE;
$GroupID;
$GroupName;
foreach ( $this->GetAllGroups() as $GName => $GID ){
if( strcmp($GroupNameArg, Gdn_Format::Url($GName)) == 0 ){
$Found = TRUE;
$GroupID = $GID;
$GroupName = $GName;
break;
}
}
if($Found){
$Sender->UserData = Gdn::SQL()->Select('User.*')->From('User')->OrderBy('User.Name')->Where('Deleted',false)->Get();
$MemberList = Gdn::SQL()->Select('us.UserID, us.Name, us.Email, us.Photo')
->Select('ug.GroupID')
->OrderBy('us.Email', 'asc')
->Select('gr.Name', '', 'GroupName')
->From('User us')
->Where('us.Deleted', 0)
->Where('gr.GroupID', $GroupID)
->Join('UserGroup ug', 'us.UserID = ug.UserID', 'left')
->Join('Group gr', 'ug.GroupID = gr.GroupID', 'left')
->Get();
$Sender->GroupName = $GroupName;
$Sender->GroupMembers = $MemberList;
if( count($Sender->GroupMembers->Result()) > 0){
$Sender->Render(dirname(__FILE__) . DS . 'views' . DS . 'membershipslist.php');
}else{
$Sender->Render(dirname(__FILE__) . DS . 'views' . DS . 'empty_membershipslist.php');
}
}else{
throw NotFoundException(T('Group'));
}
}
}
public function UserInfoModule_OnBasicInfo_Handler($Sender) {
$Groups = $this->GetUserGroupsNameArray($Sender->User->UserID);
if( count($Groups) > 0 ) {
echo '<dt>' . T('Member of these groups') . '</dt>';
echo '<dd>';
$this->GroupsArrayShow($Groups);
echo '</dd>';
}
}
/**
* Return all groups as a map name=>id
*/
private function GetAllGroups() {
$Groups = Gdn::SQL()->Select('gr.GroupID', '', 'id')
->Select('gr.Name', '', 'name')
->From('Group gr')
->Get();
$GroupsArray = array();
while ($Group = $Groups->NextRow(DATASET_TYPE_ARRAY)) {
$GroupsArray[$Group['name']] = $Group['id'];
}
return $GroupsArray;
}
/**
* Return an array with user groups names. Public so can be used by theme hooks
*/
static public function GetUserGroupsNameArray($UserID) {
$Membership = Gdn::SQL()->Select('*')
->From('UserGroup ug')
->Where('ug.UserID', $UserID)
->Join('Group gr', 'ug.GroupID = gr.GroupID', 'left')
->Get();
$MembershipGroups = array();
while ($Group = $Membership->NextRow(DATASET_TYPE_ARRAY)) {
$MembershipGroups[] = $Group['Name'];
}
return $MembershipGroups;
}
public function ProfileController_EditMyAccountAfter_Handler($Sender) {
$UserID = $Sender->User->UserID;
if($UserID){
$Groups = $this->GetUserGroupsNameArray($UserID);
if( count($Groups) > 0 ){
//if user have at least a group
//show groups and a message to explain howto edit it
echo '<li class="groups">';
echo '<label for="groups-list" class="groups-list">' . T('You are member of these groups') . '</label>';
echo '<div name="groups-list">';
$this->GroupsArrayShow($Groups);
echo T('<small><i>[To change your membership contact the webmaster.]</i></small>') ;
echo '</div></li>';
}else{
// else show select (null by default)
$this->ProfileGroupSelect($Sender);
}
}
}
/**
* Display user group membership (only a string).
* An array of string must be passed as input
*
* @access private
*/
private function GroupsArrayShow($Groups) {
$GroupsString = null;
foreach ($Groups as $Group) {
if ( !$GroupsString ) {//first group
$GroupsString = $Group;
} else {
$GroupsString .= ', ' . $Group;
}
}
echo $GroupsString . ' ';
}
/**
* Display dropdown group select
*
* @access private
*/
private function ProfileGroupSelect($Sender) {
$Groups = Gdn::SQL()->Select('gr.GroupID', '', 'value')
->Select('gr.Name', '', 'text')
->From('Group gr')
->Get();
echo "<li>";
echo $Sender->Form->Label(T('Group'), 'Plugin.Memberships.GroupID');
echo $Sender->Form->DropDown('Plugin.Memberships.GroupID', $Groups, array('IncludeNull' => TRUE));
echo "</li>";
}
/**
* Save selected group (if any) when saving the user.
*/
public function UserModel_AfterSave_Handler($Sender) {
$FormPostValues = GetValue('FormPostValues', $Sender->EventArguments);
$GroupID = GetValue('Plugin.Memberships.GroupID', $FormPostValues);
$UserID = GetValue('UserID', $FormPostValues);
if( $UserID && $GroupID ){
Gdn::SQL()->Insert('UserGroup',array(
'UserID' => $UserID,
'GroupID' => $GroupID
));
}
}
public function Controller_Index($Sender) {
$Sender->AddCssFile('admin.css');
$Sender->AddCssFile($this->GetResource('design/memberships.css', FALSE, FALSE));
$GroupCheck = Gdn::SQL()
->Select('gr.GroupID')
->From('Group gr')
->Get()->NumRows();
$MemberList = Gdn::SQL()
->Select('us.UserID, us.Name, us.Email')
->Select('ug.GroupID')
->OrderBy('us.Email', 'asc')
->Select('gr.Name', '', 'GroupName')
->From('User us')
->Where('us.Deleted', 0)
->Join('UserGroup ug', 'us.UserID = ug.UserID', 'left')
->Join('Group gr', 'ug.GroupID = gr.GroupID', 'left')
->Get();
while ($MemberItems = $MemberList->NextRow(DATASET_TYPE_ARRAY)) {
$Sender->MemberList[] = $MemberItems;
}
$Sender->GroupCheck = $GroupCheck;
unset($MemberList);
$Sender->Render($this->GetView('memberships.php'));
}
public function Controller_Edit($Sender) {
if ($Sender->Form->AuthenticatedPostBack()) {
$UserID = $Sender->Form->GetValue('Plugin.Memberships.UserID');
$GroupID = $Sender->Form->GetValue('Plugin.Memberships.GroupID');
// check for existing membership
$Membership = Gdn::SQL()->Select('ug.GroupID', '', 'OldGroupID')
->From('UserGroup ug')
->Where('ug.UserID', $UserID)
->Get();
$MembershipCheck = $Membership->FirstRow(DATASET_TYPE_ARRAY);
if ($MembershipCheck['OldGroupID'] > 0) {
try {
Gdn::SQL()->Update('UserGroup ug')
->Set('ug.GroupID', $GroupID)
->Where('ug.UserID', $UserID)
->Put();
} catch(Exception $e) {}
} else {
Gdn::SQL()->Insert('UserGroup',array(
'UserID' => $UserID,
'GroupID' => $GroupID
));
}
$Sender->StatusMessage = T("Your changes have been saved.");
$Sender->RedirectUrl = Url('plugin/memberships');
} else {
// send the group data to the form
$Arguments = $Sender->RequestArgs;
if (sizeof($Arguments) != 2) return;
list($Controller, $UserID) = $Arguments;
$UserInQuestion = Gdn::SQL()->Select('us.UserID, us.Name')
->From('User us')
->Where('us.UserID', $UserID)
->Get();
$OldMembership = Gdn::SQL()->Select('ug.GroupID', '', 'OldGroupID')
->From('UserGroup ug')
->Where('ug.UserID', $UserID)
->Get();
$Groups = Gdn::SQL()
->Select('gr.GroupID', '', 'value')
->Select('gr.Name', '', 'text')
->From('Group gr')
->Get();
$Sender->Groups = $Groups;
$Sender->OldMembership = $OldMembership->FirstRow(DATASET_TYPE_ARRAY);
$Sender->UserInQuestion = $UserInQuestion->FirstRow(DATASET_TYPE_ARRAY);
}
$Sender->Render($this->GetView('edit.php'));
}
public function Structure() {
$Structure = Gdn::Structure();
$Structure
->Table('UserGroup')
->Column('UserID', 'int(11)')
->Column('GroupID', 'int(11)')
->Set(FALSE, FALSE);
Gdn::Router()->SetRoute('groups/(.+)', '/plugin/membershipsList/$1', 'Internal');
}
public function Setup() {
$this->Structure();
}
public function OnDisable() {
Gdn::Router()->DeleteRoute('groups/(.+)');
}
}