src/Repository/Media/MediaRepository.php line 98

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2021 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\Repository\Media;
  10. use App\Entity\Media\Media;
  11. use App\Entity\Media\Album;
  12. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use App\Utils\Paginator;
  15. use App\Entity\Profile;
  16. use App\Entity\Core\Tag;
  17. use Symfony\Component\Security\Core\Security;
  18. use Doctrine\ORM\QueryBuilder;
  19. class MediaRepository extends ServiceEntityRepository
  20. {
  21.     protected $security;
  22.     
  23.     public function __construct(
  24.         Security $security,
  25.         ManagerRegistry $registry
  26.     )
  27.     {
  28.         parent::__construct($registryMedia::class);
  29.         $this->security $security;
  30.     }
  31.     public function getPortfolioList(Profile $profilebool $owner false, ?int $limit null): array
  32.     {
  33.         $qb $this->createQueryBuilder('m')
  34.             ->innerJoin('m.albums''ma')
  35.             ->innerJoin('ma.album''a')
  36.             ->andWhere(':profile = a.profile AND a.portfolio = 1')
  37.             ->setParameter('profile'$profile)
  38.             ->orderBy('ma.sortOrder''DESC')
  39.         ;
  40.         if ($limit) {
  41.             $qb->setMaxResults($limit);
  42.         }
  43.         if ($owner === false) {
  44.             $qb->andWhere('m.active = 1');
  45.         }
  46.         $qb $this->getPrivacy($qb);
  47.         return $qb->getQuery()
  48.             ->getResult();
  49.         ;
  50.     }
  51.     public function getCreditedList(Profile $profile): array
  52.     {
  53.         $qb $this->createQueryBuilder('m')
  54.             ->innerJoin('m.credits''c')
  55.             ->andWhere(':profile = c.profile AND m.active = 1 AND m.profile <> :profile')
  56.             ->setParameter('profile'$profile)
  57.             ->groupBy("m.id")
  58.         ;
  59.         $qb $this->getPrivacy($qb);
  60.         return $qb->getQuery()
  61.             ->getResult();
  62.         ;
  63.     }
  64.     public function getAlbumList(Album $albumbool $owner false, ?int $limit null): array
  65.     {
  66.         $qb $this->createQueryBuilder('m')
  67.             ->innerJoin('m.albums''ma')
  68.             ->innerJoin('ma.album''a')
  69.             ->andWhere(':album = a')
  70.             ->setParameter('album'$album)
  71.             ->orderBy('ma.sortOrder''DESC')
  72.         ;
  73.         if ($owner === false) {
  74.             $qb->andWhere(',.active = 1');
  75.         }
  76.         if ($limit) {
  77.             $qb->setMaxResults($limit);
  78.         }
  79.         $qb $this->getPrivacy($qb);
  80.         return $qb->getQuery()
  81.             ->getResult();
  82.         ;
  83.     }
  84.     public function getTagList(int $page 1Tag $tag$limit 42): Paginator
  85.     {
  86.         $qb $this->createQueryBuilder('m')
  87.             ->andWhere('m.active = 1')
  88.             ->andWhere(':tag MEMBER OF m.tags')
  89.             ->setParameter('tag'$tag)
  90.         ;
  91.         $qb $this->getPrivacy($qb);
  92.         return (new Paginator($qb$limit))->paginate($page);
  93.     }
  94.     public function getList(int $page 1$limit 42): Paginator
  95.     {
  96.         $qb $this->createQueryBuilder('m')
  97.             ->andWhere('m.active = 1')
  98.         ;
  99.         $qb $this->getPrivacy($qb);
  100.         return (new Paginator($qb$limit))->paginate($page);
  101.     }
  102.     public function getLatest($count 12): array
  103.     {
  104.         $qb $this->createQueryBuilder('m')
  105.             ->orderBy('m.createdAt''DESC')
  106.             ->andWhere('m.active = 1')
  107.         ;
  108.         $qb $this->getPrivacy($qb);
  109.         return $qb->setMaxResults($count)
  110.             ->getQuery()
  111.             ->getResult()
  112.         ;
  113.     }
  114.     public function getTags($count 12): array
  115.     {
  116.         $qb $this->createQueryBuilder('m')
  117.             ->select("t.name, t.slug, COUNT(t.id) as tagCount")
  118.             ->innerJoin('m.tags''t')
  119.             ->andWhere('m.active = 1')
  120.             ->addOrderBy('t.name''ASC')
  121.             ->groupBy('t.id')
  122.         ;
  123.         $qb $this->getPrivacy($qb);
  124.         return $qb->setMaxResults($count)
  125.             ->getQuery()
  126.             ->getResult()
  127.         ;
  128.     }
  129.     private function getPrivacy(QueryBuilder $qb): QueryBuilder
  130.     {
  131.         $query = [
  132.             'm.privacy = :all',
  133.         ];
  134.         $params = [
  135.             'all' => Media::PRIVACY_ALL,
  136.         ];
  137.         if ($this->security->getUser()) {
  138.             $query[] = 'm.privacy = :users';
  139.             $params['users'] = Media::PRIVACY_USERS;
  140.             $profiles $this->security->getUser()->getProfiles();
  141.             if ($profiles) {
  142.                 $query[] = '(m.privacy = :owner AND m.profile IN (:profiles))';
  143.                 $params['owner'] = Media::PRIVACY_OWNER;
  144.                 $params['profiles'] = $profiles;
  145.             }
  146.         }
  147.         foreach($params as $key => $value) {
  148.             $qb->setParameter($key$value);
  149.         }
  150.         $qb->andWhere(implode(' OR '$query));
  151.         return $qb;
  152.     }
  153.     public function findPrevLatestMedia(Media $media): ?Media
  154.     {
  155.         return $this->createQueryBuilder('m')
  156.             ->andWhere('m.id < :id AND m.active = 1')
  157.             ->setParameter('id'$media->getId())
  158.             ->addOrderBy('m.id''DESC')
  159.             ->setMaxResults(1)
  160.             ->getQuery()
  161.             ->getOneOrNullResult()
  162.         ;
  163.     }
  164.     public function findNextLatestMedia(Media $media): ?Media
  165.     {
  166.         return $this->createQueryBuilder('m')
  167.             ->andWhere('m.id > :id AND m.active = 1')
  168.             ->setParameter('id'$media->getId())
  169.             ->addOrderBy('m.id''ASC')
  170.             ->setMaxResults(1)
  171.             ->getQuery()
  172.             ->getOneOrNullResult()
  173.         ;
  174.     }
  175.     public function findFirstLatestMedia(): ?Media
  176.     {
  177.         return $this->createQueryBuilder('m')
  178.             ->andWhere('m.active = 1')
  179.             ->addOrderBy('m.id''ASC')
  180.             ->setMaxResults(1)
  181.             ->getQuery()
  182.             ->getOneOrNullResult()
  183.         ;
  184.     }
  185.     public function findLastLatestMedia(): ?Media
  186.     {
  187.         return $this->createQueryBuilder('m')
  188.             ->andWhere('m.active = 1')
  189.             ->addOrderBy('m.id''DESC')
  190.             ->setMaxResults(1)
  191.             ->getQuery()
  192.             ->getOneOrNullResult()
  193.         ;
  194.     }
  195.     public function findPrevTagMedia(Media $mediaTag $tag): ?Media
  196.     {
  197.         return $this->createQueryBuilder('m')
  198.             ->andWhere('m.id < :id')
  199.             ->setParameter('id'$media->getId())
  200.             ->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
  201.             ->setParameter('tag'$tag)
  202.             ->addOrderBy('m.id''DESC')
  203.             ->setMaxResults(1)
  204.             ->getQuery()
  205.             ->getOneOrNullResult()
  206.         ;
  207.     }
  208.     public function findNextTagMedia(Media $mediaTag $tag): ?Media
  209.     {
  210.         return $this->createQueryBuilder('m')
  211.             ->andWhere('m.id > :id')
  212.             ->setParameter('id'$media->getId())
  213.             ->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
  214.             ->setParameter('tag'$tag)
  215.             ->addOrderBy('m.id''ASC')
  216.             ->setMaxResults(1)
  217.             ->getQuery()
  218.             ->getOneOrNullResult()
  219.         ;
  220.     }
  221.     public function findFirstTagMedia(Tag $tag): ?Media
  222.     {
  223.         return $this->createQueryBuilder('m')
  224.             ->addOrderBy('m.id''ASC')
  225.             ->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
  226.             ->setParameter('tag'$tag)
  227.             ->setMaxResults(1)
  228.             ->getQuery()
  229.             ->getOneOrNullResult()
  230.         ;
  231.     }
  232.     public function findLastTagMedia(Tag $tag): ?Media
  233.     {
  234.         return $this->createQueryBuilder('m')
  235.             ->addOrderBy('m.id''DESC')
  236.             ->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
  237.             ->setParameter('tag'$tag)
  238.             ->setMaxResults(1)
  239.             ->getQuery()
  240.             ->getOneOrNullResult()
  241.         ;
  242.     }
  243.     public function findPrevCreditedMedia(Media $mediaProfile $profile): ?Media
  244.     {
  245.         return $this->createQueryBuilder('m')
  246.             ->andWhere('m.id < :id')
  247.             ->setParameter('id'$media->getId())
  248.             ->innerJoin('m.credits''c')
  249.             ->andWhere(':profile = c.profile AND m.active = 1')
  250.             ->setParameter('profile'$profile)
  251.             ->addOrderBy('m.id''ASC')
  252.             ->setMaxResults(1)
  253.             ->getQuery()
  254.             ->getOneOrNullResult()
  255.         ;
  256.     }
  257.     public function findNextCreditedMedia(Media $mediaProfile $profile): ?Media
  258.     {
  259.         return $this->createQueryBuilder('m')
  260.             ->andWhere('m.id > :id')
  261.             ->setParameter('id'$media->getId())
  262.             ->innerJoin('m.credits''c')
  263.             ->andWhere(':profile = c.profile AND m.active = 1')
  264.             ->setParameter('profile'$profile)
  265.             ->addOrderBy('m.id''ASC')
  266.             ->setMaxResults(1)
  267.             ->getQuery()
  268.             ->getOneOrNullResult()
  269.         ;
  270.     }
  271.     public function findFirstCreditedMedia(Profile $profile): ?Media
  272.     {
  273.         return $this->createQueryBuilder('m')
  274.             ->addOrderBy('m.id''ASC')
  275.             ->innerJoin('m.credits''c')
  276.             ->andWhere(':profile = c.profile AND m.active = 1')
  277.             ->setParameter('profile'$profile)
  278.             ->setMaxResults(1)
  279.             ->getQuery()
  280.             ->getOneOrNullResult()
  281.         ;
  282.     }
  283.     public function findLastCreditedMedia(Profile $profile): ?Media
  284.     {
  285.         return $this->createQueryBuilder('m')
  286.             ->addOrderBy('m.id''DESC')
  287.             ->innerJoin('m.credits''c')
  288.             ->andWhere(':profile = c.profile AND m.active = 1')
  289.             ->setParameter('profile'$profile)
  290.             ->setMaxResults(1)
  291.             ->getQuery()
  292.             ->getOneOrNullResult()
  293.         ;
  294.     }
  295. }