Quellcode durchsuchen

Anomalie #758: Suppression d'un element dans une playlist

Sevajol Bastien vor 11 Jahren
Ursprung
Commit
97af40d3f3

+ 13 - 0
src/Muzich/CoreBundle/Entity/Playlist.php Datei anzeigen

@@ -239,6 +239,19 @@ class Playlist
239 239
     $this->setElements($elements_manager->getContent());
240 240
   }
241 241
   
242
+  public function getElementIndex(Element $element)
243
+  {
244
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
245
+    return $elements_manager->index($element);
246
+  }
247
+  
248
+  public function removeElementWithIndex($index)
249
+  {
250
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
251
+    $elements_manager->removeWithIndex($index);
252
+    $this->setElements($elements_manager->getContent());
253
+  }
254
+  
242 255
   public function getElementsIds()
243 256
   {
244 257
     $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));

+ 7 - 0
src/Muzich/CoreBundle/Managers/PlaylistManager.php Datei anzeigen

@@ -185,6 +185,13 @@ class PlaylistManager
185 185
     $this->entity_manager->persist($playlist);
186 186
   }
187 187
   
188
+  public function removePlaylistElementWithIndex(Playlist $playlist, $index)
189
+  {
190
+    $playlist->removeElementWithIndex($index);
191
+    $this->actualizePlaylistTags($playlist);
192
+    $this->entity_manager->persist($playlist);
193
+  }
194
+  
188 195
   protected function actualizePlaylistTags(Playlist $playlist)
189 196
   {
190 197
     $tag_lib = new TagLib();

+ 3 - 2
src/Muzich/CoreBundle/Tests/Controller/PlaylistControllerTest.php Datei anzeigen

@@ -148,7 +148,7 @@ class PlaylistControllerTest extends FunctionalTest
148 148
   
149 149
   protected function checkReadPlaylist($playlist, $elements = null)
150 150
   {
151
-    $this->exist('h2:contains("'.$playlist->getName().'")');
151
+    $this->exist('h1:contains("'.$playlist->getName().'")');
152 152
     
153 153
     if ($elements !== null)
154 154
     {
@@ -364,7 +364,8 @@ class PlaylistControllerTest extends FunctionalTest
364 364
   
365 365
   protected function removeElementFromPlaylist($playlist, $element)
366 366
   {
367
-    $this->tests_cases->playlistremoveElement($playlist->getId(), $element->getId());
367
+    $index = $playlist->getElementIndex($element);
368
+    $this->tests_cases->playlistRemoveElement($playlist->getId(), $index);
368 369
   }
369 370
   
370 371
   public function testCopyWhenAddingElementToPickedPlaylist()

+ 2 - 2
src/Muzich/CoreBundle/Tests/lib/Security/ContextTestCases.php Datei anzeigen

@@ -313,13 +313,13 @@ class ContextTestCases
313 313
     );
314 314
   }
315 315
   
316
-  public function playlistRemoveElement($playlist_id, $element_id)
316
+  public function playlistRemoveElement($playlist_id, $index)
317 317
   {
318 318
     return $this->getAjaxRequestContentResponse(
319 319
       'GET',
320 320
       $this->test->generateUrl('playlist_remove_element', array(
321 321
         'playlist_id' => $playlist_id,
322
-        'element_id'  => $element_id,
322
+        'index'       => $index,
323 323
         '_locale'     => 'fr',
324 324
         'token'       => $this->test->getToken()
325 325
       ))

+ 23 - 0
src/Muzich/CoreBundle/lib/Collection/CollectionManager.php Datei anzeigen

@@ -70,6 +70,29 @@ abstract class CollectionManager
70 70
     $this->content = $new_content;
71 71
   }
72 72
   
73
+  public function removeWithIndex($index)
74
+  {
75
+    if (array_key_exists($index, $this->content))
76
+    {
77
+      unset($this->content[$index]);
78
+      $this->content = array_values($this->content);
79
+    }
80
+  }
81
+  
82
+  public function index($object)
83
+  {
84
+    $method_name = 'get' . $this->object_reference_attribute;
85
+    foreach ($this->content as $index => $content_line)
86
+    {
87
+      if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
88
+      {
89
+        return $index;
90
+      }
91
+    }
92
+    
93
+    return null;
94
+  }
95
+  
73 96
   public function removeWithReference($reference)
74 97
   {
75 98
     $new_content = array();

+ 2 - 4
src/Muzich/PlaylistBundle/Controller/EditController.php Datei anzeigen

@@ -8,8 +8,6 @@ use Muzich\CoreBundle\Security\Context as SecurityContext;
8 8
 
9 9
 class EditController extends Controller
10 10
 {
11
-  
12
-  // TODO: Cette méthode ET les autres: Mettre à jour avec le gestionnaire d'accès (Security)
13 11
   public function updateOrderAction(Request $request, $playlist_id)
14 12
   {
15 13
     if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UPDATE_ORDER)) !== false)
@@ -24,7 +22,7 @@ class EditController extends Controller
24 22
     return $this->jsonSuccessResponse();
25 23
   }
26 24
   
27
-  public function removeElementAction($playlist_id, $element_id)
25
+  public function removeElementAction($playlist_id, $index)
28 26
   {
29 27
     if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_REMOVE_ELEMENT)) !== false)
