1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProjetNormandie\ForumBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\ORMException; |
7
|
|
|
use ProjetNormandie\ForumBundle\Entity\Forum; |
8
|
|
|
|
9
|
|
|
class ForumService |
10
|
|
|
{ |
11
|
|
|
private $em; |
12
|
|
|
private $topicService; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ForumService constructor. |
16
|
|
|
* @param EntityManagerInterface $em |
17
|
|
|
* @param TopicService $topicService |
18
|
|
|
*/ |
19
|
|
|
public function __construct(EntityManagerInterface $em, TopicService $topicService) |
20
|
|
|
{ |
21
|
|
|
$this->em = $em; |
22
|
|
|
$this->topicService = $topicService; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $forum |
27
|
|
|
* @return Forum |
28
|
|
|
*/ |
29
|
|
|
private function getForum($forum): Forum |
30
|
|
|
{ |
31
|
|
|
if (!$forum instanceof Forum) { |
32
|
|
|
$forum = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Forum') |
33
|
|
|
->findOneBy(['id' => $forum]); |
34
|
|
|
} |
35
|
|
|
return $forum; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $user |
40
|
|
|
*/ |
41
|
|
|
public function readAll($user) |
42
|
|
|
{ |
43
|
|
|
$this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser')->readAll($user); |
44
|
|
|
$this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')->readAll($user); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $user |
49
|
|
|
* @param $forum |
50
|
|
|
*/ |
51
|
|
|
public function read($user, $forum) |
52
|
|
|
{ |
53
|
|
|
$this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser')->readForum($user, $forum); |
54
|
|
|
$this->setRead($forum, $user); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $forum |
59
|
|
|
* @throws ORMException |
60
|
|
|
*/ |
61
|
|
|
public function majParent($forum) |
62
|
|
|
{ |
63
|
|
|
$forum = $this->getForum($forum); |
64
|
|
|
$data = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Forum')->getParentData($forum); |
65
|
|
|
$forum->setLastMessage($this->em->getReference('ProjetNormandie\ForumBundle\Entity\Message', $data['lastMessage'])); |
66
|
|
|
$forum->setNbTopic($data['nbTopic']); |
67
|
|
|
$forum->setNbMessage($data['nbMessage']); |
68
|
|
|
$this->em->flush(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Forum $forum |
74
|
|
|
* @throws ORMException |
75
|
|
|
*/ |
76
|
|
|
public function maj(Forum $forum) |
77
|
|
|
{ |
78
|
|
|
$data = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Topic')->getForumData($forum); |
79
|
|
|
$forum->setLastMessage($this->em->getReference('ProjetNormandie\ForumBundle\Entity\Message', $data['lastMessage'])); |
80
|
|
|
$forum->setNbTopic($data['nbTopic']); |
81
|
|
|
$forum->setNbMessage($data['nbMessage']); |
82
|
|
|
$this->em->flush(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $forum |
87
|
|
|
*/ |
88
|
|
|
public function majPosition($forum) |
89
|
|
|
{ |
90
|
|
|
$forum = $this->getForum($forum); |
91
|
|
|
if ($forum->getIsParent() == true) { |
|
|
|
|
92
|
|
|
foreach ($forum->getChildrens() as $child) { |
93
|
|
|
foreach ($child->getTopics() as $topic) { |
94
|
|
|
$this->topicService->majPositions($topic); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
foreach ($forum->getTopics() as $topic) { |
99
|
|
|
$this->topicService->majPositions($topic); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Forum $forum |
107
|
|
|
* @param $user |
108
|
|
|
*/ |
109
|
|
|
public function setRead(Forum $forum, $user) |
110
|
|
|
{ |
111
|
|
|
$forumUser = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser') |
112
|
|
|
->findOneBy(['forum' => $forum, 'user' => $user]); |
113
|
|
|
$forumUser->setBoolRead(true); |
114
|
|
|
$this->em->flush(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Forum $forum |
120
|
|
|
* @param $user |
121
|
|
|
*/ |
122
|
|
|
public function setNotRead(Forum $forum, $user) |
123
|
|
|
{ |
124
|
|
|
$forumUser = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser') |
125
|
|
|
->findOneBy(['forum' => $forum, 'user' => $user]); |
126
|
|
|
$forumUser->setBoolRead(false); |
127
|
|
|
$this->em->flush(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Forum $forum |
133
|
|
|
* @param $user |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
public function countTopicNotRead(Forum $forum, $user): int |
137
|
|
|
{ |
138
|
|
|
return $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser') |
139
|
|
|
->countNotRead($forum,$user); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Forum $parent |
145
|
|
|
* @param $user |
146
|
|
|
* @return int |
147
|
|
|
*/ |
148
|
|
|
public function countSubForumNotRead(Forum $parent, $user): int |
149
|
|
|
{ |
150
|
|
|
return $this->em->getRepository('ProjetNormandieForumBundle:ForumUser') |
151
|
|
|
->countSubForumNotRead($parent, $user); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|