/
UserList.php
181 lines (162 loc) · 5.19 KB
/
UserList.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
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
<?php
namespace Concrete\Core\Legacy;
use UserAttributeKey;
use UserInfo;
use Core;
use Database;
use Group;
/**
* An object that allows a filtered list of users to be returned.
*
* \@package Files
*
* @author Tony Trupp <tony@concrete5.org>
*
* @category Concrete
*
* @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
* @license http://www.concrete5.org/license/ MIT License
*/
class UserList extends DatabaseItemList
{
protected $attributeFilters = array();
protected $autoSortColumns = array('uName', 'uEmail', 'uDateAdded', 'uLastLogin', 'uNumLogins', 'uLastOnline');
protected $itemsPerPage = 10;
protected $attributeClass = 'UserAttributeKey';
public $showInactiveUsers;
public $showInvalidatedUsers = 0;
public $searchAgainstEmail = 0;
//Filter by uName
public function filterByUserName($username)
{
$this->filter('u.uName', $username, '=');
}
public function filterByKeywords($keywords)
{
$db = Database::connection();
$qkeywords = $db->quote('%' . $keywords . '%');
$keys = UserAttributeKey::getSearchableIndexedList();
$emailSearchStr = ' OR u.uEmail like '.$qkeywords.' ';
$attribsStr = '';
foreach ($keys as $ak) {
$cnt = $ak->getController();
$attribsStr .= ' OR ' . $cnt->searchKeywords($keywords);
}
$this->filter(false, '( u.uName like ' . $qkeywords . $emailSearchStr . $attribsStr . ')');
}
/**
* Filters the user list for only users within the provided group. Accepts an instance of a group object or a string group name.
*
* @param Group|string $group
* @param bool $inGroup
*/
public function filterByGroup($group = '', $inGroup = true)
{
if (!$group instanceof Group) {
$group = Group::getByName($group);
}
$tbl = 'ug_'.$group->getGroupID();
$this->addToQuery("left join UserGroups $tbl on {$tbl}.uID = u.uID ");
if ($inGroup) {
$this->filter(false, "{$tbl}.gID=".intval($group->getGroupID()));
} else {
$this->filter(false, "{$tbl}.gID is null");
}
}
public function excludeUsers($uo)
{
if (is_object($uo)) {
$uID = $uo->getUserID();
} else {
$uID = $uo;
}
$this->filter('u.uID', $uID, '!=');
}
public function filterByGroupID($gID)
{
if (!Core::make('helper/validation/numbers')->integer($gID)) {
$gID = 0;
}
$tbl = 'ug_'.$gID;
$this->addToQuery("left join UserGroups $tbl on {$tbl}.uID = u.uID ");
$this->filter(false, "{$tbl}.gID=".$gID);
}
public function filterByDateAdded($date, $comparison = '=')
{
$this->filter('u.uDateAdded', $date, $comparison);
}
/**
* Returns an array of userInfo objects based on current filter settings.
*
* @return UserInfo[]
*/
public function get($itemsToGet = 100, $offset = 0)
{
$userInfos = array();
$this->createQuery();
$r = parent::get($itemsToGet, intval($offset));
foreach ($r as $row) {
$ui = UserInfo::getByID($row['uID']);
$userInfos[] = $ui;
}
return $userInfos;
}
/**
* Similar to get except it returns an array of userIDs.
* Much faster than getting a UserInfo object for each result if all you need is the user's id.
*
* @return array $userIDs
*/
public function getUserIDs($itemsToGet = 100, $offset = 0)
{
$this->createQuery();
$userIDs = array();
$r = parent::get($itemsToGet, intval($offset));
foreach ($r as $row) {
$userIDs[] = $row['uID'];
}
return $userIDs;
}
public function getTotal()
{
$this->createQuery();
return parent::getTotal();
}
public function filterByIsActive($val)
{
$this->showInactiveUsers = $val;
$this->filter('u.uIsActive', $val);
}
//this was added because calling both getTotal() and get() was duplicating some of the query components
protected function createQuery()
{
if (!$this->queryCreated) {
$this->setBaseQuery();
if (!isset($this->showInactiveUsers)) {
$this->filter('u.uIsActive', 1);
}
if (!$this->showInvalidatedUsers) {
$this->filter('u.uIsValidated', 0, '!=');
}
$this->setupAttributeFilters("left join UserSearchIndexAttributes on (UserSearchIndexAttributes.uID = u.uID)");
$this->queryCreated = 1;
}
}
protected function setBaseQuery()
{
$this->setQuery('SELECT DISTINCT u.uID, u.uName FROM Users u ');
}
/* magic method for filtering by page attributes. */
public function __call($nm, $a)
{
if (substr($nm, 0, 8) == 'filterBy') {
$txt = Core::make('helper/text');
$attrib = $txt->uncamelcase(substr($nm, 8));
if (count($a) == 2) {
$this->filterByAttribute($attrib, $a[0], $a[1]);
} else {
$this->filterByAttribute($attrib, $a[0]);
}
}
}
}