src/Repository/PageRepository.php line 542

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Config;
  4. use App\Entity\Page;
  5. use App\Entity\Tag;
  6. use DateInterval;
  7. use DateTime;
  8. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  9. use Doctrine\ORM\NonUniqueResultException;
  10. use Doctrine\ORM\Tools\Pagination\Paginator;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. /**
  14.  * Class PageRepository.
  15.  */
  16. class PageRepository extends ServiceEntityRepository
  17. {
  18.     /**
  19.      * PageRepository constructor.
  20.      */
  21.     public function __construct(ManagerRegistry $registry)
  22.     {
  23.         parent::__construct($registryPage::class);
  24.     }
  25.     public function paginate(string $type, array $filters, array $orderByint $limitint $offset): mixed
  26.     {
  27.         $parameters = [];
  28.         $today      = new DateTime();
  29.         $inTwoDays  = new DateTime();
  30.         $inTwoDays->add(new DateInterval('P2D'));
  31.         $parameters['type']   = $type;
  32.         $parameters['status'] = Page::STATUS_TRASH;
  33.         $qb $this->createQueryBuilder('p')
  34.             ->select('p,pt,a')
  35.             ->leftJoin('p.author''a')
  36.             ->leftJoin('p.translations''pt')
  37.             ->andWhere('p.type = :type')
  38.             ->andWhere('p.status != :status');
  39.         foreach ($filters as $key => $filter) {
  40.             switch ($key) {
  41.                 case 'locale':
  42.                     $qb->andWhere('p.locale = :locale');
  43.                     $parameters['locale'] = $filter;
  44.                     break;
  45.                 case 'author':
  46.                     $qb->andWhere('a.id = :id');
  47.                     $parameters['id'] = $filter;
  48.                     break;
  49.                 case 'status':
  50.                     $condition $qb->expr()->orX();
  51.                     foreach ($filter as $k => $f) {
  52.                         if ('soon_expired' == $f) {
  53.                             $condition->add($qb->expr()->between('p.expired'':todayExpiration'':inTwoDaysExpiration'));
  54.                             $parameters['todayExpiration']     = $today;
  55.                             $parameters['inTwoDaysExpiration'] = $inTwoDays;
  56.                         } elseif ('soon_published' == $f) {
  57.                             $condition->add($qb->expr()->between('p.published'':todayPublication'':inTwoDaysPublication'));
  58.                             $parameters['todayPublication']     = $today;
  59.                             $parameters['inTwoDaysPublication'] = $inTwoDays;
  60.                         } else {
  61.                             $condition->add($qb->expr()->eq('p.status'':status'.$f));
  62.                             $parameters['status'.$f] = $f;
  63.                         }
  64.                     }
  65.                     $qb->andWhere($condition);
  66.                     break;
  67.                 default:
  68.                     $qb->andWhere('p.'.$key.'= :'.$filter);
  69.                     $parameters[$filter] = $filter;
  70.                     break;
  71.             }
  72.         }
  73.         foreach ($orderBy as $key => $value) {
  74.             $qb->addOrderBy('p.'.$key$value);
  75.         }
  76.         $qb->setParameters($parameters);
  77.         $qb->setFirstResult($offset)
  78.             ->setMaxResults($limit);
  79.         return $qb->getQuery()->getResult();
  80.     }
  81.     public function getTrash(): array
  82.     {
  83.         $qb $this->createQueryBuilder('p')
  84.             ->select('p')
  85.             ->leftJoin('p.author''a')
  86.             ->andWhere('p.status = :status')
  87.             ->setParameter('status'Page::STATUS_TRASH)
  88.             ->orderBy('p.title');
  89.         return $this->groupByType($qb->getQuery()->getResult());
  90.     }
  91.     public function findForRedirection(array $filters): array
  92.     {
  93.         $qb $this->createQueryBuilder('p')
  94.             ->select('p, pa')
  95.             ->leftJoin('p.paths''pa')
  96.             ->orderBy('p.title');
  97.         foreach ($filters as $key => $filter) {
  98.             if ('' == $filter) {
  99.                 continue;
  100.             }
  101.             $qb->andWhere('p.'.$key.'= :'.$filter)
  102.                 ->setParameter($filter$filter);
  103.         }
  104.         return $this->groupByTypeForRedirection($qb->getQuery()->getResult());
  105.     }
  106.     public function findForSliceJsonUpdate(string $sliceName): mixed
  107.     {
  108.         $qb $this->createQueryBuilder('p')
  109.             ->select('p')
  110.             ->where("JSON_SEARCH(p.slices, 'all', :key) IS NOT NULL")
  111.             ->setParameter('key'$sliceName);
  112.         return $qb->getQuery()->getResult();
  113.     }
  114.     public function findAllValueForJsonField(string $typestring $field, ?string $contentType null): mixed
  115.     {
  116.         $qb $this->createQueryBuilder('p')
  117.             ->select('JSON_EXTRACT(p.'.$type.', :key)')
  118.             ->setParameter('key''$.'.$field);
  119.         if ($contentType) {
  120.             $qb->andWhere('p.type = :type')
  121.                 ->setParameter('type'$contentType);
  122.         }
  123.         return $qb->getQuery()->getResult();
  124.     }
  125.     public function findAll(?string $locale nullbool $groupByType falsebool $onlyMaster false$isPublished false$onlyVisible false): mixed
  126.     {
  127.         $qb $this->createQueryBuilder('p')
  128.             ->select('p');
  129.         if ($isPublished) {
  130.             $qb->where('p.status = :status')
  131.                 ->setParameter('status'Page::STATUS_PUBLISHED);
  132.         }
  133.         $qb->orderBy('p.title');
  134.         if ($locale) {
  135.             $qb->andWhere('p.locale = :locale')
  136.                 ->setParameter('locale'$locale);
  137.         }
  138.         if ($onlyMaster) {
  139.             $qb->andWhere('p.master IS NULL');
  140.         }
  141.         if ($onlyVisible) {
  142.             $qb->andWhere('SIZE(p.paths) > :count')
  143.                 ->setParameter('count'0);
  144.         }
  145.         return $groupByType $this->groupByType($qb->getQuery()->getResult()) : $qb->getQuery()->getResult();
  146.     }
  147.     public function findAllExpired(): mixed
  148.     {
  149.         $today = new DateTime();
  150.         $qb $this->createQueryBuilder('p')
  151.             ->select('p')
  152.             ->orderBy('p.title')
  153.             ->andWhere('p.expired < :today')
  154.             ->setParameter('today'$today);
  155.         return $qb->getQuery()->getResult();
  156.     }
  157.     public function findAllToPublish(): mixed
  158.     {
  159.         $today = new DateTime();
  160.         $qb $this->createQueryBuilder('p')
  161.             ->select('p')
  162.             ->orderBy('p.title')
  163.             ->andWhere('p.published < :today')
  164.             ->setParameter('today'$today);
  165.         return $qb->getQuery()->getResult();
  166.     }
  167.     public function findAllByType(string $type, ?string $locale nullbool $onlyMaster false): mixed
  168.     {
  169.         $qb $this->createQueryBuilder('p')
  170.             ->select('p');
  171.         if (!Config::isContentMulti($type)) {
  172.             $where '';
  173.             foreach (Config::getSingleContentTypes() as $k => $singleType) {
  174.                 $where .= 'p.type = :type'.$k.' OR ';
  175.                 $qb->setParameter('type'.$k$singleType);
  176.             }
  177.             $qb->where(substr($where0, -4));
  178.         } else {
  179.             $qb->where('p.type = :type')
  180.                 ->setParameter('type'$type);
  181.         }
  182.         if ($locale) {
  183.             $qb->andWhere('p.locale = :locale')
  184.                 ->setParameter('locale'$locale);
  185.         }
  186.         if ($onlyMaster) {
  187.             $qb->andWhere('p.master IS NULL');
  188.         }
  189.         return $qb->getQuery()->getResult();
  190.     }
  191.     /**
  192.      * @throws NonUniqueResultException
  193.      */
  194.     public function findOneSingleByType(string $type, ?string $lang nullbool $front false): mixed
  195.     {
  196.         $qb $this->createQueryBuilder('p')
  197.             ->select('p')
  198.             ->where('p.type = :type')
  199.             ->setParameter('type'$type);
  200.         if ($front) {
  201.             $qb->andWhere('p.status = :status')
  202.                 ->andWhere('p.visibility = :visibility')
  203.                 ->setParameter('status'Page::STATUS_PUBLISHED)
  204.                 ->setParameter('visibility''public');
  205.         }
  206.         if ($lang) {
  207.             $qb->andWhere('p.locale = :lang')
  208.                 ->setParameter('lang'$lang);
  209.         } else {
  210.             $qb->andWhere('p.master IS NULL');
  211.         }
  212.         return $qb->getQuery()->getOneOrNullResult();
  213.     }
  214.     /**
  215.      * @throws NonUniqueResultException
  216.      */
  217.     public function findOne(?int $id): mixed
  218.     {
  219.         $qb $this->createQueryBuilder('p')
  220.             ->select('p')
  221.             ->leftJoin('p.translations''pt')
  222.             ->leftJoin('p.master''pm')
  223.             ->where('p.id = :id')
  224.             ->setParameter('id'$id);
  225.         return $qb->getQuery()->getOneOrNullResult();
  226.     }
  227.     /**
  228.      * @throws NonUniqueResultException
  229.      */
  230.     public function findOneByPath(string $path, ?string $locale null): mixed
  231.     {
  232.         $qb $this->createQueryBuilder('p')
  233.             ->select('p')
  234.             ->leftJoin('p.paths''pa')
  235.             ->where('pa.path = :path')
  236.             ->setParameter('path'$path);
  237.         if ($locale) {
  238.             $qb->andWhere('pa.locale = :locale')
  239.                 ->setParameter('locale'$locale);
  240.         }
  241.         return $qb->getQuery()->getOneOrNullResult();
  242.     }
  243.     /**
  244.      * @throws NonUniqueResultException
  245.      */
  246.     public function getCachingLevelByPath(string $path, ?string $locale null): mixed
  247.     {
  248.         $qb $this->createQueryBuilder('p')
  249.             ->select('p.cachingLevel')
  250.             ->leftJoin('p.paths''pa')
  251.             ->where('pa.path = :path')
  252.             ->setParameter('path'$path);
  253.         if ($locale) {
  254.             $qb->andWhere('pa.locale = :locale')
  255.                 ->setParameter('locale'$locale);
  256.         }
  257.         return $qb->getQuery()->getOneOrNullResult();
  258.     }
  259.     /**
  260.      * @throws NonUniqueResultException
  261.      */
  262.     public function countPage(): mixed
  263.     {
  264.         $qb $this->createQueryBuilder('p')
  265.             ->select('count(p)');
  266.         return $qb->getQuery()->getOneOrNullResult();
  267.     }
  268.     private function groupByType(array $results): array
  269.     {
  270.         $data = [];
  271.         if (count($results)) {
  272.             /** @var Page $i */
  273.             foreach ($results as $i) {
  274.                 if ($i->isMulti()) {
  275.                     $data[$i->getType()][] = $i;
  276.                 } else {
  277.                     $data['main'][] = $i;
  278.                 }
  279.             }
  280.         }
  281.         return $data;
  282.     }
  283.     private function groupByTypeForRedirection(array $results): array
  284.     {
  285.         $data = [];
  286.         if (count($results)) {
  287.             /** @var Page $i */
  288.             foreach ($results as $i) {
  289.                 if (count($i->getRedirections())) {
  290.                     if ($i->isMulti()) {
  291.                         $data[$i->getType()][] = $i;
  292.                     } else {
  293.                         $data['main'][] = $i;
  294.                     }
  295.                 }
  296.             }
  297.         }
  298.         return $data;
  299.     }
  300.     /**
  301.      * @throws NonUniqueResultException
  302.      */
  303.     public function findType(string $type): mixed
  304.     {
  305.         $qb $this->createQueryBuilder('p')
  306.             ->select('p')
  307.             ->where('p.type = :type')
  308.             ->setParameter('type'$type)
  309.             ->setMaxResults(1);
  310.         return $qb->getQuery()->getOneOrNullResult();
  311.     }
  312.     public function findExpertiseFaqPages(?int $tag null): mixed
  313.     {
  314.         $qb $this->createQueryBuilder('p')
  315.             ->select('p');
  316.         $qb->where('p.type IN (:type)')
  317.             ->setParameter('type', ['faq'])
  318.             ->andWhere('p.status = :status')
  319.             ->setParameter('status'Page::STATUS_PUBLISHED)
  320.             ->orderBy('JSON_EXTRACT(p.contentProperties, :key)''DESC')
  321.             ->setParameter('key''$.publicationDate.date');
  322.         if ($tag != null) {
  323.             $qb->leftJoin('p.tags''pt')
  324.                 ->andWhere('pt.id = :id')
  325.                 ->setParameter('id'$tag);
  326.         }
  327.         return $qb->getQuery()->getResult();
  328.     }
  329.     public function findPages(array $types, ?Tag $tag null): mixed
  330.     {
  331.         $qb $this->createQueryBuilder('p')
  332.             ->select('p');
  333.         $qb->where('p.type IN (:type)')
  334.             ->setParameter('type'$types)
  335.             ->andWhere('p.status = :status')
  336.             ->setParameter('status'Page::STATUS_PUBLISHED)
  337.             ->orderBy('JSON_EXTRACT(p.contentProperties, :key)''DESC')
  338.             ->setParameter('key''$.publicationDate.date');
  339.         if ($tag != null) {
  340.             $qb->leftJoin('p.tags''pt')
  341.                 ->andWhere('pt.id = :id')
  342.                 ->setParameter('id'$tag);
  343.         }
  344.         $qb->setMaxResults(6);
  345.         return $qb->getQuery()->getResult();
  346.     }
  347.     public function findThematic(?int $tag null, ?int $limit null): mixed
  348.     {
  349.         $qb $this->createQueryBuilder('p')
  350.             ->select('p');
  351.         $qb->where('p.type IN (:type)')
  352.             ->setParameter('type', ['thematic'])
  353.             ->andWhere('p.status = :status')
  354.             ->setParameter('status'Page::STATUS_PUBLISHED);
  355. //            ->orderBy('JSON_EXTRACT(p.contentProperties, :key)', 'DESC')
  356. //            ->setParameter('key', '$.publicationDate.date');
  357.         if ($tag != null) {
  358.             $qb->leftJoin('p.tags''pt')
  359.                 ->andWhere('pt.id = :id')
  360.                 ->setParameter('id'$tag);
  361.         }
  362.         $qb->orderBy('p.created''DESC');
  363.         if ($limit != null) {
  364.             $qb->setMaxResults($limit);
  365.         }
  366.         return $qb->getQuery()->getResult();
  367.     }
  368.     public function findQuestions(?array $thematic null, ?int $limit null): mixed
  369.     {
  370.         $qb $this->createQueryBuilder('p')
  371.             ->select('p');
  372.         $qb->where('p.type IN (:type)')
  373.             ->setParameter('type', ['questions'])
  374.             ->andWhere('p.status = :status')
  375.             ->setParameter('status'Page::STATUS_PUBLISHED);
  376. //            ->orderBy('JSON_EXTRACT(p.contentProperties, :key)', 'DESC')
  377. //            ->setParameter('key', '$.publicationDate.date');
  378.         if ($thematic != null) {
  379.             $qb->andWhere('JSON_EXTRACT(p.contentProperties, :key) IN (:id)')
  380.                 ->setParameter('key''$.thematic')
  381.                 ->setParameter('id'$thematic);
  382.         }
  383.         $qb->orderBy('p.created''DESC');
  384.         if ($limit != null) {
  385.             $qb->setMaxResults($limit);
  386.         }
  387.         return $qb->getQuery()->getResult();
  388.     }
  389.     public function find2LastPortraits(): mixed
  390.     {
  391.         $qb $this->createQueryBuilder('p')
  392.             ->select('p')
  393.             ->where('p.type = :type')
  394.             ->setParameter('type''portrait')
  395.             ->andWhere('p.status = :status')
  396.             ->setParameter('status'Page::STATUS_PUBLISHED);
  397.         $qb->orderBy('p.created''DESC');
  398.         $qb->setMaxResults(2);
  399.         return $qb->getQuery()->getResult();
  400.     }
  401.     /**
  402.      * @throws NonUniqueResultException
  403.      */
  404.     public function findLastForHome(array $typesint $limit): mixed
  405.     {
  406.         $qb $this->createQueryBuilder('p')
  407.             ->select('p');
  408.         foreach ($types as $key => $type) {
  409.             if ($key == 0) {
  410.                 $qb->where('p.type = :type' $key)
  411.                     ->setParameter('type' $key$type);
  412.             } else {
  413.                 $qb->orWhere('p.type = :type' $key)
  414.                     ->setParameter('type' $key$type);
  415.             }
  416.         }
  417.         $qb->andWhere('p.status = :status')
  418.         ->setParameter('status'Page::STATUS_PUBLISHED);
  419.         $qb->orderBy('JSON_EXTRACT(p.contentProperties, :key)''DESC')
  420.             ->setParameter('key''$.publicationDate.date');
  421.         $qb->setMaxResults($limit);
  422.         if ($limit == 1) {
  423.             return $qb->getQuery()->getOneOrNullResult();
  424.         }
  425.         return $qb->getQuery()->getResult();
  426.     }
  427.     public function paginateListPages(array $typesint $pageint $limit)
  428.     {
  429.         $qb $this->createQueryBuilder('p')
  430.             ->select('p')
  431.             ->where('p.type IN (:types)')
  432.             ->setParameter('types'$types)
  433.             ->andWhere('p.status = :status')
  434.             ->setParameter('status'Page::STATUS_PUBLISHED);
  435.         $query $qb->getQuery();
  436.         $firstResult = ($page 1) * $limit;
  437.         $query->setFirstResult($firstResult)->setMaxResults($limit);
  438.         $paginator = new Paginator($query);
  439.         if (($paginator->count() <= $firstResult) && $page != 1) {
  440.             throw new NotFoundHttpException('La page demandée n\'existe pas.');
  441.         }
  442.         return $paginator->getQuery()->getResult();
  443.     }
  444.     public function countPages(array $types)
  445.     {
  446.         $qb $this->createQueryBuilder('p')
  447.             ->select('COUNT(p)')
  448.             ->where('p.type IN (:types)')
  449.             ->setParameter('types'$types)
  450.             ->andWhere('p.status = :status')
  451.             ->setParameter('status'Page::STATUS_PUBLISHED);
  452.         return $qb->getQuery()->getResult();
  453.     }
  454. }