|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProjetNormandie\ForumBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Doctrine\ORM\ORMException; |
|
7
|
|
|
use ProjetNormandie\ForumBundle\Entity\Topic; |
|
8
|
|
|
|
|
9
|
|
|
class TopicService |
|
10
|
|
|
{ |
|
11
|
|
|
private EntityManagerInterface $em; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* TopicService constructor. |
|
15
|
|
|
* @param EntityManagerInterface $em |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(EntityManagerInterface $em) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->em = $em; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param $topic |
|
24
|
|
|
* @return Topic |
|
25
|
|
|
*/ |
|
26
|
|
|
private function getTopic($topic): Topic |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
if (!$topic instanceof Topic) { |
|
29
|
|
|
$topic = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Topic') |
|
30
|
|
|
->findOneBy(['id' => $topic]); |
|
31
|
|
|
} |
|
32
|
|
|
return $topic; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Topic $topic |
|
37
|
|
|
*/ |
|
38
|
|
|
public function majPositions(Topic $topic) |
|
39
|
|
|
{ |
|
40
|
|
|
$list = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Message')->findBy(['topic' => $topic], ['id' => 'ASC']); |
|
41
|
|
|
$i = 1; |
|
42
|
|
|
foreach ($list as $message) { |
|
43
|
|
|
$message->setPosition($i); |
|
44
|
|
|
$i++; |
|
45
|
|
|
} |
|
46
|
|
|
$this->em->flush(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Topic $topic |
|
51
|
|
|
* @param $user |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setNotRead(Topic $topic, $user) |
|
54
|
|
|
{ |
|
55
|
|
|
// |
|
56
|
|
|
// Topic |
|
57
|
|
|
$this->em->getRepository('ProjetNormandie\ForumBundle\Entity\TopicUser')->setNotRead($topic, $user); |
|
58
|
|
|
// Forum |
|
59
|
|
|
$this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')->setNotRead($topic->getForum(), $user); |
|
60
|
|
|
// Forum Parent |
|
61
|
|
|
if ($topic->getForum()->getParent() != null) { |
|
62
|
|
|
$this->em->getRepository('ProjetNormandie\ForumBundle\Entity\ForumUser')->setNotRead($topic->getForum()->getParent(), $user); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Topic $topic |
|
68
|
|
|
* @throws ORMException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function maj(Topic $topic) |
|
71
|
|
|
{ |
|
72
|
|
|
$data = $this->em->getRepository('ProjetNormandie\ForumBundle\Entity\Message')->getTopicData($topic); |
|
73
|
|
|
$topic->setLastMessage($this->em->getReference('ProjetNormandie\ForumBundle\Entity\Message', $data['lastMessage'])); |
|
74
|
|
|
$topic->setNbMessage($data['nbMessage']); |
|
75
|
|
|
$this->em->flush(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.