Browse Source

Merge branch 'feature/viewprofile' into unstable

bastien 13 years ago
parent
commit
7972ea6a4a

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

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

@@ -272,4 +272,57 @@ class User extends BaseUser
272 272
     return $this->getUsername();
273 273
   }
274 274
   
275
+  /**
276
+   * Retourn si l'user_id transmis fait partis des enregistrements
277
+   * followed de l'objet.
278
+   * 
279
+   * @param int $user_id 
280
+   * @return boolean
281
+   */
282
+  public function isFollowingUser($user_id)
283
+  {
284
+    foreach ($this->followeds_users as $followed_user)
285
+    {
286
+      if ($followed_user->getFollowed()->getId() == $user_id)
287
+      {
288
+        return true;
289
+      }
290
+    }
291
+    return false;
292
+  }
293
+  
294
+  /**
295
+   * Retourn si l'user_id transmis est l'un des User suivis
296
+   * 
297
+   * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
298
+   * @param int $user_id 
299
+   * @return boolean
300
+   */
301
+  public function isFollowingUserByQuery($doctrine, $user_id)
302
+  {
303
+    return $doctrine
304
+      ->getRepository('MuzichCoreBundle:User')
305
+      ->isFollowingUser($this->getId(), $user_id)
306
+    ;
307
+  }
308
+  
309
+  /**
310
+   * Retourn si l'group_id transmis est l'un des groupe suivis
311
+   * 
312
+   * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
313
+   * @param int $user_id 
314
+   * @return boolean
315
+   */
316
+  public function isFollowingGroupByQuery($doctrine, $group_id)
317
+  {
318
+    return $doctrine
319
+      ->getRepository('MuzichCoreBundle:User')
320
+      ->isFollowingGroup($this->getId(), $group_id)
321
+    ;
322
+  }
323
+  
324
+  public function getPersonalHash()
325
+  {
326
+    return hash('sha256', $this->getSalt().$this->getUsername());
327
+  }
275 328
 }

+ 48 - 1
src/Muzich/CoreBundle/Repository/UserRepository.php View File

@@ -3,6 +3,7 @@
3 3
 namespace Muzich\CoreBundle\Repository;
4 4
 
5 5
 use Doctrine\ORM\EntityRepository;
6
+use Doctrine\ORM\Query;
6 7
 
7 8
 class UserRepository extends EntityRepository
