src/Controller/PostsController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Posts;
  4. use App\Form\PostsType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\String\Slugger\SluggerInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. class PostsController extends AbstractController
  14. {
  15.     #[Route('/crear-posts'name'app_crear-posts')]
  16.     public function index(Request $requestSluggerInterface $slugger): Response
  17.     {
  18.     $post = new Posts();
  19.     $form $this->createForm(PostsType::class, $post);
  20.     $form->handleRequest($request);
  21.     if ($form->isSubmitted() && $form->isValid()) {
  22.                 $brochureFile $form->get('foto')->getData();
  23.         // this condition is needed because the 'brochure' field is not required
  24.                 // so the PDF file must be processed only when a file is uploaded
  25.                 if ($brochureFile) {
  26.             $originalFilename pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
  27.             // this is needed to safely include the file name as part of the URL
  28.             $safeFilename $slugger->slug($originalFilename);
  29.             $newFilename $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
  30.             // Move the file to the directory where brochures are stored
  31.             try {
  32.                 $brochureFile->move(
  33.                 $this->getParameter('photos_directory'),
  34.                 $newFilename
  35.                 );
  36.             } catch (FileException $e) {
  37.                 throw new \Exception('Ups! ha ocurrido un error'.$e);
  38.             }
  39.             // updates the 'brochureFilename' property to store the PDF file name
  40.             // instead of its contents
  41.         }
  42.                 $post->setFoto($newFilename);
  43.         $user $this->getUser();
  44.         $post->setUser($user);
  45.         $em $this->getDoctrine()->getManager();
  46.         $em->persist($post);
  47.         $em->flush();
  48.         return $this->redirectToRoute('app_dashboard');
  49.     }
  50.         return $this->render('posts/index.html.twig', [
  51.             'form' => $form->createView()
  52.         ]);
  53.     }
  54.     #[Route('/post/{id}'name'app_post')]
  55.     public function verPost($id) {
  56.     $em $this->getDoctrine()->getManager();
  57.     $post $em->getRepository(Posts::class)->find($id);
  58.     return $this->render('posts/verPost.html.twig',['post'=>$post]);
  59.     }
  60.     #[Route('/misposts'name'app_misposts')]
  61.     public function misPosts() {
  62.     $em $this->getDoctrine()->getManager();
  63.     $user $this->getUser();
  64.     $posts $em->getRepository(Posts::class)->findby(['user'=>$user]);
  65.     return $this->render('posts/misPosts.html.twig',['posts'=>$posts]);
  66.     }
  67.     #[Route('/likes'name'app_likes'options: ['expose' => true] )]
  68.     public function likes(Request $request) {
  69.         if($request->isXmlHttpRequest()){
  70.         $em $this->getDoctrine()->getManager();
  71.         $user $this->getUser();
  72.         $id $request->request->get('id');
  73.         $post $em->getRepository(Posts::class)->find($id);
  74.         $likes $post->getLikes();
  75.         $likes .= $user->getId().',';
  76.         $post->setLikes($likes);
  77.         $em->flush();
  78.         return new JsonResponse(['likes'=>$likes]);
  79.         }else{
  80.             throw new \Exception('Error al recibir el like');
  81.         }
  82.     }
  83. }