浏览代码

a first working version of public events route

Olivier Massot 3 年之前
父节点
当前提交
dcc714e2ec

+ 7 - 15
src/DataProvider/Public/PublicEventDataProvider.php

@@ -6,39 +6,31 @@ use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
 use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
 use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
 use App\ApiResources\Public\FederationStructure;
+use App\Entity\Public\PublicEvent;
+use App\Repository\Booking\EventRepository;
 use App\Repository\Organization\OrganizationRepository;
 use Doctrine\Common\Collections\ArrayCollection;
 use Symfony\Component\HttpFoundation\RequestStack;
 
 
-class FederationStructureDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
+class PublicEventDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
 {
     public function __construct(
-        private RequestStack $requestStack,
-        private OrganizationRepository $organizationRepository
+        private EventRepository $eventRepository
     ) {}
 
     public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
     {
-        return FederationStructure::class === $resourceClass;
+        return PublicEvent::class === $resourceClass;
     }
 
     public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?FederationStructure
     {
-        return $this->organizationRepository->getFederationStructureByOrganizationId($id);
+        return $this->eventRepository->getAllEvents($id)[0];
     }
 
     public function getCollection(string $resourceClass, string $operationName = null): ArrayCollection
     {
-        $request = $this->requestStack->getCurrentRequest();
-        if ($request === null) {
-            throw new \RuntimeException('Undefined request');
-        }
-        $parentId = $request->query->get('parent');
-        if (empty($parentId) || !is_numeric($parentId)) {
-            throw new \RuntimeException('Bad or missing parent value');
-        }
-
-        return new ArrayCollection($this->organizationRepository->getChildrenStructuresByFederationId($parentId));
+        return new ArrayCollection($this->eventRepository->getAllEvents());
     }
 }

+ 4 - 2
src/Entity/Public/PublicEvent.php

@@ -1,10 +1,11 @@
 <?php
 declare(strict_types=1);
 
-namespace App\ApiResources\Public;
+namespace App\Entity\Public;
 
-use ApiPlatform\Core\Annotation\ApiProperty;
+use ApiPlatform\Core\Annotation\ApiFilter;
 use ApiPlatform\Core\Annotation\ApiResource;
+use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
 use App\ApiResources\ApiResourcesInterface;
 use App\Repository\Public\PublicEventRepository;
 use Doctrine\ORM\Mapping as ORM;
@@ -25,6 +26,7 @@ use Doctrine\ORM\Mapping as ORM;
         ]
     ]
 )]
