src/Controller/DoorController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Activity\Canopy;
  4. use App\Activity\Door;
  5. use App\Entity\Log;
  6. use App\Entity\Option;
  7. use App\Entity\User;
  8. use App\Helper\Company;
  9. use App\Xml\DoorXml;
  10. use setasign\Fpdi\Fpdi;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Asset\Packages;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Yaml\Yaml;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class DoorController extends AbstractController
  20. {
  21.     /**
  22.      * @Route("/porte-battante/", name="door_tool")
  23.      */
  24.     public function selectDisposition(Canopy $canopy): Response
  25.     {
  26.         return $this->render("home/index.html.twig", ["popup" => "popup_select_door"]);
  27.     }
  28.     /**
  29.      * @Route("/porte-battante/standard", name="doorb_tool")
  30.      * @Route("/porte-battante/{pos}", requirements={"pos": "\d+"}, name="door_tool_pos")
  31.      */
  32.     public function battante(Session  $session$projectDir$pos null): Response
  33.     {
  34.         return $this->showTool($session$projectDir'b'$pos);
  35.     }
  36.     /**
  37.      * @Route("/porte-battante/sur-mesure", name="door_tool_custom")
  38.      */
  39.     public function battanteSurMesure(Session  $session$projectDir): Response
  40.     {
  41.         return $this->showTool($session$projectDir'bs'null);
  42.     }
  43.     /**
  44.      * @Route("/porte-suspendue/", name="dooro_tool")
  45.      */
  46.     public function ouvrante(Session  $session$projectDir$pos null): Response
  47.     {
  48.         return $this->render("home/index.html.twig", ["popup" => "popup_select_dooro"]);
  49.     }
  50.     /**
  51.      * @Route("/porte-suspendue-40kg/", name="dooro40_tool")
  52.      */
  53.     public function ouvrante40(Session  $session$projectDir$pos null): Response
  54.     {
  55.         if($session->has("has_door")) {
  56.             $session->clear();
  57.         }
  58.         return $this->showTool($session$projectDir'o40'$pos);
  59.     }
  60.     /**
  61.      * @Route("/porte-suspendue-70kg/", name="dooro70_tool")
  62.      */
  63.     public function ouvrante70(Session  $session$projectDir$pos null): Response
  64.     {
  65.         if($session->has("has_door")) {
  66.             $session->clear();
  67.         }
  68.         return $this->showTool($session$projectDir'o70'$pos);
  69.     }
  70.     public function showTool(Session $session$projectDir$type$pos null): Response
  71.     {
  72.         if(!$session->has("door_token")) {
  73.             $token uniqid();
  74.             $session->set('door_token'$token);
  75.             if($session->has("has_door")) {
  76.                 if($session->get('has_door') === $session->get('configured_door')) {
  77.                     $session->remove('has_door');
  78.                     $session->remove('configured_door');
  79.                 }
  80.             }
  81.         }
  82.         if($pos !== null) {
  83.             $session->set('door'$pos);
  84.         }
  85.         $token $session->get('door_token');
  86.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  87.         if(file_exists($configFile)) {
  88.             unlink($configFile);
  89.         }
  90.         if ($type == 'b') {
  91.             $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_STANDARD_DOOR');
  92.         }
  93.         else if ($type == 'bs') {
  94.             $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_CUSTOM_DOOR');
  95.         }
  96.         else if ($type == 'o40') {
  97.             $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_SLIDING_DOOR_40');
  98.         }
  99.         else if ($type == 'o70') {
  100.             $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_SLIDING_DOOR_70');
  101.         }
  102.         $modules $type == 'o40' ? ['door_sliding_40'] : ($type == 'o70' ? ['door_sliding_70'] : ['door_swinging']);
  103.         $options = [];
  104.         foreach ($this->getUser()->getCompany()->getOptions() as $option) {
  105.             foreach ($option->getModules() as $module) {
  106.                 if (in_array($module->getName(), $modules)) {
  107.                     $options[$option->getType()][] = $option;
  108.                     break;
  109.                 }
  110.             }
  111.         }
  112.         return $this->render('activities/door/tool.html.twig', [
  113.             'token' => $token,
  114.             'type' => $type,
  115.             'pos' => $pos,
  116.             "options" => $options,
  117.         ]);
  118.     }
  119.     /**
  120.      * @Route("/porte/resultat", name="door_final")
  121.      */
  122.     public function finalStep(Door $doorSession $session$projectDir): Response
  123.     {
  124.         $token $session->get('door_token');
  125.         if($session->has("has_door")) {
  126.             $session->set(sprintf("module_door_%d"$session->get('door')), $token);
  127.             $session->set("configured_door"$session->get('configured_door')+1);
  128.             $session->remove("door_token");
  129.             return $this->redirectToRoute("cloison_final");
  130.         }
  131.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  132.         $config Yaml::parseFile($configFile);
  133.         unset($config['time']);
  134.         if(array_key_exists("pdf"$config)) {
  135.             unset($config['pdf']);
  136.         }
  137.         $pannel $config[0];
  138.         if ($pannel['options']) {
  139.             $options = &$pannel['options'];
  140.             $keys = ['dormant''sens''serrure''profile''handleProfile''jointvit''jointbut''color''colorCrutch'];
  141.             foreach($keys as $key) {
  142.                 if (array_key_exists($key$options) && gettype($options[$key]) == "string") {
  143.                     $options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
  144.                 }
  145.             }
  146.         }
  147.         return $this->render("activities/door/final.html.twig", ['pannel' => $pannel'type' => $pannel['type']]);
  148.     }
  149.     /**
  150.      * @Route("/porte/pdf", name="door_print")
  151.      */
  152.     public function printCloison(TranslatorInterface $translatorCompany $companyDoor $doorSession $session$projectDirPackages $pathPackage): \Symfony\Component\HttpFoundation\BinaryFileResponse
  153.     {
  154.         $token $session->get('door_token');
  155.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  156.         $config Yaml::parseFile($configFile);
  157.         $pannel $config[0];
  158.         $type $pannel['type'];
  159.         $dest self::getPdf($translator$this->getUser(), $company$token$door$session$projectDir$pathPackage);
  160.         if(!array_key_exists('pdf'$config)) {
  161.             if ($type == 'b') {
  162.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_STANDARD_DOOR');
  163.             }
  164.             else if ($type == 'bs') {
  165.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_CUSTOM_DOOR');
  166.             }
  167.             else if ($type == 'o40') {
  168.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_40');
  169.             }
  170.             else if ($type == 'o70') {
  171.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_70');
  172.             }
  173.         }
  174.         return $this->file($dest"porte.pdf"ResponseHeaderBag::DISPOSITION_INLINE);
  175.     }
  176.     public function getPdf(TranslatorInterface $translatorUser $userCompany $company$tokenDoor $doorSession $session$projectDirPackages $pathPackage): string
  177.     {
  178.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  179.         $config Yaml::parseFile($configFile);
  180.         $dayDate = (new \DateTime())->format("Y-m-d");
  181.         unset($config['time']);
  182.         unset($config['pdf']);
  183.         if(array_key_exists("pdf"$config)) {
  184.             if(file_exists($config['pdf'])) {
  185.                 unlink($config['pdf']);
  186.             }
  187.             unset($config['pdf']);
  188.         }
  189.         $pannel $config[0];
  190.         $options = &$pannel['options'];
  191.         $keys = ['dormant''sens''serrure''profile''handle_profile''jointvit''jointbut''color''colorCrutch'];
  192.         foreach($keys as $key) {
  193.             if (array_key_exists($key$options) && gettype($options[$key]) == "string") {
  194.                 $options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
  195.             }
  196.         }
  197.         if($user->isTarif()) {
  198.             $pannel['price'] = $door->handleAjax("price"$pannel['config']);
  199.         } else{
  200.             $pannel['price'] = [
  201.                 'price' => 0,
  202.                 'prices' => []
  203.             ];
  204.         }
  205.         $pdf = new Fpdi();
  206.         // Fetch specific theme colors if there is one, otherwise use seed
  207.         $profil $session->get('profil');
  208.         $file sprintf("%s/config/theme/%s.php"$projectDir$profil);
  209.         if(!file_exists($file)) {
  210.             $file sprintf("%s/config/theme/seed.php"$projectDir);
  211.         }
  212.         $profil = require($file);
  213.         $pack $door->handleAjax("getComponents"$pannel);
  214.         $dest sprintf("%s/var/cache/pdf/%s_door_%s.pdf"$projectDir$dayDateuniqid());
  215.         $page = \App\Pdf\Door::generate($translator$company->getCompany(), $profil$pack$pannel,
  216.             sprintf(
  217.                 "%s/public/uploads/%s",
  218.                 $projectDir,
  219.                 $company->getCompany()->getLogo()
  220.             )
  221.         );
  222.         if(!file_exists(dirname($dest))) {
  223.             mkdir(dirname($dest));
  224.         }
  225.         // Generate pdf to temporary file and merge it to the main pdf
  226.         $page->Output("F"$dest);
  227.         $pages $pdf->setSourceFile($dest);
  228.         for($page 0$page $pages$page++) {
  229.             $pdf->AddPage();
  230.             $i $pdf->importPage($page+1);
  231.             $pdf->useTemplate($i00200);
  232.         }
  233.         unlink($dest);
  234.         $dest sprintf("%s/var/cache/pdf/%s.pdf"$projectDiruniqid());
  235.         if (!file_exists(dirname($dest))) {
  236.             mkdir(dirname($dest), 0777true);
  237.         }
  238.         $pdf->Output("F"$dest);
  239.         $config Yaml::parseFile($configFile);
  240.         $config['pdf'] = $dest;
  241.         file_put_contents($configFileYaml::dump($config4));
  242.         return $dest;
  243.     }
  244.     /**
  245.      * @Route("/porte/xml", name="door_xml")
  246.      */
  247.     public function printXml(TranslatorInterface $translatorCompany $companyDoor $doorSession $session$projectDirPackages $pathPackage): \Symfony\Component\HttpFoundation\BinaryFileResponse
  248.     {
  249.         $token $session->get('door_token');
  250.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  251.         $config Yaml::parseFile($configFile);
  252.         $pannel $config[0];
  253.         $type $pannel['type'];
  254.         $dest self::getXml($translator$this->getUser(), $company$token$door$session$projectDir$pathPackage);
  255.         if(!array_key_exists('xml'$config)) {
  256.             if ($type == 'b') {
  257.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_STANDARD_DOOR');
  258.             }
  259.             else if ($type == 'bs') {
  260.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_CUSTOM_DOOR');
  261.             }
  262.             else if ($type == 'o40') {
  263.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_40');
  264.             }
  265.             else if ($type == 'o70') {
  266.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_70');
  267.             }
  268.         }
  269.         return $this->file($dest"porte.xml"ResponseHeaderBag::DISPOSITION_ATTACHMENT);
  270.     }
  271.     public function getXml(TranslatorInterface $translatorUser $userCompany $company$tokenDoor $doorSession $session$projectDirPackages $pathPackage): string
  272.     {
  273.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  274.         $config Yaml::parseFile($configFile);
  275.         $dayDate = (new \DateTime())->format("Y-m-d");
  276.         unset($config['time']);
  277.         unset($config['pdf']);
  278.         if(array_key_exists("xml"$config)) {
  279.             if(file_exists($config['xml'])) {
  280.                 unlink($config['xml']);
  281.             }
  282.             unset($config['xml']);
  283.         }
  284.         $pannel $config[0];
  285.         $options = &$pannel['options'];
  286.         $keys = ['dormant''sens''serrure''profile''handleProfile''jointvit''jointbut''color''colorCrutch'];
  287.         foreach($keys as $key) {
  288.             if (array_key_exists($key$options) && gettype($options[$key]) == "string") {
  289.                 $options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
  290.             }
  291.         }
  292.         if($user->isTarif()) {
  293.             $pannel['price'] = $door->handleAjax("price"$pannel['config']);
  294.         } else{
  295.             $pannel['price'] = [
  296.                 'price' => 0,
  297.                 'prices' => []
  298.             ];
  299.         }
  300.         $xmlBuilder = new DoorXml($company->getCompany(), [$pannel], $translator$door);
  301.         $xmlBuilder->build();
  302.         $xml $xmlBuilder->getXml();
  303.         $dest sprintf("%s/var/cache/xml/%s.xml"$projectDiruniqid());
  304.         if (!file_exists(dirname($dest))) {
  305.             mkdir(dirname($dest), 0777true);
  306.         }
  307.         $dom = new \DOMDocument("1.0""UTF-8");
  308.         $dom->preserveWhiteSpace false;
  309.         $dom->formatOutput true;
  310.         $dom->loadXML($xml);
  311.         $xmlFormatted $dom->saveXML();
  312.         $xmlFormatted mb_convert_encoding($xmlFormatted'UTF-8''HTML-ENTITIES');
  313.         file_put_contents($dest$xmlFormatted);
  314.         $config Yaml::parseFile($configFile);
  315.         $config['xml'] = $dest;
  316.         file_put_contents($configFileYaml::dump($config4));
  317.         return $dest;
  318.     }
  319. }