MapController.php 6.54 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6
<?php

namespace App\Controller;

use App\Entity\Comptoir;
use App\Entity\Groupeprestataire;
Julien Jorry committed
7
use App\Entity\Prestataire;
Julien Jorry committed
8 9 10
use Doctrine\ORM\EntityManagerInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Julien Jorry committed
11
use Symfony\Component\HttpFoundation\Request;
Julien Jorry committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;

class MapController extends AbstractController
{
    public function __construct(EntityManagerInterface $em, RouterInterface $router, Pool $mediaext)
    {
        $this->em = $em;
        $this->router = $router;
        $this->mediaext = $mediaext;
    }

    /**
     * @Route(
     *     name="papi_poi_all",
     *     path="/public/poi/all",
     *     methods={"GET"},
     *     defaults={
     *         "_api_resource_class"=Map::class,
     *         "_api_item_operation_name"="get_all_poi"
     *     }
     * )
     */
    public function __invoke(Request $request)
    {
        // @TODO : à tester plus !
        if ($request->query->has('geoloc') && $request->query->has('distance')) {
            $geoloc = json_decode($request->query->get('geoloc'));
            $lat = $geoloc[0];
            $lon = $geoloc[1];
            $distance = intval($request->query->get('distance'));
            $prestas = $this->em->getRepository(Prestataire::class)->findByGeoloc($lat, $lon, $distance);
            $comptoirs = $this->em->getRepository(Comptoir::class)->findByGeoloc($lat, $lon, $distance);
            $groupeprestataires = $this->em->getRepository(Groupeprestataire::class)->findByGeoloc($lat, $lon, $distance);
        } else {
Julien Jorry committed
47 48 49
            $prestas = $this->em->getRepository(Prestataire::class)->findDefault();
            $comptoirs = $this->em->getRepository(Comptoir::class)->findBy(['enabled' => true], ['name' => 'ASC']);
            $groupeprestataires = $this->em->getRepository(Groupeprestataire::class)->findBy(['enabled' => true], ['name' => 'ASC']);
Julien Jorry committed
50 51 52 53 54 55 56
        }

        $data = [];
        $count = 0;
        foreach ($prestas as $presta) {
            if (count($presta->getGeolocs()) > 0) {
                foreach ($presta->getGeolocs() as $geolocp) {
Julien Jorry committed
57
                    if ($geolocp->isEnabled() && null != $geolocp->getGeoloc()->getLat() && null != $geolocp->getGeoloc()->getLon()) {
Julien Jorry committed
58 59 60 61 62
                        $data[$count]['name'] = $presta->__toString();
                        $data[$count]['content'] = $presta->getDescription();
                        $data[$count]['web'] = $presta->getWeb();
                        $data[$count]['link'] = $this->router->generate('show_prestataire', ['slug' => $presta->getSlug()]);
                        $data[$count]['hours'] = $presta->getHoraires();
Julien Jorry committed
63
                        if (null != $presta->getMedia()) {
Julien Jorry committed
64 65 66 67 68 69 70 71 72 73
                            $data[$count]['thumbnail'] = $this->path($presta->getMedia(), 'small', $request);
                        }
                        $data[$count]['geolocs'][] = $geolocp;
                        if (count($presta->getRubriques()) > 0) {
                            foreach ($presta->getRubriques() as $rub) {
                                $data[$count]['rubriques'][] = [
                                    'name' => $rub->getName(),
                                    'url' => $this->router->generate('show_rubrique', ['slug' => $rub->getSlug()]),
                                ];
                            }
Julien Jorry committed
74
                            if (null != $presta->getRubriques()[0]->getMedia()) {
Julien Jorry committed
75 76 77
                                $data[$count]['icon'] = $this->path($presta->getRubriques()[0]->getMedia(), 'preview', $request);
                            }
                        }
Julien Jorry committed
78
                        ++$count;
Julien Jorry committed
79 80 81 82 83 84
                    }
                }
            }
        }

        foreach ($comptoirs as $comptoir) {
Julien Jorry committed
85
            if (null != $comptoir->getGeoloc() && null != $comptoir->getGeoloc()->getLat() && null != $comptoir->getGeoloc()->getLon()) {
Julien Jorry committed
86 87 88 89
                $data[$count]['name'] = $comptoir->__toString();
                $data[$count]['content'] = $comptoir->getContent();
                $data[$count]['geolocs'][] = $comptoir->getGeoloc();
                $data[$count]['link'] = $this->router->generate('show_comptoir', ['slug' => $comptoir->getSlug()]);
Julien Jorry committed
90
                if (null != $comptoir->getMedia()) {
Julien Jorry committed
91 92
                    $data[$count]['thumbnail'] = $this->path($comptoir->getMedia(), 'small', $request);
                }
Julien Jorry committed
93
                ++$count;
Julien Jorry committed
94 95 96 97
            }
        }

        foreach ($groupeprestataires as $groupeprestataire) {
Julien Jorry committed
98
            if (null != $groupeprestataire->getGeoloc() && null != $groupeprestataire->getGeoloc()->getLat() && null != $groupeprestataire->getGeoloc()->getLon()) {
Julien Jorry committed
99 100 101 102 103 104 105 106 107 108 109
                $data[$count]['name'] = $groupeprestataire->__toString();
                $data[$count]['content'] = $groupeprestataire->getContent();
                $data[$count]['geolocs'][] = $groupeprestataire->getGeoloc();
                $data[$count]['hours'] = $groupeprestataire->getHoraires();
                $data[$count]['link'] = $this->router->generate('show_groupeprestataire', ['slug' => $groupeprestataire->getSlug()]);
                // if ($groupeprestataire->getMedia() != null) {
                //     $data[$count]['thumbnail'] = $this->path($groupeprestataire->getMedia(), 'small', $request);
                // }
                foreach ($groupeprestataire->getPrestataires() as $presta) {
                    $groupeprestaData = [
                        'name' => $presta->__toString(),
Julien Jorry committed
110
                        'link' => $this->router->generate('show_prestataire', ['slug' => $presta->getSlug()]),
Julien Jorry committed
111 112
                    ];
                    if (count($presta->getRubriques()) > 0) {
Julien Jorry committed
113
                        if (null != $presta->getRubriques()[0]->getMedia()) {
Julien Jorry committed
114 115 116 117 118 119
                            $groupeprestaData['rubrique']['name'] = $presta->getRubriques()[0]->getName();
                            $groupeprestaData['rubrique']['icon'] = $this->path($presta->getRubriques()[0]->getMedia(), 'preview', $request);
                        }
                    }
                    $data[$count]['prestataires'][] = $groupeprestaData;
                }
Julien Jorry committed
120
                ++$count;
Julien Jorry committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            }
        }

        return $data;
    }

    private function path($media, $format, $request)
    {
        if (!$media) {
            return '';
        }

        $provider = $this->mediaext
           ->getProvider($media->getProviderName());

        $format = $provider->getFormatName($media, $format);

Julien Jorry committed
138
        return $request->getSchemeAndHttpHost() . $provider->generatePublicUrl($media, $format);
Julien Jorry committed
139 140
    }
}