<?php
namespace App\DataProvider;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Api\Page as Page;
use Score\CmsBundle\Entity\Page\Page as CmsPage;
final class PageCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $collectionExtensions;
private $managerRegistry;
public function __construct(ManagerRegistry $managerRegistry, iterable $collectionExtensions = [])
{
$this->managerRegistry = $managerRegistry;
$this->collectionExtensions = $collectionExtensions;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
//return $this->managerRegistry->getManagerForClass('Score\PageBundle\Entity\Page') instanceof EntityManagerInterface;
return Page::class === $resourceClass;
}
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
$repository = $manager->getRepository($resourceClass);
$cmsResourceClass = CmsPage::class;
$cmsPageRepository = $manager->getRepository($cmsResourceClass);
$queryBuilder = $cmsPageRepository->createQueryBuilder('o');
$queryBuilder->leftJoin('o.siteItemPage','a');
$queryBuilder->leftJoin('App\Entity\LanguageInfo', 'l', 'WITH', 'o.id = l.entityId AND l.entity = \'Page\'');
$pageSize = 20;
$currentPage = 1;
if(array_key_exists('page',$context['filters']))
{
$currentPage = $context['filters']['page'];
}
if(array_key_exists('itemsPerPage',$context['filters']))
{
$pageSize = $context['filters']['itemsPerPage'];
}
if(array_key_exists('filters',$context))
{
if(null != $this->getFilterCriteria('seoId',$context['filters']))
{
if(is_array($context['filters']['seoId']))
{
$orX = $queryBuilder->expr()->orX();
foreach($context['filters']['seoId'] as $key => $site)
{
$orX->add($queryBuilder->expr()->eq('o.seoId', ':seoId'.$key ));
}
foreach($context['filters']['seoId'] as $key => $site)
{
$queryBuilder->setParameter('seoId'.$key,$site);
}
$queryBuilder->andWhere($orX);
}
else
{
$queryBuilder->andWhere('o.seoId = :seoId');
$queryBuilder->setParameter('seoId',$context['filters']['seoId']);
}
}
if(null != $this->getFilterCriteria('site',$context['filters']))
{
if(is_array($context['filters']['site']))
{
$orX = $queryBuilder->expr()->orX();
foreach($context['filters']['site'] as $key => $site)
{
$orX->add($queryBuilder->expr()->eq('a.site', ':site'.$key ));
}
foreach($context['filters']['site'] as $key => $site)
{
$queryBuilder->setParameter('site'.$key,$site);
}
$queryBuilder->andWhere($orX);
}
else
{
$queryBuilder->andWhere('a.site = :site');
$queryBuilder->setParameter('site',$context['filters']['site']);
}
}
}
if(null != $this->getFilterCriteria('lang',$context['filters']))
{
$queryBuilder->andWhere('l.language = :lang');
$queryBuilder->setParameter('lang',$context['filters']['lang']);
}
$pageFrom = $pageSize * ($currentPage-1);
$cmsPages = $queryBuilder->getQuery()
->setFirstResult($pageFrom) // set the offset
->setMaxResults($pageSize) // set the limit
->getResult();
$queryBuilder->select('COUNT(o)');
$queryBuilder->resetDQLPart('orderBy');
$totalSize = $queryBuilder->getQuery()->getResult();
$total = $totalSize[0][1];
$lastPage = ceil($total/$pageSize);
$collection = [];
foreach($cmsPages as $cmsPage)
{
$cmsPage = $repository->getPageFromCmsPage($cmsPage);
$collection[] = $cmsPage;
}
$meta = [
'current_page' => (int)$currentPage,
'from' => $pageFrom,
'to' => $pageFrom+$pageSize,
'per_page' => $pageSize,
'last_page' => $lastPage,
'total' => $total
];
return $collection;
}
private function getFilterCriteria($index,$filterData)
{
$criteria = null;
if(array_key_exists($index,$filterData) and $filterData[$index] != null)
{
if(is_array($filterData[$index]))
{
$values = array_filter($filterData[$index]);
if(!empty($values ))
{
$criteria = $values ;
}
}
else
{
$criteria = trim($filterData[$index]);
}
}
if(null != $criteria and !empty($criteria))
{
return $criteria;
}
else
{
return null;
}
}
}