From 2bcaf13ee31e754786d2a0a7dd7dd96feaf48f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 03:44:01 +0200 Subject: [PATCH] RVRNEXT-11 add controller for events --- src/Controller/EventController.php | 181 +++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/Controller/EventController.php diff --git a/src/Controller/EventController.php b/src/Controller/EventController.php new file mode 100644 index 0000000..be72ca1 --- /dev/null +++ b/src/Controller/EventController.php @@ -0,0 +1,181 @@ +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): float + { + $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; + } +}