Browse Source

Evolution #90: Ajaxsification des actions

bastien 13 years ago
parent
commit
4c9fd1c4e1

+ 24 - 0
src/Muzich/CoreBundle/lib/Controller.php View File

@@ -7,6 +7,7 @@ use Muzich\CoreBundle\Searcher\ElementSearcher;
7 7
 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8 8
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
9 9
 use Muzich\CoreBundle\Form\Element\ElementAddForm;
10
+use Symfony\Component\HttpFoundation\Response;
10 11
 
11 12
 class Controller extends BaseController
12 13
 {
@@ -258,4 +259,27 @@ class Controller extends BaseController
258 259
     );
259 260
   }
260 261
   
262
+  /**
263
+   * Retourne une réponse contenant du json
264
+   * 
265
+   * @param array $content
266
+   * @return Response 
267
+   */
268
+  protected function jsonResponse($content)
269
+  {
270
+    $response = new Response(json_encode($content));
271
+    $response->headers->set('Content-Type', 'application/json; charset=utf-8');
272
+    return $response;
273
+  }
274
+  
275
+  protected function getAssetUrl($path, $packageName = null)
276
+  {
277
+    return $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
278
+  }
279
+  
280
+  protected function trans($string, $params = array(), $package = null)
281
+  {
282
+    return $this->get('translator')->trans($string, $params, $package);
283
+  }
284
+  
261 285
 }

+ 18 - 2
src/Muzich/FavoriteBundle/Controller/FavoriteController.php View File

@@ -62,7 +62,15 @@ class FavoriteController extends Controller
62 62
     
63 63
     if ($this->getRequest()->isXmlHttpRequest())
64 64
     {
65
-      
65
+      return $this->jsonResponse(array(
66
+        'favorite'      => true,
67
+        'link_new_url'  => $this->generateUrl('favorite_remove', array(
68
+            'id'    => $id,
69
+            'token' => $token
70
+        )),
71
+        'img_new_src'   => $this->getAssetUrl('bundles/muzichcore/img/favorite.png'),
72
+        'img_new_title' => $this->trans('element.favorite.remove', array(), 'elements')
73
+      ));
66 74
     }
67 75
     else
68 76
     {
@@ -101,7 +109,15 @@ class FavoriteController extends Controller
101 109
     
102 110
     if ($this->getRequest()->isXmlHttpRequest())
103 111
     {
104
-      
112
+      return $this->jsonResponse(array(
113
+        'favorite'      => true,
114
+        'link_new_url'  => $this->generateUrl('favorite_add', array(
115
+            'id'    => $id,
116
+            'token' => $token
117
+        )),
118
+        'img_new_src'   => $this->getAssetUrl('bundles/muzichcore/img/favorite_bw.png'),
119
+        'img_new_title' => $this->trans('element.favorite.add', array(), 'elements')
120
+      ));
105 121
     }
106 122
     else
107 123
     {

+ 12 - 0
web/bundles/muzichcore/js/muzich.js View File

@@ -140,6 +140,18 @@ $(document).ready(function(){
140 140
      $(location).attr('href', $('input.filter_mytags_url').val());
141 141
    });
142 142
    
143
+   // Mise en favoris
144
+   $('a.favorite_link').click(function(){
145
+     link = $(this);
146
+     $.getJSON($(this).attr('href'), function(response) {
147
+       img = link.find('img');
148
+       link.attr('href', response.link_new_url);
149
+       img.attr('src', response.img_new_src);
150
+       img.attr('title', response.img_new_title);
151
+     });
152
+     return false;
153
+   });
154
+   
143 155
  });
144 156
  
145 157