<?php
/**
* Created by PhpStorm.
* User: adv
* Date: 22.12.15
* Time: 11:45
*/
namespace Slivki\Repository;
use Doctrine\ORM\EntityRepository;
use Slivki\Entity\Category;
use Slivki\Entity\Comment;
use Slivki\Entity\Media;
use Slivki\Entity\MediaType;
class MediaRepository extends EntityRepository {
/**
* @return null|\Slivki\Entity\Media
*/
public function getMedia($entityID, $typeID) {
return $this->findBy(
array (
"entityID" => $entityID,
"typeID" => $typeID
),
array (
"sortOrder" => 'ASC'
)
);
}
public function getOneMediaByEntity($entityID, $typeID) {
$medias = $this->findBy(['entityID' => $entityID, 'typeID' => $typeID], ['sortOrder' => 'asc']);
return $medias ? $medias[0] : false;
}
public function getSaleIconMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_ICON_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getSaleAlternativeIconMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_ALTERNATIVE_ICON_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getOfferAdditionalTeaserMedia($offerID) {
$mediaList = $this->getMedia($offerID, MediaType::TYPE_OFFER_ADDITIONAL_TEASER_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getOfferHotFeedIconMedia($offerID) {
$mediaList = $this->getMedia($offerID, MediaType::TYPE_HOT_FEED_OFFER_ICON_IMAGE_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getGalleryImageMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_GALLERY_IMAGE_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getFlierImageMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_FLIER_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getFlierLogoImageMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_FLIER_LOGO_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getFlierMapLogoImageMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_FLIER_MAP_LOGO_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getFlierMapIconImageMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_HOT_FEED_OFFER_ICON_IMAGE_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getFlierSaleCarouselLogoMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_FLIER_CAROUSEL_LOGO_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getДategoryLandingImageMedia($categoryID) {
$mediaList = $this->getMedia($categoryID, MediaType::TYPE_CATEGORY_LANDING_IMAGE_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getДategoryHotFeedImageMedia($categoryID) {
$mediaList = $this->getMedia($categoryID, MediaType::TYPE_CATEGORY_HOT_FEED_IMAGE_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getДategoryHotFeedIconMedia($categoryID) {
$mediaList = $this->getMedia($categoryID, MediaType::TYPE_HOT_FEED_CATEGORY_ICON_IMAGE_ID);
if(!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getSaleMainIconMedia($saleID) {
$mediaList = $this->getMedia($saleID, MediaType::TYPE_SALE_MAIN_ICON_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getSupplierLogo($supplierID) {
$mediaList = $this->getMedia($supplierID, MediaType::TYPE_PARTNER_LOGO_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getDirectorLogo($directorID) {
$mediaList = $this->getMedia($directorID, MediaType::TYPE_DIRECTOR_LOGO_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getOfferTeaserLogo($offerID) {
$mediaList = $this->getMedia($offerID, MediaType::TYPE_OFFER_TEASER_LOGO_ID);
if (!$mediaList) {
return null;
}
return $mediaList[0];
}
public function getOfferCommentMediaListByCategoryID($categoryID, $offset = 0, $limit = 10) {
$dql = "select comment, media from Slivki:Comment comment
inner join Slivki:Offer offer with offer.ID = comment.entityID
inner join offer.categories category
inner join comment.medias media
where comment.typeID = :commentTypeID
and comment.hidden = false
and comment.confirmedPhone = true
and (comment.deleted = false or comment.deleted is null)
and offer.active = true
and category.ID = :categoryID
order by comment.ID desc";
$comments = $this->getEntityManager()->createQuery($dql)
->setParameter('commentTypeID', Comment::TYPE_OFFER_COMMENT)
->setParameter('categoryID', $categoryID)
->setMaxResults($limit)
->setFirstResult($offset)
->getResult();
return $comments;
}
public function getOfferCommentMediaListByOfferID($offerID, $offset = 0, $limit = 10) {
$dql = "select comment, media from Slivki:Comment comment
inner join comment.medias media
inner join Slivki:Offer offer with offer.ID = comment.entityID
where comment.typeID = :commentTypeID
and comment.hidden = false
and comment.confirmedPhone = true
and (comment.deleted = false or comment.deleted is null)
and offer.active = true
and offer.ID = :offerID
order by comment.ID desc";
$comments = $this->getEntityManager()->createQuery($dql)
->setParameter('commentTypeID', Comment::TYPE_OFFER_COMMENT)
->setParameter('offerID', $offerID)
->setMaxResults($limit)
->setFirstResult($offset)
->getResult();
return $comments;
}
public function getOfferCommentMediaList($offset = 0, $limit = 10) {
$dql = "select comment, media from Slivki:Comment comment
inner join comment.medias media
inner join Slivki:Offer offer with offer.ID = comment.entityID
where comment.typeID = :commentTypeID and comment.hidden = false and offer.active = true and comment.confirmedPhone = true
and (comment.deleted = false or comment.deleted is null)
order by comment.ID desc";
$comments = $this->getEntityManager()->createQuery($dql)
->setParameter('commentTypeID', Comment::TYPE_OFFER_COMMENT)
->setMaxResults($limit)
->setFirstResult($offset)
->getResult();
return $comments;
}
/**
* @deprecated use Repository/Media/ShopMediaRepositoryInterface
*/
public function getOfferShopMedias($offerID) {
return $this->getMedia($offerID, MediaType::TYPE_OFFER_SHOP_PHOTO_ID);
}
public function getDirectorPhotoMedias($directorID) {
return $this->getMedia($directorID, MediaType::TYPE_SUPPLIER_PHOTO_ID);
}
public function getOfferDetailMedias($offerID) {
return $this->getMedia($offerID, MediaType::TYPE_OFFER_DETAIL_PHOTO_ID);
}
public function getLandingCategoryMedia(Category $category) {
$mediaList = $this->getMedia($category->getID(), MediaType::TYPE_CATEGORY_LANDING_IMAGE_ID);
if (count($mediaList) == 0) {
foreach ($category->getParentCategories() as $parentCategory) {
$mediaList = $this->getMedia($parentCategory->getID(), MediaType::TYPE_CATEGORY_LANDING_IMAGE_ID);
if (count($mediaList) > 0) {
break;
}
}
}
return count($mediaList) > 0 ? $mediaList[0] : null;
}
public function save(Media\AbstractMediaBase $media): void
{
$entityManager = $this->getEntityManager();
$entityManager->persist($media);
$entityManager->flush();
}
}