Skip to content

Commit

Permalink
Post repo: drop empty search params
Browse files Browse the repository at this point in the history
Otherwise we end with things like:
`WHERE user.id IN ('')` and no results
  • Loading branch information
rjmackay committed Jun 14, 2016
1 parent 26a36cb commit e77af5d
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions application/classes/Ushahidi/Repository/Post.php
Expand Up @@ -224,20 +224,13 @@ protected function setSearchConditions(SearchData $search)

foreach (['user', 'parent', 'form'] as $key)
{
if (isset($search->$key))
if (!empty($search->$key))
{
// Make sure we have an array
if (!is_array($search->$key)) {
$search->$key = explode(',', $search->$key);
}

// If filter is empty, skip it
// This avoid where clauses like:
// `posts`.`form_id` IN ()
if (empty($search->$key)) {
continue;
}

// Special case: 'none' looks for null
if (in_array('none', $search->$key)) {
$query->and_where_open()
Expand Down Expand Up @@ -380,24 +373,24 @@ protected function setSearchConditions(SearchData $search)
// and a check that the current post has not already completed this stage
->on('form_stages.form_id', 'IN', $forms_sub)
->on('form_stages.id', 'NOT IN', $stages_posts)
// We group the results by post id
->group_by('p_id')
// We reduce the list to ensure that only results missing the stages to filter by are returned
->having('form_stages.id', 'IN', $stages)
// We group the results by post id
->group_by('p_id')
// We reduce the list to ensure that only results missing the stages to filter by are returned
->having('form_stages.id', 'IN', $stages)
// Finally we order the results by priority to ensure that if, for example,
// a post is missing multiple stages we only consider the first uncompleted stage
->order_by('priority');

//This step wraps the query and returns only the posts ids without the extra data such as form, stage or priority
$posts_sub = DB::select('p_id')
->from(array($sub, 'sub'));
$posts_sub = DB::select('p_id')
->from(array($sub, 'sub'));

$query
->where('posts.id', 'IN', $posts_sub);
}

// Filter by tag
if ($search->tags)
if (!empty($search->tags))
{
if (isset($search->tags['any']))
{
Expand Down Expand Up @@ -441,11 +434,11 @@ protected function setSearchConditions(SearchData $search)
}

// Filter by set
if ($search->set)
if (!empty($search->set))
{
$set = $search->set;
$set = $search->set;
if (!is_array($set)) {
$set = explode(',', $set);
$set = explode(',', $set);
}

$query
Expand Down

0 comments on commit e77af5d

Please sign in to comment.