src/Controller/AnnounceDetailController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Actualite;
  4. use App\Entity\Announce;
  5. use App\Entity\AnnounceChannel;
  6. use App\Entity\AnnounceChat;
  7. use App\Entity\BuildingType;
  8. use App\Entity\Company;
  9. use App\Entity\Town;
  10. use App\Entity\User;
  11. use App\Security\EmailVerifier;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use FOS\RestBundle\Controller\Annotations as Rest;
  14. use FOS\RestBundle\Controller\Annotations\View;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  25. class AnnounceDetailController extends AbstractController
  26. {
  27.     private $emailVerifier;
  28.     private $entityManager;
  29.     public function __construct(EmailVerifier $emailVerifierEntityManagerInterface $entityManager)
  30.     {
  31.         $this->emailVerifier $emailVerifier;
  32.         $this->entityManager $entityManager;
  33.     }
  34.     /**
  35.      * @Route("/announce/detail", name="app_announce_detail")
  36.      */
  37.     public function index(Request $request): Response
  38.     {
  39.         $local=$request->getLocale();
  40.         $language="";
  41.         $onlinePath=$request->getBasePath();
  42.         if(strcmp($local,"fr")==0)  {
  43.             $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>";
  44.             $body='
  45.                <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>
  46.                 <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>
  47.                 <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>
  48.                 <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>
  49.                 ';
  50.         }else   if(strcmp($local,"es")==0)   {
  51.             $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>";
  52.             $body='
  53.                 <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>
  54.                 <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>
  55.                 <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>
  56.                 <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>
  57.                 ';
  58.         }else if  (strcmp($local,"en")==0)  {
  59.             $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>";
  60.             $body='
  61.                 <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>
  62.                 <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>
  63.                 <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>
  64.                 <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>';
  65.         }else if  (strcmp($local,"it")==0)  {
  66.             $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>";
  67.             $body='
  68.                 <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>
  69.                 <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>
  70.                 <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>
  71.                 <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>';
  72.         }else if  (strcmp($local,"zh_CN")==0)  {
  73.             $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>";
  74.             $body='
  75.                 <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>
  76.                 <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>
  77.                 <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>
  78.                 <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>';
  79.         }
  80.         try{
  81.             $announceId=$request->get("announce");
  82.             $current_announce=$this->entityManager->getRepository(Announce::class)->find($announceId);
  83.              if($announceId=="all"){
  84.                 $current_announce="all";
  85.             }
  86.          }catch (\Exception $exception){
  87.             $current_announce=null;
  88.         }
  89.         $news$this->entityManager->getRepository(Actualite::class)->findAll();
  90.         $towns$this->entityManager->getRepository(Town::class)->findBy(array(),array("vues"=>"DESC"));
  91.         $announces$this->entityManager->getRepository(Announce::class)->findAll();
  92.         $buildingTypes$this->entityManager->getRepository(BuildingType::class)->findAll();
  93.         return $this->render('announce_detail/announce_detail.html.twig', [
  94.             'news' =>$news,
  95.             'announces' =>$announces,
  96.             'towns' => $towns,
  97.             'buildingTypes' => $buildingTypes,
  98.             'current_announce' => $current_announce,
  99.             'companyName' => $this->getParameter("app_client"),
  100.             'languageChoose' => $body,
  101.             'onlinePath' => $onlinePath,
  102.             'language' => $language,
  103.             'local' => $local,
  104.             'appUser' =>$this->getParameter('appUser'),
  105.             'host'=>$this->siteURL(),
  106.         ]);
  107.     }
  108.     function siteURL()
  109.     {
  110.         $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" "http://";
  111.         $domainName $_SERVER['HTTP_HOST'].'/';
  112.         if ($_SERVER['HTTP_HOST'] == "localhost") {
  113.             return $protocol.$domainName.$this->getParameter('localRepository')."/".$this->getParameter('apiLink');
  114.         }else{
  115.             return $protocol.$domainName.$this->getParameter('apiLink');
  116.         }
  117.     }
  118.     /**
  119.      * Create announce channel with an offer.
  120.      * @Rest\Post ("announce_offer/generate")
  121.      * @View
  122.      */
  123.      public function makeOffre(Request $request): Response{
  124.         //  announce/offer/_user_token
  125.          $data json_decode($request->getContent(), true);
  126.          $user$this->entityManager->getRepository(User::class)->findOneBySomeField($this->decodeJWTPayloadOnly($data["_user_token"])->username);
  127.          $announce=$this->entityManager->getRepository(Announce::class)->find($data["announce"]);
  128.          if(is_null($announce) ){
  129.              return new JsonResponse(    array(
  130.                  "resultCode"=>404,
  131.                  "message"=>"announce not found",
  132.                  "result"=>null,
  133.              ), Response::HTTP_NOT_ACCEPTABLE);
  134.          }else if(is_null($user)){
  135.              return new JsonResponse(    array(
  136.                  "resultCode"=>404,
  137.                  "message"=>"user not found",
  138.                  "result"=>null,
  139.              ), Response::HTTP_NOT_ACCEPTABLE);
  140.          }else{
  141.              $searchInBD=$this->entityManager->getRepository(AnnounceChannel::class)->findOneBySomeField($announce->getId(), $user->getId() );
  142.              if($announce->getCreatedBy()->getId()!=$user->getId()) {
  143.                  if (is_null($searchInBD)) {
  144.                      $announceChanel = new AnnounceChannel();
  145.                      $announceChanel->setAnnounce($announce);
  146.                      $announceChanel->setOffer($data["offer"]);
  147.                      $announceChanel->setCreatedBy($user);
  148.                      $this->entityManager->persist($announceChanel);
  149.                      $this->entityManager->flush();
  150.                      $newChat = new AnnounceChat();
  151.                      $newChat->setMessage("Je suis interessé par " $announce->getTitleFr() . " J'offre " $data["offer"] . " Xaf");
  152.                      $newChat->setMessageType("text");
  153.                      $newChat->setChannel($announceChanel);
  154.                      $newChat->setSendBy($user);
  155.                      $this->entityManager->persist($newChat);
  156.                      $this->entityManager->flush();
  157.                      return new JsonResponse(array(
  158.                          "resultCode" => 201,
  159.                          "message" => "Successfully Save.",
  160.                          "result" => $announceChanel->getId(),
  161.                      ), Response::HTTP_CREATED);
  162.                  } else {
  163.                      $searchInBD->setOffer($data["offer"]);
  164.                      $this->entityManager->persist($searchInBD);
  165.                      $this->entityManager->flush();
  166.                      return new JsonResponse(array(
  167.                          "resultCode" => 200,
  168.                          "message" => "Successfully Updated.",
  169.                          "result" => $searchInBD->getId(),
  170.                      ), Response::HTTP_ACCEPTED);
  171.                  }
  172.              }else{
  173.                  return new JsonResponse(array(
  174.                      "resultCode" => 403,
  175.                      "message" => "Not authorised to perform this action.",
  176.                      "result" => null,
  177.                  ), Response::HTTP_FORBIDDEN);
  178.              }
  179.          }
  180.     }
  181.     /**
  182.      * Create announce channel with an offer.
  183.      * @Rest\Post ("announce_chat/retrieved")
  184.      * @View
  185.      */
  186.     public function getMessages(Request $request): Response{
  187.         $data json_decode($request->getContent(), true);
  188.         $user$this->entityManager->getRepository(User::class)->findOneBySomeField($this->decodeJWTPayloadOnly($data["_user_token"])->username);
  189.         $announceChannel=$this->entityManager->getRepository(AnnounceChannel::class)->findOneBySomeField($data["announce"], $user->getId() );
  190.         $announceChats=$this->entityManager->getRepository(AnnounceChat::class)->findOneBySomeField($announceChannel->getId());
  191.         $result=array();
  192.         foreach ($announceChats as $announceChat){
  193.             array_push($result,
  194.                 array(
  195.                     "message"=>$announceChat->getMessage(),
  196.                     "messageType"=>$announceChat->getMessageType(),
  197.                     "id"=>$announceChat->getId(),
  198.                     "createdAt"=>$announceChat->getCreatedAt()->format("Y-m-d  H:i:s"),
  199.                     "vue"=>$announceChat->isVue(),
  200.                     "sendBy"=>$announceChat->getSendBy()->getId(),
  201.                 )
  202.             );
  203.         }
  204.         return new JsonResponse(
  205.             array(
  206.             "resultCode"=>200,
  207.             "ownerId"=>$announceChannel->getAnnounce()->getCreatedBy()->getId(),
  208.             "ownerName"=>$announceChannel->getAnnounce()->getCreatedBy()->getLastName()." ".$announceChannel->getAnnounce()->getCreatedBy()->getFirstName(),
  209.             "userId"=>$user->getId(),
  210.             "userLastName"=>$user->getLastName()." "$user->getFirstName(),
  211.             "message"=>"Successfully.",
  212.             "result"=>$result,
  213.         ), Response::HTTP_ACCEPTED);
  214.     }
  215.     /**
  216.      * Create announce channel with an offer.
  217.      * @Rest\Post ("announce_chat/post")
  218.      * @View
  219.      */
  220.     public function postMessage(Request $request): Response{
  221.         $data json_decode($request->getContent(), true);
  222.         $user$this->entityManager->getRepository(User::class)->findOneBySomeField($this->decodeJWTPayloadOnly($data["_user_token"])->username);
  223.         $announce=$this->entityManager->getRepository(Announce::class)->find($data["announce"]);
  224.         $announceChannel=$this->entityManager->getRepository(AnnounceChannel::class)->findOneBySomeField($data["announce"], $user->getId() );
  225.         if(is_null($announceChannel)){
  226.             $announceChannel=new AnnounceChannel();
  227.             $announceChannel->setAnnounce($announce);
  228.             $announceChannel->setCreatedBy($user);
  229.             $announceChannel->setOffer($announce->getAmount());
  230.             $this->entityManager->persist($announceChannel);
  231.             $this->entityManager->flush();
  232.         }
  233.         $message=new AnnounceChat();
  234.         $message->setMessage($data["message"]);
  235.         $message->setChannel($announceChannel);
  236.         $message->setSendBy($user);
  237.         $message->setVue(false );
  238.         $message->setMessageType($data["messageType"] );
  239.         $this->entityManager->persist($message);
  240.         $this->entityManager->flush();
  241.         $announceChats=$this->entityManager->getRepository(AnnounceChat::class)->findOneBySomeField($announceChannel->getId());
  242.         $result=array();
  243.         foreach ($announceChats as $announceChat){
  244.             array_push($result,
  245.                 array(
  246.                     "message"=>$announceChat->getMessage(),
  247.                     "messageType"=>$announceChat->getMessageType(),
  248.                     "id"=>$announceChat->getId(),
  249.                     "vue"=>$announceChat->isVue(),
  250.                     "createdAt"=>$announceChat->getCreatedAt()->format("Y-m-d H:i:s"),
  251.                     "sendBy"=>$announceChat->getSendBy()->getId(),
  252.                 )
  253.             );
  254.         }
  255.         return new JsonResponse(
  256.             array(
  257.             "resultCode"=>200,
  258.             "ownerId"=>$announceChannel->getAnnounce()->getCreatedBy()->getId(),
  259.             "ownerName"=>$announceChannel->getAnnounce()->getCreatedBy()->getLastName()." ".$announceChannel->getAnnounce()->getCreatedBy()->getFirstName(),
  260.             "userId"=>$user->getId(),
  261.             "userLastName"=>$user->getLastName()." "$user->getFirstName(),
  262.             "message"=>"Successfully.",
  263.             "result"=>$result,
  264.         ), Response::HTTP_ACCEPTED);
  265.     }
  266.     function decodeJWTPayloadOnly($token){
  267.         $tks explode('.'$token);
  268.         if (count($tks) != 3) {
  269.             return null;
  270.         }
  271.         list($headb64$bodyb64$cryptob64) = $tks;
  272.         $input=$bodyb64;
  273.         $remainder strlen($input) % 4;
  274.         if ($remainder) {
  275.             $padlen $remainder;
  276.             $input .= str_repeat('='$padlen);
  277.         }
  278.         $input = (base64_decode(strtr($input'-_''+/')));
  279.         if (version_compare(PHP_VERSION'5.4.0''>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE 4)) {
  280.             $obj json_decode($inputfalse512JSON_BIGINT_AS_STRING);
  281.         } else {
  282.             $max_int_length strlen((string) PHP_INT_MAX) - 1;
  283.             $json_without_bigints preg_replace('/:\s*(-?\d{'.$max_int_length.',})/'': "$1"'$input);
  284.             $obj json_decode($json_without_bigints);
  285.         }
  286.         return $obj;
  287.     }
  288. }