66
77use App \Entity \Contracts \VisibilityInterface ;
88use App \Entity \Hashtag ;
9+ use App \Entity \User ;
910use App \Pagination \NativeQueryAdapter ;
1011use App \Pagination \Transformation \ContentPopulationTransformer ;
1112use Doctrine \Bundle \DoctrineBundle \Repository \ServiceEntityRepository ;
@@ -34,7 +35,7 @@ public function __construct(
3435 parent ::__construct ($ registry , Hashtag::class);
3536 }
3637
37- public function findOverall (int $ page , string $ tag ): PagerfantaInterface
38+ public function findOverall (int $ page , string $ tag, ? User $ user ): PagerfantaInterface
3839 {
3940 $ hashtag = $ this ->findBy (['tag ' => $ tag ]);
4041 $ countAll = $ this ->tagLinkRepository ->createQueryBuilder ('link ' )
@@ -48,26 +49,34 @@ public function findOverall(int $page, string $tag): PagerfantaInterface
4849 $ sql = "SELECT e.id, e.created_at, 'entry' AS type FROM entry e
4950 INNER JOIN hashtag_link l ON e.id = l.entry_id
5051 INNER JOIN hashtag h ON l.hashtag_id = h.id AND h.tag = :tag
51- WHERE visibility = :visibility
52+ INNER JOIN magazine m ON e.magazine_id = m.id
53+ WHERE :visibilityBypass = TRUE OR (e.visibility = :visibility AND m.visibility = :visibility)
5254 UNION ALL
5355 SELECT ec.id, ec.created_at, 'entry_comment' AS type FROM entry_comment ec
5456 INNER JOIN hashtag_link l ON ec.id = l.entry_comment_id
5557 INNER JOIN hashtag h ON l.hashtag_id = h.id AND h.tag = :tag
56- WHERE visibility = :visibility
58+ INNER JOIN magazine m ON ec.magazine_id = m.id
59+ WHERE :visibilityBypass = TRUE OR (ec.visibility = :visibility AND m.visibility = :visibility)
5760 UNION ALL
5861 SELECT p.id, p.created_at, 'post' AS type FROM post p
5962 INNER JOIN hashtag_link l ON p.id = l.post_id
6063 INNER JOIN hashtag h ON l.hashtag_id = h.id AND h.tag = :tag
61- WHERE visibility = :visibility
64+ INNER JOIN magazine m ON p.magazine_id = m.id
65+ WHERE :visibilityBypass = TRUE OR (p.visibility = :visibility AND m.visibility = :visibility)
6266 UNION ALL
63- SELECT pc.id, created_at, 'post_comment' AS type FROM post_comment pc
67+ SELECT pc.id, pc. created_at, 'post_comment' AS type FROM post_comment pc
6468 INNER JOIN hashtag_link l ON pc.id = l.post_comment_id
65- INNER JOIN hashtag h ON l.hashtag_id = h.id AND h.tag = :tag WHERE visibility = :visibility
69+ INNER JOIN hashtag h ON l.hashtag_id = h.id AND h.tag = :tag
70+ INNER JOIN magazine m ON pc.magazine_id = m.id
71+ WHERE :visibilityBypass = TRUE OR (pc.visibility = :visibility AND m.visibility = :visibility)
6672 ORDER BY created_at DESC " ;
6773
74+ $ visibilityBypass = null !== $ user && ($ user ->isAdmin () || $ user ->isModerator ());
75+
6876 $ adapter = new NativeQueryAdapter ($ conn , $ sql , [
6977 'tag ' => $ tag ,
7078 'visibility ' => VisibilityInterface::VISIBILITY_VISIBLE ,
79+ 'visibilityBypass ' => $ visibilityBypass ,
7180 ], $ countAll , $ this ->populationTransformer );
7281
7382 $ pagerfanta = new Pagerfanta ($ adapter );
0 commit comments