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

Trek logica uit AgendaRepository #1157

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions lib/controller/AgendaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CsrDelft\repository\groepen\ActiviteitenRepository;
use CsrDelft\repository\maalcie\MaaltijdenRepository;
use CsrDelft\repository\ProfielRepository;
use CsrDelft\service\AgendaService;
use CsrDelft\view\agenda\AgendaItemForm;
use CsrDelft\view\bbcode\BbToProsemirror;
use CsrDelft\view\Icon;
Expand All @@ -39,6 +40,10 @@
class AgendaController extends AbstractController
{
const SECONDEN_IN_JAAR = 31557600;
/**
* @var AgendaService
*/
private $agendaService;
/**
* @var AgendaRepository
*/
Expand All @@ -65,13 +70,15 @@ class AgendaController extends AbstractController
private $profielRepository;

public function __construct(
AgendaService $agendaService,
AgendaRepository $agendaRepository,
AgendaVerbergenRepository $agendaVerbergenRepository,
ActiviteitenRepository $activiteitenRepository,
CorveeTakenRepository $corveeTakenRepository,
MaaltijdenRepository $maaltijdenRepository,
ProfielRepository $profielRepository
) {
$this->agendaService = $agendaService;
$this->agendaRepository = $agendaRepository;
$this->agendaVerbergenRepository = $agendaVerbergenRepository;
$this->activiteitenRepository = $activiteitenRepository;
Expand Down Expand Up @@ -122,7 +129,7 @@ public function ical(): Response
return $this->render(
'agenda/icalendar.ical.twig',
[
'items' => $this->agendaRepository->getICalendarItems(),
'items' => $this->agendaService->getICalendarItems(),
'published' => $this->icalDate(),
],
new IcalResponse()
Expand Down Expand Up @@ -204,7 +211,7 @@ public function zoeken(Request $request, $zoekterm = null): JsonResponse
*/
public function courant(BbToProsemirror $bbToProsemirror)
{
$items = $this->agendaRepository->getAllAgendeerbaar(
$items = $this->agendaService->getAllAgendeerbaar(
date_create_immutable(),
date_create_immutable('next saturday + 2 weeks'),
false,
Expand Down Expand Up @@ -360,7 +367,7 @@ public function verbergen($refuuid = null): JsonResponse
if (!$item) {
throw $this->createAccessDeniedException();
}
$this->agendaVerbergenRepository->toggleVerbergen($item);
$this->agendaVerbergenRepository->toggleVerbergen($this->getUid(), $item);
return new JsonResponse(true);
}

Expand Down Expand Up @@ -421,7 +428,7 @@ public function feed(Request $request): JsonResponse
);
}

$events = $this->agendaRepository->getAllAgendeerbaar(
$events = $this->agendaService->getAllAgendeerbaar(
$startMoment,
$eindMoment
);
Expand Down Expand Up @@ -491,7 +498,7 @@ public function details($uuid): Response

return $this->render('agenda/details.html.twig', [
'item' => $item,
'verborgen' => $this->agendaVerbergenRepository->isVerborgen($item),
'verborgen' => $this->agendaVerbergenRepository->isVerborgen($this->getUid(), $item),
]);
}

Expand Down
11 changes: 6 additions & 5 deletions lib/controller/VoorpaginaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use CsrDelft\repository\instellingen\LidInstellingenRepository;
use CsrDelft\repository\maalcie\MaaltijdenRepository;
use CsrDelft\repository\WoordVanDeDagRepository;
use CsrDelft\service\AgendaService;
use CsrDelft\service\forum\ForumDelenService;
use CsrDelft\service\maalcie\MaaltijdenService;
use CsrDelft\service\security\LoginService;
Expand Down Expand Up @@ -41,16 +42,16 @@ public function forum(
}

/**
* @param AgendaRepository $agendaRepository
* @param AgendaService $agendaService
* @return Response
* @Route("/voorpagina/agenda")
*/
public function agenda(AgendaRepository $agendaRepository): Response
public function agenda(AgendaService $agendaService): Response
{
// Agenda
if (LoginService::mag(P_AGENDA_READ)) {
$aantalWeken = InstellingUtil::lid_instelling('zijbalk', 'agendaweken');
$items = $agendaRepository->getAllAgendeerbaar(
$items = $agendaService->getAllAgendeerbaar(
date_create_immutable(),
date_create_immutable('next saturday + ' . $aantalWeken . ' weeks'),
false,
Expand Down Expand Up @@ -171,13 +172,13 @@ public function civisaldo(): Response
public function ishetal(
LidInstellingenRepository $lidInstellingenRepository,
RequestStack $requestStack,
AgendaRepository $agendaRepository,
AgendaService $agendaService,
WoordVanDeDagRepository $woordVanDeDagRepository
): Response {
$isHetAlView = new IsHetAlView(
$lidInstellingenRepository,
$requestStack,
$agendaRepository,
$agendaService,
$woordVanDeDagRepository,
InstellingUtil::lid_instelling('zijbalk', 'ishetal')
);
Expand Down
197 changes: 4 additions & 193 deletions lib/repository/agenda/AgendaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use CsrDelft\entity\agenda\AgendaVerbergen;
use CsrDelft\entity\agenda\Agendeerbaar;
use CsrDelft\entity\groepen\Activiteit;
use CsrDelft\entity\security\enum\AccessAction;
use CsrDelft\entity\security\enum\AuthenticationMethod;
use CsrDelft\repository\AbstractRepository;
use CsrDelft\repository\corvee\CorveeTakenRepository;
Expand All @@ -34,48 +33,18 @@
*/
class AgendaRepository extends AbstractRepository
{
/**
* @var AgendaVerbergenRepository
*/
private $agendaVerbergenRepository;
/**
* @var ActiviteitenRepository
*/
private $activiteitenRepository;
/**
* @var CorveeTakenRepository
*/
private $corveeTakenRepository;
/**
* @var VerjaardagenService
*/
private $verjaardagenService;
/**
* @var Security
*/
private $security;
/**
* @var MaaltijdenService
*/
private $maaltijdenService;

public function __construct(
ManagerRegistry $registry,
Security $security,
AgendaVerbergenRepository $agendaVerbergenRepository,
ActiviteitenRepository $activiteitenRepository,
CorveeTakenRepository $corveeTakenRepository,
MaaltijdenService $maaltijdenService,
VerjaardagenService $verjaardagenService
) {
parent::__construct($registry, AgendaItem::class);

$this->agendaVerbergenRepository = $agendaVerbergenRepository;
$this->activiteitenRepository = $activiteitenRepository;
$this->corveeTakenRepository = $corveeTakenRepository;
$this->verjaardagenService = $verjaardagenService;
$this->security = $security;
$this->maaltijdenService = $maaltijdenService;
}

/**
Expand Down Expand Up @@ -108,44 +77,6 @@ public function getAgendaItem($itemId)
return $this->find($itemId);
}

public function getICalendarItems()
{
return $this->filterVerborgen(
$this->getAllAgendeerbaar(
date_create_immutable(
InstellingUtil::instelling('agenda', 'ical_from')
),
date_create_immutable(InstellingUtil::instelling('agenda', 'ical_to')),
true
)
);
}

public function filterVerborgen(array $items)
{
// Items verbergen
$itemsByUUID = [];
foreach ($items as $index => $item) {
$itemsByUUID[$item->getUUID()] = $item;
unset($items[$index]);
}
if (!empty($itemsByUUID)) {
/** @var AgendaVerbergen[] $verborgen */
$verborgen = $this->agendaVerbergenRepository
->createQueryBuilder('av')
->where('av.uid = :uid and av.refuuid in (:uuids)')
->setParameter('uid', LoginService::getUid())
->setParameter('uuids', array_keys($itemsByUUID))
->getQuery()
->getResult();

foreach ($verborgen as $verbergen) {
unset($itemsByUUID[$verbergen->refuuid]);
}
}
return $itemsByUUID;
}

/**
* @param DateTimeImmutable $van
* @param DateTimeImmutable $tot
Expand Down Expand Up @@ -175,23 +106,11 @@ public function zoeken(
}

/**
* @param DateTimeImmutable $van
* @param DateTimeImmutable $tot
* @param bool $ical
* @param bool $zijbalk
* @return Agendeerbaar[]
* @return AgendaItem[]
*/
public function getAllAgendeerbaar(
DateTimeImmutable $van,
DateTimeImmutable $tot,
$ical = false,
$zijbalk = false
) {
$result = [];

// AgendaItems
/** @var AgendaItem[] $items */
$items = $this->createQueryBuilder('a')
public function getAgendaItems(DateTimeImmutable $van, DateTimeImmutable $tot)
{
return $this->createQueryBuilder('a')
->where(
'a.begin_moment >= :begin_moment and a.begin_moment < :eind_moment'
)
Expand All @@ -206,114 +125,6 @@ public function getAllAgendeerbaar(
)
->getQuery()
->getResult();
foreach ($items as $item) {
if ($item->magBekijken($ical)) {
$result[] = $item;
}
}

$auth = $ical ? AuthenticationMethod::getEnumValues() : null;

// Activiteiten
/** @var Activiteit[] $activiteiten */
$activiteiten = $this->activiteitenRepository->getGroepenVoorAgenda(
$van,
$tot
);
foreach ($activiteiten as $activiteit) {
if (
$this->security->isGranted(AbstractGroepVoter::BEKIJKEN, $activiteit)
) {
$result[] = $activiteit;
}
}

// Maaltijden
if (InstellingUtil::lid_instelling('agenda', 'toonMaaltijden') === 'ja') {
// TODO: Dit moet altijd aanstaan
$result = array_merge(
$result,
$this->maaltijdenService->getMaaltijdenVoorAgenda(
$van->getTimestamp(),
$tot->getTimestamp()
)
);
}

// CorveeTaken
if (InstellingUtil::lid_instelling('agenda', 'toonCorvee') === 'iedereen') {
$result = array_merge(
$result,
$this->corveeTakenRepository->getTakenVoorAgenda($van, $tot, true)
);
} elseif (
InstellingUtil::lid_instelling('agenda', 'toonCorvee') === 'eigen'
) {
$result = array_merge(
$result,
$this->corveeTakenRepository->getTakenVoorAgenda($van, $tot, false)
);
}

// Verjaardagen
$toonVerjaardagen = $ical ? 'toonVerjaardagenICal' : 'toonVerjaardagen';
if (
!$zijbalk &&
LoginService::mag(P_VERJAARDAGEN, $auth) &&
InstellingUtil::lid_instelling('agenda', $toonVerjaardagen) === 'ja'
) {
//Verjaardagen. Omdat Lid-objectjes eigenlijk niet Agendeerbaar, maar meer iets als
//PeriodiekAgendeerbaar zijn, maar we geen zin hebben om dat te implementeren,
//doen we hier even een vieze hack waardoor het wel soort van werkt.
$GLOBALS['agenda_van'] = $van;
$GLOBALS['agenda_tot'] = $tot;

$result = array_merge(
$result,
$this->verjaardagenService->getTussen($van, $tot)
);
}

// Sorteren
usort($result, [AgendaRepository::class, 'vergelijkAgendeerbaars']);

return $result;
}

/**
* Zoek in de activiteiten (titel en beschrijving) van vandaag
* naar het woord $woord, geef de eerste terug.
* @param $woord string
* @return Agendeerbaar|null
*/
public function zoekWoordAgenda($woord)
{
return $this->zoekRegexAgenda('/' . preg_quote($woord, '/') . '/iu');
}

/**
* Vind de eerste activiteit van vandaag waarvan de
* titel of omschrijving wordt gematcht door $patroon.
* @param $patroon string
* @return Agendeerbaar|null
*/
public function zoekRegexAgenda($patroon)
{
$beginDag = date_create_immutable('today');
foreach ($this->getItemsByDay($beginDag) as $item) {
if (
preg_match($patroon, $item->getTitel()) ||
preg_match($patroon, $item->getBeschrijving())
) {
return $item;
}
}
return null;
}

public function getItemsByDay(DateTimeImmutable $dag)
{
return $this->getAllAgendeerbaar($dag, $dag);
}

public function nieuw($beginMoment, $eindMoment)
Expand Down
Loading
Loading