From 720fb7b339de2075258acbb812310fe1b6ff7e2d 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 | 146 +++++++++++++++++++++++++++++ 1 file changed, 146 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..cbe7ff0 --- /dev/null +++ b/src/Controller/EventController.php @@ -0,0 +1,146 @@ +communityRepository = new CommunityRepository(); + $this->communityMemberRepository = new CommunityMemberRepository(); + $this->eventRepository = new EventRepository(); + } + + 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 + ]); + } + + 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(['success' => true]); + } +}