<?php
namespace App\Controller;
use App\Entity\Actualite;
use App\Entity\Announce;
use App\Entity\AnnounceChannel;
use App\Entity\AnnounceChat;
use App\Entity\BuildingType;
use App\Entity\Company;
use App\Entity\Town;
use App\Entity\User;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\View;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class AnnounceDetailController extends AbstractController
{
private $emailVerifier;
private $entityManager;
public function __construct(EmailVerifier $emailVerifier, EntityManagerInterface $entityManager)
{
$this->emailVerifier = $emailVerifier;
$this->entityManager = $entityManager;
}
/**
* @Route("/announce/detail", name="app_announce_detail")
*/
public function index(Request $request): Response
{
$local=$request->getLocale();
$language="";
$onlinePath=$request->getBasePath();
if(strcmp($local,"fr")==0) {
$language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language' style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/french_flag.png\" style=\"height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> Français</a>";
$body='
<li> <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Anglais</a></li>
<li> <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Espagnol</a></li>
<li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Italien</a></li>
<li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Chinois</a></li>
';
}else if(strcmp($local,"es")==0) {
$language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language' style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/flag_spanish.png\" style=\"height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> Español</a>";
$body='
<li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Francés</a></li>
<li> <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Inglés</a></li>
<li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Italiano</a></li>
<li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Chinise</a></li>
';
}else if (strcmp($local,"en")==0) {
$language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language'style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/flag_england.png\" style=\"height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> English</a>";
$body='
<li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> French</a></li>
<li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Italiano</a></li>
<li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Chinise</a></li>
<li> <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Spanish</a></li>';
}else if (strcmp($local,"it")==0) {
$language=" <a href=\"#\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language'style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/italiano_flag.png\" style=\"height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> Italiano</a>";
$body='
<li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Francese</a></li>
<li> <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> inglese</a></li>
<li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Cinese</a></li>
<li> <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Spagnolo</a></li>';
}else if (strcmp($local,"zh_CN")==0) {
$language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language'style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/china_flag.png\" style=\"height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> 中國人</a>";
$body='
<li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 法語</a></li>
<li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 意大利語</a></li>
<li> <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 中國人</a></li>
<li> <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 西班牙語</a></li>';
}
try{
$announceId=$request->get("announce");
$current_announce=$this->entityManager->getRepository(Announce::class)->find($announceId);
if($announceId=="all"){
$current_announce="all";
}
}catch (\Exception $exception){
$current_announce=null;
}
$news= $this->entityManager->getRepository(Actualite::class)->findAll();
$towns= $this->entityManager->getRepository(Town::class)->findBy(array(),array("vues"=>"DESC"));
$announces= $this->entityManager->getRepository(Announce::class)->findAll();
$buildingTypes= $this->entityManager->getRepository(BuildingType::class)->findAll();
return $this->render('announce_detail/announce_detail.html.twig', [
'news' =>$news,
'announces' =>$announces,
'towns' => $towns,
'buildingTypes' => $buildingTypes,
'current_announce' => $current_announce,
'companyName' => $this->getParameter("app_client"),
'languageChoose' => $body,
'onlinePath' => $onlinePath,
'language' => $language,
'local' => $local,
'appUser' =>$this->getParameter('appUser'),
'host'=>$this->siteURL(),
]);
}
function siteURL()
{
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'].'/';
if ($_SERVER['HTTP_HOST'] == "localhost") {
return $protocol.$domainName.$this->getParameter('localRepository')."/".$this->getParameter('apiLink');
}else{
return $protocol.$domainName.$this->getParameter('apiLink');
}
}
/**
* Create announce channel with an offer.
* @Rest\Post ("announce_offer/generate")
* @View
*/
public function makeOffre(Request $request): Response{
// announce/offer/_user_token
$data = json_decode($request->getContent(), true);
$user= $this->entityManager->getRepository(User::class)->findOneBySomeField($this->decodeJWTPayloadOnly($data["_user_token"])->username);
$announce=$this->entityManager->getRepository(Announce::class)->find($data["announce"]);
if(is_null($announce) ){
return new JsonResponse( array(
"resultCode"=>404,
"message"=>"announce not found",
"result"=>null,
), Response::HTTP_NOT_ACCEPTABLE);
}else if(is_null($user)){
return new JsonResponse( array(
"resultCode"=>404,
"message"=>"user not found",
"result"=>null,
), Response::HTTP_NOT_ACCEPTABLE);
}else{
$searchInBD=$this->entityManager->getRepository(AnnounceChannel::class)->findOneBySomeField($announce->getId(), $user->getId() );
if($announce->getCreatedBy()->getId()!=$user->getId()) {
if (is_null($searchInBD)) {
$announceChanel = new AnnounceChannel();
$announceChanel->setAnnounce($announce);
$announceChanel->setOffer($data["offer"]);
$announceChanel->setCreatedBy($user);
$this->entityManager->persist($announceChanel);
$this->entityManager->flush();
$newChat = new AnnounceChat();
$newChat->setMessage("Je suis interessé par " . $announce->getTitleFr() . " J'offre " . $data["offer"] . " Xaf");
$newChat->setMessageType("text");
$newChat->setChannel($announceChanel);
$newChat->setSendBy($user);
$this->entityManager->persist($newChat);
$this->entityManager->flush();
return new JsonResponse(array(
"resultCode" => 201,
"message" => "Successfully Save.",
"result" => $announceChanel->getId(),
), Response::HTTP_CREATED);
} else {
$searchInBD->setOffer($data["offer"]);
$this->entityManager->persist($searchInBD);
$this->entityManager->flush();
return new JsonResponse(array(
"resultCode" => 200,
"message" => "Successfully Updated.",
"result" => $searchInBD->getId(),
), Response::HTTP_ACCEPTED);
}
}else{
return new JsonResponse(array(
"resultCode" => 403,
"message" => "Not authorised to perform this action.",
"result" => null,
), Response::HTTP_FORBIDDEN);
}
}
}
/**
* Create announce channel with an offer.
* @Rest\Post ("announce_chat/retrieved")
* @View
*/
public function getMessages(Request $request): Response{
$data = json_decode($request->getContent(), true);
$user= $this->entityManager->getRepository(User::class)->findOneBySomeField($this->decodeJWTPayloadOnly($data["_user_token"])->username);
$announceChannel=$this->entityManager->getRepository(AnnounceChannel::class)->findOneBySomeField($data["announce"], $user->getId() );
$announceChats=$this->entityManager->getRepository(AnnounceChat::class)->findOneBySomeField($announceChannel->getId());
$result=array();
foreach ($announceChats as $announceChat){
array_push($result,
array(
"message"=>$announceChat->getMessage(),
"messageType"=>$announceChat->getMessageType(),
"id"=>$announceChat->getId(),
"createdAt"=>$announceChat->getCreatedAt()->format("Y-m-d H:i:s"),
"vue"=>$announceChat->isVue(),
"sendBy"=>$announceChat->getSendBy()->getId(),
)
);
}
return new JsonResponse(
array(
"resultCode"=>200,
"ownerId"=>$announceChannel->getAnnounce()->getCreatedBy()->getId(),
"ownerName"=>$announceChannel->getAnnounce()->getCreatedBy()->getLastName()." ".$announceChannel->getAnnounce()->getCreatedBy()->getFirstName(),
"userId"=>$user->getId(),
"userLastName"=>$user->getLastName()." ". $user->getFirstName(),
"message"=>"Successfully.",
"result"=>$result,
), Response::HTTP_ACCEPTED);
}
/**
* Create announce channel with an offer.
* @Rest\Post ("announce_chat/post")
* @View
*/
public function postMessage(Request $request): Response{
$data = json_decode($request->getContent(), true);
$user= $this->entityManager->getRepository(User::class)->findOneBySomeField($this->decodeJWTPayloadOnly($data["_user_token"])->username);
$announce=$this->entityManager->getRepository(Announce::class)->find($data["announce"]);
$announceChannel=$this->entityManager->getRepository(AnnounceChannel::class)->findOneBySomeField($data["announce"], $user->getId() );
if(is_null($announceChannel)){
$announceChannel=new AnnounceChannel();
$announceChannel->setAnnounce($announce);
$announceChannel->setCreatedBy($user);
$announceChannel->setOffer($announce->getAmount());
$this->entityManager->persist($announceChannel);
$this->entityManager->flush();
}
$message=new AnnounceChat();
$message->setMessage($data["message"]);
$message->setChannel($announceChannel);
$message->setSendBy($user);
$message->setVue(false );
$message->setMessageType($data["messageType"] );
$this->entityManager->persist($message);
$this->entityManager->flush();
$announceChats=$this->entityManager->getRepository(AnnounceChat::class)->findOneBySomeField($announceChannel->getId());
$result=array();
foreach ($announceChats as $announceChat){
array_push($result,
array(
"message"=>$announceChat->getMessage(),
"messageType"=>$announceChat->getMessageType(),
"id"=>$announceChat->getId(),
"vue"=>$announceChat->isVue(),
"createdAt"=>$announceChat->getCreatedAt()->format("Y-m-d H:i:s"),
"sendBy"=>$announceChat->getSendBy()->getId(),
)
);
}
return new JsonResponse(
array(
"resultCode"=>200,
"ownerId"=>$announceChannel->getAnnounce()->getCreatedBy()->getId(),
"ownerName"=>$announceChannel->getAnnounce()->getCreatedBy()->getLastName()." ".$announceChannel->getAnnounce()->getCreatedBy()->getFirstName(),
"userId"=>$user->getId(),
"userLastName"=>$user->getLastName()." ". $user->getFirstName(),
"message"=>"Successfully.",
"result"=>$result,
), Response::HTTP_ACCEPTED);
}
function decodeJWTPayloadOnly($token){
$tks = explode('.', $token);
if (count($tks) != 3) {
return null;
}
list($headb64, $bodyb64, $cryptob64) = $tks;
$input=$bodyb64;
$remainder = strlen($input) % 4;
if ($remainder) {
$padlen = 4 - $remainder;
$input .= str_repeat('=', $padlen);
}
$input = (base64_decode(strtr($input, '-_', '+/')));
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
} else {
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
$obj = json_decode($json_without_bigints);
}
return $obj;
}
}