8 9
 {
@@ -18,6 +19,8 @@ class UserRepository extends EntityRepository
18 19
   {
19 20
     $select = 'u';
20 21
     $join = '';
22
+    $where = '';
23
+    $parameters = array('uid' => $user_id);
21 24
     
22 25
     if (in_array('followeds_users', $join_list))
23 26
     {
@@ -37,13 +40,23 @@ class UserRepository extends EntityRepository
37 40
       $join   .= ' JOIN u.followed_groups fdg JOIN fdg.group fdg_g';
38 41
     }
39 42
     
43
+//    if (array_key_exists('followed_user_id', $join_list))
44
+//    {
45
+//      $select .= ', fu, fu_u';
46
+//      $join   .= ' LEFT JOIN u.followeds_users fu WITH fu.followed = :fuid LEFT JOIN fu.followed fu_u';
47
+//      $parameters = array_merge($parameters, array(
48
+//        'fuid' => $join_list['followed_user_id']
49
+//      ));
50
+//    }
51
+    
40 52
     return $this->getEntityManager()
41 53
       ->createQuery("
42 54
         SELECT $select FROM MuzichCoreBundle:User u
43 55
         $join
44 56
         WHERE u.id = :uid
57
+        $where
45 58
       ")
46
-      ->setParameter('uid', $user_id)
59
+      ->setParameters($parameters)
47 60
     ;
48 61
   }
49 62
   
@@ -116,5 +129,39 @@ class UserRepository extends EntityRepository
116 129
     ;
117 130
   }
118 131
   
132
+  public function isFollowingUser($follower_id, $followed_id)
133
+  {
134
+    $result = $this->getEntityManager()
135
+      ->createQuery("
136
+        SELECT COUNT(fu.id) FROM MuzichCoreBundle:FollowUser fu
137
+        WHERE fu.follower = :frid AND fu.followed = :fdid
138
+      ")
139
+      ->setParameters(array(
140
+        'frid' => $follower_id,
141
+        'fdid' => $followed_id
142
+      ))
143
+      ->getSingleResult(Query::HYDRATE_ARRAY)
144
+    ;
145
+    
146
+    return $result[1];
147
+  }
148
+  
149
+  public function isFollowingGroup($follower_id, $group_id)
150
+  {
151
+    $result = $this->getEntityManager()
152
+      ->createQuery("
153
+        SELECT COUNT(fg.id) FROM MuzichCoreBundle:FollowGroup fg
154
+        WHERE fg.follower = :frid AND fg.group = :fdgid
155
+      ")
156
+      ->setParameters(array(
157
+        'frid' => $follower_id,
158
+        'fdgid' => $group_id
159
+      ))
160
+      ->getSingleResult(Query::HYDRATE_ARRAY)
161
+    ;
162
+    
163
+    return $result[1];
164
+  }
165
+  
119 166
 }
120 167
   

+ 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
      

+ 13 - 3
src/Muzich/HomeBundle/Controller/ShowController.php View File

@@ -16,18 +16,23 @@ class ShowController extends Controller
16 16
   public function showUserAction($slug)
17 17
   {
18 18
     try {
19
+      
19 20
       $viewed_user = $this->getDoctrine()
20 21
         ->getRepository('MuzichCoreBundle:User')
21 22
         ->findOneBySlug($slug)
22 23
         ->getSingleResult()
23 24
       ;
25
+      $user = $this->getUser();
26
+      
24 27
     } catch (\Doctrine\ORM\NoResultException $e) {
25 28
         throw $this->createNotFoundException('Utilisateur introuvable.');
26 29
     }
27 30
     
28 31
     return array(
29 32
       'viewed_user' => $viewed_user,
30
-      'elements' => $this->getShowedEntityElements($viewed_user->getId(), 'User')
33
+      'elements'    => $this->getShowedEntityElements($viewed_user->getId(), 'User'),
34
+      'following'   => $user->isFollowingUserByQuery($this->getDoctrine(), $viewed_user->getId()),
35
+      'user'        => $user
31 36
     );
32 37
   }
33 38
   
@@ -39,18 +44,23 @@ class ShowController extends Controller
39 44
   public function showGroupAction($slug)
40 45
   {
41 46
     try {
47
+      
42 48
       $group = $this->getDoctrine()
43 49
         ->getRepository('MuzichCoreBundle:Group')
44 50
         ->findOneBySlug($slug)
45 51
         ->getSingleResult()
46 52
       ;
53
+      $user = $this->getUser();
54
+      
47 55
     } catch (\Doctrine\ORM\NoResultException $e) {
48 56
         throw $this->createNotFoundException('Groupe introuvable.');
49 57
     }
50 58
     
51 59
     return array(
52
-      'group' => $group,
53
-      'elements' => $this->getShowedEntityElements($group->getId(), 'Group')
60
+      'group'       => $group,
61
+      'elements'    => $this->getShowedEntityElements($group->getId(), 'Group'),
62
+      'following'   => $user->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()),
63
+      'user'        => $user
54 64
     );
55 65
   }
56 66
   

+ 10 - 0
src/Muzich/HomeBundle/Resources/views/Show/showGroup.html.twig View File

@@ -3,6 +3,16 @@
3 3
 {% block title %}{% endblock %}
4 4
 
5 5
 {% block content %}
6
+
7
+  {% if following %}
8
+    <a href="" class="follow_link following" >
9
+      Ne plus suivre
10
+    </a>
11
+  {% else %}
12
+    <a href="" class="follow_link notfollowing" >
13
+      Suivre
14
+    </a>
15
+  {% endif %}
6 16
     
7 17
   <h2>{{ group.name }}</h2>
8 18
   

+ 8 - 0
src/Muzich/HomeBundle/Resources/views/Show/showUser.html.twig View File

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

+ 5 - 11
web/bundles/muzichhome/css/home.css View File

@@ -1,13 +1,7 @@
1
-/* 
2
-    Document   : home
3
-    Created on : 10 sept. 2011, 12:49:23
4
-    Author     : bux
5
-    Description:
6
-        Purpose of the stylesheet follows.
7
-*/
8 1
 
9
-/* 
10
-   TODO customize this sample style
11
-   Syntax recommendation http://www.w3.org/TR/REC-CSS2/
12
-*/
13 2
 
3
+#container .follow_link
4
+{
5
+  float: right;
6
+  text-decoration: none;
7
+}