<?php
namespace App\Controller;
use App\Entity\EventsPageFrequencyInstance;
use App\Entity\Pages\EventsPage;
use App\Repository\EventsPageFrequencyInstanceRepository;
use App\Repository\EventsPageRepository;
use Gedmo\Tree\RepositoryInterface;
use GuzzleHttp\Client;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use GuzzleHttp\Exception\RequestException;
class EventsDirectusController extends Controller
{
private $directusUrl;
private $directusApiUrl;
private $directusToken;
public function __construct(TranslatorInterface $translator)
{
$this->directusUrl = $translator->trans($_ENV['DIRECTUS_URL']);
$this->directusApiUrl = $translator->trans($_ENV['DIRECTUS_API_URL']);
$this->directusToken = $translator->trans($_ENV['DIRECTUS_API_TOKEN']);
}
public function serviceAction(Request $request)
{
$objectsUrl = $this->directusApiUrl . '/get-objects';
$disciplinesUrl = $this->directusApiUrl . '/get-disciplines';
$token = $this->directusToken;
$client = new Client();
try {
$objects = $client->post($objectsUrl, [
'headers' => [
'ga-auth-token' => $token
],
]);
$objectsData = $objects->getBody()->getContents();
$objectsData = json_decode($objectsData, true);
$objects = ['tags' => $objectsData['data']];
$disciplines = $client->post($disciplinesUrl, [
'headers' => [
'ga-auth-token' => $token
],
]);
$disciplinesData = $disciplines->getBody()->getContents();
$disciplinesData = json_decode($disciplinesData, true);
$disciplines = $disciplinesData['data'];
} catch (RequestException $e) {
$objects = [];
$disciplines = [];
}
return ['objects' => $objects, 'disciplines' => $disciplines];
}
public function eventAction(Request $request)
{
$requestUri = $request->getRequestUri();
$uriParts = explode('/', $requestUri);
$lang= $uriParts[1];
$slug = $uriParts[2];
$eventSlug = $uriParts[3];
$url = $this->directusUrl;
$apiUrl = $this->directusApiUrl . '/gdynia-arena?slug=' . $eventSlug;
$token = $this->directusToken;
$client = new Client();
try {
$response = $client->post($apiUrl, [
'headers' => [
'ga-auth-token' => $token
],
]);
$responseData = $response->getBody()->getContents();
$apiData = json_decode($responseData, true);
if ($apiData['data']) {
$item = $apiData['data'][0];
$start_date = null;
$end_date = null;
$nearest_date = null;
$currentDate = new \DateTime();
$currentDate->setTime(0, 0, 0);
foreach ($item['attributes']['dates'] as $date) {
$item_start_date = new \DateTime($date['start_date']);
if ($item_start_date > $currentDate) {
if ($nearest_date) {
if ($item_start_date < $nearest_date) {
$nearest_date = $item_start_date;
}
} else {
$nearest_date = $item_start_date;
}
}
}
if (isset($item['attributes']['dates']) && is_array($item['attributes']['dates'])) {
$count = count($item['attributes']['dates']);
if ($count > 0) {
$start_date = date($item['attributes']['dates'][0]['start_date']);
$end_date = date($item['attributes']['dates'][$count - 1]['end_date']);
if (!$nearest_date) {
$nearest_date = date($item['attributes']['dates'][$count - 1]['start_date']);
}
}
}
$places = [];
if (isset($item['attributes']['objects']) && is_array($item['attributes']['objects'])) {
foreach ($item['attributes']['objects']['data'] as $object) {
if (isset($object['attributes']['name'])) {
$places[] = ['name' => $object['attributes']['name'], 'address' => $object['attributes']['address']];
}
}
}
$background = null;
if (isset($item['attributes']['poster'])) {
$background = $url . $item['attributes']['poster']['data']['attributes']['url'];
}
$sportsIds = [];
if (isset($item['attributes']['disciplines']) && is_array($item['attributes']['disciplines'])) {
foreach ($item['attributes']['disciplines'] as $discipline) {
if (isset($discipline['data']['id'])) {
$sportsIds[] = $discipline['discipline']['id'];
}
}
}
$buttons = array_filter($item['attributes']['gdynia_sport'], function($item) {
return $item['__component'] === 'gdynia-sport.buttons';
});
$youtubes = array_filter($item['attributes']['gdynia_sport'], function($item) {
return $item['__component'] === 'gdynia-sport.youtubes';
});
$transformedItem = [
'id' => $item['id'],
'name' => $item['attributes']['title'],
'content' => $item['attributes']['content'],
'background_img_url' => $background,
'path' => $item['attributes']['slug'],
'places' => $places,
'startDate' => $start_date,
'endDate' => $end_date,
'nearestDate' => $nearest_date,
'sports' => $item['attributes']['disciplines'],
'sportsIds' => $sportsIds,
'background_img_alt' => $item['attributes']['title'],
'buttons' => $buttons,
'youtubes' => $youtubes
];
return $this->render('Pages/EventsDirectusPage/page.html.twig', [
'lang' => $lang,
'slug' => $slug,
'eventSlug' => $eventSlug,
'event' => $transformedItem
]);
} else {
return $this->render('Error/error.html.twig', [
'status_code' => 404,
]);
}
} catch (RequestException $e) {
return $this->render('Error/error.html.twig', [
'status_code' => 404,
]);
}
}
public function lastEvents(Request $request, $limit = 12, array $sports = null, $slug = null)
{
$sportsIds = implode(', ', $sports);
$currentDate = date('Y-m-d');
$yearsLater = date('Y-m-d', strtotime($currentDate . ' +5 years'));
$apiUrl = $this->directusApiUrl . '/gdynia-arena?disciplines=' . $sportsIds . '&start_date=' . $currentDate . '&end_date=' . $yearsLater;
$url = $this->directusUrl;
$token = $this->directusToken;
$client = new Client();
try {
$response = $client->post($apiUrl, [
'headers' => [
'ga-auth-token' => $token
],
]);
$responseData = $response->getBody()->getContents();
$apiData = json_decode($responseData, true);
$events = [];
foreach ($apiData['data'] as $item) {
$nearest_date = null;
$currentDate = new \DateTime();
$currentDate->setTime(0, 0, 0);
foreach ($item['attributes']['dates'] as $date) {
$item_start_date = new \DateTime($date['start_date']);
if ($item_start_date > $currentDate) {
if ($nearest_date) {
if ($item_start_date < $nearest_date) {
$nearest_date = $item_start_date;
}
} else {
$nearest_date = $item_start_date;
}
}
}
if (isset($item['attributes']['dates']) && is_array($item['attributes']['dates'])) {
$count = count($item['attributes']['dates']);
if ($count > 0) {
$start_date = date($item['attributes']['dates'][0]['start_date']);
$end_date = date($item['attributes']['dates'][$count - 1]['end_date']);
if (!$nearest_date) {
$nearest_date = date($item['attributes']['dates'][$count - 1]['start_date']);
}
}
}
$places = [];
if (isset($item['attributes']['objects']) && isset($item['attributes']['objects']['data']) && is_array($item['attributes']['objects']['data'])) {
foreach ($item['attributes']['objects']['data'] as $object) {
if (isset($object['attributes'])) {
$places[] = ['name' => $object['attributes']['name'], 'address' => $object['attributes']['address']];
}
}
}
$poster = null;
if (isset($item['attributes']['background']) && isset($item['attributes']['poster']['data'])) {
$poster = $url . $item['attributes']['background']['data']['attributes']['url'];
}
$background = null;
if (isset($item['attributes']['poster']) && isset($item['attributes']['background']['data'])) {
$background = $url . $item['attributes']['poster']['data']['attributes']['url'];
}
$transformedItem = [
'id' => $item['id'],
'url' => $item['attributes']['slug'],
'name' => $item['attributes']['title'],
'content' => $item['attributes']['content'],
'header_img_url' => $poster . '?key=gcs-list',
'background_img_url' => $background,
'path' => $item['attributes']['slug'],
'places' => $places,
'nearestDate' => $nearest_date,
'header_img_alt' => $item['attributes']['title']
];
$events[] = $transformedItem;
}
$events_nearest_dates = array_column($events, "nearestDate");
array_multisort( $events_nearest_dates, SORT_ASC, $events);
if (count($events) > 3) {
$events = array_slice($events, 0, $limit);
}
} catch (RequestException $e) {
$events = [];
}
return $this->render(
'Events/viewdirectus.html.twig',
array(
'eventsInstances' => [],
'events' => $events,
'slug' => $slug
)
);
}
}