Explorar el Código

Evolution #738: Playlist

Sevajol Bastien hace 11 años
padre
commit
ce1d9230b5

+ 27 - 2
src/Muzich/CoreBundle/Entity/Playlist.php Ver fichero

@@ -42,7 +42,7 @@ class Playlist
42 42
   
43 43
   /**
44 44
    * @ORM\ManyToOne(targetEntity="Playlist", inversedBy="copys")
45
-   * @ORM\JoinColumn(name="copied_id", referencedColumnName="id")
45
+   * @ORM\JoinColumn(name="copied_id", referencedColumnName="id", onDelete="SET NULL")
46 46
    */
47 47
   protected $copied;
48 48
   
@@ -57,7 +57,7 @@ class Playlist
57 57
   protected $public = false;
58 58
   
59 59
   /**
60
-   * @ORM\OneToMany(targetEntity="UserPlaylistPicked", mappedBy="playlist")
60
+   * @ORM\OneToMany(targetEntity="UserPlaylistPicked", mappedBy="playlist", cascade={"remove"})
61 61
    */
62 62
   protected $user_playlists_pickeds;
63 63
   
@@ -121,6 +121,16 @@ class Playlist
121 121
     return $this->user_playlists_pickeds;
122 122
   }
123 123
   
124
+  public function getPickedsUsers()
125
+  {
126
+    $users = new ArrayCollection();
127
+    foreach ($this->getUserPlaylistsPickeds() as $user_playlist_picked)
128
+    {
129
+      $users->add($user_playlist_picked->getUser());
130
+    }
131
+    return $users;
132
+  }
133
+  
124 134
   public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds = null)
125 135
   {
126 136
     $this->user_playlists_pickeds = $user_playlists_pickeds;
@@ -248,4 +258,19 @@ class Playlist
248 258
     return $this->copys;
249 259
   }
250 260
   
261
+  public function setCopys($copys)
262
+  {
263
+    $this->copys = $copys;
264
+  }
265
+  
266
+  public function isOwned(User $user)
267
+  {
268
+    if ($this->getOwner()->getId() == $user->getId())
269
+    {
270
+      return true;
271
+    }
272
+    
273
+    return false;
274
+  }
275
+  
251 276
 }

+ 33 - 0
src/Muzich/CoreBundle/Managers/PlaylistManager.php Ver fichero

@@ -42,6 +42,11 @@ class PlaylistManager
42 42
     return $this->getUserPublicsOrOwnedPlaylists($user, $user);
43 43
   }
44 44
   
45
+  public function getOwnedsOrPickedsPlaylists(User $user)
46
+  {
47
+    return $this->getUserPublicsOrOwnedOrPickedPlaylists($user, $user);
48
+  }
49
+  
45 50
   /** @return Playlist */
46 51
   public function findOneAccessiblePlaylistWithId($playlist_id, User $user = null)
47 52
   {
@@ -60,6 +65,14 @@ class PlaylistManager
60 65
     ;
61 66
   }
62 67
   
68
+  /** @return Playlist */
69
+  public function findPlaylistWithId($playlist_id, User $user)
70
+  {
71
+    return $this->entity_manager->getRepository('MuzichCoreBundle:Playlist')
72
+      ->findOneById($playlist_id)
73
+    ;
74
+  }
75
+  
63 76
   public function getPlaylistElements(Playlist $playlist, $offset = null)
64 77
   {
65 78
     $element_ids = $playlist->getElementsIds();
@@ -116,10 +129,13 @@ class PlaylistManager
116 129
     }
117 130
   }
118 131
   
132
+  /** @return Playlist */
119 133
   public function copyPlaylist(User $user, Playlist $playlist)
120 134
   {
121 135
     $playlist_copied = new Playlist();
122 136
     $playlist_copied->setOwner($user);
137
+    $playlist_copied->setName($playlist->getName());
138
+    $playlist_copied->setPublic(true);
123 139
     $playlist_copied->setTags($playlist->getTags());
124 140
     $playlist_copied->setElements($playlist->getElements());
125 141
     $playlist_copied->setCopied($playlist);
@@ -127,12 +143,15 @@ class PlaylistManager
127 143
     $user->getPlaylistsOwneds()->add($playlist_copied);
128 144
     $this->entity_manager->persist($playlist_copied);
129 145
     $this->entity_manager->persist($user);
146
+    
147
+    return $playlist_copied;
130 148
   }
131 149
   
