src/MDS/ApiBundle/Controller/ApiAttendantsController.php line 40

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: DEVELUP-USER3
  5.  * Date: 05/03/2019
  6.  * Time: 10:57
  7.  */
  8. namespace App\MDS\ApiBundle\Controller;
  9. use App\Entity\MobileAppEventContact;
  10. use App\Entity\MobileAppUsersBehavior;
  11. use App\Entity\EmailRegisteredEventAssistants;
  12. use DateInterval;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. use FOS\RestBundle\Controller\Annotations as Rest;
  22. use FOS\RestBundle\Controller\AbstractFOSRestController;
  23. use FOS\RestBundle\View\View;
  24. use Symfony\Component\DependencyInjection\ContainerInterface;
  25. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  26. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  27. use Swift_Mailer;
  28. use Swift_Message;
  29. use Swift_Attachment;
  30. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  31. class ApiAttendantsController extends AbstractFOSRestController
  32. {
  33.     /**
  34.      * @Route("/api/email/registered/event/assistants", methods={"GET"})
  35.      */
  36.     public function apiGuestAction(Request $requestEntityManagerInterface $em)
  37.     {
  38.         $serverHour = new \DateTime();
  39.         $serverHour->add(new \DateInterval('PT1H'));
  40.         $serverHourLess = clone $serverHour;
  41.         $serverHourLess->sub(new \DateInterval('PT5M'));
  42.         $serverHourLess $serverHourLess->format('H:i');
  43.         $serverHourMore = clone $serverHour;
  44.         $serverHourMore->add(new \DateInterval('PT5M'));
  45.         $serverHourMore $serverHourMore->format('H:i');
  46.         $configurations $em->getRepository('App:EmailRegisteredEventAssistants')->findAll();
  47.         foreach ($configurations as $configuration) {
  48.             $hour $configuration->getHour()->format('H:i');
  49.             $rows = [];
  50.             $headers $configuration->getColumns();
  51.             $headers explode(';'$headers);
  52.             $event $em->getRepository('App:MobileAppEvent')->findOneBy(['id' => $configuration->getEid()]);
  53.             $guests $em->getRepository('App:AppMobileGuest')->findBy(['eid' => $event->getId()]);
  54.             if ($hour >= $serverHourLess && $hour <= $serverHourMore) {
  55.                 foreach ($guests as $guest) {
  56.                     $rowData = [];
  57.                     foreach ($headers as $header) {
  58.                         $getterMethod 'get' ucfirst(strtolower(str_replace(' '''$header)));
  59.                         if (method_exists($guest$getterMethod)) {
  60.                             $rowData[$header] = $guest->{$getterMethod}();
  61.                         } else {
  62.                             $rowData[$header] = '';
  63.                         }
  64.                     }
  65.                     $rows[] = $rowData;
  66.                 }
  67.                 $tempFile tempnam(sys_get_temp_dir(), 'excel') . '.xlsx';
  68.                 $this->generateCsvRegisteredAttendantsSold($headers$rows$tempFile);
  69.                 $message = (new Swift_Message('Datos de registros - ' $event->getName()))
  70.                     ->setFrom('eventos@develup.solutions')
  71.                     ->setTo($configuration->getEmailTo())
  72.                     ->setBody('Datos de registrados en fichero adjunto''text/plain''utf-8')
  73.                     ->attach(Swift_Attachment::fromPath($tempFile)->setFilename($event->getName() . '.xlsx'));
  74.                 $this->mailer->send($message);
  75.                 unlink($tempFile);
  76.             }
  77.         }
  78.         return new Response();
  79.     }
  80.     protected function generateCsvRegisteredAttendantsSold($headers$rows$tempFile)
  81.     {
  82.         $spreadsheet = new Spreadsheet();
  83.         $sheet $spreadsheet->getActiveSheet();
  84.         $columnIndex 'A';
  85.         foreach ($headers as $header) {
  86.             $sheet->setCellValue($columnIndex '1'$header);
  87.             $columnIndex++;
  88.         }
  89.         $rowIndex 2;
  90.         foreach ($rows as $row) {
  91.             $columnIndex 'A';
  92.             foreach ($row as $cellValue) {
  93.                 $sheet->setCellValue($columnIndex $rowIndex$cellValue);
  94.                 $sheet->getStyle($columnIndex $rowIndex)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
  95.                 $columnIndex++;
  96.             }
  97.             $rowIndex++;
  98.         }
  99.         $writer = new Xlsx($spreadsheet);
  100.         $writer->save($tempFile);
  101.     }
  102.     /**
  103.      * @Rest\Get("/api/attendants/get")
  104.      */
  105.     public function selectApiAttendantAction(Request $request)
  106.     {
  107.         $eid $request->headers->get('eid');
  108.         $em $this->getDoctrine()->getManager();
  109.         $token $request->headers->get('token');
  110.         $uid $em->getRepository(MobileAppToken::class)->findOneBy(
  111.             array('token' => $token)
  112.         );
  113.         if(empty($uid)){
  114.             $response = new JsonResponse("Session expired! Login again"401);
  115.             return $response;
  116.         }
  117.         $uid $uid->getUid();
  118.         $eventusers $em->getRepository(AppUsersByEvent::class)->findBy(
  119.             array(
  120.                 'eventId' => $eid,
  121.                 'status' => 1,
  122.             ),
  123.             array('qr' => 'ASC')
  124.         );
  125.         $attendants = array();
  126. //        $attendants = [];
  127.         if (!empty($eventusers)) {
  128.             foreach ($eventusers as $usuario){
  129.                 if($usuario->getUserId() !== $uid){
  130.                     $user $em->getRepository(MobileAppUsers::class)->findOneBy(
  131.                         array('shareContact' => 1'id' => $usuario->getUserId() )
  132.                     );
  133.                     $attendantsUser $em->getrepository(AppMobileGuest::class)->findOneBy(
  134.                         array( 'token' => $usuario->getQrToken() )
  135.                     );
  136.                     if(!empty($user) and !empty($attendantsUser)){
  137.                         $attendants[] = array(
  138.                             "id" => $user->getId(),
  139.                             "attendantid" => $attendantsUser->getId(),
  140.                             "name" => $user->getFullname(),
  141.                             "image" => empty($user->getImage()) ? null 'https://' $_SERVER['HTTP_HOST']. '/' $user->getImage()
  142.                         );
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         $count count($attendants);
  148.         $response_body json_encode(array(
  149.             'attendants' => $attendants,
  150.             'meta' => array('count' => $count),
  151.         ));
  152.         $response = new JsonResponse($response_body);
  153.         $em->clear();
  154.         return $response;
  155.     }
  156.     /**
  157.      * @Rest\Get("/api/attendants/getOne")
  158.      */
  159.     public function selectApiOneAttendantsAction(Request $request) {
  160.         $attendantid $request->headers->get('attendantid');
  161.         $id $request->headers->get('id');
  162.         $em $this->getDoctrine()->getManager();
  163.         $attendantsConsulta $em->getrepository(AppMobileGuest::class)->findOneBy(
  164.             array('id' => $attendantid)
  165.         );
  166.         $user $em->getRepository(MobileAppUsers::class)->findOneBy(
  167.             array('id' => $id )
  168.         );
  169.         $data = array();
  170.         if (!empty($attendantsConsulta)){
  171.             $prefix='https://'.$_SERVER['HTTP_HOST'].'/';
  172.             $var=(is_null($user->getImage()))?NULL:$prefix.$user->getImage();
  173.             $data = array(
  174.                 "id" => $id,
  175.                 "attendantid" => $attendantsConsulta->getId(),
  176.                 "eid" => $attendantsConsulta->getEid(),
  177.                 "image" =>$var,
  178.                 "name" => $attendantsConsulta->getFirstName().' '.$attendantsConsulta->getLastName(),
  179.                 "jobTitle" => is_null($attendantsConsulta->getJobTitle())?"":$attendantsConsulta->getJobTitle(),
  180.                 "company" => is_null($attendantsConsulta->getCompany())?"":$attendantsConsulta->getCompany(),
  181.                 "email" => $attendantsConsulta->getEmail(),
  182.                 "telephone" => is_null($attendantsConsulta->getTelephone())?"":$attendantsConsulta->getTelephone(),
  183.             );
  184.         }
  185.         $response_body json_encode(array(
  186.             'data' => $data,
  187.         ));
  188.         $response = new JsonResponse($response_body);
  189.         $em->clear();
  190.         return $response;
  191.     }
  192.     /**
  193.      * @Rest\Get("/api/attendants/getNat")
  194.      */
  195.     public function selectApiAttendantNativoAction(Request $request)
  196.     {
  197.         $eid $request->headers->get('eid');
  198.         $token $request->headers->get('token');
  199.         /* Mobile Operating System */
  200.         if ($request->headers->has('operatingsystem') == true){
  201.             if (empty($request->headers->get('operatingsystem'))){
  202.                 $os null;
  203.             }else{
  204.                 $os $request->headers->get('operatingsystem');
  205.             }
  206.         }else{
  207.             $os null;
  208.         }
  209.         /* Mobile Model */
  210.         if ($request->headers->has('mobilemodel') == true){
  211.             if (empty($request->headers->get('mobilemodel'))){
  212.                 $mm null;
  213.             }else{
  214.                 $mm $request->headers->get('mobilemodel');
  215.             }
  216.         }else{
  217.             $mm null;
  218.         }
  219.         /* Mobile Version */
  220.         if ($request->headers->has('mobileversion') == true){
  221.             if (empty($request->headers->get('mobileversion'))){
  222.                 $mv null;
  223.             }else{
  224.                 $mv $request->headers->get('mobileversion');
  225.             }
  226.         }else{
  227.             $mv null;
  228.         }
  229.         /* Mobile lang */
  230.         if ($request->headers->has('lang') == true){
  231.             if (empty($request->headers->get('lang'))){
  232.                 $lg null;
  233.             }else{
  234.                 $lg $request->headers->get('lang');
  235.             }
  236.         }else{
  237.             $lg null;
  238.         }
  239.         $em $this->getDoctrine()->getManager();
  240.         $uid $em->getRepository(MobileAppToken::class)->findOneBy(
  241.             array('token' => $token)
  242.         );
  243.         if(empty($uid)){
  244.             $response = new JsonResponse("Session expired! Login again"401);
  245.             return $response;
  246.         }
  247.         $uid $uid->getUid();
  248.         $eventusers $em->getRepository(AppUsersByEvent::class)->findBy(
  249.             array(
  250.                 'eventId' => $eid,
  251.                 'status' => 1,
  252.             ),
  253.             array('qr' => 'ASC')
  254.         );
  255.         $attendants = array();
  256. //        $attendants = [];
  257.         if (!empty($eventusers)) {
  258.             foreach ($eventusers as $usuario){
  259.                 if($usuario->getUserId() !== $uid){
  260.                     $user $em->getRepository(MobileAppUsers::class)->findOneBy(
  261.                         array('shareContact' => 1'id' => $usuario->getUserId() )
  262.                     );
  263.                     $attendantsUser $em->getrepository(AppMobileGuest::class)->findOneBy(
  264.                         array( 'token' => $usuario->getQrToken() )
  265.                     );
  266.                     if(!empty($user) and !empty($attendantsUser)){
  267.                         $attendants[] = array(
  268.                             "id" => $user->getId(),
  269.                             "attendantid" => $attendantsUser->getId(),
  270. //                            "fullname" => $user->getFullname(),
  271.                             "fullname" => $attendantsUser->getLastName().' '.$attendantsUser->getFirstName(),
  272.                             "name" => $attendantsUser->getFirstName(),
  273.                             "lastname" => $attendantsUser->getLastName(),
  274.                             "image" => empty($user->getImage()) ? null 'https://' $_SERVER['HTTP_HOST']. '/' $user->getImage()
  275.                         );
  276.                     }
  277.                 }
  278.                 /* LIBERAR MEMORIA A MANO */
  279.                 $em->detach($usuario);
  280.                 unset($usuario);
  281.             }
  282.         }
  283.         $action 'List Attendants';
  284.         $this->saveUserBehavior($uid$eid$os$mm$mv$lg$action,'Attendants');
  285.         $count count$attendants );
  286.         $response_base =array(
  287.             'data' => $attendants,
  288.             'meta' => array( 'count' => $count ),
  289.         );
  290.         $response_body json_encode($response_baseJSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES );
  291.         $response = new Response($response_body200);
  292.         $em->clear();
  293.         return $response;
  294.     }
  295.     /**
  296.      * @Rest\Get("/api/attendants/getOneNat")
  297.      */
  298.     public function selectApiOneAttendantsNativoAction(Request $request) {
  299.         $attendantid $request->headers->get('attendantid');
  300.         $id $request->headers->get('id');
  301. //        $token = $request->headers->get('token');
  302.         $em $this->getDoctrine()->getManager();
  303. //        $uid = $em->getRepository(MobileAppToken::class)->findOneBy(
  304. //            array('token' => $token)
  305. //        );
  306. //
  307. //        if(empty($uid)){
  308. //            $response = new JsonResponse("Session expired! Login again", 401);
  309. //            return $response;
  310. //        }
  311.         $attendantsConsulta $em->getrepository(AppMobileGuest::class)->findOneBy(
  312.             array('id' => $attendantid)
  313.         );
  314.         $user $em->getRepository(MobileAppUsers::class)->findOneBy(
  315.             array('id' => $id )
  316.         );
  317.         $data = array();
  318.         if (!empty($attendantsConsulta)){
  319.             $prefix='https://'.$_SERVER['HTTP_HOST'].'/';
  320.             $var=(is_null($user->getImage()))?NULL:$prefix.$user->getImage();
  321.             $data = array(
  322.                 "id" => $id,
  323.                 "attendantid" => $attendantsConsulta->getId(),
  324.                 "eid" => $attendantsConsulta->getEid(),
  325.                 "image" =>$var,
  326.                 "fullname" => $attendantsConsulta->getFirstName().' '.$attendantsConsulta->getLastName(),
  327.                 "name" => $attendantsConsulta->getFirstName(),
  328.                 "lastname" => $attendantsConsulta->getLastName(),
  329.                 "jobTitle" => is_null($attendantsConsulta->getJobTitle())?"":$attendantsConsulta->getJobTitle(),
  330.                 "company" => is_null($attendantsConsulta->getCompany())?"":$attendantsConsulta->getCompany(),
  331.                 "email" => $attendantsConsulta->getEmail(),
  332.                 "telephone" => is_null($attendantsConsulta->getTelephone())?"":$attendantsConsulta->getTelephone(),
  333.             );
  334.         }
  335.         $response_base =array(
  336.             'data' => $data,
  337.         );
  338.         $response_body json_encode($response_baseJSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES );
  339.         $response = new Response($response_body200);
  340.         $em->clear();
  341.         return $response;
  342.     }
  343.     /**
  344.      * @Rest\Get("/api/attendants/getTwoNat")
  345.      */
  346.     public function selectApiTwoAttendantsNativoAction(Request $request) {
  347.         $attendantid $request->headers->get('attendantid');
  348.         $id $request->headers->get('id');
  349.         $token $request->headers->get('token');
  350.         /* Mobile Operating System */
  351.         if ($request->headers->has('operatingsystem') == true){
  352.             if (empty($request->headers->get('operatingsystem'))){
  353.                 $os null;
  354.             }else{
  355.                 $os $request->headers->get('operatingsystem');
  356.             }
  357.         }else{
  358.             $os null;
  359.         }
  360.         /* Mobile Model */
  361.         if ($request->headers->has('mobilemodel') == true){
  362.             if (empty($request->headers->get('mobilemodel'))){
  363.                 $mm null;
  364.             }else{
  365.                 $mm $request->headers->get('mobilemodel');
  366.             }
  367.         }else{
  368.             $mm null;
  369.         }
  370.         /* Mobile Version */
  371.         if ($request->headers->has('mobileversion') == true){
  372.             if (empty($request->headers->get('mobileversion'))){
  373.                 $mv null;
  374.             }else{
  375.                 $mv $request->headers->get('mobileversion');
  376.             }
  377.         }else{
  378.             $mv null;
  379.         }
  380.         /* Mobile lang */
  381.         if ($request->headers->has('lang') == true){
  382.             if (empty($request->headers->get('lang'))){
  383.                 $lg null;
  384.             }else{
  385.                 $lg $request->headers->get('lang');
  386.             }
  387.         }else{
  388.             $lg null;
  389.         }
  390.         $em $this->getDoctrine()->getManager();
  391.         $uid $em->getRepository(MobileAppToken::class)->findOneBy(
  392.             array('token' => $token)
  393.         );
  394.         if(empty($uid)){
  395.             $response = new JsonResponse("Session expired! Login again"401);
  396.             return $response;
  397.         }
  398.         $attendantsConsulta $em->getrepository(AppMobileGuest::class)->findOneBy(
  399.             array('id' => $attendantid)
  400.         );
  401.         $user $em->getRepository(MobileAppUsers::class)->findOneBy(
  402.             array('id' => $id )
  403.         );
  404.         $data = array();
  405.         if (!empty($attendantsConsulta)){
  406.             // comprobamos que ya no tenga los permisos previamente
  407.             $MobileAppScanCardPermission =  $em->getRepository(MobileAppScanCardPermission::class)->findOneBy(
  408.                 array(
  409.                     'eid' => $attendantsConsulta->getEid(),
  410.                     'asksUserId' => $uid->getUid(),
  411.                     'grantUserGuestId' => $attendantid,
  412.                     'grantUserId' => $user->getId(),
  413.                 )
  414.             );
  415.             if (empty($MobileAppScanCardPermission)){
  416.                 $jobTitle "";
  417.                 $company "";
  418.                 $email "";
  419.                 $telephone "";
  420.             }else{
  421.                 $jobTitle is_null($attendantsConsulta->getJobTitle())?"":$attendantsConsulta->getJobTitle();
  422.                 $company is_null($attendantsConsulta->getCompany())?"":$attendantsConsulta->getCompany();
  423.                 $email $attendantsConsulta->getEmail();
  424.                 $telephone is_null($attendantsConsulta->getTelephone())?"":$attendantsConsulta->getTelephone();
  425.             }
  426.             $prefix='https://'.$_SERVER['HTTP_HOST'].'/';
  427.             $var=(is_null($user->getImage()))?NULL:$prefix.$user->getImage();
  428.             $data = array(
  429.                 "id" => $id,
  430.                 "attendantid" => $attendantsConsulta->getId(),
  431.                 "eid" => $attendantsConsulta->getEid(),
  432.                 "image" =>$var,
  433.                 "fullname" => $attendantsConsulta->getFirstName().' '.$attendantsConsulta->getLastName(),
  434.                 "name" => $attendantsConsulta->getFirstName(),
  435.                 "lastname" => $attendantsConsulta->getLastName(),
  436.                 "jobTitle" => $jobTitle,
  437.                 "company" => $company,
  438.                 "email" => $email,
  439.                 "telephone" => $telephone,
  440.             );
  441.         }
  442.         $action 'Details Attendants, '.$attendantid;
  443.         $this->saveUserBehavior($uid->getUid(), $attendantsConsulta->getEid(), $os$mm$mv$lg$action,'Attendants');
  444.         $response_base =array(
  445.             'data' => $data,
  446.         );
  447.         $response_body json_encode($response_baseJSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES );
  448.         $response = new Response($response_body200);
  449.         $em->clear();
  450.         return $response;
  451.     }
  452.     private function saveUserBehavior($userId$event$os$mm$mv$lg$action$module){
  453.         $em $this->getDoctrine()->getManager();
  454.         $MobileAppUsersBehavior = new MobileAppUsersBehavior();
  455.         $MobileAppUsersBehavior->setUserId($userId);
  456.         $MobileAppUsersBehavior->setEventId($event);
  457.         $MobileAppUsersBehavior->setOperatingSystem($os);
  458.         $MobileAppUsersBehavior->setMobileModel($mm);
  459.         $MobileAppUsersBehavior->setMobileVersion($mv);
  460.         $MobileAppUsersBehavior->setAction($action);
  461.         $MobileAppUsersBehavior->setModule($module);
  462.         $MobileAppUsersBehavior->setLang($lg);
  463.         $em->persist($MobileAppUsersBehavior);
  464.         $em->flush();
  465.         $em->clear();
  466.     }
  467. }