30 28
       return $this->jsonResponseError($uncondition);
@@ -33,7 +31,7 @@ class EditController extends Controller
33 31
     if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
34 32
       return $this->jsonNotFoundResponse();
35 33
     
36
-    $playlist_manager->removePlaylistElementWithId($playlist, $element_id);
34
+    $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
37 35
     $this->flush();
38 36
     return $this->jsonSuccessResponse();
39 37
   }

+ 1 - 1
src/Muzich/PlaylistBundle/Resources/config/routing.yml Datei anzeigen

@@ -27,7 +27,7 @@ playlist_update_order:
27 27
   defaults: { _controller: MuzichPlaylistBundle:Edit:updateOrder }
28 28
 
29 29
 playlist_remove_element:
30
-  pattern: /ajax/playlist/element/remove/{playlist_id}/{element_id}/{token}
30
+  pattern: /ajax/playlist/element/remove/{playlist_id}/{index}/{token}
31 31
   defaults: { _controller: MuzichPlaylistBundle:Edit:removeElement }
32 32
 
33 33
 playlists_add_element_prompt:

+ 2 - 8
src/Muzich/PlaylistBundle/Resources/views/Show/show.html.twig Datei anzeigen

@@ -33,10 +33,6 @@
33 33
     </div>
34 34
     
35 35
     <h1>{{ playlist.name }}</h1>
36
-    <!--
37
-    <a href="{{ path('playlists_user', { 'user_slug' : playlist.owner.slug }) }}">
38
-      {{ 'playlist.gotoplaylists'|trans({ '%user_username%' : playlist.owner.username }, 'elements') }}
39
-    </a>-->
40 36
     
41 37
     {% include "MuzichCoreBundle:Tag:tag_cloud.html.twig" with {
42 38
       'tags' : playlist.tags
@@ -76,7 +72,7 @@
76 72
                   {% if playlist.owned(app.user) %}
77 73
                     <a 
78 74
                       class="remove_element" 
79
-                      href="{{ path_token('playlist_remove_element', { 'playlist_id' : playlist.id, 'element_id' : element.id }) }}"
75
+                      href="{{ path_token('playlist_remove_element', { 'playlist_id' : playlist.id, 'index' : loop.index0 }) }}"
80 76
                       title="{{ 'playlist.remove_element'|trans({}, 'elements') }}"
81 77
                     >
82 78
                       <img src="{{ asset('/img/icon_close_2.png') }}" alt="delete" />
@@ -96,9 +92,7 @@
96 92
               </div>
97 93
               
98 94
               <div class="title">
99
-                <a class="title" href="{{ path('element_get_one', { 'element_id' : element.id }) }}" data-id="{{ element.id }}">
100
-                  {{ element.name }}
101
-                </a>
95
+                {{ element.name }}
102 96
               </div>
103 97
               
104 98
               <div class="content_opened"></div>