src/Controller/EventsDirectusController.php line 336

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\EventsPageFrequencyInstance;
  4. use App\Entity\Pages\EventsPage;
  5. use App\Repository\EventsPageFrequencyInstanceRepository;
  6. use App\Repository\EventsPageRepository;
  7. use Gedmo\Tree\RepositoryInterface;
  8. use GuzzleHttp\Client;
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Pagerfanta\Adapter\ArrayAdapter;
  14. use Pagerfanta\Exception\OutOfRangeCurrentPageException;
  15. use Pagerfanta\Pagerfanta;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use GuzzleHttp\Exception\RequestException;
  19. class EventsDirectusController extends Controller
  20. {
  21.     private $directusUrl;
  22.     private $directusApiUrl;
  23.     private $directusToken;
  24.     public function __construct(TranslatorInterface $translator)
  25.     {
  26.         $this->directusUrl $translator->trans($_ENV['DIRECTUS_URL']);
  27.         $this->directusApiUrl $translator->trans($_ENV['DIRECTUS_API_URL']);
  28.         $this->directusToken $translator->trans($_ENV['DIRECTUS_API_TOKEN']);
  29.     }
  30.     public function serviceAction(Request $request)
  31.     {
  32.         $objectsUrl $this->directusApiUrl '/get-objects';
  33.         $disciplinesUrl $this->directusApiUrl '/get-disciplines';
  34.         $token $this->directusToken;
  35.         $client = new Client();
  36.         try {
  37.             $objects $client->post($objectsUrl, [
  38.                 'headers' => [
  39.                     'ga-auth-token' => $token
  40.                 ],
  41.             ]);
  42.             $objectsData $objects->getBody()->getContents();
  43.             $objectsData json_decode($objectsDatatrue);
  44.             $objects = [];
  45.             if (isset($objectsData['data'])) {
  46.                 $objects = ['tags' => $objectsData['data']];
  47.             }
  48.             $disciplines $client->post($disciplinesUrl, [
  49.                 'headers' => [
  50.                     'ga-auth-token' => $token
  51.                 ],
  52.             ]);
  53.             $disciplinesData $disciplines->getBody()->getContents();
  54.             $disciplinesData json_decode($disciplinesDatatrue);
  55.             $disciplines = [];
  56.             if (isset($disciplinesData['data'])) {
  57.                 $disciplines $disciplinesData['data'];
  58.             }
  59.         } catch (RequestException $e) {
  60.             $objects = [];
  61.             $disciplines = [];
  62.         }
  63.         return ['objects' => $objects'disciplines' => $disciplines];
  64.     }
  65.     public function setImageKey(string $publishedAt)
  66.     {
  67.         $publishedDate = new \DateTime($publishedAt);
  68.         $targetDate = new \DateTime('2025-01-27');
  69.         if ($publishedDate $targetDate) {
  70.             return ['background' => 'poster''poster' => 'background'];
  71.         }
  72.         return ['background' => 'background''poster' => 'poster'];
  73.     }
  74.     public function eventAction(Request $request)
  75.     {
  76.         $requestUri $request->getRequestUri();
  77.         $uriParts explode('/'$requestUri);
  78.         $lang$uriParts[1];
  79.         $slug $uriParts[2];
  80.         $eventSlug $uriParts[3];
  81.         $url $this->directusUrl;
  82.         $apiUrl $this->directusApiUrl '/gdynia-arena?slug=' $eventSlug;
  83.         $token $this->directusToken;
  84.         $client = new Client();
  85.         try {
  86.             $response $client->post($apiUrl, [
  87.                 'headers' => [
  88.                     'ga-auth-token' => $token
  89.                 ],
  90.             ]);
  91.             $responseData $response->getBody()->getContents();
  92.             $apiData json_decode($responseDatatrue);
  93.             if (isset($apiData['data']) && is_array($apiData['data']) && !empty($apiData['data'])) {
  94.                 $item $apiData['data'][0];
  95.                 $start_date null;
  96.                 $end_date null;
  97.                 $nearest_date null;
  98.                 $currentDate = new \DateTime();
  99.                 $currentDate->setTime(000);
  100.                 foreach ($item['attributes']['dates'] as $date) {
  101.                     $item_start_date = new \DateTime($date['start_date']);
  102.                     if ($item_start_date $currentDate) {
  103.                         if ($nearest_date) {
  104.                             if ($item_start_date $nearest_date) {
  105.                                 $nearest_date $item_start_date;
  106.                             }
  107.                         } else {
  108.                             $nearest_date $item_start_date;
  109.                         }
  110.                     }
  111.                 }
  112.                 if (isset($item['attributes']['dates']) && is_array($item['attributes']['dates'])) {
  113.                     $count count($item['attributes']['dates']);
  114.                     if ($count 0) {
  115.                         $start_date date($item['attributes']['dates'][0]['start_date']);
  116.                         $end_date date($item['attributes']['dates'][$count 1]['end_date']);
  117.                         if (!$nearest_date) {
  118.                             $nearest_date date($item['attributes']['dates'][$count 1]['start_date']);
  119.                         }
  120.                     }
  121.                 }
  122.                 $places = [];
  123.                 if (isset($item['attributes']['objects']) && is_array($item['attributes']['objects'])) {
  124.                     foreach ($item['attributes']['objects']['data'] as $object) {
  125.                         if (isset($object['attributes']['name'])) {
  126.                             $places[] = ['name' => $object['attributes']['name'], 'address' => $object['attributes']['address']];
  127.                         }
  128.                     }
  129.                 }
  130.                 $imageKey = ['background' => 'background''poster' => 'poster'];
  131.                 if ($item['attributes']['publishedAt']) {
  132.                     $imageKey $this->setImageKey($item['attributes']['publishedAt']);
  133.                 }
  134.                 $background null;
  135.                 $background_img_alt $item['attributes']['title'];
  136.                 if (isset($item['attributes'][$imageKey['background']])) {
  137.                     $background $url $item['attributes'][$imageKey['background']]['data']['attributes']['url'];
  138.                     if ($item['attributes'][$imageKey['background']]['data']['attributes']['alternativeText']) {
  139.                         $background_img_alt $item['attributes'][$imageKey['background']]['data']['attributes']['alternativeText'];
  140.                     }
  141.                 }
  142.                 $poster null;
  143.                 $poster_img_alt $item['attributes']['title'];
  144.                 if (isset($item['attributes'][$imageKey['poster']])) {
  145.                     $poster $url $item['attributes'][$imageKey['poster']]['data']['attributes']['url'];
  146.                     if ($item['attributes'][$imageKey['poster']]['data']['attributes']['alternativeText']) {
  147.                         $poster_img_alt $item['attributes'][$imageKey['poster']]['data']['attributes']['alternativeText'];
  148.                     }
  149.                 }
  150.                 $sportsIds = [];
  151.                 if (isset($item['attributes']['disciplines']) && is_array($item['attributes']['disciplines'])) {
  152.                     foreach ($item['attributes']['disciplines'] as $discipline) {
  153.                         if (isset($discipline['data']['id'])) {
  154.                             $sportsIds[] = $discipline['discipline']['id'];
  155.                         }
  156.                     }
  157.                 }
  158.                 $buttons array_filter($item['attributes']['gdynia_sport'], function($item) {
  159.                     return $item['__component'] === 'gdynia-sport.buttons';
  160.                 });
  161.                 $youtubes array_filter($item['attributes']['gdynia_sport'], function($item) {
  162.                     return $item['__component'] === 'gdynia-sport.youtubes';
  163.                 });
  164.                 $transformedItem = [
  165.                     'id' => $item['id'],
  166.                     'name' => $item['attributes']['title'],
  167.                     'content' => $item['attributes']['content'],
  168.                     'background_img_url' => $background,
  169.                     'poster_img_url' => $poster,
  170.                     'path' => $item['attributes']['slug'],
  171.                     'places' => $places,
  172.                     'startDate' => $start_date,
  173.                     'endDate' => $end_date,
  174.                     'nearestDate' => $nearest_date,
  175.                     'sports' => $item['attributes']['disciplines'],
  176.                     'sportsIds' => $sportsIds,
  177.                     'background_img_alt' => $background_img_alt,
  178.                     'poster_img_alt' => $poster_img_alt,
  179.                     'buttons' => $buttons,
  180.                     'youtubes' => $youtubes
  181.                 ];
  182.                 return $this->render('Pages/EventsDirectusPage/page.html.twig', [
  183.                     'lang' => $lang,
  184.                     'slug' => $slug,
  185.                     'eventSlug' => $eventSlug,
  186.                     'event' => $transformedItem
  187.                 ]);
  188.             } else {
  189.                 return $this->render('Error/error.html.twig', [
  190.                     'status_code' => 404,
  191.                 ]);
  192.             }
  193.         } catch (RequestException $e) {
  194.             return $this->render('Error/error.html.twig', [
  195.                 'status_code' => 404,
  196.             ]);
  197.         }
  198.     }
  199.     public function lastEvents(Request $request$limit 12, array $sports null$slug null)
  200.     {
  201.         $sportsIds implode(', '$sports);
  202.         $currentDate date('Y-m-d');
  203.         $yearsLater date('Y-m-d'strtotime($currentDate ' +5 years'));
  204.         $apiUrl $this->directusApiUrl '/gdynia-arena?disciplines=' $sportsIds '&start_date=' $currentDate '&end_date=' $yearsLater;
  205.         $url $this->directusUrl;
  206.         $token $this->directusToken;
  207.         $client = new Client();
  208.         try {
  209.             $response $client->post($apiUrl, [
  210.                 'headers' => [
  211.                     'ga-auth-token' => $token
  212.                 ],
  213.             ]);
  214.             $responseData $response->getBody()->getContents();
  215.             $apiData json_decode($responseDatatrue);
  216.             $events = [];
  217.             if (isset($apiData['data'])) {
  218.                 foreach ($apiData['data'] as $item) {
  219.                     $nearest_date null;
  220.                     $currentDate = new \DateTime();
  221.                     $currentDate->setTime(000);
  222.                     foreach ($item['attributes']['dates'] as $date) {
  223.                         $item_start_date = new \DateTime($date['start_date']);
  224.                         if ($item_start_date $currentDate) {
  225.                             if ($nearest_date) {
  226.                                 if ($item_start_date $nearest_date) {
  227.                                     $nearest_date $item_start_date;
  228.                                 }
  229.                             } else {
  230.                                 $nearest_date $item_start_date;
  231.                             }
  232.                         }
  233.                     }
  234.                     if (isset($item['attributes']['dates']) && is_array($item['attributes']['dates'])) {
  235.                         $count count($item['attributes']['dates']);
  236.                         if ($count 0) {
  237.                             $start_date date($item['attributes']['dates'][0]['start_date']);
  238.                             $end_date date($item['attributes']['dates'][$count 1]['end_date']);
  239.                             if (!$nearest_date) {
  240.                                 $nearest_date date($item['attributes']['dates'][$count 1]['start_date']);
  241.                             }
  242.                         }
  243.                     }
  244.                     $places = [];
  245.                     if (isset($item['attributes']['objects']) && isset($item['attributes']['objects']['data']) && is_array($item['attributes']['objects']['data'])) {
  246.                         foreach ($item['attributes']['objects']['data'] as $object) {
  247.                             if (isset($object['attributes'])) {
  248.                                 $places[] = ['name' => $object['attributes']['name'], 'address' => $object['attributes']['address']];
  249.                             }
  250.                         }
  251.                     }
  252.                     $imageKey = ['background' => 'background''poster' => 'poster'];
  253.                     if ($item['attributes']['publishedAt']) {
  254.                         $imageKey $this->setImageKey($item['attributes']['publishedAt']);
  255.                     }
  256.                     $poster null;
  257.                     if (isset($item['attributes'][$imageKey['poster']])) {
  258.                         $poster $url $item['attributes'][$imageKey['poster']]['data']['attributes']['url'];
  259.                     }
  260.                     $background null;
  261.                     if (isset($item['attributes'][$imageKey['background']])) {
  262.                         $background $url $item['attributes'][$imageKey['background']]['data']['attributes']['url'];
  263.                     }
  264.                     $transformedItem = [
  265.                         'id' => $item['id'],
  266.                         'url' => $item['attributes']['slug'],
  267.                         'name' => $item['attributes']['title'],
  268.                         'content' => $item['attributes']['content'],
  269.                         'header_img_url' => $poster  '?key=gcs-list',
  270.                         'background_img_url' => $background,
  271.                         'path' => $item['attributes']['slug'],
  272.                         'places' => $places,
  273.                         'nearestDate' => $nearest_date,
  274.                         'header_img_alt' => $item['attributes']['title']
  275.                     ];
  276.                     $events[] = $transformedItem;
  277.                 }
  278.                 $events_nearest_dates array_column($events"nearestDate");
  279.                 array_multisort$events_nearest_datesSORT_ASC$events);
  280.                 if (count($events) > 3) {
  281.                     $events array_slice($events0$limit);
  282.                 }
  283.             }
  284.         } catch (RequestException $e) {
  285.             $events = [];
  286.         }
  287.         return $this->render(
  288.             'Events/viewdirectus.html.twig',
  289.             array(
  290.                 'eventsInstances' => [],
  291.                 'events' => $events,
  292.                 'slug' => $slug
  293.             )
  294.         );
  295.     }
  296. }