vendor/orbitale/cms-bundle/Repository/PageRepository.php line 112

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the OrbitaleCmsBundle package.
  4. *
  5. * (c) Alexandre Rock Ancelet <alex@orbitale.io>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Orbitale\Bundle\CmsBundle\Repository;
  11. use Doctrine\ORM\Tools\Pagination\Paginator;
  12. use Orbitale\Bundle\CmsBundle\Entity\Category;
  13. use Orbitale\Bundle\CmsBundle\Entity\Page;
  14. class PageRepository extends AbstractCmsRepository
  15. {
  16.     /**
  17.      * @param Category $category
  18.      * @param string   $order
  19.      * @param string   $orderBy
  20.      * @param int      $page
  21.      * @param int      $limit
  22.      *
  23.      * @return Paginator
  24.      */
  25.     public function findByCategory(Category $category$order$orderBy$page$limit): Paginator
  26.     {
  27.         $qb $this->createQueryBuilder('page')
  28.             ->where('page.category = :category')
  29.             ->andWhere('page.enabled = :enabled')
  30.             ->orderBy('page.'.$orderBy$order)
  31.             ->setMaxResults($limit)
  32.             ->setFirstResult($limit * ($page-1))
  33.             ->setParameter('category'$category)
  34.             ->setParameter('enabled'true)
  35.         ;
  36.         return new Paginator($qb->getQuery()->useResultCache($this->cacheEnabled$this->cacheTtl));
  37.     }
  38.     /**
  39.      * Will search for pages to show in front depending on the arguments.
  40.      * If slugs are defined, there's no problem in looking for nulled host or locale,
  41.      * because slugs are unique, so it does not.
  42.      *
  43.      * @param array       $slugs
  44.      * @param string|null $host
  45.      * @param string|null $locale
  46.      *
  47.      * @return Page[]
  48.      */
  49.     public function findFrontPages(array $slugs = [], $host null$locale null)
  50.     {
  51.         $qb $this->createQueryBuilder('page')
  52.             ->where('page.enabled = :enabled')
  53.             ->leftJoin('page.category''category')
  54.             ->andWhere('page.category is null OR category.enabled = :enabled')
  55.             ->setParameter('enabled'true)
  56.         ;
  57.         // Will search differently if we're looking for homepage.
  58.         $searchForHomepage === count($slugs);
  59.         if (true === $searchForHomepage) {
  60.             // If we are looking for homepage, let's get only the first one.
  61.             $qb
  62.                 ->andWhere('page.homepage = :homepage')
  63.                 ->setParameter('homepage'true)
  64.                 ->setMaxResults(1)
  65.             ;
  66.         } elseif (=== count($slugs)) {
  67.             $qb
  68.                 ->andWhere('page.slug = :slug')
  69.                 ->setParameter('slug'reset($slugs))
  70.                 ->setMaxResults(1)
  71.             ;
  72.         } else {
  73.             $qb
  74.                 ->andWhere('page.slug IN ( :slugs )')
  75.                 ->setParameter('slugs'$slugs)
  76.             ;
  77.         }
  78.         $hostWhere 'page.host IS NULL';
  79.         if (null !== $host) {
  80.             $hostWhere .= ' OR page.host = :host';
  81.             $qb->setParameter('host'$host);
  82.             $qb->addOrderBy('page.host''asc');
  83.         }
  84.         $qb->andWhere($hostWhere);
  85.         $localeWhere 'page.locale IS NULL';
  86.         if (null !== $locale) {
  87.             $localeWhere .= ' OR page.locale = :locale';
  88.             $qb->setParameter('locale'$locale);
  89.             $qb->addOrderBy('page.locale''asc');
  90.         }
  91.         $qb->andWhere($localeWhere);
  92.         // Then the last page will automatically be one that has both properties.
  93.         $qb
  94.             ->orderBy('page.host''asc')
  95.             ->addOrderBy('page.locale''asc')
  96.         ;
  97.         /** @var Page[] $results */
  98.         $results $qb->getQuery()
  99.             ->useResultCache($this->cacheEnabled$this->cacheTtl)
  100.             ->getResult()
  101.         ;
  102.         if (=== count($results)) {
  103.             return $results;
  104.         }
  105.         // If we're looking for a homepage, only get the first result (matching more properties).
  106.         if (true === $searchForHomepage && count($results) > 0) {
  107.             reset($results);
  108.             $results = [$results[0]];
  109.         }
  110.         $resultsSortedBySlug = [];
  111.         foreach ($results as $page) {
  112.             $resultsSortedBySlug[$page->getSlug()] = $page;
  113.         }
  114.         $pages $resultsSortedBySlug;
  115.         if (count($slugs) > 0) {
  116.             $pages = [];
  117.             foreach ($slugs as $value) {
  118.                 if (!array_key_exists($value$resultsSortedBySlug)) {
  119.                     // Means at least one page in the tree is not enabled
  120.                     return [];
  121.                 }
  122.                 $pages[$value] = $resultsSortedBySlug[$value];
  123.             }
  124.         }
  125.         return $pages;
  126.     }
  127. }