src/Controller/LicenseCommentController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use App\Service\TrafficTracker;
  9. use App\Entity\Licensecomment;
  10. use App\Entity\License;
  11. use App\Entity\User;
  12. use App\Repository\LicenseCommentRepository;
  13. use App\Repository\UserRepository;
  14. class LicenseCommentController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/license/comment", name="app_license_comment")
  18.      */
  19.     public function index(): Response
  20.     {
  21.         return $this->render('license_comment/index.html.twig', [
  22.             'controller_name' => 'LicenseCommentController',
  23.         ]);
  24.     }
  25.     /**
  26.      * @Route("/licensecomment/createComment/{id}", name="license_create_comment", requirements={"id"="\d+"})
  27.      */
  28.     public function createLicenseComment(Request $request$message=null$idTranslatorInterface $translatorTrafficTracker $trafficTracker)
  29.     {
  30.         $trafficTracker->registerTraffic();
  31.         /** @var EntityManager */
  32.         $entityManager $this->getDoctrine()->getManager();
  33.         $user $this->getUser();
  34.         $response = new Response();
  35.         $message=$request->get('message');
  36.         if (!ctype_space($message)) {
  37.             $comment = new Licensecomment();
  38.             $comment->setComment($message);
  39.             $comment->setIdLicense($id);
  40.             $comment->setIdUser($user->getId());
  41.             $comment->setIsPrivate($request->get('private'));
  42.             $entityManager->persist($comment);
  43.             $entityManager->flush();
  44.         }
  45.         else{
  46.             $message="";
  47.         }
  48.         $formatter = \IntlDateFormatter::create($user->getLocale(), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
  49.         $date=$formatter->format($comment->getCreatedAt());
  50.         $response json_encode(array('message' => $message'private' => $request->get('private'), 'date' => $date));
  51.               
  52.         return new Response($response200, array(
  53.             'Content-Type' => 'application/json'
  54.         ));  
  55.     }
  56.     /**
  57.      * @Route("/licensecomment/retrieveLicenseComments/{id}", name="license_retrieve_comments", requirements={"id"="\d+"})
  58.      */
  59.     public function retrieveLicenseComments(Request $request$idTranslatorInterface $translatorTrafficTracker $trafficTracker)
  60.     {
  61.         $trafficTracker->registerTraffic();
  62.         $user $this->getUser();
  63.         $response = new Response();
  64.         /** @var EntityManager */
  65.         $entityManager $this->getDoctrine()->getManager();
  66.         /** @var LicenseCommentRepository */
  67.         $licenseCommentRepository $entityManager->getRepository(LicenseComment::class);
  68.         /** @var LicenseRepository */
  69.         $licenseRepository $entityManager->getRepository(License::class);
  70.         /** @var UserRepository */
  71.         $userRepository $entityManager->getRepository(User::class);
  72.     
  73.         $license=$licenseRepository->find($id);
  74.         $comments=$licenseCommentRepository->findLicenseComments($license$user);
  75.         $formatter = \IntlDateFormatter::create($user->getLocale(), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
  76.         $arrayComments=array();
  77.         foreach($comments as $LicenseComment){
  78.             $commentUser=$userRepository->find($LicenseComment->getIdUser());
  79.             $userFullName=$commentUser->getName()." ".$commentUser->getSurName();
  80.             array_push($arrayComments, array("comment" => $LicenseComment->getComment(), "userFullName" => $userFullName"private" => $LicenseComment->getIsPrivate(), "date" => $formatter->format($LicenseComment->getCreatedAt())));
  81.         }
  82.         $response json_encode($arrayComments);
  83.               
  84.         return new Response($response200, array(
  85.             'Content-Type' => 'application/json'
  86.         ));  
  87.     }
  88. }