|
@@ -0,0 +1,71 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace Muzich\CoreBundle\Controller;
|
|
4
|
+
|
|
5
|
+use Muzich\CoreBundle\lib\Controller;
|
|
6
|
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
|
7
|
+use Muzich\CoreBundle\Entity\FollowUser;
|
|
8
|
+use Doctrine\ORM\Query;
|
|
9
|
+
|
|
10
|
+class CoreController extends Controller
|
|
11
|
+{
|
|
12
|
+
|
|
13
|
+ /**
|
|
14
|
+ *
|
|
15
|
+ * @param string $type
|
|
16
|
+ * @param int $id
|
|
17
|
+ * @param string $salt
|
|
18
|
+ */
|
|
19
|
+ public function followAction($type, $id, $token)
|
|
20
|
+ {
|
|
21
|
+ $user = $this->getUser();
|
|
22
|
+ // Vérifications préléminaires
|
|
23
|
+ if ($user->getPersonalHash() != $token || !in_array($type, array('user', 'group')) || !is_numeric($id))
|
|
24
|
+ {
|
|
25
|
+ throw $this->createNotFoundException();
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ $em = $this->getDoctrine()->getEntityManager();
|
|
29
|
+ $FollowUser = $em
|
|
30
|
+ ->getRepository('MuzichCoreBundle:Follow' . ucfirst($type))
|
|
31
|
+ ->findOneBy(
|
|
32
|
+ array(
|
|
33
|
+ 'follower' => $user->getId(),
|
|
34
|
+ ($type == 'user') ? 'followed' : 'group' => $id
|
|
35
|
+ )
|
|
36
|
+ )
|
|
37
|
+ ;
|
|
38
|
+
|
|
39
|
+ if ($FollowUser)
|
|
40
|
+ {
|
|
41
|
+ // L'utilisateur suis déjà, on doit détruire l'entité
|
|
42
|
+ $em->remove($FollowUser);
|
|
43
|
+ $em->flush();
|
|
44
|
+ }
|
|
45
|
+ else
|
|
46
|
+ {
|
|
47
|
+ $followed_user = $em->getRepository('MuzichCoreBundle:User')->find($id);
|
|
48
|
+
|
|
49
|
+ if (!$followed_user) {
|
|
50
|
+ throw $this->createNotFoundException('No user found for id '.$id);
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ $FollowUser = new FollowUser();
|
|
54
|
+ $FollowUser->setFollowed($followed_user);
|
|
55
|
+ $FollowUser->setFollower($user);
|
|
56
|
+
|
|
57
|
+ $em->persist($FollowUser);
|
|
58
|
+ $em->flush();
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ if ($this->getRequest()->isXmlHttpRequest())
|
|
62
|
+ {
|
|
63
|
+
|
|
64
|
+ }
|
|
65
|
+ else
|
|
66
|
+ {
|
|
67
|
+ return $this->redirect($this->container->get('request')->headers->get('referer'));
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+}
|