<?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\Event as Event;
use Score\CmsBundle\Entity\Event\Event as CmsEvent;
final class EventCollectionDataProvider 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 Event::class === $resourceClass;
}
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
$repository = $manager->getRepository($resourceClass);
$cmsResourceClass = CmsEvent::class;
$cmsEventRepository = $manager->getRepository($cmsResourceClass);
$queryBuilder = $cmsEventRepository->createQueryBuilder('o');
$queryBuilder->leftJoin('o.siteItemEvent','a');
$queryBuilder->leftJoin('App\Entity\LanguageInfo', 'l', 'WITH', 'o.id = l.entityId AND l.entity = \'Event\'');
$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('slug',$context['filters']))
{
if(is_array($context['filters']['slug']))
{
$orX = $queryBuilder->expr()->orX();
foreach($context['filters']['slug'] as $key => $site)
{
$orX->add($queryBuilder->expr()->eq('o.slug', ':slug'.$key ));
}
foreach($context['filters']['slug'] as $key => $site)
{
$queryBuilder->setParameter('slug'.$key,$site);
}
$queryBuilder->andWhere($orX);
}
else
{
$queryBuilder->andWhere('o.slug = :slug');
$queryBuilder->setParameter('slug',$context['filters']['slug']);
}
}
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('dateFrom',$context['filters']))
{
//dump($context['filters']['dateFrom']);
if(array_key_exists('before',$context['filters']['dateFrom']) )
{
$queryBuilder->andWhere('o.date_from <= :dateFromBefore');
$queryBuilder->setParameter('dateFromBefore',$context['filters']['dateFrom']['before'].' 23:59:59');
}
if(array_key_exists('strictly_before',$context['filters']['dateFrom']) )
{
$queryBuilder->andWhere('o.date_from <= :dateFromStrictlyBefore');
$queryBuilder->setParameter('dateFromStrictlyBefore',$context['filters']['dateFrom']['strictly_before'].' 00:00:00');
}
if(array_key_exists('after',$context['filters']['dateFrom']) )
{
$queryBuilder->andWhere('o.date_from >= :dateFromAfter');
$queryBuilder->setParameter('dateFromAfter',$context['filters']['dateFrom']['after'].' 00:00:00');
}
if(array_key_exists('strictly_after',$context['filters']['dateFrom']) )
{
$queryBuilder->andWhere('o.date_from >= :dateFromStrictlyAfter');
$queryBuilder->setParameter('dateFromStrictlyAfter',$context['filters']['dateFrom']['strictly_after'].' 23:59:59');
}
}
}
if(null != $this->getFilterCriteria('lang',$context['filters']))
{
$queryBuilder->andWhere('l.language = :lang');
$queryBuilder->setParameter('lang',$context['filters']['lang']);
}
if(array_key_exists('filters',$context) and array_key_exists('order',$context['filters']))
{
foreach($context['filters']['order'] as $col => $direction)
{
if('dateFrom' == $col)
{
$col = 'date_from';
}
$queryBuilder->addOrderBy('o.'.$col, $direction);
}
}
$pageFrom = $pageSize * ($currentPage-1);
$cmsEvents = $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($cmsEvents as $cmsEvent)
{
$event = $repository->getEventFromCmsEvent($cmsEvent);
$collection[] = $event;
}
$meta = [
'current_page' => (int)$currentPage,
'from' => $pageFrom,
'to' => $pageFrom+$pageSize,
'per_page' => $pageSize,
'last_page' => $lastPage,
'total' => $total
];
$result = ['data' => $collection,'meta' => $meta];
return $result;
/*
$collection = [];
foreach($cmsEvents as $cmsEvent)
{
$cmsEvent = $repository->getEventFromCmsEvent($cmsEvent);
$collection[] = $cmsEvent;
}
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;
}
}
}