Commit a14c94a0 authored by Richard Mansfield's avatar Richard Mansfield Committed by Nigel McNie
Browse files

Move common search expression stuff out of plugin and up to searchlib

level
parent 43e35852
......@@ -130,11 +130,33 @@ function build_admin_user_search_results($search) {
// In admin search, the search string is interpreted as either a
// name search or an email search depending on its contents
if (!empty($search->query)) {
$search->name = $search->query;
$search->expr['or'] = array(array('field' => 'firstname',
'type' => 'contains',
'string' => $search->query),
array('field' => 'lastname',
'type' => 'contains',
'string' => $search->query));
if (strpos($search->query, '@') !== false) {
$search->email = $search->query;
$search->expr['or'][] = array('field' => 'email',
'type' => 'contains',
'string' => $search->query);
}
unset($search->query);
} else {
if (!empty($search->f)) {
$search->expr['and'][] = array('field' => 'firstname',
'type' => 'starts',
'string' => $search->f);
}
if (!empty($search->l)) {
$search->expr['and'][] = array('field' => 'lastname',
'type' => 'starts',
'string' => $search->l);
}
}
if (!empty($search->institution)) {
$search->expr['and'][] = array('field' => 'institution',
'type' => 'equals',
'string' => $search->institution);
}
$results = admin_user_search($search);
......@@ -158,6 +180,7 @@ function build_admin_user_search_results($search) {
function admin_user_search($searchparams) {
$plugin = 'internal'; // No admin search with solr yet.
//$plugin = get_config('searchplugin');
safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'admin_search_user', $searchparams);
}
......
......@@ -252,40 +252,29 @@ class PluginSearchInternal extends PluginSearch {
public static function admin_search_user_pg($s) {
$where = 'WHERE u.id <> 0 AND u.deleted = 0';
$strings = array();
$values = array();
if (!empty($s->name)) {
$strings[] = ' u.firstname ILIKE \'%\' || ? || \'%\' OR u.lastname ILIKE \'%\' || ? || \'%\' ';
$values = array($s->name, $s->name);
} else {
if (!empty($s->f)) {
$strings[] = ' u.firstname ILIKE ? || \'%\'';
$values[] = $s->f;
}
if (!empty($s->l)) {
$strings[] = ' u.lastname ILIKE ? || \'%\'';
$values[] = $s->l;
}
}
if (!empty($strings)) {
$strings = '( ' . join(' AND ', $strings) . ' )';
} else {
$strings = '';
}
if (!empty($s->email)) {
$strings = $strings . ' OR u.email ILIKE \'%\' || ? || \'%\'';
$values[] = $s->email;
}
// Only handle OR/AND expressions at the top level. Eventually we may need subexpressions.
if (!empty($strings)) {
$where .= ' AND ( ' . $strings . ' ) ';
}
$matchtypes = array('starts' => ' ILIKE ? || \'%\'',
'equals' => ' = ? ',
'contains' => ' ILIKE \'%\' || ? || \'%\'');
if (!empty($s->institution)) {
$where .= ' AND u.institution = ? ';
$values[] = $s->institution;
if (!empty($s->expr['or'])) {
$where .= ' AND ( ';
$str = array();
foreach ($s->expr['or'] as $f) {
$str[] = 'u.' . $f['field'] . $matchtypes[$f['type']];
$values[] = $f['string'];
}
$where .= join(' OR ', $str) . ') ';
}
if (!empty($s->expr['and'])) {
foreach ($s->expr['and'] as $f) {
$where .= ' AND u.' . $f['field'] . $matchtypes[$f['type']];
$values[] = $f['string'];
}
}
$count = get_field_sql('SELECT COUNT(*) FROM {usr} u ' . $where, $values);
......
......@@ -192,6 +192,12 @@ END;
return $results;
}
public static function admin_search_user($s) {
//$results = self::send_query($query_string, $limit, $offset, array('type' => 'user'));
return array();
}
/**
* Given a query string and limits, return an array of matching objects
* owned by the current user. Possible return types are ...
......
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