Commit 0397c4ae authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Move name search sql generation into separate function



This will be useful for group member searches, which should use the
same query string parsing and the same same constraints on searching
usernames, real names, and display names as are used in the general
user searches.

Change-Id: I9e648b50012c9834944cf5bc53450cc718842841
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 5bc6c5d3
......@@ -107,18 +107,6 @@ class PluginSearchInternal extends PluginSearch {
* );
*/
public static function search_user($query_string, $limit, $offset = 0, $data=array()) {
safe_require('artefact', 'internal');
// Get the list of searchable profile fields from the internal artefact
$required = array_keys(ArtefactTypeProfile::get_always_searchable_fields());
$optional = array_diff(array_keys(ArtefactTypeProfile::get_searchable_fields()), $required);
$required[] = 'username'; // Not a profile field, but used in the search query.
// Get a list of match expressions to use in the WHERE clause
$matches = new StdClass;
foreach (array_merge($required, $optional) as $f) {
$matches->{$f} = self::match_user_field_expression($f, 'u');
}
$data = self::prepare_search_user_options($data);
$sql = '
......@@ -150,32 +138,10 @@ class PluginSearchInternal extends PluginSearch {
$sql .= "
LEFT OUTER JOIN {usr_account_preference} h ON (u.id = h.usr AND h.field = 'hiderealname')";
$querydata = self::split_query_string(strtolower(trim($query_string)));
$hidenameallowed = get_config('userscanhiderealnames') ? 'TRUE' : 'FALSE';
$searchusernamesallowed = get_config('searchusernames') ? 'TRUE' : 'FALSE';
$termsql = "$matches->preferredname
OR (
(u.preferredname IS NULL OR u.preferredname = '' OR NOT $hidenameallowed OR h.value != '1')
AND ($matches->firstname OR $matches->lastname)
)
OR ($searchusernamesallowed AND $matches->username)";
list($namesql, $values) = self::name_search_sql($query_string);
if ($optional) {
foreach ($optional as $f) {
$termsql .= "
OR {$matches->$f}";
}
}
$values = array();
foreach ($querydata as $term) {
$where .= '
AND (
' . $termsql . '
)';
$values = array_pad($values, count($values) + 4 + count($optional), $term);
}
$where .= $namesql;
if (isset($data['exclude'])) {
$where .= '
......@@ -222,6 +188,60 @@ class PluginSearchInternal extends PluginSearch {
return $result;
}
/**
* Returns a snippet of an sql WHERE clause to filter users whose (visible)
* names match the terms in a given query string.
*/
function name_search_sql($query_string, $usralias='u', $usrprefalias='h') {
safe_require('artefact', 'internal');
// Get the list of searchable profile fields from the internal artefact
$required = array_keys(ArtefactTypeProfile::get_always_searchable_fields());
$optional = array_diff(array_keys(ArtefactTypeProfile::get_searchable_fields()), $required);
$required[] = 'username'; // Not a profile field, but used in the search query.
// Get a list of match expressions to use in the WHERE clause
$matches = new StdClass;
foreach (array_merge($required, $optional) as $f) {
$matches->{$f} = self::match_user_field_expression($f, $usralias);
}
$querydata = self::split_query_string(strtolower(trim($query_string)));
$hidenameallowed = get_config('userscanhiderealnames') ? 'TRUE' : 'FALSE';
$searchusernamesallowed = get_config('searchusernames') ? 'TRUE' : 'FALSE';
$termsql = "$matches->preferredname
OR (
($usralias.preferredname IS NULL OR $usralias.preferredname = '' OR NOT $hidenameallowed OR $usrprefalias.value != '1')
AND ($matches->firstname OR $matches->lastname)
)
OR ($searchusernamesallowed AND $matches->username)";
if ($optional) {
foreach ($optional as $f) {
$termsql .= "
OR {$matches->$f}";
}
}
$where = '';
$values = array();
foreach ($querydata as $term) {
$where .= '
AND (
' . $termsql . '
)';
$values = array_pad($values, count($values) + 4 + count($optional), $term);
}
return array($where, $values);
}
private static function match_user_field_expression($field, $alias) {
if (get_config_plugin('search', 'internal', 'exactusersearch')) {
return 'LOWER(' . $alias . '.' . $field . ') = ?';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment