Browse Source

Evolution #738: Playlist

Sevajol Bastien 11 years ago
parent
commit
ce1d9230b5

+ 27 - 2
src/Muzich/CoreBundle/Entity/Playlist.php View File

42
   
42
   
43
   /**
43
   /**
44
    * @ORM\ManyToOne(targetEntity="Playlist", inversedBy="copys")
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
   protected $copied;
47
   protected $copied;
48
   
48
   
57
   protected $public = false;
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
   protected $user_playlists_pickeds;
62
   protected $user_playlists_pickeds;
63
   
63
   
121
     return $this->user_playlists_pickeds;
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
   public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds = null)
134
   public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds = null)
125
   {
135
   {
126
     $this->user_playlists_pickeds = $user_playlists_pickeds;
136
     $this->user_playlists_pickeds = $user_playlists_pickeds;
248
     return $this->copys;
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 View File

42
     return $this->getUserPublicsOrOwnedPlaylists($user, $user);
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
   /** @return Playlist */
50
   /** @return Playlist */
46
   public function findOneAccessiblePlaylistWithId($playlist_id, User $user = null)
51
   public function findOneAccessiblePlaylistWithId($playlist_id, User $user = null)
47
   {
52
   {
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
   public function getPlaylistElements(Playlist $playlist, $offset = null)
76
   public function getPlaylistElements(Playlist $playlist, $offset = null)
64
   {
77
   {
65
     $element_ids = $playlist->getElementsIds();
78
     $element_ids = $playlist->getElementsIds();
116
     }
129
     }
117
   }
130
   }
118
   
131
   
132
+  /** @return Playlist */
119
   public function copyPlaylist(User $user, Playlist $playlist)
133
   public function copyPlaylist(User $user, Playlist $playlist)
120
   {
134
   {
121
     $playlist_copied = new Playlist();
135
     $playlist_copied = new Playlist();
122
     $playlist_copied->setOwner($user);
136
     $playlist_copied->setOwner($user);
137
+    $playlist_copied->setName($playlist->getName());
138
+    $playlist_copied->setPublic(true);
123
     $playlist_copied->setTags($playlist->getTags());
139
     $playlist_copied->setTags($playlist->getTags());
124
     $playlist_copied->setElements($playlist->getElements());
140
     $playlist_copied->setElements($playlist->getElements());
125
     $playlist_copied->setCopied($playlist);
141
     $playlist_copied->setCopied($playlist);
127
     $user->getPlaylistsOwneds()->add($playlist_copied);
143
     $user->getPlaylistsOwneds()->add($playlist_copied);
128
     $this->entity_manager->persist($playlist_copied);
144
     $this->entity_manager->persist($playlist_copied);
129
     $this->entity_manager->persist($user);
145
     $this->entity_manager->persist($user);
146
+    
147
+    return $playlist_copied;
130
   }
148
   }
131
   
149
   
132
   public function addElementToPlaylist(Element $element, Playlist $playlist)
150
   public function addElementToPlaylist(Element $element, Playlist $playlist)
133
   {
151
   {
134
     $playlist->addElement($element);
152
     $playlist->addElement($element);
135
     $this->actualizePlaylistTags($playlist);
153
     $this->actualizePlaylistTags($playlist);
154
+    $this->entity_manager->persist($playlist);
136
   }
155
   }
137
   
156
   
138
   public function addElementsToPlaylist($elements, Playlist $playlist)
157
   public function addElementsToPlaylist($elements, Playlist $playlist)
191
     return null;
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 View File

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

+ 30 - 1
src/Muzich/PlaylistBundle/Controller/EditController.php View File

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
   public function deleteAction($playlist_id)
85
   public function deleteAction($playlist_id)
70
   {
86
   {
71
     if (!($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
87
     if (!($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
72
       throw $this->createNotFoundException();
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
     $this->flush();
104
     $this->flush();
76
     $this->setFlash('success', 'playlist.delete.success');
105
     $this->setFlash('success', 'playlist.delete.success');
77
     return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
106
     return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));

+ 5 - 2
src/Muzich/PlaylistBundle/Controller/ShowController.php View File

24
   
24
   
25
   public function showAction($user_slug, $playlist_id)
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
       throw $this->createNotFoundException();
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
     return $this->render('MuzichPlaylistBundle:Show:show.html.twig', array(
33
     return $this->render('MuzichPlaylistBundle:Show:show.html.twig', array(
31
       'playlist' => $playlist
34
       'playlist' => $playlist
32
     ));
35
     ));
53
       $this->render('MuzichPlaylistBundle:Show:prompt.html.twig', array(
56
       $this->render('MuzichPlaylistBundle:Show:prompt.html.twig', array(
54
         'form'       => $this->getPlaylistForm()->createView(),
57
         'form'       => $this->getPlaylistForm()->createView(),
55
         'element_id' => $element_id,
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
       ))->getContent()
60
       ))->getContent()
58
     );
61
     );
59
   }
62
   }

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

10
   pattern: /playlist/delete/{playlist_id}
10
   pattern: /playlist/delete/{playlist_id}
11
   defaults: { _controller: MuzichPlaylistBundle:Edit:delete }
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
 playlist_datas_for_autoplay:
17
 playlist_datas_for_autoplay:
14
   pattern: /ajax/autoplay/playlist/datas/{playlist_id}/{offset}
18
   pattern: /ajax/autoplay/playlist/datas/{playlist_id}/{offset}
15
   defaults: { _controller: MuzichPlaylistBundle:Show:getAutoplayData, offset: null }
19
   defaults: { _controller: MuzichPlaylistBundle:Show:getAutoplayData, offset: null }
29
 playlists_add_element:
33
 playlists_add_element:
30
   pattern: /ajax/playlist/element/add/{playlist_id}/{element_id}
34
   pattern: /ajax/playlist/element/add/{playlist_id}/{element_id}
31
   defaults: { _controller: MuzichPlaylistBundle:Edit:addElement }
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
 playlist_add_element_and_create:
41
 playlist_add_element_and_create:
34
   pattern: /ajax/playlist/element/add-and-create/{element_id}
42
   pattern: /ajax/playlist/element/add-and-create/{element_id}

+ 0 - 0
src/Muzich/PlaylistBundle/Resources/views/Show/form.html.twig View File


+ 13 - 4
src/Muzich/PlaylistBundle/Resources/views/Show/prompt.html.twig View File

10
     <ul>
10
     <ul>
11
       {% for playlist in playlists %}
11
       {% for playlist in playlists %}
12
         <li>
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
             {{ playlist.name }}
26
             {{ playlist.name }}
18
           </a>
27
           </a>
19
         </li>
28
         </li>

+ 10 - 4
src/Muzich/PlaylistBundle/Resources/views/Show/user.html.twig View File

14
           Lire
14
           Lire
15
         </a>
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
           {{ playlist.name }}
18
           {{ playlist.name }}
19
         </a>
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
         {% include "MuzichCoreBundle:Tag:tag_cloud.html.twig" with {
31
         {% include "MuzichCoreBundle:Tag:tag_cloud.html.twig" with {
26
           'tags' : playlist.tags
32
           'tags' : playlist.tags