Commit a6348332 authored by Clare Lenihan's avatar Clare Lenihan Committed by Clare Lenihan
Browse files

modified search_group to order the groups (by group name) and allow to filter...

modified search_group to order the groups (by group name) and allow to filter search to groups the user isn't in
parent 0671237d
...@@ -46,7 +46,7 @@ $offset = param_integer('offset', 0); ...@@ -46,7 +46,7 @@ $offset = param_integer('offset', 0);
switch($type) { switch($type) {
case 'group': case 'group':
$data = search_group($query, $limit, $offset, true); $data = search_group($query, $limit, $offset, 'all');
$data['type'] = 'group'; $data['type'] = 'group';
break; break;
default: default:
......
...@@ -231,11 +231,11 @@ function admin_user_search($queries, $constraints, $offset, $limit, $sortfield, ...@@ -231,11 +231,11 @@ function admin_user_search($queries, $constraints, $offset, $limit, $sortfield,
* ), * ),
* ); * );
*/ */
function search_group($query_string, $limit, $offset = 0, $all = false) { function search_group($query_string, $limit, $offset = 0, $type = 'member') {
$plugin = get_config('searchplugin'); $plugin = get_config('searchplugin');
safe_require('search', $plugin); safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'search_group', $query_string, $limit, $offset, $all); return call_static_method(generate_class_name('search', $plugin), 'search_group', $query_string, $limit, $offset, $type);
} }
function search_selfsearch($query_string, $limit, $offset, $type = 'all') { function search_selfsearch($query_string, $limit, $offset, $type = 'all') {
......
...@@ -307,6 +307,7 @@ class PluginSearchInternal extends PluginSearch { ...@@ -307,6 +307,7 @@ class PluginSearchInternal extends PluginSearch {
* @param string The query string * @param string The query string
* @param integer How many results to return * @param integer How many results to return
* @param integer What result to start at (0 == first result) * @param integer What result to start at (0 == first result)
* @param string Which groups to search (all, member, notmember)
* @return array A data structure containing results looking like ... * @return array A data structure containing results looking like ...
* $results = array( * $results = array(
* count => integer, // total number of results * count => integer, // total number of results
...@@ -335,19 +336,19 @@ class PluginSearchInternal extends PluginSearch { ...@@ -335,19 +336,19 @@ class PluginSearchInternal extends PluginSearch {
* ), * ),
* ); * );
*/ */
public static function search_group($query_string, $limit, $offset=0, $all=false) { public static function search_group($query_string, $limit, $offset=0, $type='member') {
if (is_postgres()) { if (is_postgres()) {
return self::search_group_pg($query_string, $limit, $offset, $all); return self::search_group_pg($query_string, $limit, $offset, $type);
} }
else if (is_mysql()) { else if (is_mysql()) {
return self::search_group_my($query_string, $limit, $offset, $all); return self::search_group_my($query_string, $limit, $offset, $type);
} }
else { else {
throw new SQLException('search_group() is not implemented for your database engine (' . get_config('dbtype') . ')'); throw new SQLException('search_group() is not implemented for your database engine (' . get_config('dbtype') . ')');
} }
} }
public static function search_group_pg($query_string, $limit, $offset, $all) { public static function search_group_pg($query_string, $limit, $offset, $type) {
global $USER; global $USER;
$sql = " $sql = "
SELECT SELECT
...@@ -359,8 +360,8 @@ class PluginSearchInternal extends PluginSearch { ...@@ -359,8 +360,8 @@ class PluginSearchInternal extends PluginSearch {
OR description ILIKE '%' || ? || '%' OR description ILIKE '%' || ? || '%'
)"; )";
$values = array($query_string, $query_string); $values = array($query_string, $query_string);
if (!$all) { if ($type == 'member') {
$sql .= 'AND ( $sql .= 'AND (
owner = ? OR id IN ( owner = ? OR id IN (
SELECT "group" FROM {group_member} WHERE member = ? SELECT "group" FROM {group_member} WHERE member = ?
) )
...@@ -368,6 +369,16 @@ class PluginSearchInternal extends PluginSearch { ...@@ -368,6 +369,16 @@ class PluginSearchInternal extends PluginSearch {
$values[] = $USER->get('id'); $values[] = $USER->get('id');
$values[] = $USER->get('id'); $values[] = $USER->get('id');
} }
else if ($type == 'notmember') {
$sql .= 'AND (
owner != ? AND id NOT IN (
SELECT "group" FROM {group_member} WHERE member = ?
)
)';
$values[] = $USER->get('id');
$values[] = $USER->get('id');
}
$sql .= 'ORDER BY name';
$data = get_records_sql_array($sql, $values, $offset, $limit); $data = get_records_sql_array($sql, $values, $offset, $limit);
$sql = " $sql = "
...@@ -379,14 +390,22 @@ class PluginSearchInternal extends PluginSearch { ...@@ -379,14 +390,22 @@ class PluginSearchInternal extends PluginSearch {
name ILIKE '%' || ? || '%' name ILIKE '%' || ? || '%'
OR description ILIKE '%' || ? || '%' OR description ILIKE '%' || ? || '%'
)"; )";
if (!$all) { if ($type == 'member') {
$sql .= 'AND ( $sql .= 'AND (
owner = ? OR id IN ( owner = ? OR id IN (
SELECT "group" FROM {group_member} WHERE member = ? SELECT "group" FROM {group_member} WHERE member = ?
) )
) )
'; ';
} }
else if ($type == 'notmember') {
$sql .= 'AND (
owner != ? AND id NOT IN (
SELECT "group" FROM {group_member} WHERE member = ?
)
)
';
}
$count = get_field_sql($sql, $values); $count = get_field_sql($sql, $values);
return array( return array(
...@@ -397,7 +416,7 @@ class PluginSearchInternal extends PluginSearch { ...@@ -397,7 +416,7 @@ class PluginSearchInternal extends PluginSearch {
); );
} }
public static function search_group_my($query_string, $limit, $offset, $all) { public static function search_group_my($query_string, $limit, $offset, $type) {
global $USER; global $USER;
$sql = " $sql = "
SELECT SELECT
...@@ -409,7 +428,7 @@ class PluginSearchInternal extends PluginSearch { ...@@ -409,7 +428,7 @@ class PluginSearchInternal extends PluginSearch {
OR description LIKE '%' || ? || '%' OR description LIKE '%' || ? || '%'
)"; )";
$values = array($query_string, $query_string); $values = array($query_string, $query_string);
if (!$all) { if ($type == 'member') {
$sql .= "AND ( $sql .= "AND (
owner = ? OR id IN ( owner = ? OR id IN (
SELECT group FROM {group_member} WHERE member = ? SELECT group FROM {group_member} WHERE member = ?
...@@ -418,6 +437,16 @@ class PluginSearchInternal extends PluginSearch { ...@@ -418,6 +437,16 @@ class PluginSearchInternal extends PluginSearch {
$values[] = $USER->get('id'); $values[] = $USER->get('id');
$values[] = $USER->get('id'); $values[] = $USER->get('id');
} }
else if ($type == 'notmember') {
$sql .= "AND (
owner != ? AND id NOT IN (
SELECT group FROM {group_member} WHERE member = ?
)
)";
$values[] = $USER->get('id');
$values[] = $USER->get('id');
}
$sql .= 'ORDER BY name';
$data = get_records_sql_array($sql, $values, $offset, $limit); $data = get_records_sql_array($sql, $values, $offset, $limit);
$sql = " $sql = "
...@@ -429,14 +458,22 @@ class PluginSearchInternal extends PluginSearch { ...@@ -429,14 +458,22 @@ class PluginSearchInternal extends PluginSearch {
name LIKE '%' || ? || '%' name LIKE '%' || ? || '%'
OR description LIKE '%' || ? || '%' OR description LIKE '%' || ? || '%'
)"; )";
if (!$all) { if ($type == 'member') {
$sql .= "AND ( $sql .= "AND (
owner = ? OR id IN ( owner = ? OR id IN (
SELECT group FROM {group_member} WHERE member = ? SELECT group FROM {group_member} WHERE member = ?
) )
) )
"; ";
} }
else if ($type == 'notmember') {
$sql .= "AND (
owner != ? AND id NOT IN (
SELECT group FROM {group_member} WHERE member = ?
)
)
";
}
$count = get_field_sql($sql, $values); $count = get_field_sql($sql, $values);
return array( return array(
......
...@@ -76,8 +76,7 @@ abstract class PluginSearch extends Plugin { ...@@ -76,8 +76,7 @@ abstract class PluginSearch extends Plugin {
* @param string The query string * @param string The query string
* @param integer How many results to return * @param integer How many results to return
* @param integer What result to start at (0 == first result) * @param integer What result to start at (0 == first result)
* @param boolean Return all matching groups, or just ones this user * @param string Which groups to search (all, member, notmember)
* is a member of?
* @return array A data structure containing results looking like ... * @return array A data structure containing results looking like ...
* $results = array( * $results = array(
* count => integer, // total number of results * count => integer, // total number of results
...@@ -106,7 +105,7 @@ abstract class PluginSearch extends Plugin { ...@@ -106,7 +105,7 @@ abstract class PluginSearch extends Plugin {
* ), * ),
* ); * );
*/ */
public static abstract function search_group($query_string, $limit, $offset=0, $all=false); public static abstract function search_group($query_string, $limit, $offset=0, $type='member');
/** /**
* Given a query string and limits, return an array of matching objects * Given a query string and limits, return an array of matching objects
......
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