Files
timebank-cc-public/app/Http/Livewire/WireChat/New/Group.php
T

59 lines
1.6 KiB
PHP

<?php
namespace App\Http\Livewire\WireChat\New;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Namu\WireChat\Facades\WireChat;
use Namu\WireChat\Livewire\New\Group as BaseGroup;
class Group extends BaseGroup
{
/**
* Add all linked users of the active Organization, Bank, or Admin profile
* to the selected members list, up to the group member limit.
*/
public function addLinkedMembers(): void
{
$activeProfile = getActiveProfile();
if (!$activeProfile) {
return;
}
\App\Helpers\ProfileAuthorizationHelper::authorize($activeProfile);
$activeProfileType = getActiveProfileType();
if (!in_array($activeProfileType, ['Organization', 'Bank', 'Admin'])) {
return;
}
$relationMethod = $activeProfileType === 'Bank' ? 'managers' : 'users';
$linkedUsers = $activeProfile->$relationMethod()
->select('users.id')
->get();
$maxMembers = WireChat::maxGroupMembers();
foreach ($linkedUsers as $linkedUser) {
// Stop if we've hit the group member limit
if (count($this->selectedMembers) >= $maxMembers) {
$this->dispatch('show-member-limit-error');
break;
}
$user = User::find($linkedUser->id);
if (!$user) {
continue;
}
// Skip if already selected
if ($this->selectedMembers->contains(fn ($m) => $m->id == $user->id && get_class($m) == get_class($user))) {
continue;
}
$this->selectedMembers->push($user);
}
}
}