132 150
   public function addElementToPlaylist(Element $element, Playlist $playlist)
133 151
   {
134 152
     $playlist->addElement($element);
135 153
     $this->actualizePlaylistTags($playlist);
154
+    $this->entity_manager->persist($playlist);
136 155
   }
137 156
   
138 157
   public function addElementsToPlaylist($elements, Playlist $playlist)
@@ -191,4 +210,18 @@ class PlaylistManager
191 210
     return null;
192 211
   }
193 212
   
213
+  public function deletePlaylist(Playlist $playlist)
214
+  {
215
+    $this->copyPlaylistForPickedUsers($playlist);
216
+    $this->entity_manager->remove($playlist);
217
+  }
218
+  
219
+  protected function copyPlaylistForPickedUsers(Playlist $playlist)
220
+  {
221
+    foreach ($playlist->getPickedsUsers() as $user)
222
+    {
223
+      $this->entity_manager->persist($this->copyPlaylist($user, $playlist));
224
+    }
225
+  }
226
+  
194 227
 }

+ 1 - 1
src/Muzich/CoreBundle/Repository/PlaylistRepository.php Ver fichero

@@ -45,7 +45,7 @@ class PlaylistRepository extends EntityRepository
45 45
     }
46 46
     
47 47
     return $this->getPlaylistsQueryBuilder()
48
-      ->where('p.owner = :owner_id OR pickers.user = :picker_id')
48
+      ->where('p.owner = :owner_id')
49 49
       ->setParameter('owner_id', $current_user->getId())
50 50
     ;
51 51
   }

+ 30 - 1
src/Muzich/PlaylistBundle/Controller/EditController.php Ver fichero

@@ -66,12 +66,41 @@ class EditController extends Controller
66 66
     );
67 67
   }
68 68
   
69
+  public function addElementAndCopyAction($playlist_id, $element_id)
70
+  {
71
+    if (!($element = $this->getElementWithId($element_id)))
72
+      return $this->jsonNotFoundResponse();
73
+    
74
+    if (!($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id, $this->getUser())))
75
+      return $this->jsonNotFoundResponse();
76
+    
77
+    $new_playlist = $this->getPlaylistManager()->copyPlaylist($this->getUser(), $playlist);
78
+    $this->getPlaylistManager()->addElementToPlaylist($element, $new_playlist);
79
+    $this->getPlaylistManager()->removePickedPlaylistToUser($this->getUser(), $playlist);
80
+    $this->flush();
81
+    
82
+    return $this->jsonSuccessResponse();
83
+  }
84
+  
69 85
   public function deleteAction($playlist_id)
70 86
   {
71 87
     if (!($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
72 88
       throw $this->createNotFoundException();
73 89
     
74
-    $this->remove($playlist);
90
+    $this->getPlaylistManager()->deletePlaylist($playlist);
91
+    $this->flush();
92
+    $this->setFlash('success', 'playlist.delete.success');
93
+    return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
94
+  }
95
+  
96
+  public function unpickAction($playlist_id)
97
+  {
98
+    $playlist_manager = $this->getPlaylistManager();
99
+    
100
+    if (!($playlist = $playlist_manager->findPlaylistWithId($playlist_id, $this->getUser())))
101
+      throw $this->createNotFoundException();
102
+    
103
+    $playlist_manager->removePickedPlaylistToUser($this->getUser(), $playlist);
75 104
     $this->flush();
76 105
     $this->setFlash('success', 'playlist.delete.success');
77 106
     return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));

+ 5 - 2
src/Muzich/PlaylistBundle/Controller/ShowController.php Ver fichero

@@ -24,9 +24,12 @@ class ShowController extends Controller
24 24
   
25 25
   public function showAction($user_slug, $playlist_id)
26 26
   {
27
-    if (!($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id, $this->getUserOrNullIfVisitor())))
27
+    if (!($viewed_user = $this->findUserWithSlug($user_slug)))
28 28
       throw $this->createNotFoundException();
29 29
     
30
+    if (!($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id, $this->getUserOrNullIfVisitor())))
31
+      return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $user_slug)));
32
+    
30 33
     return $this->render('MuzichPlaylistBundle:Show:show.html.twig', array(
31 34
       'playlist' => $playlist
32 35
     ));
