src/Controller/EventsDirectusController.php line 66

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 = ['tags' => $objectsData['data']];
  45.             $disciplines $client->post($disciplinesUrl, [
  46.                 'headers' => [
  47.                     'ga-auth-token' => $token
  48.                 ],
  49.             ]);
  50.             $disciplinesData $disciplines->getBody()->getContents();
  51.             $disciplinesData json_decode($disciplinesDatatrue);
  52.             $disciplines $disciplinesData['data'];
  53.         } catch (RequestException $e) {
  54.             $objects = [];
  55.             $disciplines = [];
  56.         }
  57.         return ['objects' => $objects'disciplines' => $disciplines];
  58.     }
  59.     public function eventAction(Request $request)
  60.     {
  61.         $requestUri $request->getRequestUri();
  62.         $uriParts explode('/'$requestUri);
  63.         $lang$uriParts[1];
  64.         $slug $uriParts[2];
  65.         $eventSlug $uriParts[3];
  66.         $url $this->directusUrl;
  67.         $apiUrl $this->directusApiUrl '/gdynia-arena?slug=' $eventSlug;
  68.         $token $this->directusToken;
  69.         $client = new Client();
  70.         try {
  71.             $response $client->post($apiUrl, [
  72.                 'headers' => [
  73.                     'ga-auth-token' => $token
  74.                 ],
  75.             ]);
  76.             $responseData $response->getBody()->getContents();
  77.             $apiData json_decode($responseDatatrue);
  78.             if ($apiData['data']) {
  79.                 $item $apiData['data'][0];
  80.                 $start_date null;
  81.                 $end_date null;
  82.                 $nearest_date null;
  83.                 $currentDate = new \DateTime();
  84.                 $currentDate->setTime(000);
  85.                 foreach ($item['attributes']['dates'] as $date) {
  86.                     $item_start_date = new \DateTime($date['start_date']);
  87.                     if ($item_start_date $currentDate) {
  88.                         if ($nearest_date) {
  89.                             if ($item_start_date $nearest_date) {
  90.                                 $nearest_date $item_start_date;
  91.                             }
  92.                         } else {
  93.                             $nearest_date $item_start_date;
  94.                         }
  95.                     }
  96.                 }
  97.                 if (isset($item['attributes']['dates']) && is_array($item['attributes']['dates'])) {
  98.                     $count count($item['attributes']['dates']);
  99.                     if ($count 0) {
  100.                         $start_date date($item['attributes']['dates'][0]['start_date']);
  101.                         $end_date date($item['attributes']['dates'][$count 1]['end_date']);
  102.                         if (!$nearest_date) {
  103.                             $nearest_date date($item['attributes']['dates'][$count 1]['start_date']);
  104.                         }
  105.                     }
  106.                 }
  107.                 $places = [];
  108.                 if (isset($item['attributes']['objects']) && is_array($item['attributes']['objects'])) {
  109.                     foreach ($item['attributes']['objects']['data'] as $object) {
  110.                         if (isset($object['attributes']['name'])) {
  111.                             $places[] = ['name' => $object['attributes']['name'], 'address' => $object['attributes']['address']];
  112.                         }
  113.                     }
  114.                 }
  115.                 $background null;
  116.                 if (isset($item['attributes']['poster'])) {
  117.                     $background $url $item['attributes']['poster']['data']['attributes']['url'];
  118.                 }
  119.                 $sportsIds = [];
  120.                 if (isset($item['attributes']['disciplines']) && is_array($item['attributes']['disciplines'])) {
  121.                     foreach ($item['attributes']['disciplines'] as $discipline) {
  122.                         if (isset($discipline['data']['id'])) {
  123.                             $sportsIds[] = $discipline['discipline']['id'];
  124.                         }
  125.                     }
  126.                 }
  127.                 $buttons array_filter($item['attributes']['gdynia_sport'], function($item) {
  128.                     return $item['__component'] === 'gdynia-sport.buttons';
  129.                 });
  130.                 $youtubes array_filter($item['attributes']['gdynia_sport'], function($item) {
  131.                     return $item['__component'] === 'gdynia-sport.youtubes';
  132.                 });
  133.                 $transformedItem = [
  134.                     'id' => $item['id'],
  135.                     'name' => $item['attributes']['title'],
  136.                     'content' => $item['attributes']['content'],
  137.                     'background_img_url' => $background,
  138.                     'path' => $item['attributes']['slug'],
  139.                     'places' => $places,
  140.                     'startDate' => $start_date,
  141.                     'endDate' => $end_date,
  142.                     'nearestDate' => $nearest_date,
  143.                     'sports' => $item['attributes']['disciplines'],
  144.                     'sportsIds' => $sportsIds,
  145.                     'background_img_alt' => $item['attributes']['title'],
  146.                     'buttons' => $buttons,
  147.                     'youtubes' => $youtubes
  148.                 ];
  149.                 return $this->render('Pages/EventsDirectusPage/page.html.twig', [
  150.                     'lang' => $lang,
  151.                     'slug' => $slug,
  152.                     'eventSlug' => $eventSlug,
  153.                     'event' => $transformedItem
  154.                 ]);
  155.             } else {
  156.                 return $this->render('Error/error.html.twig', [
  157.                     'status_code' => 404,
  158.                 ]);
  159.             }
  160.         } catch (RequestException $e) {
  161.             return $this->render('Error/error.html.twig', [
  162.                 'status_code' => 404,
  163.             ]);
  164.         }
  165.     }
  166.     public function lastEvents(Request $request$limit 12, array $sports null$slug null)
  167.     {
  168.         $sportsIds implode(', '$sports);
  169.         $currentDate date('Y-m-d');
  170.         $yearsLater date('Y-m-d'strtotime($currentDate ' +5 years'));
  171.         $apiUrl $this->directusApiUrl '/gdynia-arena?disciplines=' $sportsIds '&start_date=' $currentDate '&end_date=' $yearsLater;
  172.         $url $this->directusUrl;
  173.         $token $this->directusToken;
  174.         $client = new Client();
  175.         try {
  176.             $response $client->post($apiUrl, [
  177.                 'headers' => [
  178.                     'ga-auth-token' => $token
  179.                 ],
  180.             ]);
  181.             $responseData $response->getBody()->getContents();
  182.             $apiData json_decode($responseDatatrue);
  183.             $events = [];
  184.             foreach ($apiData['data'] as $item) {
  185.                 $nearest_date null;
  186.                 $currentDate = new \DateTime();
  187.                 $currentDate->setTime(000);
  188.                 foreach ($item['attributes']['dates'] as $date) {
  189.                     $item_start_date = new \DateTime($date['start_date']);
  190.                     if ($item_start_date $currentDate) {
  191.                         if ($nearest_date) {
  192.                             if ($item_start_date $nearest_date) {
  193.                                 $nearest_date $item_start_date;
  194.                             }
  195.                         } else {
  196.                             $nearest_date $item_start_date;
  197.                         }
  198.                     }
  199.                 }
  200.                 if (isset($item['attributes']['dates']) && is_array($item['attributes']['dates'])) {
  201.                     $count count($item['attributes']['dates']);
  202.                     if ($count 0) {
  203.                         $start_date date($item['attributes']['dates'][0]['start_date']);
  204.                         $end_date date($item['attributes']['dates'][$count 1]['end_date']);
  205.                         if (!$nearest_date) {
  206.                             $nearest_date date($item['attributes']['dates'][$count 1]['start_date']);
  207.                         }
  208.                     }
  209.                 }
  210.                 $places = [];
  211.                 if (isset($item['attributes']['objects']) && isset($item['attributes']['objects']['data']) && is_array($item['attributes']['objects']['data'])) {
  212.                     foreach ($item['attributes']['objects']['data'] as $object) {
  213.                         if (isset($object['attributes'])) {
  214.                             $places[] = ['name' => $object['attributes']['name'], 'address' => $object['attributes']['address']];
  215.                         }
  216.                     }
  217.                 }
  218.                 $poster null;
  219.                 if (isset($item['attributes']['background']) && isset($item['attributes']['poster']['data'])) {
  220.                     $poster $url $item['attributes']['background']['data']['attributes']['url'];
  221.                 }
  222.                 $background null;
  223.                 if (isset($item['attributes']['poster']) && isset($item['attributes']['background']['data'])) {
  224.                     $background $url $item['attributes']['poster']['data']['attributes']['url'];
  225.                 }
  226.                 $transformedItem = [
  227.                     'id' => $item['id'],
  228.                     'url' => $item['attributes']['slug'],
  229.                     'name' => $item['attributes']['title'],
  230.                     'content' => $item['attributes']['content'],
  231.                     'header_img_url' => $poster  '?key=gcs-list',
  232.                     'background_img_url' => $background,
  233.                     'path' => $item['attributes']['slug'],
  234.                     'places' => $places,
  235.                     'nearestDate' => $nearest_date,
  236.                     'header_img_alt' => $item['attributes']['title']
  237.                 ];
  238.                 $events[] = $transformedItem;
  239.             }
  240.             $events_nearest_dates array_column($events"nearestDate");
  241.             array_multisort$events_nearest_datesSORT_ASC$events);
  242.             if (count($events) > 3) {
  243.                 $events array_slice($events0$limit);
  244.             }
  245.         } catch (RequestException $e) {
  246.             $events = [];
  247.         }
  248.         return $this->render(
  249.             'Events/viewdirectus.html.twig',
  250.             array(
  251.                 'eventsInstances' => [],
  252.                 'events' => $events,
  253.                 'slug' => $slug
  254.             )
  255.         );
  256.     }
  257. }