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

Simplify search expression

parent 514c7d20
......@@ -129,37 +129,38 @@ 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
$queries = array();
if (!empty($search->query)) {
$search->expr['or'] = array(array('field' => 'firstname',
'type' => 'contains',
'string' => $search->query),
array('field' => 'lastname',
'type' => 'contains',
'string' => $search->query));
$queries = array(array('field' => 'firstname',
'type' => 'contains',
'string' => $search->query),
array('field' => 'lastname',
'type' => 'contains',
'string' => $search->query));
if (strpos($search->query, '@') !== false) {
$search->expr['or'][] = array('field' => 'email',
'type' => 'contains',
'string' => $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);
$queries[] = array('field' => 'email',
'type' => 'contains',
'string' => $search->query);
}
}
$constraints = array();
if (!empty($search->f)) {
$constraints[] = array('field' => 'firstname',
'type' => 'starts',
'string' => $search->f);
}
if (!empty($search->l)) {
$constraints[] = array('field' => 'lastname',
'type' => 'starts',
'string' => $search->l);
}
if (!empty($search->institution) && $search->institution != 'all') {
$search->expr['and'][] = array('field' => 'institution',
'type' => 'equals',
'string' => $search->institution);
$constraints[] = array('field' => 'institution',
'type' => 'equals',
'string' => $search->institution);
}
$results = admin_user_search($search);
$results = admin_user_search($queries, $constraints, $search->offset, $search->limit);
$smarty->assign_by_ref('params', $paramstring);
$results['pages'] = ceil($results['count'] / $results['limit']);
......@@ -178,10 +179,11 @@ function build_admin_user_search_results($search) {
}
function admin_user_search($searchparams) {
function admin_user_search($queries, $constraints, $offset, $limit) {
$plugin = get_config('searchplugin');
safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'admin_search_user', $searchparams);
return call_static_method(generate_class_name('search', $plugin), 'admin_search_user',
$queries, $constraints, $offset, $limit);
}
......
......@@ -237,9 +237,9 @@ class PluginSearchInternal extends PluginSearch {
}
public static function admin_search_user($s) {
public static function admin_search_user($queries, $constratints, $offset, $limit) {
if (is_postgres()) {
return self::admin_search_user_pg($s);
return self::admin_search_user_pg($queries, $constratints, $offset, $limit);
}
//else if (is_mysql()) {
// return self::admin_search_user_my($query_string, $limit, $offset);
......@@ -250,7 +250,7 @@ class PluginSearchInternal extends PluginSearch {
}
public static function admin_search_user_pg($s) {
public static function admin_search_user_pg($queries, $constraints, $offset, $limit) {
$where = 'WHERE u.id <> 0 AND u.deleted = 0';
$values = array();
......@@ -260,18 +260,18 @@ class PluginSearchInternal extends PluginSearch {
'equals' => ' = ? ',
'contains' => ' ILIKE \'%\' || ? || \'%\'');
if (!empty($s->expr['or'])) {
if (!empty($queries)) {
$where .= ' AND ( ';
$str = array();
foreach ($s->expr['or'] as $f) {
foreach ($queries 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) {
if (!empty($constraints)) {
foreach ($constraints as $f) {
$where .= ' AND u.' . $f['field'] . $matchtypes[$f['type']];
$values[] = $f['string'];
}
......@@ -288,8 +288,8 @@ class PluginSearchInternal extends PluginSearch {
{usr} u ' . $where . '
ORDER BY u.firstname, u.lastname, u.id',
$values,
$s->offset,
$s->limit);
$offset,
$limit);
if ($data) {
foreach ($data as &$item) {
......@@ -303,8 +303,8 @@ class PluginSearchInternal extends PluginSearch {
return array(
'count' => $count,
'limit' => $s->limit,
'offset' => $s->offset,
'limit' => $limit,
'offset' => $offset,
'data' => $data,
);
}
......
......@@ -198,32 +198,34 @@ END;
}
public static function admin_search_user($s) {
if (!empty($s->expr['or'])) {
$op = 'or';
} else if (!empty($s->expr['and'])) {
$op = 'and';
} else {
$q = '';
}
if (!empty($op)) {
$solrfields = array(
'id' => 'id',
'institution' => 'ref_institution',
'email' => 'text_email',
'username' => 'text_username',
'firstname' => 'text_firstname',
'lastname' => 'text_lastname'
);
public static function admin_search_user($queries, $constraints, $offset, $limit) {
$q = '';
$solrfields = array(
'id' => 'id',
'institution' => 'ref_institution',
'email' => 'text_email',
'username' => 'text_username',
'firstname' => 'text_firstname',
'lastname' => 'text_lastname'
);
if (!empty($queries)) {
$terms = array();
foreach ($queries as $f) {
$terms[] = $solrfields[$f['field']] . ':' . strtolower($f['string'])
. ($f['type'] != 'equals' ? '*' : '');
}
$q .= '(' . join(' OR ', $terms) . ')';
}
if (!empty($constraints)) {
$terms = array();
foreach ($s->expr[$op] as $f) {
foreach ($constraints as $f) {
$terms[] = $solrfields[$f['field']] . ':' . strtolower($f['string'])
. ($f['type'] != 'equals' ? '*' : '');
}
$q = join(' '.strtoupper($op).' ', $terms);
$q .= join(' AND ', $terms);
}
$results = self::send_query($q, $s->limit, $s->offset, array('type' => 'user'));
$results = self::send_query($q, $limit, $offset, array('type' => 'user'));
self::remove_key_prefix(&$results);
return $results;
}
......
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