RVRNEXT-11 add controller for events
This commit is contained in:
parent
2fc5812eba
commit
7f052d3a0c
181
src/Controller/EventController.php
Normal file
181
src/Controller/EventController.php
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
<?php namespace RVR\Controller;
|
||||||
|
|
||||||
|
use Container;
|
||||||
|
use DateTime;
|
||||||
|
use RVR\Finance\ExchangeRateCalculator;
|
||||||
|
use RVR\PersistentData\Model\Community;
|
||||||
|
use RVR\PersistentData\Model\CommunityMember;
|
||||||
|
use RVR\PersistentData\Model\Event;
|
||||||
|
use RVR\PersistentData\Model\User;
|
||||||
|
use RVR\Repository\CommunityMemberRepository;
|
||||||
|
use RVR\Repository\CommunityRepository;
|
||||||
|
use RVR\Repository\EventRepository;
|
||||||
|
use RVR\Repository\TransactionRepository;
|
||||||
|
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||||
|
use SokoWeb\Interfaces\Authorization\ISecured;
|
||||||
|
use SokoWeb\Interfaces\Response\IContent;
|
||||||
|
use SokoWeb\Response\HtmlContent;
|
||||||
|
use SokoWeb\Response\JsonContent;
|
||||||
|
|
||||||
|
class EventController implements IAuthenticationRequired, ISecured
|
||||||
|
{
|
||||||
|
private CommunityRepository $communityRepository;
|
||||||
|
|
||||||
|
private CommunityMemberRepository $communityMemberRepository;
|
||||||
|
|
||||||
|
private EventRepository $eventRepository;
|
||||||
|
|
||||||
|
private TransactionRepository $transactionRepository;
|
||||||
|
|
||||||
|
private ?Community $community;
|
||||||
|
|
||||||
|
private ?CommunityMember $ownCommunityMember;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->communityRepository = new CommunityRepository();
|
||||||
|
$this->communityMemberRepository = new CommunityMemberRepository();
|
||||||
|
$this->eventRepository = new EventRepository();
|
||||||
|
$this->transactionRepository = new TransactionRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isAuthenticationRequired(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
$communitySlug = \Container::$request->query('communitySlug');
|
||||||
|
$this->community = $this->communityRepository->getBySlug($communitySlug);
|
||||||
|
if ($this->community === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var User $user
|
||||||
|
*/
|
||||||
|
$user = \Container::$request->user();
|
||||||
|
$this->ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($this->community, $user);
|
||||||
|
if ($this->ownCommunityMember === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEvents(): IContent
|
||||||
|
{
|
||||||
|
$itemsPerPage = 10;
|
||||||
|
$numberOfEvents = $this->eventRepository->countAllByCommunity($this->community);
|
||||||
|
$pages = ceil($numberOfEvents / $itemsPerPage);
|
||||||
|
$currentPage = Container::$request->query('page') ?: 0;
|
||||||
|
$events = $this->eventRepository->getPagedByCommunity(
|
||||||
|
$this->community,
|
||||||
|
$currentPage * $itemsPerPage,
|
||||||
|
$itemsPerPage
|
||||||
|
);
|
||||||
|
|
||||||
|
return new HtmlContent('events/events', [
|
||||||
|
'community' => $this->community,
|
||||||
|
'pages' => $pages,
|
||||||
|
'currentPage' => $currentPage,
|
||||||
|
'numberOfEvents' => $numberOfEvents,
|
||||||
|
'events' => $events
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function searchEvent(): IContent
|
||||||
|
{
|
||||||
|
$events = iterator_to_array($this->eventRepository->searchByTitle($this->community, Container::$request->query('q')));
|
||||||
|
$results = [];
|
||||||
|
foreach ($events as $event) {
|
||||||
|
$results[] = ['value' => $event->getId(), 'label' => $event->getTitle()];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JsonContent([
|
||||||
|
'results' => $results
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEvent(): IContent
|
||||||
|
{
|
||||||
|
$event = $this->eventRepository->getBySlug(Container::$request->query('eventSlug'));
|
||||||
|
if (!$event) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Container::$persistentDataManager->loadRelationsFromDb($this->community, true, ['main_currency']);
|
||||||
|
|
||||||
|
return new HtmlContent('events/event', [
|
||||||
|
'community' => $this->community,
|
||||||
|
'event' => $event,
|
||||||
|
'totalCost' => $this->sumTransactions($event)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEventEdit(): ?IContent
|
||||||
|
{
|
||||||
|
$eventSlug = Container::$request->query('eventSlug');
|
||||||
|
if ($eventSlug) {
|
||||||
|
$event = $this->eventRepository->getBySlug($eventSlug);
|
||||||
|
if ($event === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$event = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HtmlContent('events/event_edit', [
|
||||||
|
'community' => $this->community,
|
||||||
|
'event' => $event
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function saveEvent(): ?IContent
|
||||||
|
{
|
||||||
|
$eventSlug = Container::$request->query('eventSlug');
|
||||||
|
if ($eventSlug) {
|
||||||
|
$event = $this->eventRepository->getBySlug($eventSlug);
|
||||||
|
} else {
|
||||||
|
$event = new Event();
|
||||||
|
$event->setCommunity($this->community);
|
||||||
|
}
|
||||||
|
|
||||||
|
$event->setTitle(Container::$request->post('title'));
|
||||||
|
$event->setDescription(Container::$request->post('description'));
|
||||||
|
$event->setStartDate(new DateTime(Container::$request->post('start')));
|
||||||
|
$event->setEndDate(new DateTime(Container::$request->post('end')));
|
||||||
|
Container::$persistentDataManager->saveToDb($event);
|
||||||
|
|
||||||
|
return new JsonContent([
|
||||||
|
'redirect' => ['target' => \Container::$routeCollection->getRoute('community.event')->generateLink(['communitySlug' => $this->community->getSlug(), 'eventSlug' => $event->getSlug()])]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteEvent(): IContent
|
||||||
|
{
|
||||||
|
$event = $this->eventRepository->getBySlug(Container::$request->query('eventSlug'));
|
||||||
|
|
||||||
|
foreach ($this->transactionRepository->getAllByEvent($event) as $transaction) {
|
||||||
|
$transaction->setEventId(null);
|
||||||
|
Container::$persistentDataManager->saveToDb($transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
Container::$persistentDataManager->deleteFromDb($event);
|
||||||
|
|
||||||
|
return new JsonContent(['success' => true]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function sumTransactions(Event $event): int
|
||||||
|
{
|
||||||
|
$exchangeRateCalculator = new ExchangeRateCalculator($this->community->getMainCurrency());
|
||||||
|
$transactions = $this->transactionRepository->getAllByEvent($event, true, ['currency']);
|
||||||
|
$sum = 0.0;
|
||||||
|
|
||||||
|
foreach ($transactions as $transaction) {
|
||||||
|
$sum += $exchangeRateCalculator->calculate($transaction->getSum(), $transaction->getCurrency(), $transaction->getTimeDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user