@@ -53,7 +56,7 @@ class ShowController extends Controller
53 56
       $this->render('MuzichPlaylistBundle:Show:prompt.html.twig', array(
54 57
         'form'       => $this->getPlaylistForm()->createView(),
55 58
         'element_id' => $element_id,
56
-        'playlists'  => (!$this->isVisitor())?$this->getPlaylistManager()->getOwnedsPlaylists($this->getUser()):array()
59
+        'playlists'  => (!$this->isVisitor())?$this->getPlaylistManager()->getOwnedsOrPickedsPlaylists($this->getUser()):array()
57 60
       ))->getContent()
58 61
     );
59 62
   }

+ 8 - 0
src/Muzich/PlaylistBundle/Resources/config/routing.yml Ver fichero

@@ -10,6 +10,10 @@ playlist_delete:
10 10
   pattern: /playlist/delete/{playlist_id}
11 11
   defaults: { _controller: MuzichPlaylistBundle:Edit:delete }
12 12
 
13
+playlist_unpick:
14
+  pattern: /playlist/unpick/{playlist_id}
15
+  defaults: { _controller: MuzichPlaylistBundle:Edit:unpick }
16
+
13 17
 playlist_datas_for_autoplay:
14 18
   pattern: /ajax/autoplay/playlist/datas/{playlist_id}/{offset}
15 19
   defaults: { _controller: MuzichPlaylistBundle:Show:getAutoplayData, offset: null }
@@ -29,6 +33,10 @@ playlists_add_element_prompt:
29 33
 playlists_add_element:
30 34
   pattern: /ajax/playlist/element/add/{playlist_id}/{element_id}
31 35
   defaults: { _controller: MuzichPlaylistBundle:Edit:addElement }
36
+  
37
+playlists_add_element_and_copy:
38
+  pattern: /ajax/playlist/element/add-and-copy/{playlist_id}/{element_id}
39
+  defaults: { _controller: MuzichPlaylistBundle:Edit:addElementAndCopy }
32 40
 
33 41
 playlist_add_element_and_create:
34 42
   pattern: /ajax/playlist/element/add-and-create/{element_id}

+ 0 - 0
src/Muzich/PlaylistBundle/Resources/views/Show/form.html.twig Ver fichero


+ 13 - 4
src/Muzich/PlaylistBundle/Resources/views/Show/prompt.html.twig Ver fichero

@@ -10,10 +10,19 @@
10 10
     <ul>
11 11
       {% for playlist in playlists %}
12 12
         <li>
13
-          <a class="add_element_to_playlist" href="{{ path('playlists_add_element', {
14
-            'playlist_id' : playlist.id,
15
-            'element_id'  : element_id
16
-          }) }}">
13
+          <a class="add_element_to_playlist"
14
+            {% if playlist.owned(app.user) %}
15
+              href="{{ path('playlists_add_element', {
16
+                'playlist_id' : playlist.id,
17
+                'element_id'  : element_id
18
+              }) }}"
19
+            {% else %}
20
+              href="{{ path('playlists_add_element_and_copy', {
21
+                'playlist_id' : playlist.id,
22
+                'element_id'  : element_id
23
+              }) }}"
24
+            {% endif %}
25
+          >
17 26
             {{ playlist.name }}
18 27
           </a>
19 28
         </li>

+ 10 - 4
src/Muzich/PlaylistBundle/Resources/views/Show/user.html.twig Ver fichero

@@ -14,13 +14,19 @@
14 14
           Lire
15 15
         </a>
16 16
         
17
-        <a href="{{ path('playlist', { 'user_slug' : viewed_user.slug, 'playlist_id' : playlist.id }) }}">
17
+        <a href="{{ path('playlist', { 'user_slug' : playlist.owner.slug, 'playlist_id' : playlist.id }) }}">
18 18
           {{ playlist.name }}
19 19
         </a>
20 20
         
21
-        <a class="" href="{{ path('playlist_delete', { 'playlist_id' : playlist.id }) }}" >
22
-          X
23
-        </a>
21
+        {% if playlist.owned(app.user) %}
22
+          <a class="" href="{{ path('playlist_delete', { 'playlist_id' : playlist.id }) }}" >
23
+            X
24
+          </a>
25
+        {% else %}
26
+          <a class="" href="{{ path('playlist_unpick', { 'playlist_id' : playlist.id }) }}" >
27
+            X
28
+          </a>
29
+        {% endif %}
24 30
         
25 31
         {% include "MuzichCoreBundle:Tag:tag_cloud.html.twig" with {
26 32
           'tags' : playlist.tags