-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUserGroupsProcessUserExtendedIntegration.module
114 lines (95 loc) · 3.03 KB
/
UserGroupsProcessUserExtendedIntegration.module
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
<?php
/**
* User Groups - Process User Extended Integration
*
* This module provides features that connect User Groups modules with Process
* User Extended. Said features are required by neither module and in order to
* keep both as lean as possible separated into external "integration module".
*
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*
*/
class UserGroupsProcessUserExtendedIntegration extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Process User Extended Integration for User Groups',
'version' => 100,
'summary' => 'Integrates User Groups and Process User Extended.',
'singular' => true,
'autoload' => true,
'requires' => array('ProcessUserGroups', 'ProcessUserExtended')
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// hooks (see function declarations for more information)
$this->addHookAfter("ProcessUserExtended::getFilterForm", $this, 'hookGetFilterForm');
$this->addHookAfter("ProcessUserExtended::getFilterSelector", $this, 'hookGetFilterSelector');
}
/**
* Hook into Process User Extended getFilterForm()
*
* Replace roles field with groups field.
*
* @param HookEvent $event
*/
public function hookGetFilterForm(HookEvent $event) {
$fields = $event->return;
$form = $fields->get(0);
if ($this->input->get->groups) {
$form->collapsed = Inputfield::collapsedNo;
}
$field = $this->modules->get("InputfieldCheckboxes");
$field->attr('name', 'groups');
$field->optionColumns = 5;
foreach(wire('groups') as $group) {
if (in_array($group->name, array("everyone", "logged"))) continue;
$attrs = array();
$this->input->whitelist("groups", $this->input->get->groups);
if (is_array($this->input->get->groups)) {
if(in_array($group->name, $this->input->get->groups)) {
$attrs['selected'] = 'selected';
}
}
$field->addOption($group->name, $group->get('title|name'), $attrs);
}
$field->label = $this->_("Filter by group");
$submit = $form->get("submit");
$form->insertBefore($field, $submit);
$event->return = $fields;
}
/**
* Hook into Process User Extended getFilterSelector()
*
* Add groups to selector instead of roles.
*
* @param HookEvent $event
*/
public function hookGetFilterSelector(HookEvent $event) {
$selector = $event->return;
if ($this->input->get->groups) {
$groups = new PageArray;
foreach($this->input->get->groups as $groupName) {
$group = $this->groups->get($groupName);
if ($group->id) $groups->add($group);
}
if ($groups->count()) {
if ($selector) $selector .= ", ";
$selector .= "user_groups=$groups";
}
}
$event->return = $selector;
}
}