+#[ApiFilter(SearchFilter::class, properties: ['name' => 'exact'])]
 class PublicEvent implements ApiResourcesInterface
 {
     #[ORM\Id]

+ 11 - 2
src/Enum/Public/PublicEventOriginEnum.php

@@ -1,6 +1,15 @@
 <?php
 
-class PublicEventOriginEnum
-{
+namespace App\Enum\Public;
+
+use MyCLabs\Enum\Enum;
 
+/**
+ * @method static AWIN()
+ * @method static OPENTALENT()
+ */
+class PublicEventOriginEnum extends Enum
+{
+    private const OPENTALENT = 'opentalent';
+    private const AWIN = 'awin';
 }

+ 96 - 1
src/Repository/Booking/EventRepository.php

@@ -1,8 +1,103 @@
 <?php
+declare(strict_types=1);
 
 namespace App\Repository\Booking;
 
-class EventRepository
+use App\Entity\Public\PublicEvent;
+use App\Entity\Booking\Event;
+use App\Enum\Public\PublicEventOriginEnum;
+use App\Service\Utils\ArrayUtils;
+use App\Service\Utils\DatesUtils;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\ORM\EntityManagerInterface;
+use Doctrine\Persistence\ManagerRegistry;
+
+class EventRepository extends ServiceEntityRepository
 {
+    public function __construct(ManagerRegistry $registry, private EntityManagerInterface $em)
+    {
+        parent::__construct($registry, Event::class);
+    }
+
+    /**
+     * Route optimisée pour retourner les données de d'un évènement publique, sous forme d'Api resources
+     * de type PublicEvent.
+     *
+     * Cette route est utilisée par l'iframe de recherche des évènements
+     * @see https://gitlab.2iopenservice.com/opentalent/frames
+     *
+     * @return array
+     * @throws \Doctrine\DBAL\DBALException
+     * @throws \Doctrine\DBAL\Driver\Exception
+     * @throws \Doctrine\DBAL\Exception
+     */
+    public function getAllEvents(): array
+    {
+        $sql = "    SELECT
+                            b.id as id, b.organization_id AS organizationId, b.name, b.description, b.url, b.datetimeStart, b.datetimeEnd,
+                            p.name AS placeName, p.description AS placeDescription, p.floorSize AS placeFloorSize, p.capacity AS placeCapacity,
+                            ap.addressCity AS city, ap.postalCode, 
+                            TRIM(BOTH ' ' FROM CONCAT( if(ap.streetAddress is null,'',ap.streetAddress), ' ', if(ap.streetAddressSecond is null,'',ap.streetAddressSecond), ' ', if(ap.streetAddressThird is null,'',ap.streetAddressThird))) AS streetAddress, 
+                            ap.longitude, ap.latitude, 
+                            r.name AS roomName, r.description AS roomDescription, r.localisation AS roomLocalisation, r.capacity AS roomCapacity, r.floorSize AS roomFloorSize, b.image_id AS imageId, 
+                            (SELECT CONCAT('[',GROUP_CONCAT(CONCAT(f.code)),']')
+                                FROM event_categories AS ec
+                                LEFT JOIN Categories AS cs ON(cs.id = ec.categories_id)
+                                LEFT JOIN Familly AS f ON(f.id = cs.familly_id)
+                                WHERE ec.event_id = b.id
+                            ) AS categories, '" . PublicEventOriginEnum::OPENTALENT()->getValue() . "' as origin
+                    FROM Booking AS b
+                        INNER JOIN Organization o ON o.id = b.organization_id
+                        INNER JOIN Parameters par ON par.id = o.parameters_id
+                        LEFT JOIN Place AS p ON (p.id = b.place_id)
+                        LEFT JOIN AddressPostal AS ap ON (ap.id = p.addressPostal_id)
+                        LEFT JOIN Room AS r ON (r.id = b.room_id)
+                    WHERE b.discr = 'event' AND b.datetimeEnd >= NOW() AND b.visibility = 'PUBLIC_VISIBILITY' AND b.isCanceled = 0
+                UNION
+                    SELECT
+                           auto_increment_value(null) as id, null AS organizationId, aw.name, aw.description, NULL AS url, aw.datetimeStart, aw.datetimeEnd,
+                           aw.place AS placeName, NULL AS placeDescription, NULL AS placeFloorSize, NULL AS placeCapacity,
+                           aw.city, aw.postalCode, aw.streetAddress, aw.longitude, aw.latitude,
+                           NULL AS roomName, NULL AS roomDescription, NULL AS roomLocalisation, NULL AS roomCapacity, NULL AS roomFloorSize,
+                           aw.largeimage AS imageId, aw.categories AS categories, '" . PublicEventOriginEnum::AWIN()->getValue() . "' as origin
+                    FROM  AwinProduct as aw
+                    WHERE
+                     aw.datetimeEnd >= NOW() AND aw.datetimeStart IS NOT NULL;
+                ";
+
+        $stmt = $this->em->getConnection()->prepare($sql);
+        $rows = $stmt->executeQuery()->fetchAllAssociative();
+
+        return array_map('self::buildPublicEvent', $rows);
+    }
+
+    /**
+     * Build a PublicEvent with the data provided by getAllEvents() and ...
+     */
+    private static function buildPublicEvent(array $data): PublicEvent
+    {
+        return (new PublicEvent)
+            ->setId((int)$data['id'])
+            ->setOrganizationId(ArrayUtils::getAndCast($data, 'organizationId', 'int'))
+            ->setName($data['name'])
+            ->setDescription($data['description'])
+            ->setUrl($data['url'])
+            ->setDatetimeStart(DatesUtils::new($data['datetimeStart']))
+            ->setDatetimeEnd(DatesUtils::new($data['datetimeEnd']))
+            ->setCity($data['city'])
+            ->setPostalCode($data['postalCode'])
+            ->setStreetAddress($data['streetAddress'])
+            ->setLongitude(ArrayUtils::getAndCast($data, 'longitude', 'float'))
+            ->setLatitude(ArrayUtils::getAndCast($data, 'latitude', 'float'))
+            ->setRoomName($data['roomName'])
+            ->setRoomDescription($data['roomDescription'])
+            ->setRoomLocalisation($data['roomLocalisation'])
+            ->setRoomCapacity($data['roomCapacity'])
+            ->setRoomFloorSize($data['roomFloorSize'])
+            ->setImageId(ArrayUtils::getAndCast($data, 'imageId', 'int'))
+            ->setCategories($data['categories'])
+            ->setOrigin($data['origin']);
+    }
+
 
 }