Browse Source

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

Sevajol Bastien 11 years ago
parent
commit
97af40d3f3

+ 13 - 0
src/Muzich/CoreBundle/Entity/Playlist.php View File

239
     $this->setElements($elements_manager->getContent());
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
   public function getElementsIds()
255
   public function getElementsIds()
243
   {
256
   {
244
     $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
257
     $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));

+ 7 - 0
src/Muzich/CoreBundle/Managers/PlaylistManager.php View File

185
     $this->entity_manager->persist($playlist);
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
   protected function actualizePlaylistTags(Playlist $playlist)
195
   protected function actualizePlaylistTags(Playlist $playlist)
189
   {
196
   {
190
     $tag_lib = new TagLib();
197
     $tag_lib = new TagLib();

+ 3 - 2
src/Muzich/CoreBundle/Tests/Controller/PlaylistControllerTest.php View File

148
   
148
   
149
   protected function checkReadPlaylist($playlist, $elements = null)
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
     if ($elements !== null)
153
     if ($elements !== null)
154
     {
154
     {
364
   
364
   
365
   protected function removeElementFromPlaylist($playlist, $element)
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
   public function testCopyWhenAddingElementToPickedPlaylist()
371
   public function testCopyWhenAddingElementToPickedPlaylist()

+ 2 - 2
src/Muzich/CoreBundle/Tests/lib/Security/ContextTestCases.php View File

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

+ 23 - 0
src/Muzich/CoreBundle/lib/Collection/CollectionManager.php View File

70
     $this->content = $new_content;
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
   public function removeWithReference($reference)
96
   public function removeWithReference($reference)
74
   {
97
   {
75
     $new_content = array();
98
     $new_content = array();

+ 2 - 4
src/Muzich/PlaylistBundle/Controller/EditController.php View File

8
 
8
 
9
 class EditController extends Controller
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
   public function updateOrderAction(Request $request, $playlist_id)
11
   public function updateOrderAction(Request $request, $playlist_id)
14
   {
12
   {
15
     if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UPDATE_ORDER)) !== false)
13
     if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UPDATE_ORDER)) !== false)
24
     return $this->jsonSuccessResponse();
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
     if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_REMOVE_ELEMENT)) !== false)
27
     if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_REMOVE_ELEMENT)) !== false)
30
       return $this->jsonResponseError($uncondition);
28
       return $this->jsonResponseError($uncondition);
33
     if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
31
     if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
34
       return $this->jsonNotFoundResponse();
32
       return $this->jsonNotFoundResponse();
35
     
33
     
36
-    $playlist_manager->removePlaylistElementWithId($playlist, $element_id);
34
+    $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
37
     $this->flush();
35
     $this->flush();
38
     return $this->jsonSuccessResponse();
36
     return $this->jsonSuccessResponse();
39
   }
37
   }

+ 1 - 1
src/Muzich/PlaylistBundle/Resources/config/routing.yml View File

27
   defaults: { _controller: MuzichPlaylistBundle:Edit:updateOrder }
27
   defaults: { _controller: MuzichPlaylistBundle:Edit:updateOrder }
28
 
28
 
29
 playlist_remove_element:
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
   defaults: { _controller: MuzichPlaylistBundle:Edit:removeElement }
31
   defaults: { _controller: MuzichPlaylistBundle:Edit:removeElement }
32
 
32
 
33
 playlists_add_element_prompt:
33
 playlists_add_element_prompt:

+ 2 - 8
src/Muzich/PlaylistBundle/Resources/views/Show/show.html.twig View File

33
     </div>
33
     </div>
34
     
34
     
35
     <h1>{{ playlist.name }}</h1>
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
     {% include "MuzichCoreBundle:Tag:tag_cloud.html.twig" with {
37
     {% include "MuzichCoreBundle:Tag:tag_cloud.html.twig" with {
42
       'tags' : playlist.tags
38
       'tags' : playlist.tags
76
                   {% if playlist.owned(app.user) %}
72
                   {% if playlist.owned(app.user) %}
77
                     <a 
73
                     <a 
78
                       class="remove_element" 
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
                       title="{{ 'playlist.remove_element'|trans({}, 'elements') }}"
76
                       title="{{ 'playlist.remove_element'|trans({}, 'elements') }}"
81
                     >
77
                     >
82
                       <img src="{{ asset('/img/icon_close_2.png') }}" alt="delete" />
78
                       <img src="{{ asset('/img/icon_close_2.png') }}" alt="delete" />
96
               </div>
92
               </div>
97
               
93
               
98
               <div class="title">
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
               </div>
96
               </div>
103
               
97
               
104
               <div class="content_opened"></div>
98
               <div class="content_opened"></div>