/
GroupSelector.php
117 lines (100 loc) · 3.84 KB
/
GroupSelector.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
<?php
namespace Concrete\Core\Form\Service\Widget;
use Concrete\Core\Form\Service\Form;
use Concrete\Core\Http\ResponseAssetGroup;
use Concrete\Core\Tree\Node\Type\Group as GroupTreeNode;
use Concrete\Core\Tree\Type\Group as GroupTree;
use Concrete\Core\User\Group\Group;
use Concrete\Core\User\Group\GroupList;
use Concrete\Core\Utility\Service\Identifier;
use Symfony\Component\HttpFoundation\Request;
class GroupSelector
{
protected $assetGroup;
protected $request;
protected $formHelper;
protected $groupList;
public function __construct(Form $formHelper, GroupList $groupList)
{
$this->assetGroup = ResponseAssetGroup::get();
$this->request = Request::createFromGlobals();
$this->formHelper = $formHelper;
$this->groupList = $groupList;
}
public function selectGroup($field, $group = null, $noneText = null)
{
$selected = 0;
if ($group) {
$selected = ($group instanceof Group) ? $group->getGroupID() : $group;
}
if (!$noneText) {
$noneText = t('** Select Group');
}
$groups = ['' => $noneText];
foreach($this->groupList->getResults() as $groupResult) {
$groups[$groupResult->getGroupID()] = $groupResult->getGroupDisplayName();
}
print $this->formHelper->select($field, $groups, $selected);
}
public function getGroupFromGroupTreeRequestValue($value)
{
// We have this helper method because you're not actually submitting a group ID – you're submitting a
// group node ID. So we need to translate that into a group.
if ($value) {
$node = \Concrete\Core\Tree\Node\Type\Group::getByID($value);
if ($node) {
return $node->getTreeNodeGroupObject();
}
}
}
public function selectGroupWithTree($field, $group = null)
{
$identifier = new Identifier();
$identifier = $identifier->getString(32);
$selectedGroupID = 0;
$selectedNodeID = 0;
if ($this->request->getRealMethod() == 'POST') {
if ($this->request->request->has($field)) {
$selectedGroupID = intval($this->request->request->get($field));
}
} elseif ($group) {
$selectedGroupID = ($group instanceof Group) ? $group->getGroupID() : $group;
}
// At this point selected is the ID of the group, but we need the group Node ID.
if ($selectedGroupID > 0) {
$selectedNode = \Concrete\Core\Tree\Node\Type\Group::getTreeNodeByGroupID($selectedGroupID);
if ($selectedNode) {
$selectedNodeID = $selectedNode->getTreeNodeID();
}
}
$tree = GroupTree::get();
$guestGroupNode = GroupTreeNode::getTreeNodeByGroupID(GUEST_GROUP_ID);
$registeredGroupNode = GroupTreeNode::getTreeNodeByGroupID(REGISTERED_GROUP_ID);
$guestGroupNodeID = $guestGroupNode->getTreeNodeID();
$registeredGroupNodeID = $registeredGroupNode->getTreeNodeID();
$treeID = $tree->getTreeID();
$html = <<<EOL
<input type="hidden" name="{$field}" value="{$selectedNodeID}">
<div data-group-selector="{$identifier}"></div>
<script type="text/javascript">
jQuery(function() {
$('[data-group-selector={$identifier}]').concreteTree({
'treeID': {$treeID},
'enableDragAndDrop': false,
selectNodesByKey: [{$selectedNodeID}],
'removeNodesByKey': [{$guestGroupNodeID}, {$registeredGroupNodeID}],
onSelect : function(nodes) {
if (nodes.length) {
$('input[name={$field}]').val(nodes[0]);
} else {
$('input[name={$field}]').val('');
}
},
chooseNodeInForm: 'single'
});
});
</script>
EOL;
return $html;
}
}