diff --git a/application/classes/Ushahidi/Repository/Post.php b/application/classes/Ushahidi/Repository/Post.php index 8d1d14be9c..7ac889f3e3 100644 --- a/application/classes/Ushahidi/Repository/Post.php +++ b/application/classes/Ushahidi/Repository/Post.php @@ -898,51 +898,62 @@ protected function updatePostValues($post_id, $attributes) protected function updatePostTags($post_id, $tags) { - // Load existing tags - $existing = $this->getTagsForPost($post_id); + // deletes all tags if $tags is empty + if (empty($tags)) + { + DB::delete('posts_tags') + ->where('post_id', '=', $post_id) + ->execute($this->db); + } + else + { + // Load existing tags + $existing = $this->getTagsForPost($post_id); - $insert = DB::insert('posts_tags', ['post_id', 'tag_id']); + $insert = DB::insert('posts_tags', ['post_id', 'tag_id']); - $tag_ids = []; - $new_tags = FALSE; - foreach ($tags as $tag) - { - if (is_array($tag)) { - $tag = $tag['id']; - } + $tag_ids = []; + $new_tags = FALSE; - // Find the tag by id or name - // @todo this should happen before we even get here - $tag_entity = $this->tag_repo->getByTag($tag); - if (! $tag_entity->id) + foreach ($tags as $tag) { - $tag_entity = $this->tag_repo->get($tag); + if (is_array($tag)) { + $tag = $tag['id']; + } + + // Find the tag by id or name + // @todo this should happen before we even get here + $tag_entity = $this->tag_repo->getByTag($tag); + if (! $tag_entity->id) + { + $tag_entity = $this->tag_repo->get($tag); + } + + // Does the post already have this tag? + if (! in_array($tag_entity->id, $existing)) + { + // Add to insert query + $insert->values([$post_id, $tag_entity->id]); + $new_tags = TRUE; + } + + $tag_ids[] = $tag_entity->id; } - // Does the post already have this tag? - if (! in_array($tag_entity->id, $existing)) + // Save + if ($new_tags) { - // Add to insert query - $insert->values([$post_id, $tag_entity->id]); - $new_tags = TRUE; + $insert->execute($this->db); } - $tag_ids[] = $tag_entity->id; - } - - // Save - if ($new_tags) - { - $insert->execute($this->db); - } - - // Remove any other tags - if (! empty($tag_ids)) - { - DB::delete('posts_tags') - ->where('tag_id', 'NOT IN', $tag_ids) - ->where('post_id', '=', '$post_id') - ->execute($this->db); + // Remove any other tags + if (! empty($tag_ids)) + { + DB::delete('posts_tags') + ->where('tag_id', 'NOT IN', $tag_ids) + ->and_where('post_id', '=', $post_id) + ->execute($this->db); + } } } diff --git a/application/tests/features/api.posts.feature b/application/tests/features/api.posts.feature index defef66f6a..a2262003a4 100644 --- a/application/tests/features/api.posts.feature +++ b/application/tests/features/api.posts.feature @@ -462,6 +462,92 @@ Feature: Testing the Posts API And the "values.last_location_point.0.lon" property equals "-85.39" Then the guzzle status code should be 200 + @update + Scenario: Updating a Post to update tags + Given that I want to update a "Post" + And that the request "data" is: + """ + { + "form":1, + "title":"Updated Test Post", + "type":"report", + "status":"published", + "locale":"en_US", + "values": + { + "full_name":["David Kobia"], + "description":["Skinny, homeless Kenyan last seen in the vicinity of the greyhound station"], + "date_of_birth":[], + "missing_date":["2012/09/25"], + "last_location":["atlanta"], + "last_location_point":[ + { + "lat": 33.755, + "lon": -85.39 + } + ], + "missing_status":["believed_missing"] + }, + "tags":["disaster"], + "completed_stages":[1] + } + """ + And that its "id" is "1" + When I request "/posts" + Then the response is JSON + And the response has a "id" property + And the type of the "id" property is "numeric" + And the "id" property equals "1" + And the response has a "tags.0.id" property + And the response does not have a "tags.1.id" property + And the response has a "title" property + And the "title" property equals "Updated Test Post" + And the "values.last_location_point.0.lon" property equals "-85.39" + Then the guzzle status code should be 200 + + @update + Scenario: Updating a Post to remove all tags + Given that I want to update a "Post" + And that the request "data" is: + """ + { + "form":1, + "title":"Updated Test Post", + "type":"report", + "status":"published", + "locale":"en_US", + "values": + { + "full_name":["David Kobia"], + "description":["Skinny, homeless Kenyan last seen in the vicinity of the greyhound station"], + "date_of_birth":[], + "missing_date":["2012/09/25"], + "last_location":["atlanta"], + "last_location_point":[ + { + "lat": 33.755, + "lon": -85.39 + } + ], + "missing_status":["believed_missing"] + }, + "tags":[], + "completed_stages":[1] + } + """ + And that its "id" is "1" + When I request "/posts" + Then the response is JSON + And the response has a "id" property + And the type of the "id" property is "numeric" + And the "id" property equals "1" + And the response has a "tags" property + And the "tags" property is empty + And the response has a "title" property + And the "title" property equals "Updated Test Post" + And the "values.last_location_point.0.lon" property equals "-85.39" + Then the guzzle status code should be 200 + @update Scenario: Updating a Post using JSON date formate Given that I want to update a "Post"