src/DataProvider/PageCollectionDataProvider.php line 36

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\Page as Page;
  12. use Score\CmsBundle\Entity\Page\Page as CmsPage;
  13. final class PageCollectionDataProvider 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 $this->managerRegistry->getManagerForClass('Score\PageBundle\Entity\Page') instanceof EntityManagerInterface;
  27.          return Page::class === $resourceClass;
  28.     }
  29.     public function getCollection(string $resourceClassstring $operationName null, array $context = []): iterable
  30.     {
  31.         $manager $this->managerRegistry->getManagerForClass($resourceClass);
  32.         $repository $manager->getRepository($resourceClass);
  33.         $cmsResourceClass CmsPage::class; 
  34.         $cmsPageRepository $manager->getRepository($cmsResourceClass);
  35.         $queryBuilder $cmsPageRepository->createQueryBuilder('o');
  36.         $queryBuilder->leftJoin('o.siteItemPage','a');
  37.         $queryBuilder->leftJoin('App\Entity\LanguageInfo''l''WITH''o.id = l.entityId AND l.entity = \'Page\'');
  38.         $pageSize 20;
  39.         $currentPage 1;
  40.         if(array_key_exists('page',$context['filters']))
  41.         {
  42.             $currentPage $context['filters']['page'];
  43.         }
  44.         if(array_key_exists('itemsPerPage',$context['filters']))
  45.         {
  46.             $pageSize $context['filters']['itemsPerPage'];
  47.         }
  48.         if(array_key_exists('filters',$context))
  49.         {
  50.             if(null != $this->getFilterCriteria('seoId',$context['filters']))
  51.             {
  52.                 if(is_array($context['filters']['seoId']))
  53.                 {
  54.                     $orX $queryBuilder->expr()->orX();
  55.                     foreach($context['filters']['seoId'] as  $key => $site)
  56.                     {
  57.                         $orX->add($queryBuilder->expr()->eq('o.seoId'':seoId'.$key ));
  58.                     }
  59.                     foreach($context['filters']['seoId'] as  $key => $site)
  60.                     {
  61.                         $queryBuilder->setParameter('seoId'.$key,$site);
  62.                     }
  63.                     $queryBuilder->andWhere($orX);
  64.                 }
  65.                 else
  66.                 {
  67.                     $queryBuilder->andWhere('o.seoId = :seoId');
  68.                     $queryBuilder->setParameter('seoId',$context['filters']['seoId']);
  69.                 }
  70.             }
  71.             if(null != $this->getFilterCriteria('site',$context['filters']))
  72.             {
  73.                 if(is_array($context['filters']['site']))
  74.                 {
  75.                     $orX $queryBuilder->expr()->orX();
  76.                     foreach($context['filters']['site'] as  $key => $site)
  77.                     {
  78.                         $orX->add($queryBuilder->expr()->eq('a.site'':site'.$key ));
  79.                     }
  80.                     foreach($context['filters']['site'] as  $key => $site)
  81.                     {
  82.                         $queryBuilder->setParameter('site'.$key,$site);
  83.                     }
  84.                     $queryBuilder->andWhere($orX);
  85.                 }
  86.                 else
  87.                 {
  88.                     $queryBuilder->andWhere('a.site = :site');
  89.                     $queryBuilder->setParameter('site',$context['filters']['site']);
  90.                 }
  91.             }
  92.         }
  93.         if(null != $this->getFilterCriteria('lang',$context['filters']))
  94.         {
  95.             $queryBuilder->andWhere('l.language = :lang');
  96.             $queryBuilder->setParameter('lang',$context['filters']['lang']);
  97.         }
  98.         $pageFrom $pageSize * ($currentPage-1);
  99.         $cmsPages =  $queryBuilder->getQuery()
  100.         ->setFirstResult($pageFrom// set the offset
  101.         ->setMaxResults($pageSize// set the limit
  102.         ->getResult();
  103.         $queryBuilder->select('COUNT(o)');
  104.         $queryBuilder->resetDQLPart('orderBy');
  105.         $totalSize $queryBuilder->getQuery()->getResult();
  106.         $total $totalSize[0][1];
  107.         $lastPage ceil($total/$pageSize);
  108.         $collection = [];
  109.         foreach($cmsPages  as $cmsPage)
  110.         {
  111.             $cmsPage $repository->getPageFromCmsPage($cmsPage);
  112.             $collection[] = $cmsPage;
  113.         }
  114.         $meta = [
  115.             'current_page' => (int)$currentPage,
  116.             'from' => $pageFrom,
  117.             'to' => $pageFrom+$pageSize,
  118.             'per_page' => $pageSize,
  119.             'last_page' => $lastPage,
  120.             'total' =>  $total
  121.         ];
  122.         return  $collection;
  123.     }
  124.     private function getFilterCriteria($index,$filterData)
  125.     {
  126.         $criteria null;
  127.         if(array_key_exists($index,$filterData) and $filterData[$index] != null)
  128.         {
  129.             if(is_array($filterData[$index]))
  130.             {
  131.                 $values array_filter($filterData[$index]);
  132.                 if(!empty($values ))
  133.                 {
  134.                    $criteria $values ;
  135.                 }
  136.             }
  137.             else
  138.             {
  139.                 $criteria trim($filterData[$index]);
  140.             }
  141.         }
  142.         if(null != $criteria and !empty($criteria))
  143.         {
  144.             return $criteria;
  145.         }
  146.         else
  147.         {
  148.             return null;
  149.         }
  150.     }
  151. }