RVRNEXT-11 add controller for events
This commit is contained in:
parent
d29fc1c339
commit
3ba173e1c3
146
src/Controller/EventController.php
Normal file
146
src/Controller/EventController.php
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
<?php namespace RVR\Controller;
|
||||||
|
|
||||||
|
use Container;
|
||||||
|
use DateTime;
|
||||||
|
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 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 ?Community $community;
|
||||||
|
|
||||||
|
private ?CommunityMember $ownCommunityMember;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->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]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user