src/Services/Search/SearchPost.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Services\Search;
  3. use App\Services\Search\BaseSearch;
  4. use App\Entity\Post;
  5. use \DateTime;
  6. use App\Cache\Helper as CacheHelper;
  7. class SearchPost extends BaseSearch
  8. {
  9.     public function get($aOptions)
  10.     {
  11.         $default_options = [
  12.             'ids_collection' => false,
  13.             'sorting' => 'name',
  14.             'aStatus' => [
  15.                 Post::STATUS_INIT,
  16.                 Post::STATUS_READY,
  17.                 Post::STATUS_WAITING,
  18.             ],
  19.             'max_results' => false,
  20.             'page' => 1,
  21.             'aExcluded_ids' => false,
  22.             //'show_hidden' => false,
  23.             'show_pagination' => false,
  24.             'return_count' => false // if true, will only return the count instead of the rows
  25.         ];
  26.         $aOptions $this->getOptions($default_options$aOptions);
  27.         $alias '_'.uniqid();
  28.         $qb $this->em->createQueryBuilder();
  29.         if($aOptions['return_count'])
  30.         {
  31.             $qb->select($qb->expr()->countDistinct($alias.'.id'));
  32.         } else {
  33.             $qb->select('DISTINCT '.$alias.'.id');
  34.         }
  35.         $qb->from(Post::class, $alias);
  36.         // Static collection of IDs
  37.         if(is_array($aOptions['ids_collection']))
  38.         {
  39.             if(empty($aOptions['ids_collection']))
  40.             {
  41.                 // 'ids_collection' rules over any other entry; if it comes empty, show empty results
  42.                 return [
  43.                     'results' => [],
  44.                     'pagination' => false
  45.                 ];
  46.             } else {
  47.                 $qb->andWhere(
  48.                     $qb->expr()->in($alias.'.id'$aOptions['ids_collection'])
  49.                 );
  50.             }
  51.         }
  52.         if(!empty($aOptions['aExcluded_ids']))
  53.         {
  54.             $qb->andWhere(
  55.                 $qb->expr()->notIn($alias.'.id'$aOptions['aExcluded_ids'])
  56.             );
  57.         }
  58.         if($aOptions['aStatus'])
  59.         {
  60.             $qb->andWhere(
  61.                 $qb->expr()->in($alias.'.status'$aOptions['aStatus'])
  62.             );
  63.         }
  64.         switch($aOptions['sorting'])
  65.         {
  66.             case 'date':
  67.                 $qb->orderBy($alias.'.publish_date','DESC');
  68.                 break;
  69.             case 'field'// preserve order set in 'ids_collection' param
  70.                 if(is_array($aOptions['ids_collection']) && !empty($aOptions['ids_collection']))
  71.                 {
  72.                     $qb
  73.                         ->addSelect("FIELD(".$alias.".id, ".implode(', '$aOptions['ids_collection']).") AS HIDDEN sorting")
  74.                         ->orderBy('sorting''ASC');
  75.                 }
  76.                 break;
  77.             case 'name':
  78.                 $qb->orderBy($alias.'.name','ASC');
  79.                 break;
  80.             case 'random'// expensive query
  81.                 $qb
  82.                     ->addSelect("RAND() AS HIDDEN random_value")
  83.                     ->orderBy('random_value''ASC');
  84.                 break;
  85.         }
  86.         $query $qb->getQuery();
  87.         /*if(!$aOptions['show_pagination'] && $aOptions['max_results'])
  88.         {
  89.             $query->setMaxResults($aOptions['max_results']);
  90.         }*/
  91.         $resultCache_id CacheHelper::fixId(Post::class).'_collection_'.md5($query->getSQL());
  92.         $resultCache_ttl 604800;
  93.         if(!$aOptions['no_cache'])
  94.         {
  95.             $query
  96.                 ->useQueryCache(true)
  97.                 ->setResultCacheLifetime($resultCache_ttl)
  98.                 ->setResultCacheId($resultCache_id);
  99.         }
  100.         return $aOptions['return_count'] ? [
  101.             'results' => $query->getSingleScalarResult(),
  102.             'pagination' => false
  103.         ] : $this->paginate([
  104.             'aResults' => $query->getArrayResult(),
  105.             'repository_class' => Post::class,
  106.             'max_results' => $aOptions['max_results'],
  107.             'page' => $aOptions['page'],
  108.             'show_pagination' => $aOptions['show_pagination'],
  109.             'paginator_params' => $aOptions['paginator_params'],
  110.             'paginator_route' => $aOptions['paginator_route']
  111.         ]);
  112.     }
  113. }