src/DataProvider/EventCollectionDataProvider.php line 155

Open in your IDE?
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
  4. use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
  6. use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
  7. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  8. use ApiPlatform\Core\Exception\RuntimeException;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use App\Entity\Api\Event as Event;
  12. use Score\CmsBundle\Entity\Event\Event as CmsEvent;
  13. final class EventCollectionDataProvider implements ContextAwareCollectionDataProviderInterfaceRestrictedDataProviderInterface
  14. {
  15.     private $collectionExtensions;
  16.     private $managerRegistry;
  17.    
  18.     public function __construct(ManagerRegistry $managerRegistryiterable $collectionExtensions = [])
  19.     {
  20.         $this->managerRegistry $managerRegistry;
  21.         $this->collectionExtensions $collectionExtensions;
  22.     }
  23.     
  24.     public function supports(string $resourceClassstring $operationName null, array $context = []): bool
  25.     {
  26.         return Event::class === $resourceClass;
  27.     }
  28.     public function getCollection(string $resourceClassstring $operationName null, array $context = []): iterable
  29.     {
  30.         $manager $this->managerRegistry->getManagerForClass($resourceClass);
  31.         $repository $manager->getRepository($resourceClass);
  32.         $cmsResourceClass CmsEvent::class; 
  33.         $cmsEventRepository $manager->getRepository($cmsResourceClass);
  34.         $queryBuilder $cmsEventRepository->createQueryBuilder('o');
  35.         $queryBuilder->leftJoin('o.siteItemEvent','a');
  36.         $queryBuilder->leftJoin('App\Entity\LanguageInfo''l''WITH''o.id = l.entityId AND l.entity = \'Event\'');
  37.         $pageSize 20;
  38.         $currentPage 1;
  39.         if(array_key_exists('page',$context['filters']))
  40.         {
  41.             $currentPage $context['filters']['page'];
  42.         }
  43.         if(array_key_exists('itemsPerPage',$context['filters']))
  44.         {
  45.             $pageSize $context['filters']['itemsPerPage'];
  46.         }
  47.         if(array_key_exists('filters',$context))
  48.         {
  49.             if(null != $this->getFilterCriteria('slug',$context['filters']))
  50.             {
  51.                 if(is_array($context['filters']['slug']))
  52.                 {
  53.                     $orX $queryBuilder->expr()->orX();
  54.                     foreach($context['filters']['slug'] as  $key => $site)
  55.                     {
  56.                         $orX->add($queryBuilder->expr()->eq('o.slug'':slug'.$key ));
  57.                     }
  58.                     foreach($context['filters']['slug'] as  $key => $site)
  59.                     {
  60.                         $queryBuilder->setParameter('slug'.$key,$site);
  61.                     }
  62.                     $queryBuilder->andWhere($orX);
  63.                 }
  64.                 else
  65.                 {
  66.                     $queryBuilder->andWhere('o.slug = :slug');
  67.                     $queryBuilder->setParameter('slug',$context['filters']['slug']);
  68.                 }
  69.             }
  70.             if(null != $this->getFilterCriteria('site',$context['filters']))
  71.             {
  72.                 if(is_array($context['filters']['site']))
  73.                 {
  74.                     $orX $queryBuilder->expr()->orX();
  75.                     foreach($context['filters']['site'] as  $key => $site)
  76.                     {
  77.                         $orX->add($queryBuilder->expr()->eq('a.site'':site'.$key ));
  78.                     }
  79.                     foreach($context['filters']['site'] as  $key => $site)
  80.                     {
  81.                         $queryBuilder->setParameter('site'.$key,$site);
  82.                     }
  83.                     $queryBuilder->andWhere($orX);
  84.                 }
  85.                 else
  86.                 {
  87.                     $queryBuilder->andWhere('a.site = :site');
  88.                     $queryBuilder->setParameter('site',$context['filters']['site']);
  89.                 }
  90.             }
  91.             if(null != $this->getFilterCriteria('dateFrom',$context['filters']))
  92.             {
  93.                 //dump($context['filters']['dateFrom']);
  94.                 if(array_key_exists('before',$context['filters']['dateFrom']) )
  95.                 {
  96.                     $queryBuilder->andWhere('o.date_from <= :dateFromBefore');
  97.                     $queryBuilder->setParameter('dateFromBefore',$context['filters']['dateFrom']['before'].' 23:59:59');
  98.                 }
  99.                 if(array_key_exists('strictly_before',$context['filters']['dateFrom']) )
  100.                 {
  101.                     $queryBuilder->andWhere('o.date_from <= :dateFromStrictlyBefore');
  102.                     $queryBuilder->setParameter('dateFromStrictlyBefore',$context['filters']['dateFrom']['strictly_before'].' 00:00:00');
  103.                 }
  104.                 if(array_key_exists('after',$context['filters']['dateFrom']) )
  105.                 {
  106.                     $queryBuilder->andWhere('o.date_from >= :dateFromAfter');
  107.                     $queryBuilder->setParameter('dateFromAfter',$context['filters']['dateFrom']['after'].' 00:00:00');
  108.                 }
  109.                 if(array_key_exists('strictly_after',$context['filters']['dateFrom']) )
  110.                 {
  111.                     $queryBuilder->andWhere('o.date_from >= :dateFromStrictlyAfter');
  112.                     $queryBuilder->setParameter('dateFromStrictlyAfter',$context['filters']['dateFrom']['strictly_after'].' 23:59:59');
  113.                 }
  114.             }
  115.         }    
  116.         if(null != $this->getFilterCriteria('lang',$context['filters']))
  117.         {
  118.             $queryBuilder->andWhere('l.language = :lang');
  119.             $queryBuilder->setParameter('lang',$context['filters']['lang']);
  120.         }
  121.         if(array_key_exists('filters',$context) and array_key_exists('order',$context['filters']))
  122.         {
  123.             foreach($context['filters']['order'] as $col => $direction)
  124.             {
  125.                 if('dateFrom' == $col)
  126.                 {
  127.                     $col 'date_from';
  128.                 }
  129.                 
  130.                 $queryBuilder->addOrderBy('o.'.$col$direction);
  131.             }
  132.         }
  133.         $pageFrom $pageSize * ($currentPage-1);
  134.         $cmsEvents =   $queryBuilder->getQuery()
  135.         ->setFirstResult($pageFrom// set the offset
  136.         ->setMaxResults($pageSize// set the limit
  137.         ->getResult();
  138.         $queryBuilder->select('COUNT(o)');
  139.         $queryBuilder->resetDQLPart('orderBy');
  140.         $totalSize $queryBuilder->getQuery()->getResult();
  141.         $total $totalSize[0][1];
  142.         $lastPage ceil($total/$pageSize);
  143.         $collection = [];
  144.         foreach($cmsEvents  as $cmsEvent)
  145.         {
  146.             $event $repository->getEventFromCmsEvent($cmsEvent);
  147.             $collection[] = $event;
  148.         }
  149.         $meta = [
  150.             'current_page' => (int)$currentPage,
  151.             'from' => $pageFrom,
  152.             'to' => $pageFrom+$pageSize,
  153.             'per_page' => $pageSize,
  154.             'last_page' => $lastPage,
  155.             'total' =>  $total
  156.         ];
  157.         $result = ['data' => $collection,'meta' => $meta];
  158.        
  159.         return $result;
  160.         /*
  161.         $collection = [];
  162.         foreach($cmsEvents  as $cmsEvent)
  163.         {
  164.             $cmsEvent = $repository->getEventFromCmsEvent($cmsEvent);
  165.             $collection[] = $cmsEvent;
  166.         }
  167.         return $collection;
  168.         */
  169.         
  170.     }
  171.     private function getFilterCriteria($index,$filterData)
  172.     {
  173.         $criteria null;
  174.         if(array_key_exists($index,$filterData) and $filterData[$index] != null)
  175.         {
  176.             if(is_array($filterData[$index]))
  177.             {
  178.                 $values array_filter($filterData[$index]);
  179.                 if(!empty($values ))
  180.                 {
  181.                    $criteria $values ;
  182.                 }
  183.             }
  184.             else
  185.             {
  186.                 $criteria trim($filterData[$index]);
  187.             }
  188.         }
  189.         if(null != $criteria and !empty($criteria))
  190.         {
  191.             return $criteria;
  192.         }
  193.         else
  194.         {
  195.             return null;
  196.         }
  197.     }
  198. }