Просмотр исходного кода

Procédure d'ajout d'un élément en favoris.

bastien 13 лет назад
Родитель
Сommit
ca7c97a9ee

+ 5 - 1
app/Resources/translations/elements.fr.yml Просмотреть файл

1
 
1
 
2
 noelements:
2
 noelements:
3
-  sentence:             Aucun element a afficher
3
+  sentence:             Aucun element a afficher
4
+  
5
+element:
6
+  favorite:
7
+    add:                Ajouter aux favoris

+ 5 - 0
app/config/routing.yml Просмотреть файл

42
 MuzichGroupBundle:
42
 MuzichGroupBundle:
43
   resource: "@MuzichGroupBundle/Resources/config/routing.yml"
43
   resource: "@MuzichGroupBundle/Resources/config/routing.yml"
44
   prefix:   /{_locale}/
44
   prefix:   /{_locale}/
45
+  defaults: { _locale: fr }
46
+  
47
+MuzichFavoriteBundle:
48
+  resource: "@MuzichFavoriteBundle/Resources/config/routing.yml"
49
+  prefix:   /{_locale}/
45
   defaults: { _locale: fr }
50
   defaults: { _locale: fr }

+ 4 - 1
src/Muzich/CoreBundle/Controller/SearchController.php Просмотреть файл

61
     
61
     
62
     return $this->render(
62
     return $this->render(
63
       'MuzichCoreBundle:SearchElement:default.html.twig', 
63
       'MuzichCoreBundle:SearchElement:default.html.twig', 
64
-      array('elements' => $elements)
64
+      array(
65
+        'user' => $this->getUser(),
66
+        'elements' => $elements
67
+      )
65
     );
68
     );
66
   }   
69
   }   
67
   
70
   

+ 7 - 1
src/Muzich/CoreBundle/Entity/Element.php Просмотреть файл

63
    * Cet attribu stocke les enregistrements UsersElementsFavorites liés
63
    * Cet attribu stocke les enregistrements UsersElementsFavorites liés
64
    * a ce Tag dans le cadre des Elements favoris.
64
    * a ce Tag dans le cadre des Elements favoris.
65
    * 
65
    * 
66
-   * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="tag")
66
+   * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="element")
67
    */
67
    */
68
   protected $elements_favorites;
68
   protected $elements_favorites;
69
   
69
   
346
       $this->addTag($tag);
346
       $this->addTag($tag);
347
     }
347
     }
348
   }
348
   }
349
+  
350
+  public function getCountFavorite()
351
+  {
352
+    return count($this->elements_favorites);
353
+  }
354
+  
349
 }
355
 }

+ 4 - 1
src/Muzich/CoreBundle/Repository/ElementRepository.php Просмотреть файл

75
       $params['tid'.$tag_id] = $tag_id;
75
       $params['tid'.$tag_id] = $tag_id;
76
     }
76
     }
77
         
77
         
78
-    $query_string = "SELECT e, et, t2, eu, g
78
+    $query_string = "SELECT e, et, t2, eu, g, fav
79
       FROM MuzichCoreBundle:Element e 
79
       FROM MuzichCoreBundle:Element e 
80
       LEFT JOIN e.group g 
80
       LEFT JOIN e.group g 
81
       LEFT JOIN e.type et 
81
       LEFT JOIN e.type et 
82
       LEFT JOIN e.tags t 
82
       LEFT JOIN e.tags t 
83
       LEFT JOIN e.tags t2 
83
       LEFT JOIN e.tags t2 
84
+      LEFT JOIN e.elements_favorites fav WITH fav.user = :uid
84
       JOIN e.owner eu $join_personal
85
       JOIN e.owner eu $join_personal
85
       $where
86
       $where
86
       ORDER BY e.created DESC "
87
       ORDER BY e.created DESC "
87
     ;
88
     ;
88
     
89
     
90
+    $params['uid'] = $user_id;
91
+    
89
     $query = $this->getEntityManager()
92
     $query = $this->getEntityManager()
90
       ->createQuery($query_string)
