Skip to content

Commit f45fc00

Browse files
committed
Allow searching for multiple values within a post attributes
Allow queries like `?values[key][]=value1&values[key][]=value2` rather than just `?values[key]=value1`
1 parent a3bf331 commit f45fc00

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

application/classes/Ushahidi/Repository/Post.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ protected function setSearchConditions(SearchData $search)
478478
{
479479
$attribute = $this->form_attribute_repo->getByKey($key);
480480

481+
if (!is_array($value)) {
482+
$value = explode(',', $value);
483+
}
484+
481485
$sub = $this->post_value_factory
482486
->getRepo($attribute->type)
483487
->getValueQuery($attribute->id, $value);

application/classes/Ushahidi/Repository/Post/Tags.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ protected function selectQuery(Array $where = [])
4949
}
5050

5151
// PostValueRepository
52-
public function getValueQuery($form_attribute_id, $match)
52+
public function getValueQuery($form_attribute_id, array $matches)
5353
{
54-
return $this->selectQuery(compact('form_attribute_id'))
55-
->where('tag_id', 'LIKE', "%$match%");
54+
$query = $this->selectQuery(compact('form_attribute_id'))
55+
->and_where_open();
56+
57+
foreach ($matches as $match) {
58+
$query->or_where('tag_id', 'LIKE', "%$match%");
59+
}
60+
61+
$query->and_where_close();
62+
63+
return $query;
5664
}
5765

5866
// UpdatePostValueRepository

application/classes/Ushahidi/Repository/Post/Value.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,18 @@ public function deleteAllForPost($post_id)
8383
}
8484

8585
// PostValueRepository
86-
public function getValueQuery($form_attribute_id, $match)
86+
public function getValueQuery($form_attribute_id, array $matches)
8787
{
88-
return $this->selectQuery(compact('form_attribute_id'))
89-
->where('value', 'LIKE', "%$match%");
88+
$query = $this->selectQuery(compact('form_attribute_id'))
89+
->and_where_open();
90+
91+
foreach ($matches as $match) {
92+
$query->or_where('value', 'LIKE', "%$match%");
93+
}
94+
95+
$query->and_where_close();
96+
97+
return $query;
9098
}
9199

92100
// PostValueRepository

src/Core/Entity/PostValueRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function get($id, $post_id = null, $form_attribute_id = null);
2828
* @param string $match
2929
* @return Database_Query
3030
*/
31-
public function getValueQuery($form_attribute_id, $match);
31+
public function getValueQuery($form_attribute_id, array $matches);
3232

3333
/**
3434
* Get the table name for use in joins

tests/integration/posts.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,20 @@ Feature: Testing the Posts API
11561156
And the "count" property equals "1"
11571157
Then the guzzle status code should be 200
11581158

1159+
@resetFixture @search
1160+
Scenario: Search All Posts by attribute
1161+
Given that I want to get all "Posts"
1162+
And that the request "query string" is:
1163+
"""
1164+
values[test_varchar][]=special&values[test_varchar][]=things
1165+
"""
1166+
When I request "/posts"
1167+
Then the response is JSON
1168+
And the response has a "count" property
1169+
And the type of the "count" property is "numeric"
1170+
And the "count" property equals "1"
1171+
Then the guzzle status code should be 200
1172+
11591173
@resetFixture @search
11601174
Scenario: Search All Posts by single tag
11611175
Given that I want to get all "Posts"

0 commit comments

Comments
 (0)