-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProcessUserExtended.module
178 lines (144 loc) · 5.06 KB
/
ProcessUserExtended.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
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
<?php
class ProcessUserExtended extends ProcessUser {
static public function getModuleInfo() {
return array(
'title' => __('Users extended', __FILE__), // getModuleInfo title
'version' => 101,
'summary' => __('Extended view for user management', __FILE__), // getModuleInfo summary
'permanent' => false,
'permission' => 'user-admin',
);
}
public function ___execute() {
$out = '';
// Grab filter form
$form = $this->getFilterForm();
if ($form instanceof InputfieldFieldset || $form instanceof InputfieldForm) {
$out .= $form->render();
} else if ($form) {
$out .= $form;
}
// Build filter selector (if filter form exists)
$selector = $form ? $this->getFilterSelector() : null;
if ($this->input->whitelist('search')) {
$directHit = $this->users->get("name=" . $this->input->whitelist('search'));
if ($directHit->id) $out .= "<h3>". $this->_('Found by username') .": <a href='./edit/?id=$directHit->id'>$directHit->name</a></h3>";
}
return $out . $this->renderList($selector, array("arrayToCSV" => false));
}
/**
* Build filter forms for execute method
*
* This being a method of it's own helps us both keep this separate from
* main execute method, but also makes it possible for external code to
* alter return value (add/remove fields etc.) based on their needs.
*
* @return mixed InputfieldFieldset by default, but can be altered by hooks
*/
public function ___getFilterForm() {
$fields = $this->modules->get("InputfieldFieldset");
$form = $this->modules->get("InputfieldForm");
$form->attr('method', 'get');
$form->attr('id', 'userSearch');
$form->label = $this->_("Filter users");
if (!$this->input->get->roles && !$this->input->get->search) {
$form->collapsed = Inputfield::collapsedYes;
}
$field = $this->modules->get("InputfieldText");
$field->attr('name', 'search');
$field->label = $this->_("Search");
if ($this->input->get->search) {
$this->input->whitelist('search', $this->sanitizer->text($this->input->get->search));
$field->attr('value', $this->input->whitelist('search'));
}
$form->add($field);
$field = $this->modules->get("InputfieldCheckboxes");
$field->attr('name', 'roles');
$field->optionColumns = 5;
foreach(wire('roles') as $role) {
if ($role->name == "guest") continue;
$attrs = array();
$this->input->whitelist("roles", $this->input->get->roles);
if (is_array($this->input->get->roles)) {
if(in_array($role->name, $this->input->get->roles)) {
$attrs['selected'] = 'selected';
}
}
$field->addOption($role->name, $role->get('title|name'), $attrs);
}
$field->label = $this->_("Filter by role");
$form->add($field);
$field = $this->modules->get("InputfieldSubmit");
$field->attr('value', $this->_("Search"));
$form->add($field);
$fields->add($form);
return $fields;
}
/**
* Build selector for execute method
*
* @return string selector string
*/
public function ___getFilterSelector() {
$selector = "limit=25, status<" . Page::statusMax;
if ($this->input->get->roles) {
$roles = new PageArray;
foreach($this->input->get->roles as $roleName) {
$role = $this->roles->get($roleName);
if ($role->id) $roles->add($role);
}
if ($roles->count()) $selector .= ", roles=$roles";
}
if ($this->input->get->search) {
$search = $this->sanitizer->selectorValue($this->input->get->search);
$selector .= ", name|title%=$search";
}
return $selector;
}
public function ___executeList() {
return $this->renderList("limit=25, status<" . Page::statusMax);
}
protected function renderList($selector = '', $pagerOptions = array()) {
$out = '';
if(!$this->pages->getTemplate()) {
$form = $this->getTemplateFilterForm();
$out = $form->render();
}
$table = $this->modules->get("MarkupAdminDataTable");
$table->setEncodeEntities(false);
$fieldNames = $this->showFields;
$fieldLabels = $fieldNames;
foreach($fieldLabels as $key => $name) {
if($name == 'name') {
$fieldLabels[$key] = $this->_('Name'); // Label for 'name' field
continue;
}
$field = wire('fields')->get($name);
$languageID = wire('user')->language ? wire('user')->language->id : '';
$label = $field->get('label' . $languageID);
if(!$label) $label = $field->label;
if(!$label) $label = $name;
$fieldLabels[$key] = htmlentities($label, ENT_QUOTES, "UTF-8");
}
$table->headerRow($fieldLabels);
$pages = $this->users->find($selector);
foreach($pages as $page) {
if(!$page->editable()) continue;
$n = 0;
$row = array();
foreach($fieldNames as $name) {
if(!$n) $row[$page->get($name) . ' '] = "edit/?id={$page->id}";
else $row[] = $this->renderListFieldValue($name, $page->get($name));
$n++;
}
$table->row($row);
}
if($this->page->addable()) $table->action(array($this->_('Add New') => 'add/'));
if($pages->getTotal() > count($pages)) {
$pager = $this->modules->get("MarkupPagerNav");
$out .= $pager->render($pages, $pagerOptions);
}
$out .= $table->render();
return $out;
}
}