<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2021 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\Repository\Media;
use App\Entity\Media\Media;
use App\Entity\Media\Album;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Utils\Paginator;
use App\Entity\Profile;
use App\Entity\Core\Tag;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\QueryBuilder;
class MediaRepository extends ServiceEntityRepository
{
protected $security;
public function __construct(
Security $security,
ManagerRegistry $registry
)
{
parent::__construct($registry, Media::class);
$this->security = $security;
}
public function getPortfolioList(Profile $profile, bool $owner = false, ?int $limit = null): array
{
$qb = $this->createQueryBuilder('m')
->innerJoin('m.albums', 'ma')
->innerJoin('ma.album', 'a')
->andWhere(':profile = a.profile AND a.portfolio = 1')
->setParameter('profile', $profile)
->orderBy('ma.sortOrder', 'DESC')
;
if ($limit) {
$qb->setMaxResults($limit);
}
if ($owner === false) {
$qb->andWhere('m.active = 1');
}
$qb = $this->getPrivacy($qb);
return $qb->getQuery()
->getResult();
;
}
public function getCreditedList(Profile $profile): array
{
$qb = $this->createQueryBuilder('m')
->innerJoin('m.credits', 'c')
->andWhere(':profile = c.profile AND m.active = 1 AND m.profile <> :profile')
->setParameter('profile', $profile)
->groupBy("m.id")
;
$qb = $this->getPrivacy($qb);
return $qb->getQuery()
->getResult();
;
}
public function getAlbumList(Album $album, bool $owner = false, ?int $limit = null): array
{
$qb = $this->createQueryBuilder('m')
->innerJoin('m.albums', 'ma')
->innerJoin('ma.album', 'a')
->andWhere(':album = a')
->setParameter('album', $album)
->orderBy('ma.sortOrder', 'DESC')
;
if ($owner === false) {
$qb->andWhere(',.active = 1');
}
if ($limit) {
$qb->setMaxResults($limit);
}
$qb = $this->getPrivacy($qb);
return $qb->getQuery()
->getResult();
;
}
public function getTagList(int $page = 1, Tag $tag, $limit = 42): Paginator
{
$qb = $this->createQueryBuilder('m')
->andWhere('m.active = 1')
->andWhere(':tag MEMBER OF m.tags')
->setParameter('tag', $tag)
;
$qb = $this->getPrivacy($qb);
return (new Paginator($qb, $limit))->paginate($page);
}
public function getList(int $page = 1, $limit = 42): Paginator
{
$qb = $this->createQueryBuilder('m')
->andWhere('m.active = 1')
;
$qb = $this->getPrivacy($qb);
return (new Paginator($qb, $limit))->paginate($page);
}
public function getLatest($count = 12): array
{
$qb = $this->createQueryBuilder('m')
->orderBy('m.createdAt', 'DESC')
->andWhere('m.active = 1')
;
$qb = $this->getPrivacy($qb);
return $qb->setMaxResults($count)
->getQuery()
->getResult()
;
}
public function getTags($count = 12): array
{
$qb = $this->createQueryBuilder('m')
->select("t.name, t.slug, COUNT(t.id) as tagCount")
->innerJoin('m.tags', 't')
->andWhere('m.active = 1')
->addOrderBy('t.name', 'ASC')
->groupBy('t.id')
;
$qb = $this->getPrivacy($qb);
return $qb->setMaxResults($count)
->getQuery()
->getResult()
;
}
private function getPrivacy(QueryBuilder $qb): QueryBuilder
{
$query = [
'm.privacy = :all',
];
$params = [
'all' => Media::PRIVACY_ALL,
];
if ($this->security->getUser()) {
$query[] = 'm.privacy = :users';
$params['users'] = Media::PRIVACY_USERS;
$profiles = $this->security->getUser()->getProfiles();
if ($profiles) {
$query[] = '(m.privacy = :owner AND m.profile IN (:profiles))';
$params['owner'] = Media::PRIVACY_OWNER;
$params['profiles'] = $profiles;
}
}
foreach($params as $key => $value) {
$qb->setParameter($key, $value);
}
$qb->andWhere(implode(' OR ', $query));
return $qb;
}
public function findPrevLatestMedia(Media $media): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.id < :id AND m.active = 1')
->setParameter('id', $media->getId())
->addOrderBy('m.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findNextLatestMedia(Media $media): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.id > :id AND m.active = 1')
->setParameter('id', $media->getId())
->addOrderBy('m.id', 'ASC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findFirstLatestMedia(): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.active = 1')
->addOrderBy('m.id', 'ASC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findLastLatestMedia(): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.active = 1')
->addOrderBy('m.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findPrevTagMedia(Media $media, Tag $tag): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.id < :id')
->setParameter('id', $media->getId())
->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
->setParameter('tag', $tag)
->addOrderBy('m.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findNextTagMedia(Media $media, Tag $tag): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.id > :id')
->setParameter('id', $media->getId())
->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
->setParameter('tag', $tag)
->addOrderBy('m.id', 'ASC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findFirstTagMedia(Tag $tag): ?Media
{
return $this->createQueryBuilder('m')
->addOrderBy('m.id', 'ASC')
->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
->setParameter('tag', $tag)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findLastTagMedia(Tag $tag): ?Media
{
return $this->createQueryBuilder('m')
->addOrderBy('m.id', 'DESC')
->andWhere(':tag MEMBER OF m.tags AND m.active = 1')
->setParameter('tag', $tag)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findPrevCreditedMedia(Media $media, Profile $profile): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.id < :id')
->setParameter('id', $media->getId())
->innerJoin('m.credits', 'c')
->andWhere(':profile = c.profile AND m.active = 1')
->setParameter('profile', $profile)
->addOrderBy('m.id', 'ASC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findNextCreditedMedia(Media $media, Profile $profile): ?Media
{
return $this->createQueryBuilder('m')
->andWhere('m.id > :id')
->setParameter('id', $media->getId())
->innerJoin('m.credits', 'c')
->andWhere(':profile = c.profile AND m.active = 1')
->setParameter('profile', $profile)
->addOrderBy('m.id', 'ASC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findFirstCreditedMedia(Profile $profile): ?Media
{
return $this->createQueryBuilder('m')
->addOrderBy('m.id', 'ASC')
->innerJoin('m.credits', 'c')
->andWhere(':profile = c.profile AND m.active = 1')
->setParameter('profile', $profile)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function findLastCreditedMedia(Profile $profile): ?Media
{
return $this->createQueryBuilder('m')
->addOrderBy('m.id', 'DESC')
->innerJoin('m.credits', 'c')
->andWhere(':profile = c.profile AND m.active = 1')
->setParameter('profile', $profile)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
}