All checks were successful
rvr-nextgen/pipeline/pr-master This commit looks good
109 lines
4.4 KiB
PHP
109 lines
4.4 KiB
PHP
<?php namespace RVR\Repository;
|
|
|
|
use Container;
|
|
use DateTime;
|
|
use DateInterval;
|
|
use Generator;
|
|
use RVR\PersistentData\Model\Community;
|
|
use RVR\PersistentData\Model\Event;
|
|
use RVR\PersistentData\Model\User;
|
|
use SokoWeb\Database\Query\Select;
|
|
|
|
class EventRepository
|
|
{
|
|
public function getById(int $id): ?Event
|
|
{
|
|
return Container::$persistentDataManager->selectFromDbById($id, Event::class);
|
|
}
|
|
|
|
public function getBySlug(string $slug): ?Event
|
|
{
|
|
return \Container::$persistentDataManager->selectFromDbBySlug($slug, Event::class);
|
|
}
|
|
|
|
public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator
|
|
{
|
|
$select = $this->selectAllByCommunity($community);
|
|
|
|
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations);
|
|
}
|
|
|
|
public function countAllByCommunity(Community $community): int
|
|
{
|
|
return $this->selectAllByCommunity($community)->count();
|
|
}
|
|
|
|
public function getUpcomingAndRecentByCommunity(Community $community, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator
|
|
{
|
|
$select = $this->selectAllByCommunity($community);
|
|
$this->selectUpcomingAndRecent($select, $from, $days);
|
|
$select->orderBy('start', 'DESC');
|
|
$select->limit($limit, 0);
|
|
|
|
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations);
|
|
}
|
|
|
|
public function getUpcomingAndRecentByUser(User $user, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator
|
|
{
|
|
$select = $this->selectAllByUser($user);
|
|
$this->selectUpcomingAndRecent($select, $from, $days);
|
|
$select->orderBy('start', 'DESC');
|
|
$select->limit($limit, 0);
|
|
|
|
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations);
|
|
}
|
|
|
|
public function getCurrentByUser(User $user, DateTime $from, int $days, bool $useRelations = false, array $withRelations = []): ?Event
|
|
{
|
|
$select = $this->selectAllByUser($user);
|
|
$this->selectUpcomingAndRecent($select, $from, $days);
|
|
$select->orderBy('start', 'DESC');
|
|
$select->limit(1, 0);
|
|
|
|
return Container::$persistentDataManager->selectFromDb($select, Event::class, $useRelations, $withRelations);
|
|
}
|
|
|
|
public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator
|
|
{
|
|
$select = $this->selectAllByCommunity($community);
|
|
$select->orderBy('start', 'DESC');
|
|
$select->paginate($page, $itemsPerPage);
|
|
|
|
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations);
|
|
}
|
|
|
|
public function searchByTitle(Community $community, string $title): Generator
|
|
{
|
|
$select = $this->selectAllByCommunity($community);
|
|
$select->where('title', 'LIKE', '%' . $title . '%');
|
|
$select->orderBy('start', 'DESC');
|
|
$select->limit(10);
|
|
|
|
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class);
|
|
}
|
|
|
|
private function selectAllByCommunity(Community $community)
|
|
{
|
|
$select = new Select(Container::$dbConnection, Event::getTable());
|
|
$select->where('community_id', '=', $community->getId());
|
|
return $select;
|
|
}
|
|
|
|
private function selectAllByUser(User $user)
|
|
{
|
|
$select = new Select(Container::$dbConnection, Event::getTable());
|
|
$select->innerJoin('communities', ['communities', 'id'], '=', ['events', 'community_id']);
|
|
$select->innerJoin('community_members', ['communities', 'id'], '=', ['community_members', 'community_id']);
|
|
$select->where(['community_members', 'user_id'], '=', $user->getId());
|
|
return $select;
|
|
}
|
|
|
|
private function selectUpcomingAndRecent(Select $select, DateTime $from, int $days)
|
|
{
|
|
$select->where(function (Select $select) use ($from, $days) {
|
|
$select->where('start', '<', (clone $from)->add(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s'));
|
|
$select->orWhere('end', '>', (clone $from)->sub(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s'));
|
|
});
|
|
}
|
|
}
|