93
       ->createQuery($query_string)
91
       ->setParameters($params)
94
       ->setParameters($params)

+ 8 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/default.html.twig Просмотреть файл

8
         {% endif %}
8
         {% endif %}
9
         {{ element.name }} ({{ element.owner.username }})
9
         {{ element.name }} ({{ element.owner.username }})
10
           <a href="{{ element.url }}" target="_blank">link</a>
10
           <a href="{{ element.url }}" target="_blank">link</a>
11
+          
12
+          {% if element.getCountFavorite %}
13
+          {% else %}
14
+            | <a href="{{ path('favorite_add', { 'id': element.id, 'token': user.personalHash }) }}" >
15
+              {{ 'element.favorite.add'|trans({}, 'elements') }}
16
+            </a>
17
+          {% endif %}
18
+          
11
           <ul>
19
           <ul>
12
             {% for tag in element.tags %} 
20
             {% for tag in element.tags %} 
13
               <li>
21
               <li>

+ 41 - 1
src/Muzich/FavoriteBundle/Controller/FavoriteController.php Просмотреть файл

4
 
4
 
5
 use Muzich\CoreBundle\lib\Controller;
5
 use Muzich\CoreBundle\lib\Controller;
6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
+use Muzich\CoreBundle\Entity\UsersElementsFavorites;
7
 //use Muzich\CoreBundle\Entity\Group;
8
 //use Muzich\CoreBundle\Entity\Group;
8
 //use Muzich\CoreBundle\Form\Group\GroupForm;
9
 //use Muzich\CoreBundle\Form\Group\GroupForm;
9
 //use Symfony\Component\HttpFoundation\Request;
10
 //use Symfony\Component\HttpFoundation\Request;
12
 class FavoriteController extends Controller
13
 class FavoriteController extends Controller
13
 {
14
 {
14
   
15
   
15
-  
16
+  /**
17
+   * Ajoute comme favoris l'element en id
18
+   * 
19
+   * @param int $id
20
+   * @param string $token 
21
+   */
22
+  public function addAction($id, $token)
23
+  {
24
+    $user = $this->getUser();
25
+    $em = $this->getDoctrine()->getEntityManager();
26
+    
27
+    if ($user->getPersonalHash() != $token || !is_numeric($id)
28
+      || !($element = $em->getRepository('MuzichCoreBundle:Element')->findOneById($id))
29
+    )
30
+    {
31
+      throw $this->createNotFoundException();
32
+    }
33
+    
34
+    if (!$em->getRepository('MuzichCoreBundle:UsersElementsFavorites')
35
+      ->findOneBy(array(
36
+        'user'    => $user->getId(),
37
+        'element' => $id
38
+      )))
39
+    {
40
+      $favorite = new UsersElementsFavorites();
41
+      $favorite->setUser($user);
42
+      $favorite->setElement($element);
43
+      $em->persist($favorite);
44
+      $em->flush();
45
+    }
46
+    
47
+    if ($this->getRequest()->isXmlHttpRequest())
48
+    {
49
+      
50
+    }
51
+    else
52
+    {
53
+      return $this->redirect($this->container->get('request')->headers->get('referer'));
54
+    }
55
+  }
16
   
56
   
17
 }
57
 }

+ 3 - 0
src/Muzich/FavoriteBundle/Resources/config/routing.yml Просмотреть файл

1
 
1
 
2
+favorite_add:
3
+  pattern:   /favorite/add/{id}/{token}
4
+  defaults: { _controller: MuzichFavoriteBundle:Favorite:add }
2
 
5
 

+ 1 - 1
src/Muzich/HomeBundle/Controller/HomeController.php Просмотреть файл

37
     );
37
     );
38
         
38
         
39
     return array(
39
     return array(
40
-      'add_form' => $add_form->createView(),
40
+      'add_form'      => $add_form->createView(),
41
       'search_object' => $search_object,
41
       'search_object' => $search_object,
42
       'search_form'   => $search_form->createView()
42
       'search_form'   => $search_form->createView()
43
     );
43
     );