Skip to content

Commit

Permalink
fix: sort and group relationships by relationship type (#4985)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosh635 authored Mar 21, 2021
1 parent f058050 commit 105b74f
Show file tree
Hide file tree
Showing 11 changed files with 15,814 additions and 103 deletions.
Binary file added .rnd
Binary file not shown.
15 changes: 15 additions & 0 deletions app/Helpers/CollectionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ private static function valueRetriever($value)
return data_get($item, $value);
};
}

/**
* Group collection based on a specific property from its items.
*
* @param \Illuminate\Support\Collection $collection
* @param string $property
*
* @return mixed
*/
public static function groupByItemsProperty($collection, $property)
{
return $collection->mapToGroups(function ($item) use ($property) {
return [data_get($item, $property) => $item];
});
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,20 +259,36 @@ public function show(Contact $contact)
$relationships = $contact->relationships;
// get love relationship type
$loveRelationships = $relationships->filter(function ($item) {
$item->relationshipTypeLocalized = $item->relationshipType->getLocalizedName(null, false, $item->ofContact->gender->type ?? null);

return $item->relationshipType->relationshipTypeGroup->name == 'love';
});
$loveRelationships->sortByCollator('relationshipTypeLocalized');

// get family relationship type
$familyRelationships = $relationships->filter(function ($item) {
$item->relationshipTypeLocalized = $item->relationshipType->getLocalizedName(null, false, $item->ofContact->gender->type ?? null);

return $item->relationshipType->relationshipTypeGroup->name == 'family';
});
$familyRelationships->sortByCollator('relationshipTypeLocalized');

// get friend relationship type
$friendRelationships = $relationships->filter(function ($item) {
$item->relationshipTypeLocalized = $item->relationshipType->getLocalizedName(null, false, $item->ofContact->gender->type ?? null);

return $item->relationshipType->relationshipTypeGroup->name == 'friend';
});
$friendRelationships->sortByCollator('relationshipTypeLocalized');

// get work relationship type
$workRelationships = $relationships->filter(function ($item) {
$item->relationshipTypeLocalized = $item->relationshipType->getLocalizedName(null, false, $item->ofContact->gender->type ?? null);

return $item->relationshipType->relationshipTypeGroup->name == 'work';
});
$workRelationships->sortByCollator('relationshipTypeLocalized');

// reminders
$reminders = $contact->activeReminders;
$relevantRemindersFromRelatedContacts = $contact->getBirthdayRemindersAboutRelatedContacts();
Expand Down
9 changes: 9 additions & 0 deletions app/Providers/MacroServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@ public function boot()
return CollectionHelper::sortByCollator($collect, $callback, $options, $descending);
});
}

if (! Collection::hasMacro('groupByItemsProperty')) {
Collection::macro('groupByItemsProperty', function ($property) {
/** @var Collection */
$collect = $this;

return CollectionHelper::groupByItemsProperty($collect, $property);
});
}
}
}
Loading

0 comments on commit 105b74f

Please sign in to comment.