src/Repository/CommentRepository.php line 49

Open in your IDE?
  1. <?php
  2. namespace Slivki\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\Query\Expr\Join;
  5. use Slivki\Entity\Category;
  6. use Slivki\Entity\City;
  7. use Slivki\Entity\Comment;
  8. use Slivki\Entity\Director;
  9. use Slivki\Entity\EntityVote;
  10. use Slivki\Entity\MainMenu;
  11. use Slivki\Entity\MallBrand;
  12. use Slivki\Entity\Offer;
  13. use Slivki\Entity\Sale;
  14. use Slivki\Entity\Seo;
  15. /**
  16.  * @deprecated Use CommentRepositoryInterface. Refactoring required.
  17.  */
  18. class CommentRepository extends EntityRepository
  19. {
  20.     const PG_SERIAL_MAX_VALUE 2147483647;
  21.     const CACHE_NAME 'details-comments-3-';
  22.     const MOBILE_CACHE_NAME 'mobile-details-comment-3-';
  23.     const COMMENTS_PER_PAGE =  20;
  24.     public const COMMENTS_FOR_DIRECTOR_RATING_PERIOD_IN_DAYS 90;
  25.     public function getCommentsByEntityID($entityID$limit$lastCommentID self::PG_SERIAL_MAX_VALUE) {
  26.         if (!$lastCommentID) {
  27.             $lastCommentID self::PG_SERIAL_MAX_VALUE;
  28.         }
  29.         $dql "select comment, user from Slivki:Comment comment 
  30.             join comment.user user
  31.             where comment.entityID = :entityID  
  32.               and comment.hidden != true
  33.               and comment.parentID is null
  34.               and comment.ID < :lastCommentID
  35.               and comment.confirmedPhone = true
  36.             order by comment.createdOn desc";
  37.         $query $this->getEntityManager()->createQuery($dql)
  38.             ->setParameter('entityID'$entityID)
  39.             ->setParameter('lastCommentID'$lastCommentID);
  40.         $query->setMaxResults($limit);
  41.         return $query->getResult();
  42.     }
  43.     public function getCommentsByCategoryID($categoryID$limit$lastCommentID self::PG_SERIAL_MAX_VALUE) {
  44.         if (!$lastCommentID) {
  45.             $lastCommentID self::PG_SERIAL_MAX_VALUE;
  46.         }
  47.         $categoryCached $this->getEntityManager()->getRepository(Category::class)->findCached($categoryID);
  48.         $dql "select comment, user, media, likes from Slivki:Comment comment
  49.             left join comment.likes likes
  50.             left join comment.medias media  
  51.             join comment.user user
  52.             where comment.entityID in (:entityID)
  53.               and comment.typeID = :typeID
  54.               and comment.hidden != true
  55.               and comment.ID < :lastCommentID
  56.               and comment.confirmedPhone = true
  57.             order by comment.createdOn desc";
  58.         $query $this->getEntityManager()->createQuery($dql)
  59.             ->setParameter('entityID'array_values($categoryCached['entityList']))
  60.             ->setParameter('typeID'Comment::TYPE_OFFER_COMMENT)
  61.             ->setParameter('lastCommentID'$lastCommentID);
  62.         $query->setMaxResults($limit);
  63.         return $query->getResult();
  64.     }
  65.     public function getOfferCategoryComments($categoryID$page$perPage 10) {
  66.         $offerIDList $this->getEntityManager()->getRepository(Offer::class)->getOfferIDListByCategory($categoryID);
  67.         $dql "select comment, user from Slivki:Comment comment 
  68.             join comment.user user
  69.             where comment.entityID in (:offerIDList)
  70.               and comment.typeID = :typeID
  71.               and (comment.hidden = false or comment.hidden is null)
  72.               and (comment.deleted = false or comment.deleted is null)
  73.               and comment.confirmedPhone = true
  74.             order by comment.ID desc";
  75.         return $this->getEntityManager()->createQuery($dql)
  76.             ->setParameter('typeID'Comment::TYPE_OFFER_COMMENT)
  77.             ->setParameter('offerIDList'$offerIDList)
  78.             ->setMaxResults($perPage)
  79.             ->setFirstResult($perPage * ($page 1))
  80.             ->getResult();
  81.     }
  82.     public function getOfferCategoryCommentsCount($categoryID) {
  83.         $sql "select count(*) from comment
  84.             inner join customer on customer.id = comment.user_id
  85.             where entity_id in (select entity_id from category2entity 
  86.               where category_id = $categoryID) and type_id = " Comment::TYPE_OFFER_COMMENT 
  87.               and (hidden is null or not hidden) and (deleted is null or not deleted) and confirmed_phone";
  88.         return $this->getEntityManager()->getConnection()->executeQuery($sql)->fetchColumn();
  89.     }
  90.     public function getCommentsByDirectorID($directorID$limit self::COMMENTS_PER_PAGE$lastCommentID null) {
  91.         $director $this->getEntityManager()->getRepository(Director::class)->find($directorID);
  92.         if (!$director) {
  93.             return [];
  94.         }
  95.         $directorEntityIDList $director->getEntityIDList();
  96.         if (empty($directorEntityIDList)) {
  97.             return [];
  98.         }
  99.         if (!$lastCommentID) {
  100.             $lastCommentID self::PG_SERIAL_MAX_VALUE;
  101.         }
  102.         $dql "select comment, media from Slivki:Comment comment
  103.             left join comment.medias media
  104.             where comment.entityID in (:directorEntityIDList) and (comment.hidden = false or comment.hidden is null)
  105.             and comment.confirmedPhone = true
  106.             and comment.typeID in (:typeIDs) and comment.ID < :lastCommentID
  107.             order by comment.createdOn desc";
  108.         $comments $this->getEntityManager()->createQuery($dql)
  109.             ->setParameter('directorEntityIDList',  $directorEntityIDList)
  110.             ->setParameter('typeIDs',  [Comment::TYPE_OFFER_COMMENTComment::TYPE_SALE_COMMENT])
  111.             ->setParameter('lastCommentID',  $lastCommentID)
  112.             ->setMaxResults($limit)
  113.             ->getResult();
  114.         return $comments;
  115.     }
  116.     public function getCommentsByOfferIDReversed($offerID$offset$limit null) {
  117.         $dql "select comment, user from Slivki:Comment comment
  118.             join comment.user user
  119.             where comment.entityID = :offerID
  120.               and comment.typeID = :typeID
  121.               and comment.hidden != true 
  122.               and comment.confirmedPhone = true
  123.               and comment.parentID is null
  124.               and (comment.deleted is null or comment.deleted = false)
  125.             order by comment.createdOn desc";
  126.         $query $this->getEntityManager()->createQuery($dql)
  127.             ->setParameter('typeID'Comment::TYPE_OFFER_COMMENT)
  128.             ->setParameter('offerID'$offerID)
  129.             ->setFirstResult($offset);
  130.         if ($limit) {
  131.             $query->setMaxResults($limit);
  132.         }
  133.         return $query->getResult();
  134.     }
  135.     public function getLiveComments(int $limit, ?int $lastCommentID null): array
  136.     {
  137.         if (null === $lastCommentID) {
  138.             $lastCommentID self::PG_SERIAL_MAX_VALUE;
  139.         }
  140.         $qb $this->createQueryBuilder('comment');
  141.         $expr $qb->expr();
  142.         return $qb
  143.             ->addSelect('user')
  144.             ->innerJoin('comment.user','user')
  145.             ->innerJoin(Offer::class, 'offer'Join::WITH$expr->eq('offer.ID''comment.entityID'))
  146.             ->andWhere($expr->eq('comment.hidden'':hidden'))
  147.             ->andWhere($expr->eq('comment.confirmedPhone'':confirmedPhone'))
  148.             ->andWhere($expr->neq('offer.defaultCity'':tashkentCityId'))
  149.             ->andWhere(
  150.                 $expr->orX(
  151.                     $expr->isNull('comment.hiddenEntity'),
  152.                     $expr->eq('comment.hiddenEntity'':hiddenEntity')
  153.                 )
  154.             )
  155.             ->andWhere($expr->lt('comment.ID'':lastCommentID'))
  156.             ->setParameters([
  157.                 'hidden' => false,
  158.                 'hiddenEntity' => false,
  159.                 'confirmedPhone' => true,
  160.                 'tashkentCityId' => City::TASHKENT_CITY_ID,
  161.                 'lastCommentID' => $lastCommentID,
  162.             ])
  163.             ->addOrderBy($expr->desc('comment.createdOn'))
  164.             ->setMaxResults($limit)
  165.             ->getQuery()
  166.             ->getResult()
  167.         ;
  168.     }
  169.     public function updateCommentEntityHidden($entityID$typeID$entityHidden) {
  170.         $this->getEntityManager()->getConnection()
  171.             ->executeQuery("update comment set hidden_entity = $entityHidden where type_id = $typeID and entity_id = $entityID");
  172.     }
  173.     public function getNotSeenByDirectorOfferComments($offerID$userID$limit) {
  174.         $dql "select comment, media from Slivki:Offer offer
  175.             join Slivki:Comment comment with comment.entityID = offer.ID
  176.             left join comment.medias media
  177.             where comment.typeID = :commentTypeID and comment.userID != :userID and offer.ID = :offerID
  178.             and (comment.seenBySupplier = false or comment.seenBySupplier is null)
  179.             and (comment.hidden = false or comment.hidden is null) and comment.confirmedPhone = true
  180.             order by comment.createdOn desc";
  181.         return $this->getEntityManager()->createQuery($dql)
  182.             ->setParameter('offerID'$offerID)
  183.             ->setParameter('userID'$userID)
  184.             ->setParameter('commentTypeID'Comment::TYPE_OFFER_COMMENT)
  185.             ->setMaxResults($limit)
  186.             ->getResult();
  187.     }
  188.     public function getNotSeenOfferCommentsByDirectorID($directorID$userID$limit) {
  189.         $dql "select comment, media from Slivki:Director director
  190.             join director.offers offer
  191.             join Slivki:Comment comment with comment.entityID = offer.ID
  192.             left join comment.medias media
  193.             where director.ID in (:directorID) and comment.typeID = :commentTypeID and comment.userID != :userID
  194.             and (comment.seenBySupplier = false or comment.seenBySupplier is null)
  195.             and (comment.hidden = false or comment.hidden is null) and comment.confirmedPhone = true
  196.             order by comment.createdOn desc";
  197.         $directorRepository $this->getEntityManager()->getRepository(Director::class);
  198.         $director $directorRepository->find($directorID);
  199.         $partners $directorRepository->findBy(['email' => $director->getEmail()]);
  200.         $partnerList = [];
  201.         foreach ($partners as $partner) {
  202.             $partnerList[] = $partner->getID();
  203.         }
  204.         $comments $this->getEntityManager()->createQuery($dql)
  205.             ->setParameter('directorID'$partnerList)
  206.             ->setParameter('userID'$userID)
  207.             ->setParameter('commentTypeID'Comment::TYPE_OFFER_COMMENT)
  208.             ->setMaxResults($limit)
  209.             ->getResult();
  210.         return $comments;
  211.     }
  212.     public function getDirectorsByOfferComment(Comment $comment) {
  213.         if($comment->getTypeID() != Comment::TYPE_OFFER_COMMENT) {
  214.             return false;
  215.         }
  216.         $offer $this->getEntityManager()->getRepository(Offer::class)->find($comment->getEntityID());
  217.         return $offer->getDirectors()->toArray();
  218.     }
  219.     public function isUserAllowedToRate($userID$entityID$typeID) {
  220.         $dql "select max(comment.createdOn) from Slivki:Comment comment
  221.             where comment.userID = :userID
  222.                 and comment.entityID = :offerID
  223.                 and comment.typeID = :typeID
  224.                 and comment.rating > 0";
  225.         $userLastRatedCommentCreatedOn $this->getEntityManager()->createQuery($dql)
  226.             ->setParameter('userID'$userID)
  227.             ->setParameter('offerID'$entityID)
  228.             ->setParameter('typeID'$typeID)
  229.             ->getSingleResult();
  230.         if(!$userLastRatedCommentCreatedOn[1]) {
  231.             return true;
  232.         }
  233.         return (strtotime($userLastRatedCommentCreatedOn[1]) < strtotime('-7 days'));
  234.     }
  235.     /**
  236.      * @deprecated use CommentDaoInterface
  237.      */
  238.     public function getCommentsCountByEntityID($entityID$typeID) {
  239.         $dql "select count(comment) as commentAmount
  240.             from Slivki:Comment as comment
  241.             where comment.entityID = :entityID
  242.               and comment.typeID = :typeID
  243.               and comment.confirmedPhone = true
  244.               and comment.hidden != true and (comment.deleted != true or comment.deleted is null)";
  245.         $query $this->getEntityManager()->createQuery($dql)
  246.             ->setParameter('entityID'$entityID)
  247.             ->setParameter('typeID'$typeID);
  248.         $result $query->getScalarResult();
  249.         return $result[0]['commentAmount'];
  250.     }
  251.     public function getRootCommentsCountByEntityID($entityID$typeID) {
  252.         $dql "select count(comment) as commentAmount
  253.             from Slivki:Comment as comment
  254.             where comment.entityID = :entityID
  255.               and comment.typeID = :typeID
  256.               and comment.confirmedPhone = true
  257.               and comment.hidden != true and (comment.deleted != true or comment.deleted is null) and comment.parent is null";
  258.         $query $this->getEntityManager()->createQuery($dql)
  259.             ->setParameter('entityID'$entityID)
  260.             ->setParameter('typeID'$typeID);
  261.         $result $query->getScalarResult();
  262.         return $result[0]['commentAmount'];
  263.     }
  264.     public function getCommentsCountByUserID($userID$entityID$typeID) {
  265.         $dql "select count(comment) as commentAmount
  266.             from Slivki:Comment as comment
  267.             where comment.entityID = :entityID
  268.               and comment.typeID = :typeID
  269.               and comment.userID = :userID
  270.               and comment.confirmedPhone = true
  271.               and comment.hidden != true ";
  272.         $query $this->getEntityManager()->createQuery($dql)
  273.             ->setParameter('entityID'$entityID)
  274.             ->setParameter('typeID'$typeID)
  275.             ->setParameter('userID'$userID);
  276.         $query->setMaxResults(1);
  277.         $result $query->getScalarResult();
  278.         return $result[0]['commentAmount'];
  279.     }
  280.     public function getCommentEntity($entityID$typeID) {
  281.         switch($typeID) {
  282.             case Comment::TYPE_OFFER_COMMENT:
  283.                 $repositoryName Offer::class;
  284.                 break;
  285.             case Comment::TYPE_SALE_COMMENT:
  286.                 $repositoryName Sale::class;
  287.                 break;
  288.             case Comment::TYPE_MALL_BRAND_COMMENT:
  289.                 $repositoryName MallBrand::class;
  290.                 break;
  291.             case Comment::TYPE_DIRECTOR_COMMENT:
  292.                 $repositoryName Director::class;
  293.                 break;
  294.             default:
  295.                 $repositoryName '';
  296.                 break;
  297.         }
  298.         if ($repositoryName == '') {
  299.             return false;
  300.         }
  301.         return $this->getEntityManager()->getRepository($repositoryName)->find($entityID);
  302.     }
  303.     public function getTopMenu() {
  304.         $seoRepository $this->getEntityManager()->getRepository(Seo::class);
  305.         $mainMenu $this->getEntityManager()->getRepository(MainMenu::class)->getItemListCached(11);
  306.         $removeMenuItemIDList = [23143];
  307.         foreach($mainMenu as $key => $item) {
  308.             if($item->getType() != MainMenu::TYPE_OFFER_CATEGORY || in_array($item->getEntityID(), $removeMenuItemIDList)) {
  309.                 unset($mainMenu[$key]);
  310.                 continue;
  311.             }
  312.             $seo $seoRepository->getByEntity(SeoRepository::RESOURCE_URL_CATEGORY_COMMENTS$item->getEntityID());
  313.             $item->setURL($seo->getMainAlias());
  314.         }
  315.         return array_values($mainMenu);
  316.     }
  317.     public function getEntityRating($entityType$entityID$dateFrom false$dateTo false) {
  318.         $dateCondition "";
  319.         if ($dateFrom) {
  320.             $dateCondition " and created_on between '$dateFrom' and '$dateTo'";
  321.         }
  322.         $sql "select avg(rating) as rating from comment where type_id=$entityType and entity_id=$entityID and rating > 0 and checked and confirmed_phone and not hidden $dateCondition";
  323.         $result $this->getEntityManager()->getConnection()->executeQuery($sql)->fetchColumn(0);
  324.         return $result round($result2) : 0;
  325.     }
  326.     public function getEntityRatingWithCount($entityType$entityID$dateFrom false$dateTo false) {
  327.         $dateCondition "";
  328.         if ($dateFrom) {
  329.             $dateCondition " and created_on between '$dateFrom' and '$dateTo'";
  330.         }
  331.         $sql "select avg(rating) as rating, count(*) as amount from comment where created_on > date_trunc('day', now() + '-90 days')::timestamp without time zone and type_id=$entityType and entity_id=$entityID and rating > 0 and confirmed_phone and not hidden $dateCondition";
  332.         $dbConnection $this->getEntityManager()->getConnection();
  333.         $result $dbConnection->executeQuery($sql)->fetch(\PDO::FETCH_ASSOC);
  334.         if (!$result) {
  335.             return ['rating' => 0'amount' => 0'ratingCount' => 0];
  336.         }
  337.         $result['ratingCount'] = $result['amount'];
  338.         $sql "select count(*) as amount from comment where type_id=$entityType and entity_id=$entityID and confirmed_phone and not hidden";
  339.         $countAll $dbConnection->executeQuery($sql)->fetchColumn(0);
  340.         $result['amount'] = $countAll;
  341.         if ($entityType == Category::SALE_CATEGORY_ID) {
  342.             $sql 'select count(*) as amount, coalesce(avg(vote), 0) as rating from entity_vote where entity_id = ' $entityID ' and entity_type_id = ' EntityVote::TYPE_SALE $dateCondition;
  343.             $voteStatistic $dbConnection->executeQuery($sql)->fetchAll(\PDO::FETCH_ASSOC);
  344.             $voteStatistic $voteStatistic[0];
  345.             $totalAmount $voteStatistic['amount'] + $result['ratingCount'];
  346.             if ($totalAmount 0) {
  347.                 $totalRating = ($voteStatistic['amount'] * $voteStatistic['rating'] + $result['ratingCount'] * $result['rating']) / $totalAmount;
  348.                 $result['amount'] = $voteStatistic['amount'] +  $result['amount'];
  349.                 $result['rating'] = $totalRating;
  350.                 $result['ratingCount'] = $totalAmount;
  351.             }
  352.         }
  353.         return $result['amount'] > 0  $result : ['rating' => 0'amount' => 0'ratingCount' => 0];
  354.     }
  355.     public function getCategoryRatingForPeriod($category$dateFrom$dateTo) {
  356.         $sql "select avg(comment.rating) as rating, count(*) as amount from comment inner join offer on comment.entity_id = offer.id
  357.           inner join category2entity on offer.id = category2entity.entity_id where comment.rating > 0 and comment.checked and comment.confirmed_phone = true and not comment.hidden 
  358.           and category2entity.category_id = " $category->getID() . '  and comment.type_id = ' $category->getDomainObjectID() . 
  359.           and comment.created_on between '$dateFrom' and '$dateTo'";
  360.         $result $this->getEntityManager()->getConnection()->executeQuery($sql)->fetch(\PDO::FETCH_ASSOC);
  361.         return $result $result : ['rating' => 0'amount' => 0];
  362.     }
  363. }