57 lines
1.6 KiB
PHP
57 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Enumerations\UserRole;
|
|
use App\Models\CostUnit;
|
|
use App\Models\Event;
|
|
use App\Resources\CostUnitResource;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class EventRepository {
|
|
public function listAll() : array {
|
|
return $this->getEventsByCriteria([
|
|
'archived' => false
|
|
]);
|
|
|
|
}
|
|
|
|
public function getById(int $id, bool $accessCheck = true) : ?Event {
|
|
$events = self::getEventsByCriteria(['id' => $id], $accessCheck);
|
|
return $events[0] ?? null;
|
|
}
|
|
|
|
public function getEventsByCriteria(array $criteria, $accessCheck = true) : array {
|
|
$tenant = app('tenant');
|
|
|
|
$canSeeAll = false;
|
|
$user = Auth()->user();
|
|
|
|
if ($tenant->slug !== 'lv') {
|
|
if (
|
|
$user->user_role_main === UserRole::USER_ROLE_ADMIN ||
|
|
in_array( $user->user_role_local_group, [UserRole::USER_ROLE_GROUP_LEADER, UserRole::USER_ROLE_ADMIN])
|
|
) {
|
|
$canSeeAll = true;
|
|
}
|
|
} else {
|
|
if (
|
|
in_array( $user->user_role_main, [UserRole::USER_ROLE_GROUP_LEADER, UserRole::USER_ROLE_ADMIN])
|
|
) {
|
|
$canSeeAll = true;
|
|
}
|
|
}
|
|
|
|
$visibleEvents = [];
|
|
/** @var Event $event */
|
|
foreach (Event::where($criteria)->get() as $event) {
|
|
|
|
if ($event->eventManagers()->where('user_id', $user->id)->exists() || $canSeeAll || !$accessCheck) {
|
|
$visibleEvents[] = $event;
|
|
}
|
|
}
|
|
|
|
return $visibleEvents;
|
|
}
|
|
}
|