/** * @Route("/gallery/photoAlbum/{id}") */ public function photoAlbumction(Request $request, $id) { $album = $this->getDoctrine()->getRepository('AppBundle:Album')->find($id); $userSession = $this->get('security.context')->getToken()->getUser()->getId(); if ($album->getUser() != $userSession) { return new Response("<h2>No tienes acceso a este album<h2>"); } // 1) build the form $photo = new Photo(); $photo->setAlbum($id); $photo->setUser($userSession); $photo->setTags(""); $form = $this->createForm(new PhotoType(), $photo); // 2) handle the submit (will only happen on POST) $form->handleRequest($request); if (!$form->isValid()) { echo $form->getErrorsAsString(); } if ($form->isValid() && $form->isSubmitted()) { //$file = $request->files->get('image'); $strm = fopen($photo->getFile()->getRealPath(), 'rb'); $var = file_get_contents($photo->getFile()->getPathname()); $photo->setImage($var); $em = $this->getDoctrine()->getManager(); $em->persist($photo); $em->flush(); return new RedirectResponse($this->generateUrl('app_photo_photoalbumction', array('id' => $id))); } $em = $this->getDoctrine()->getManager(); $query = $em->createQuery('SELECT p FROM AppBundle:Photo p, AppBundle:Album a WHERE p.album = :album AND a.user = :user')->setParameters(array('album' => $id, 'user' => $userSession)); $photos = $query->getResult(); return $this->render('default/addPhoto.html.twig', array('form' => $form->createView(), 'datos' => $photos)); }
public function photosAction($album) { /** @var \AppBundle\Entity\PhotoRepository $photoRepository */ $photoRepository = $this->getDoctrine()->getRepository('AppBundle:Photo'); /** @var \AppBundle\Entity\AlbumRepository $albumRepository */ $albumRepository = $this->getDoctrine()->getRepository('AppBundle:Album'); $album = $albumRepository->findOneBy(array('id' => $album)); $photos = $photoRepository->findBy(array('album' => $album)); $photo = new Photo(); $photo->setAlbum($album); $form = $this->createForm(new PhotoType(), $photo); return $this->render('AppBundle:Admin:photos.html.twig', array('photos' => $photos, 'album' => $album, 'form' => $form->createView())); }
/** * Add photo * * @param \AppBundle\Entity\Photo $photo * * @return Album */ public function addPhoto(Photo $photo) { $this->photos[] = $photo; $photo->setAlbum($this); return $this; }