Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sort and group relationships by relationship type #4985

Merged
merged 7 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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