forked from SFWLtd/plg_user_teamroles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamRolesAdminTab.php
98 lines (85 loc) · 2.81 KB
/
TeamRolesAdminTab.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
<?php
/**
* @version 1.3
* @package plg_user_teamroles
* @author Simon Champion
* @copyright SFW Ltd
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
* @requires Joomdle >= 1.0.6
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class TeamRolesAdminTab
{
protected $baseXML = <<<'eof'
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields name="team">
<fieldset name="team" label="Team Role">
{teamFields}
</fieldset>
</fields>
</form>
eof;
protected $userInfo;
protected $adminMode = false;
public function __construct(TeamRolesUserInfo $userInfo, $adminMode)
{
JFormHelper::addFieldPath(__DIR__ . '/field');
$this->userInfo = $userInfo;
$this->adminMode = $adminMode;
}
public function addTeamTabToAdminForm($form)
{
if (!$this->userInfo->iAmAParent) {
return;
}
$teamData = $this->getTeamData();
if(!count($teamData)) {
return;
}
$fieldsXML = $this->getTeamDataFields($teamData);
$fieldsXML .= $this->getResyncButton();
$teamXML = str_replace('{teamFields}', $fieldsXML, $this->baseXML);
$form->load($teamXML, false);
}
protected function getResyncButton()
{
return "<field type='resyncbutton' name='resync-teams' leader='{$this->userInfo->userID}' />";
}
protected function getTeamDataFields($teamData)
{
$xml = [];
foreach ($teamData as $groupID=>$teamGroupData) {
$xml[] = "<field type='team' ".($this->adminMode ? "admin='true'" : "")." name='team-{$groupID}' leader='{$this->userInfo->userID}' groupid='{$groupID}' label='{$teamGroupData['groupName']}'>";
foreach ($teamGroupData['users'] as $userID) {
$user = JFactory::getUser($userID);
$username = $user->get('username');
$name = $user->get('name');
$show = $this->userInfo->loadTeamRoleToggleFromProfile($userID) ? 1 : 0;
$xml[] = "<member userid='{$userID}' username='{$username}' on='{$show}'>{$name}</member>";
}
$xml[] = "</field>";
}
return implode('',$xml);
}
protected function getTeamData()
{
$output = [];
foreach ($this->userInfo->userGroups as $groupID) {
$users = TeamRolesUserInfo::usersInGroup($groupID);
$output[$groupID] = [
'groupName'=>$this->loadGroupName($groupID),
'users'=>$users
];
}
return $output;
}
protected function loadGroupName($groupID)
{
$db = JFactory::getDBO();
$query = 'SELECT title FROM #__usergroups WHERE id = ' . (int)$groupID;
$db->setQuery($query);
return $db->loadResult();
}
}