|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProjetNormandie\ForumBundle\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Exception; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
|
8
|
|
|
use Doctrine\ORM\NoResultException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Specific repository that serves the Forum entity. |
|
12
|
|
|
*/ |
|
13
|
|
|
class TopicUserRepository extends EntityRepository |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param $user |
|
18
|
|
|
* @throws Exception |
|
19
|
|
|
*/ |
|
20
|
|
|
public function init($user) |
|
21
|
|
|
{ |
|
22
|
|
|
$query ="INSERT INTO forum_topic_user (idTopic, idUser) |
|
23
|
|
|
SELECT id, :idUser FROM forum_topic"; |
|
24
|
|
|
$this->_em->getConnection()->executeStatement($query, array('idUser' => $user->getId())); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $user |
|
30
|
|
|
* @param $forum |
|
31
|
|
|
* @throws Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
public function readForum($user, $forum) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->_em->getConnection()->executeStatement( |
|
36
|
|
|
"UPDATE forum_topic_user |
|
37
|
|
|
SET boolRead = 1 |
|
38
|
|
|
WHERE idUser=:idUser |
|
39
|
|
|
AND idTopic IN (SELECT id FROM forum_topic WHERE idForum=:idForum)", |
|
40
|
|
|
['idUser' => $user->getId(), 'idForum' => $forum->getId()] |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $topic |
|
46
|
|
|
* @param $user |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setNotRead($topic, $user) |
|
49
|
|
|
{ |
|
50
|
|
|
$qb = $this->_em->createQueryBuilder(); |
|
51
|
|
|
$query = $qb->update('ProjetNormandie\ForumBundle\Entity\TopicUser', 'tu') |
|
52
|
|
|
->set('tu.boolRead', ':boolRead') |
|
53
|
|
|
->where('tu.user != :user') |
|
54
|
|
|
->andWhere('tu.topic = :topic') |
|
55
|
|
|
->setParameter('boolRead', 0) |
|
56
|
|
|
->setParameter('topic', $topic) |
|
57
|
|
|
->setParameter('user', $user); |
|
58
|
|
|
|
|
59
|
|
|
$query->getQuery()->execute(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $forum |
|
64
|
|
|
* @param $user |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
* @throws NonUniqueResultException |
|
67
|
|
|
* @throws NoResultException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function countNotRead($forum, $user) |
|
70
|
|
|
{ |
|
71
|
|
|
$query = $this->createQueryBuilder('tu') |
|
72
|
|
|
->select('COUNT(tu.id)') |
|
73
|
|
|
->join('tu.topic', 't') |
|
74
|
|
|
->where('t.forum = :forum') |
|
75
|
|
|
->andWhere('tu.user = :user') |
|
76
|
|
|
->andWhere('tu.boolRead = 0') |
|
77
|
|
|
->setParameter('forum', $forum) |
|
78
|
|
|
->setParameter('user', $user); |
|
79
|
|
|
|
|
80
|
|
|
return $query->getQuery()->getSingleScalarResult(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|