From 99bd8e17f8ac78af45937673a38a15c72f1e0278 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Tue, 22 Jun 2021 18:59:31 +0200 Subject: [PATCH] fix: fix tags list filtering (#5123) --- app/Http/Controllers/ContactsController.php | 64 +++++++-------------- app/Models/Contact/Tag.php | 2 +- resources/js/components/people/Tags.vue | 21 +++---- resources/views/people/index.blade.php | 2 +- tests/Feature/ContactTest.php | 33 ++++++++++- 5 files changed, 64 insertions(+), 58 deletions(-) diff --git a/app/Http/Controllers/ContactsController.php b/app/Http/Controllers/ContactsController.php index a8c422b04be..01a894037b6 100644 --- a/app/Http/Controllers/ContactsController.php +++ b/app/Http/Controllers/ContactsController.php @@ -86,31 +86,22 @@ private function contacts(Request $request, bool $active) $nbArchived = $contacts->count(); } + $tagsCount = Tag::contactsCount(); $tags = null; - $url = ''; + $url = null; $count = 1; - if ($request->input('tag1')) { + if ($request->input('tags')) { + $tagsInput = $request->input('tags'); - // get contacts with selected tags - $tags = collect(); + $tags = $tagsCount->filter(function ($tag) use ($tagsInput) { + return in_array($tag->name, $tagsInput); + }); - while ($request->input('tag'.$count)) { - $tag = Tag::where([ - 'account_id' => auth()->user()->account_id, - 'name_slug' => $request->input('tag'.$count), - ]); - if ($tag->count() > 0) { - $tag = $tag->get(); + $url = $tags->map(function ($tag) { + return 'tags[]='.urlencode($tag->name); + })->join('&'); - if (! $tags->contains($tag[0])) { - $tags = $tags->concat($tag); - } - - $url .= 'tag'.$count.'='.$tag[0]->name_slug.'&'; - } - $count++; - } if ($tags->count() === 0) { return redirect()->route('people.index'); } else { @@ -131,14 +122,14 @@ private function contacts(Request $request, bool $active) return view('people.index') ->withAccountHasLimitations($accountHasLimitations) - ->with('hidingDeceased', $showDeceased != 'true') - ->with('deceasedCount', $deceasedCount) + ->withHidingDeceased($showDeceased !== 'true') + ->withDeceasedCount($deceasedCount) ->withActive($active) ->withContactsCount($contactsCount) ->withHasArchived($nbArchived > 0) ->withArchivedContacts($nbArchived) ->withTags($tags) - ->withTagsCount(Tag::contactsCount()) + ->withTagsCount($tagsCount) ->withUrl($url) ->withTagCount($count) ->withTagLess($request->input('no_tag') ?? false); @@ -730,8 +721,6 @@ public function list(Request $request) } $tags = null; - $url = ''; - $count = 1; $contacts = $user->account->contacts()->real(); @@ -747,30 +736,17 @@ public function list(Request $request) $contacts = $contacts->alive(); } - if ($request->input('no_tag')) { - // get tag less contacts - $contacts = $contacts->tags('NONE'); - } elseif ($request->input('tag1')) { - // get contacts with selected tags - $tags = collect(); - - while ($request->input('tag'.$count)) { - $tag = Tag::where([ - 'account_id' => $accountId, - 'name_slug' => $request->input('tag'.$count), - ])->get(); - - if (! ($tags->contains($tag[0]))) { - $tags = $tags->concat($tag); - } - - $url = $url.'tag'.$count.'='.$tag[0]->name_slug.'&'; + if ($request->input('tags')) { + $tags = Tag::where('account_id', $accountId) + ->whereIn('name', $request->input('tags')) + ->get(); - $count++; - } if ($tags->count() > 0) { $contacts = $contacts->tags($tags); } + } elseif ($request->input('no_tag')) { + // get tag less contacts + $contacts = $contacts->tags('NONE'); } // get the number of contacts per page diff --git a/app/Models/Contact/Tag.php b/app/Models/Contact/Tag.php index ef91dfa12d9..9e5b5d6857d 100644 --- a/app/Models/Contact/Tag.php +++ b/app/Models/Contact/Tag.php @@ -47,7 +47,7 @@ public function contacts() */ public static function contactsCount() { - return DB::table('contact_tag')->selectRaw('COUNT(tag_id) AS contact_count, name, name_slug') + return DB::table('contact_tag')->selectRaw('COUNT(tag_id) AS contact_count, name, tag_id AS id') ->join('tags', function ($join) { $join->on('tags.id', '=', 'contact_tag.tag_id') ->on('tags.account_id', '=', 'contact_tag.account_id'); diff --git a/resources/js/components/people/Tags.vue b/resources/js/components/people/Tags.vue index 17a2c6d09a4..30f53411a8b 100644 --- a/resources/js/components/people/Tags.vue +++ b/resources/js/components/people/Tags.vue @@ -8,6 +8,11 @@ background-color: #4AAE9B; color: white; } + +.tag-link, +.tag-link:hover{ + text-decoration: none; +}