Browse Source

Ecriture de l'action permettant de suivre / ne plus suivre.

bastien 13 years ago
parent
commit
5e01d7a439

+ 71 - 0
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -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
+}

+ 4 - 0
src/Muzich/CoreBundle/Entity/User.php View File

@@ -321,4 +321,8 @@ class User extends BaseUser
321 321
     ;
322 322
   }
323 323
   
324
+  public function getPersonalHash()
325
+  {
326
+    return hash('sha256', $this->getSalt().$this->getUsername());
327
+  }
324 328
 }

+ 5 - 0
src/Muzich/CoreBundle/Resources/config/routing.yml View File

@@ -3,4 +3,9 @@
3 3
 search_elements:
4 4
    pattern:  /search-elements
5 5
    defaults: { _controller: MuzichCoreBundle:Search:searchElements }
6
+   
7
+follow:
8
+   pattern:  /follow/{type}/{id}/{token}
9
+   defaults: { _controller: MuzichCoreBundle:Core:follow }
10
+   
6 11
      

+ 4 - 2
src/Muzich/HomeBundle/Controller/ShowController.php View File

@@ -31,7 +31,8 @@ class ShowController extends Controller
31 31
     return array(
32 32
       'viewed_user' => $viewed_user,
33 33
       'elements'    => $this->getShowedEntityElements($viewed_user->getId(), 'User'),
34
-      'following'   => $user->isFollowingUserByQuery($this->getDoctrine(), $viewed_user->getId())
34
+      'following'   => $user->isFollowingUserByQuery($this->getDoctrine(), $viewed_user->getId()),
35
+      'user'        => $user
35 36
     );
36 37
   }
37 38
   
@@ -58,7 +59,8 @@ class ShowController extends Controller
58 59
     return array(
59 60
       'group'       => $group,
60 61
       'elements'    => $this->getShowedEntityElements($group->getId(), 'Group'),
61
-      'following'   => $user->isFollowingGroupByQuery($this->getDoctrine(), $group->getId())
62
+      'following'   => $user->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()),
63
+      'user'        => $user
62 64
     );
63 65
   }
64 66
   

+ 5 - 7
src/Muzich/HomeBundle/Resources/views/Show/showUser.html.twig View File

@@ -4,15 +4,13 @@
4 4
 
5 5
 {% block content %}
6 6
     
7
-  {% if following %}
8
-    <a href="" class="follow_link following" >
7
+  <a href="{{ path('follow', { 'type': 'user', 'id': viewed_user.id, 'token': user.personalHash }) }}" class="follow_link following" >
8
+    {% if following %}
9 9
       Ne plus suivre
10
-    </a>
11
-  {% else %}
12
-    <a href="" class="follow_link notfollowing" >
10
+    {% else %}
13 11
       Suivre
14
-    </a>
15
-  {% endif %}
12
+    {% endif %}
13
+  </a>
16 14
 
17 15
   <h2>{{ viewed_user.name }}</h2>
18 16