Commit a333b4ee authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Allow normal user searches with > 1 term

parent 76df03a7
...@@ -106,9 +106,8 @@ class PluginSearchInternal extends PluginSearch { ...@@ -106,9 +106,8 @@ class PluginSearchInternal extends PluginSearch {
'; ';
} }
} }
$sql .= 'WHERE $querydata = split(' ', preg_replace('/\s\s+/', ' ', strtolower(trim($query_string))));
u.id <> 0 AND u.active = 1 AND u.deleted = 0 $namesql = '(
AND ((
u.preferredname ILIKE \'%\' || ? || \'%\' u.preferredname ILIKE \'%\' || ? || \'%\'
) )
OR ( OR (
...@@ -121,11 +120,20 @@ class PluginSearchInternal extends PluginSearch { ...@@ -121,11 +120,20 @@ class PluginSearchInternal extends PluginSearch {
OR ( OR (
a.artefacttype IN ' . $fieldlist . ' a.artefacttype IN ' . $fieldlist . '
AND ( a.title ILIKE \'%\' || ? || \'%\') AND ( a.title ILIKE \'%\' || ? || \'%\')
) )';
$namesql = join(' OR ', array_fill(0, count($querydata), $namesql));
$values = array();
foreach ($querydata as $w) {
$values = array_pad($values, count($values) + 4, $w);
}
$sql .= 'WHERE
u.id <> 0 AND u.active = 1 AND u.deleted = 0
AND ( ' . $namesql . '
) )
' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . ' ' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . '
'; ';
$count = get_field_sql($sql, array($query_string, $query_string, $query_string, $query_string)); $count = get_field_sql($sql, $values);
if ($count > 0) { if ($count > 0) {
$sql = 'SELECT DISTINCT ON (u.firstname, u.lastname, u.id) $sql = 'SELECT DISTINCT ON (u.firstname, u.lastname, u.id)
...@@ -143,27 +151,11 @@ class PluginSearchInternal extends PluginSearch { ...@@ -143,27 +151,11 @@ class PluginSearchInternal extends PluginSearch {
} }
$sql .= 'WHERE $sql .= 'WHERE
u.id <> 0 AND u.active = 1 AND u.deleted = 0 u.id <> 0 AND u.active = 1 AND u.deleted = 0
AND (( AND (' . $namesql . '
u.preferredname ILIKE \'%\' || ? || \'%\'
)
OR (
(u.preferredname IS NULL OR u.preferredname = \'\')
AND (
u.firstname ILIKE \'%\' || ? || \'%\'
OR u.lastname ILIKE \'%\' || ? || \'%\'
)
)
OR (
a.artefacttype IN ' . $fieldlist . '
AND ( a.title ILIKE \'%\' || ? || \'%\')
)
) )
' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . ' ' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . '
ORDER BY u.firstname, u.lastname, u.id'; ORDER BY u.firstname, u.lastname, u.id';
$data = get_records_sql_array($sql, $data = get_records_sql_array($sql, $values, $offset, $limit);
array($query_string, $query_string, $query_string, $query_string),
$offset,
$limit);
if ($data) { if ($data) {
foreach ($data as &$item) { foreach ($data as &$item) {
...@@ -200,9 +192,8 @@ class PluginSearchInternal extends PluginSearch { ...@@ -200,9 +192,8 @@ class PluginSearchInternal extends PluginSearch {
'; ';
} }
} }
$sql .= 'WHERE $querydata = split(' ', preg_replace('/\s\s+/', ' ', strtolower(trim($query_string))));
u.id <> 0 AND u.active = 1 AND u.deleted = 0 $namesql = '(
AND ((
u.preferredname LIKE \'%\' || ? || \'%\' u.preferredname LIKE \'%\' || ? || \'%\'
) )
OR ( OR (
...@@ -215,12 +206,21 @@ class PluginSearchInternal extends PluginSearch { ...@@ -215,12 +206,21 @@ class PluginSearchInternal extends PluginSearch {
OR ( OR (
a.artefacttype IN ' . $fieldlist . ' a.artefacttype IN ' . $fieldlist . '
AND ( a.title LIKE \'%\' || ? || \'%\') AND ( a.title LIKE \'%\' || ? || \'%\')
) )';
$namesql = join(' OR ', array_fill(0, count($querydata), $namesql));
$values = array();
foreach ($querydata as $w) {
$values = array_pad($values, count($values) + 4, $w);
}
$sql .= 'WHERE
u.id <> 0 AND u.active = 1 AND u.deleted = 0
AND (' . $namesql . '
) )
' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . ' ' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . '
'; ';
$count = get_field_sql($sql, array($query_string, $query_string, $query_string, $query_string)); $count = get_field_sql($sql, $values);
if ($count > 0) { if ($count > 0) {
// @todo This is quite possibly not correct. See the postgres // @todo This is quite possibly not correct. See the postgres
// query. It should be DISTINCT ON the fields as specified by the // query. It should be DISTINCT ON the fields as specified by the
...@@ -242,26 +242,11 @@ class PluginSearchInternal extends PluginSearch { ...@@ -242,26 +242,11 @@ class PluginSearchInternal extends PluginSearch {
} }
$sql .= 'WHERE $sql .= 'WHERE
u.id <> 0 AND u.active = 1 AND u.deleted = 0 u.id <> 0 AND u.active = 1 AND u.deleted = 0
AND (( AND ( ' . $namesql . '
u.preferredname LIKE \'%\' || ? || \'%\'
)
OR (
(u.preferredname IS NULL OR u.preferredname = \'\')
AND (
u.firstname LIKE \'%\' || ? || \'%\'
OR u.lastname LIKE \'%\' || ? || \'%\'
)
)
OR (
a.artefacttype IN ' . $fieldlist . '
AND ( a.title LIKE \'%\' || ? || \'%\')
)
) )
' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . ' ' . (isset($data['exclude']) ? 'AND u.id != ' . $data['exclude'] : '') . '
ORDER BY u.firstname, u.lastname, u.id'; ORDER BY u.firstname, u.lastname, u.id';
$data = get_records_sql_array($sql, array($query_string, $query_string, $query_string, $query_string), $data = get_records_sql_array($sql, $values, $offset, $limit);
$offset,
$limit);
if ($data) { if ($data) {
foreach ($data as &$item) { foreach ($data as &$item) {
$item = (array)$item; $item = (array)$item;
......
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