Advertisement
Guest User

Song Controller

a guest
Dec 5th, 2016
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MusicShareBundle\Controller;
  4.  
  5. use MusicShareBundle\Entity\Sound;
  6. use MusicShareBundle\Form\SoundType;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11.  
  12. class SoundController extends Controller
  13. {
  14. /**
  15. * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
  16. * @Route("/song/upload", name="upload_song")
  17. * @param Request $request
  18. * @return \Symfony\Component\HttpFoundation\Response
  19. */
  20. public function uploadSong(Request $request)
  21. {
  22. $song = new Sound();
  23.  
  24. $form = $this->createForm(SoundType::class, $song);
  25. $form->handleRequest($request);
  26.  
  27. if ($form->isSubmitted() && $form->isValid())
  28. {
  29. $file = $song->getFile();
  30. $user = $this->getUser();
  31.  
  32. $fileName = $this
  33. ->get('app.file_uploader')
  34. ->setDir($this->get('kernel')->getRootDir()."/../web".$this->getParameter('songs_directory'))
  35. ->upload($file);
  36.  
  37. $song->setFile($fileName);
  38.  
  39. $file = $song->getCoverFile();
  40. if ($file === null)
  41. {
  42. $song->setCoverFile($this->getParameter('default_cover'));
  43. }
  44. else
  45. {
  46. $fileName = $this
  47. ->get('app.file_uploader')
  48. ->setDir($this->get('kernel')->getRootDir()."/../web".$this->getParameter('covers_directory'))
  49. ->upload($file);
  50.  
  51. $song->setCoverFile($fileName);
  52. }
  53.  
  54. $song->setUploader($user);
  55. $song->setUploaderID($user->getId());
  56. $user->addSong($song);
  57.  
  58. $entityManager = $this->getDoctrine()->getManager();
  59. $entityManager->persist($song);
  60. $entityManager->flush();
  61.  
  62. return $this->redirectToRoute('song_view', [
  63. 'id' => $song->getId()
  64. ]);
  65. }
  66.  
  67. return $this->render('song/upload.html.twig', [
  68. 'form' => $form->createView()
  69. ]);
  70. }
  71.  
  72. /**
  73. * @Route("/song/{id}", name="song_view")
  74. * @param $id
  75. * @return \Symfony\Component\HttpFoundation\Response
  76. */
  77. public function viewSong($id)
  78. {
  79. $song = $this->getDoctrine()->getRepository(Sound::class)->find($id);
  80.  
  81. if ($song === null)
  82. {
  83. return $this->render('song/notfound.html.twig');
  84. }
  85.  
  86. return $this->render('song/view.html.twig', [
  87. 'song' => $song
  88. ]);
  89. }
  90.  
  91. /**
  92. * @Route("/catalog", name="print_all_songs")
  93. * @param $id
  94. * @return \Symfony\Component\HttpFoundation\Response
  95. */
  96. public function printAllSongs()
  97. {
  98. $songs = $this->getDoctrine()->getRepository(Sound::class)->findAll();
  99.  
  100. return $this->render('song/view_all.html.twig', [
  101. 'songs' => $songs
  102. ]);
  103. }
  104.  
  105.  
  106. /**
  107. * @Route("/song/edit/{id}", name="song_edit")
  108. * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
  109. *
  110. * @param $id
  111. * @param Request $request
  112. * @return \Symfony\Component\HttpFoundation\Response
  113. */
  114. public function editSound($id, Request $request)
  115. {
  116. $song = $this->getDoctrine()->getRepository(Sound::class)->find($id);
  117.  
  118. if ($song === null){
  119. return $this->redirectToRoute("musicshare_index");
  120. }
  121.  
  122. $form = $this->createForm(SoundType::class, $song);
  123.  
  124. $form->handleRequest($request);
  125.  
  126. if ($form->isSubmitted() && $form->isValid())
  127. {
  128. $em = $this->getDoctrine()->getManager();
  129. $em->persist($song);
  130. $em->flush();
  131.  
  132. return $this->redirectToRoute('song_view',
  133. array('id' => $song->getId()));
  134. }
  135.  
  136. return $this->render('song/edit.html.twig',
  137. array(
  138. 'song' => $song,
  139. 'form' => $form->createView(),
  140. ));
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement