public function search($Filter, $OrderFields = '', $OrderDirection = 'asc', $Limit = false, $Offset = false)
{
$Optimize = false;
if (is_array($Filter)) {
$Where = $Filter;
$Keywords = $Where['Keywords'];
$Optimize = val('Optimize', $Filter);
unset($Where['Keywords'], $Where['Optimize']);
} else {
$Keywords = $Filter;
}
$Keywords = trim($Keywords);
// Check for an IP address.
if (preg_match('`\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}`', $Keywords)) {
$IPAddress = $Keywords;
} elseif (strtolower($Keywords) == 'banned') {
$this->SQL->where('u.Banned >', 0);
$Keywords = '';
} elseif (preg_match('/^\\d+$/', $Keywords)) {
$numericQuery = $Keywords;
$Keywords = '';
} else {
// Check to see if the search exactly matches a role name.
$RoleID = $this->SQL->getWhere('Role', ['Name' => $Keywords])->value('RoleID');
}
$this->userQuery();
if (isset($Where)) {
$this->SQL->where($Where);
}
if (!empty($RoleID)) {
$this->SQL->join('UserRole ur2', "u.UserID = ur2.UserID and ur2.RoleID = {$RoleID}");
} elseif (isset($IPAddress)) {
$fields = ['LastIPAddress'];
$this->addIpFilters($IPAddress, $fields);
} elseif (isset($numericQuery)) {
// We've searched for a number. Return UserID AND any exact numeric name match.
$this->SQL->beginWhereGroup()->where('u.UserID', $numericQuery)->orWhere('u.Name', $numericQuery)->endWhereGroup();
} elseif ($Keywords) {
if ($Optimize) {
// An optimized search should only be done against name OR email.
if (strpos($Keywords, '@') !== false) {
$this->SQL->like('u.Email', $Keywords, 'right');
} else {
$this->SQL->like('u.Name', $Keywords, 'right');
}
} else {
// Search on the user table.
$Like = ['u.Name' => $Keywords, 'u.Email' => $Keywords];
$this->SQL->orOp()->beginWhereGroup()->orLike($Like, '', 'right')->endWhereGroup();
}
}
// Optimized searches need at least some criteria before performing a query.
if ($Optimize && $this->SQL->whereCount() == 0 && empty($RoleID)) {
$this->SQL->reset();
return new Gdn_DataSet([]);
}
$Data = $this->SQL->where('u.Deleted', 0)->orderBy($OrderFields, $OrderDirection)->limit($Limit, $Offset)->groupBy('u.UserID')->get();
$Result =& $Data->result();
foreach ($Result as &$Row) {
if ($Row->Photo && !isUrl($Row->Photo)) {
$Row->Photo = Gdn_Upload::url(changeBasename($Row->Photo, 'n%s'));
}
$Row->Attributes = dbdecode($Row->Attributes);
$Row->Preferences = dbdecode($Row->Preferences);
}
return $Data;
}