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

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
     return $this->getUsername();
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
 namespace Muzich\CoreBundle\Repository;
3
 namespace Muzich\CoreBundle\Repository;
4
 
4
 
5
 use Doctrine\ORM\EntityRepository;
5
 use Doctrine\ORM\EntityRepository;
6
+use Doctrine\ORM\Query;
6
 
7
 
7
 class UserRepository extends EntityRepository
8
 class UserRepository extends EntityRepository
8
 {
9
 {
18
   {
19
   {
19
     $select = 'u';
20
     $select = 'u';
20
     $join = '';
21
     $join = '';
22
+    $where = '';
23
+    $parameters = array('uid' => $user_id);
21
     
24
     
22
     if (in_array('followeds_users', $join_list))
25
     if (in_array('followeds_users', $join_list))
23
     {
26
     {
37
       $join   .= ' JOIN u.followed_groups fdg JOIN fdg.group fdg_g';
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
     return $this->getEntityManager()
52
     return $this->getEntityManager()
41
       ->createQuery("
53
       ->createQuery("
42
         SELECT $select FROM MuzichCoreBundle:User u
54
         SELECT $select FROM MuzichCoreBundle:User u
43
         $join
55
         $join
44
         WHERE u.id = :uid
56
         WHERE u.id = :uid
57
+        $where
45
       ")
58
       ")
46
-      ->setParameter('uid', $user_id)
59
+      ->setParameters($parameters)
47
     ;
60
     ;
48
   }
61
   }
49
   
62
   
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
 search_elements:
3
 search_elements:
4
    pattern:  /search-elements
4
    pattern:  /search-elements
5
    defaults: { _controller: MuzichCoreBundle:Search:searchElements }
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
   public function showUserAction($slug)
16
   public function showUserAction($slug)
17
   {
17
   {
18
     try {
18
     try {
19
+      
19
       $viewed_user = $this->getDoctrine()
20
       $viewed_user = $this->getDoctrine()
20
         ->getRepository('MuzichCoreBundle:User')
21
         ->getRepository('MuzichCoreBundle:User')
21
         ->findOneBySlug($slug)
22
         ->findOneBySlug($slug)
22
         ->getSingleResult()
23
         ->getSingleResult()
23
       ;
24
       ;
25
+      $user = $this->getUser();
26
+      
24
     } catch (\Doctrine\ORM\NoResultException $e) {
27
     } catch (\Doctrine\ORM\NoResultException $e) {
25
         throw $this->createNotFoundException('Utilisateur introuvable.');
28
         throw $this->createNotFoundException('Utilisateur introuvable.');
26
     }
29
     }
27
     
30
     
28
     return array(
31
     return array(
29
       'viewed_user' => $viewed_user,
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
   public function showGroupAction($slug)
44
   public function showGroupAction($slug)
40
   {
45
   {
41
     try {
46
     try {
47
+      
42
       $group = $this->getDoctrine()
48
       $group = $this->getDoctrine()
43
         ->getRepository('MuzichCoreBundle:Group')
49
         ->getRepository('MuzichCoreBundle:Group')
44
         ->findOneBySlug($slug)
50
         ->findOneBySlug($slug)
45
         ->getSingleResult()
51
         ->getSingleResult()
46
       ;
52
       ;
53
+      $user = $this->getUser();
54
+      
47
     } catch (\Doctrine\ORM\NoResultException $e) {
55
     } catch (\Doctrine\ORM\NoResultException $e) {
48
         throw $this->createNotFoundException('Groupe introuvable.');
56
         throw $this->createNotFoundException('Groupe introuvable.');
49
     }
57
     }
50
     
58
     
51
     return array(
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
 {% block title %}{% endblock %}
3
 {% block title %}{% endblock %}
4
 
4
 
5
 {% block content %}
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
   <h2>{{ group.name }}</h2>
17
   <h2>{{ group.name }}</h2>
8
   
18
   

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

4
 
4
 
5
 {% block content %}
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
   <h2>{{ viewed_user.name }}</h2>
15
   <h2>{{ viewed_user.name }}</h2>
8
   
16
   
9
   {% include "MuzichCoreBundle:SearchElement:default.html.twig" %}
17
   {% include "MuzichCoreBundle:SearchElement:default.html.twig" %}

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

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