<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\TrafficTracker;
use App\Entity\Licensecomment;
use App\Entity\License;
use App\Entity\User;
use App\Repository\LicenseCommentRepository;
use App\Repository\UserRepository;
class LicenseCommentController extends AbstractController
{
/**
* @Route("/license/comment", name="app_license_comment")
*/
public function index(): Response
{
return $this->render('license_comment/index.html.twig', [
'controller_name' => 'LicenseCommentController',
]);
}
/**
* @Route("/licensecomment/createComment/{id}", name="license_create_comment", requirements={"id"="\d+"})
*/
public function createLicenseComment(Request $request, $message=null, $id, TranslatorInterface $translator, TrafficTracker $trafficTracker)
{
$trafficTracker->registerTraffic();
/** @var EntityManager */
$entityManager = $this->getDoctrine()->getManager();
$user = $this->getUser();
$response = new Response();
$message=$request->get('message');
if (!ctype_space($message)) {
$comment = new Licensecomment();
$comment->setComment($message);
$comment->setIdLicense($id);
$comment->setIdUser($user->getId());
$comment->setIsPrivate($request->get('private'));
$entityManager->persist($comment);
$entityManager->flush();
}
else{
$message="";
}
$formatter = \IntlDateFormatter::create($user->getLocale(), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
$date=$formatter->format($comment->getCreatedAt());
$response = json_encode(array('message' => $message, 'private' => $request->get('private'), 'date' => $date));
return new Response($response, 200, array(
'Content-Type' => 'application/json'
));
}
/**
* @Route("/licensecomment/retrieveLicenseComments/{id}", name="license_retrieve_comments", requirements={"id"="\d+"})
*/
public function retrieveLicenseComments(Request $request, $id, TranslatorInterface $translator, TrafficTracker $trafficTracker)
{
$trafficTracker->registerTraffic();
$user = $this->getUser();
$response = new Response();
/** @var EntityManager */
$entityManager = $this->getDoctrine()->getManager();
/** @var LicenseCommentRepository */
$licenseCommentRepository = $entityManager->getRepository(LicenseComment::class);
/** @var LicenseRepository */
$licenseRepository = $entityManager->getRepository(License::class);
/** @var UserRepository */
$userRepository = $entityManager->getRepository(User::class);
$license=$licenseRepository->find($id);
$comments=$licenseCommentRepository->findLicenseComments($license, $user);
$formatter = \IntlDateFormatter::create($user->getLocale(), \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
$arrayComments=array();
foreach($comments as $LicenseComment){
$commentUser=$userRepository->find($LicenseComment->getIdUser());
$userFullName=$commentUser->getName()." ".$commentUser->getSurName();
array_push($arrayComments, array("comment" => $LicenseComment->getComment(), "userFullName" => $userFullName, "private" => $LicenseComment->getIsPrivate(), "date" => $formatter->format($LicenseComment->getCreatedAt())));
}
$response = json_encode($arrayComments);
return new Response($response, 200, array(
'Content-Type' => 'application/json'
));
}
}