vendor/score/cms/src/Repository/ArticleRepository.php line 87

Open in your IDE?
  1. <?php
  2. namespace Score\CmsBundle\Repository;
  3. use DateTime;
  4. use Doctrine\ORM\EntityRepository;
  5. use Doctrine\ORM\Query\ResultSetMapping;
  6. use Doctrine\ORM\Query\ResultSetMappingBuilder;
  7. use Score\CmsBundle\Entity\Article\Article;
  8. use Score\CmsBundle\Entity\Article\ArticleCategory;
  9. use Score\CmsBundle\Entity\User;
  10. /**
  11.  * EventRepository
  12.  */
  13. class ArticleRepository extends EntityRepository {
  14.     public function findTopArticlesByCategory($category, $maxResult = 4) {
  15.         $qd = $this->createQueryBuilder('a');
  16.         $query = $qd->select('a')
  17.             ->andWhere('a.published = 1')
  18.             ->andWhere('a.isDeleted = 0')
  19.             ->leftJoin("a.articleCategories", "t")
  20.             ->andWhere('t.seoName = :category')
  21.             ->add('orderBy', 'a.priority ASC, a.publishedFrom DESC')
  22.             ->setParameter("category", $category)
  23.             ->setFirstResult(0)
  24.             ->setMaxResults($maxResult)
  25.             ->getQuery();
  26.         $items = $query->getResult();
  27.         return $items;
  28.     }
  29.     public function findTopArticles($maxResult = 4) {
  30.         $qd = $this->createQueryBuilder('o');
  31.         $query = $qd->select('o')
  32.             ->andWhere('o.published = 1')
  33.             ->andWhere('o.isDeleted = 0 OR o.isDeleted IS NULL')
  34.             ->add('orderBy', 'o.priority ASC, o.publishedFrom DESC')
  35.             ->setFirstResult(0)
  36.             ->setMaxResults($maxResult)
  37.             ->getQuery();
  38.         $items = $query->getResult();
  39.         return $items;
  40.     }
  41.     public function findSidebarArticles($article, $maxResult = 4) {
  42.         $qd = $this->createQueryBuilder('o');
  43.         $query = $qd->select('o')
  44.             ->andWhere('o.published = 1')
  45.             ->andWhere('o.isDeleted = 0 OR o.isDeleted IS NULL')
  46.             ->andWhere('o.id != :currentId')
  47.             ->setParameter('currentId', intval($article->getId()))
  48.             ->andWhere('(o.publishedFrom < :publishedFrom or o.publishedFrom is null) and (o.publishedTo > :publishedTo or o.publishedTo is null)')
  49.             ->setParameter('publishedFrom', new \DateTime())
  50.             ->setParameter('publishedTo', new \DateTime())
  51.             ->add('orderBy', 'o.priority DESC, o.publishedFrom DESC')
  52.             ->setFirstResult(0)
  53.             ->setMaxResults($maxResult)
  54.             ->getQuery();
  55.         $items = $query->getResult();
  56.         return $items;
  57.     }
  58.     public function findAllArticlesForUser(User $user, $checkAccess = false, $count = "all") {
  59.         $builder = $this->createQueryBuilder('a');
  60.         if ($checkAccess) {
  61.             $builder->leftJoin("a.accesses", "acc")
  62.                 ->leftJoin("acc.user", "u")
  63.                 ->andWhere("u = :user")
  64.                 ->setParameter("user", $user);
  65.         }
  66.         $builder->andWhere('a.isDeleted = 0 OR a.isDeleted IS NULL')
  67.             ->add('orderBy', 'a.publishedFrom DESC, a.priority DESC, a.name ASC');
  68.         if ($count !== "all") {
  69.             $builder->setMaxResults(intval($count));
  70.         }
  71.         return $builder->getQuery()->getResult();
  72.     }
  73.     public function findArticlesByPage($settings = ['page' => 1, 'pageSize' => 10], $criteria = []) {
  74.         $qd = $this->createQueryBuilder('o');
  75.         $qd->select('o');
  76.         if (array_key_exists('visible', $criteria) and $criteria['visible'] == true) {
  77.             $qd->andWhere('o.published = :published');
  78.             $qd->setParameter('published', 1);
  79.             $qd->andWhere('o.isDeleted = 0 OR o.isDeleted IS NULL');
  80.             $qd->andWhere('(o.publishedFrom < :publishedFrom or o.publishedFrom is null) and (o.publishedTo > :publishedTo or o.publishedTo is null)');
  81.             $qd->setParameter('publishedFrom', new \DateTime());
  82.             $qd->setParameter('publishedTo', new \DateTime());
  83.         }
  84.         $qd->add('orderBy', 'o.priority DESC, o.publishedFrom DESC, o.name ASC');
  85.         $query = $qd->getQuery();
  86.         $pageSize = $settings['pageSize'];
  87.         $page = $settings['page'];
  88.         // load doctrine Paginator
  89.         $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
  90.         // you can get total items
  91.         $totalItems = count($paginator);
  92.         // get total pages
  93.         $pagesCount = ceil($totalItems / $pageSize);
  94.         // now get one page's items:
  95.         $paginator
  96.             ->getQuery()
  97.             ->setFirstResult($pageSize * ($page - 1)) // set the offset
  98.             ->setMaxResults($pageSize); // set the limit
  99.         $list = array();
  100.         foreach ($paginator as $pageItem) {
  101.             $list[] = $pageItem;
  102.         }
  103.         return ['list' => $list, 'total' => $totalItems, 'pagesCount' => $pagesCount];
  104.     }
  105. }