src/Controller/CanopyController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Activity\Canopy;
  4. use App\Entity\Log;
  5. use App\Entity\Option;
  6. use App\Helper\Company;
  7. use setasign\Fpdi\Fpdi;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Asset\Packages;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\Mailer\Mailer;
  16. use Symfony\Component\Mime\Email;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Yaml\Yaml;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use App\Xml\CanopyXml;
  21. class CanopyController extends AbstractController
  22. {
  23.     /**
  24.      * @Route("/verriere", name="canopy_home")
  25.      *
  26.      * Show disposition popup
  27.      */
  28.     public function selectDisposition(Canopy $canopy): Response
  29.     {
  30.         return $this->render("home/index.html.twig", ["popup" => "popup_select_canopy"]);
  31.     }
  32.     /**
  33.      * @Route("/verriere/conception", name="canopy_select_concept")
  34.      *
  35.      * Show tool popup
  36.      */
  37.     public function selectTool(Canopy $canopy): Response
  38.     {
  39.         return $this->render("home/index.html.twig", ["popup" => "popup_select_canopy"]);
  40.     }
  41.     /**
  42.      * @Route("/verriere/builder", name="canopy_tool_concept")
  43.      *
  44.      * Show tool popup
  45.      */
  46.     public function selectBuilder(Canopy $canopy): Response
  47.     {
  48.         return $this->render("home/index.html.twig", ["popup" => "popup_tool_canopy"]);
  49.     }
  50.     /**
  51.      * @Route("/ajax/verriere/disposition", name="canopy_set_disp", methods={"POST"})
  52.      *
  53.      * Set disposition when selected
  54.      */
  55.     public function setDisposition(Session $sessionRequest $request): \Symfony\Component\HttpFoundation\JsonResponse
  56.     {
  57.         $content json_decode($request->getContent(), true);
  58.         $token uniqid();
  59.         $session->set("token"$token);
  60.         $session->set("canopy.disp"intval($content['disp']));
  61.         return $this->json(['disp' => $session->get("canopy.disp")]);
  62.     }
  63.     /**
  64.      * @Route("/verriere/conception/panneau-{step}", requirements={"number": "\d+", "step": "\d+"}, name="canopy_concept_step")
  65.      * @Route("/verriere/conception/{tool}", name="canopy_concept")
  66.      * @Route("/verriere/conception/{tool}/panneau-{step}", requirements={"number": "\d+", "step": "\d+"}, name="canopy_concept_pannel_step")
  67.      *
  68.      * Display tool
  69.      */
  70.     public function showTool(Canopy $canopySession $session$projectDir$step null$tool null, \App\Helper\Log $logHelper): Response
  71.     {
  72.         $nextStep null;
  73.         // If no disposition set
  74.         if(!$session->has("canopy.disp") || !$session->has('token')) {
  75.             return $this->redirectToRoute("home");
  76.         }
  77.         $token $session->get("token");
  78.         $number $session->get('canopy.disp');
  79.         if($step === null) {
  80.             $step 1;
  81.         }
  82.         // Check if we have a next step for this disposition and the current step
  83.         if($canopy->hasNextStep($number$step)) {
  84.             $nextStep $step 1;
  85.         }
  86.         // Find disposition name and index
  87.         $disposition "";
  88.         $dispositionIndex 0;
  89.         foreach($canopy->getDispositions() as $i => $dispo) {
  90.             if($i === intval($number)) {
  91.                 $disposition $dispo;
  92.                 $dispositionIndex $i;
  93.             }
  94.         }
  95.         // Create token file
  96.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  97.         if(!file_exists($configFile) || $step === 1) {
  98.             if (!file_exists(dirname($configFile))) {
  99.                 mkdir(dirname($configFile), 0777true);
  100.             }
  101.             file_put_contents($configFileYaml::dump(["disposition" => $dispositionIndex"time" => time()]));
  102.         }
  103.         $width 0;
  104.         $height 0;
  105.         $configOptions = [];
  106.         $reference null;
  107.         // If we're not in the first step, find first step configuration
  108.         if($step 1) {
  109.             $config Yaml::parseFile($configFile);
  110.             // Only use $config[0] as it's the first step
  111.             // Copy 'option' to 'options' as it was misspelled sometimes
  112.             if(!array_key_exists('options'$config[0])) {
  113.                 $config[0]['options'] = $config[0]['option'];
  114.             }
  115.             $configOptions $config[0]['options'];
  116.             $width $config[0]['dimensions']['width'];
  117.             $height $config[0]['dimensions']['height'];
  118.             $reference $config[0]['reference'];
  119.         }
  120.         else {
  121.             if ($number == 1) {
  122.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_SIMPLE_CANOPY');
  123.             }
  124.             else {
  125.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_CORNER_CANOPY');
  126.             }
  127.         }
  128.         $keys = ['frameType''color''rempl'];
  129.         foreach($keys as $key) {
  130.             if (array_key_exists($key$configOptions) && gettype($configOptions[$key]) == "string") {
  131.                 $configOptions[$key] = $this->getDoctrine()->getRepository(Option::class)->find($configOptions[$key]);
  132.             }
  133.         }
  134.         $modules = ['canopy_simple''canopy_corner'];
  135.         $options = [];
  136.         foreach ($this->getUser()->getCompany()->getOptions() as $option) {
  137.             foreach ($option->getModules() as $module) {
  138.                 if (in_array($module->getName(), $modules)) {
  139.                     $options[$option->getType()][] = $option;
  140.                     break;
  141.                 }
  142.             }
  143.         }
  144.         return $this->render("activities/canopy/tool.html.twig",
  145.             [
  146.                 'token' => $token,
  147.                 'step' => $step,
  148.                 'nextStep' => $nextStep,
  149.                 'isFirstStep' => $step == 1,
  150.                 'config' => [
  151.                     'options' => $configOptions,
  152.                     'width' => $width,
  153.                     'height' => $height
  154.                 ],
  155.                 "nbStep" => $number,
  156.                 'disposition' => $disposition,
  157.                 'rempl' => [],
  158.                 'tool' => $tool,
  159.                 "panel" => $canopy->getPanelNameByDisposition($number$step),
  160.                 "options" => $options,
  161.                 "reference" => $reference
  162.             ]);
  163.     }
  164.     /**
  165.      * @Route("/verriere/resultat", name="canopy_final")
  166.      *
  167.      * Show resume of conception
  168.      */
  169.     public function finalStep(Canopy $canopySession $session$projectDir): Response
  170.     {
  171.         $token $session->get('token');
  172.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  173.         $config Yaml::parseFile($configFile);
  174.         // Remove unnecessary information
  175.         unset($config['time']);
  176.         $dispositionIndex $config['disposition'];
  177.         unset($config['disposition']);
  178.         if(array_key_exists("pdf"$config)) {
  179.             unset($config['pdf']);
  180.         }
  181.         $dispositions $canopy->getDispositions();
  182.         $disposition $dispositions[$dispositionIndex];
  183.         $pannels = [];
  184.         foreach($config as $pannel) {
  185.             if ($pannel['options']) {
  186.                 $options = &$pannel['options'];
  187.                 $keys = ['frameType''color''rempl'];
  188.                 foreach($keys as $key) {
  189.                     if (array_key_exists($key$options) && gettype($options[$key]) == "string") {
  190.                         $options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
  191.                     }
  192.                 }
  193.             }
  194.             $pannels[intval($pannel['step'])] = $pannel;
  195.             $pannels[intval($pannel['step'])]['name'] = $canopy->getPanelNameByDisposition($dispositionIndexintval($pannel['step']));
  196.         }
  197.         ksort($pannels);
  198.         return $this->render("activities/canopy/final.html.twig", ['pannels' => $pannels'disposition' => $disposition]);
  199.     }
  200.     /**
  201.      * @Route("/verriere/pdf", name="canopy_pdf")
  202.      *
  203.      * Print PDF
  204.      */
  205.     public function printPdf(Canopy $canopySession $session$projectDirPackages $pathPackageCompany $companyTranslatorInterface $translator): \Symfony\Component\HttpFoundation\BinaryFileResponse
  206.     {
  207.         $token $session->get('token');
  208.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  209.         $config Yaml::parseFile($configFile);
  210.         $dispositionIndex $config['disposition'];
  211.         if(!array_key_exists('pdf'$config)) {
  212.             if ($dispositionIndex == 1) {
  213.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SIMPLE_CANOPY');
  214.             }
  215.             else {
  216.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_CORNER_CANOPY');
  217.             }
  218.         }
  219.         return $this->file($this->getPdf($canopy$session$projectDir$pathPackage$company$translator), "verriere.pdf"ResponseHeaderBag::DISPOSITION_INLINE);
  220.     }
  221.     /**
  222.      * @Route("/verriere/xml", name="canopy_xml")
  223.      *
  224.      * Print XML
  225.      */
  226.     public function printXML(Canopy $canopySession $session$projectDirPackages $pathPackageCompany $companyTranslatorInterface $translator): \Symfony\Component\HttpFoundation\BinaryFileResponse
  227.     {
  228.         $token $session->get('token');
  229.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  230.         $config Yaml::parseFile($configFile);
  231.         $dispositionIndex $config['disposition'];
  232.         if(!array_key_exists('xml'$config)) {
  233.             if ($dispositionIndex == 1) {
  234.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SIMPLE_CANOPY');
  235.             }
  236.             else {
  237.                 $this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_CORNER_CANOPY');
  238.             }
  239.         }
  240.         return $this->file($this->getXML($canopy$session$projectDir$pathPackage$company$translator), "verriere.xml"ResponseHeaderBag::DISPOSITION_ATTACHMENT);
  241.     }
  242.     /**
  243.      * @Route("/verriere/email", name="canopy_email")
  244.      *
  245.      * Send PDF by mail
  246.      */
  247.     public function emailPdf($emailCanopy $canopySession $session$projectDirPackages $pathPackageCompany $companyMailer $mailerTranslatorInterface $translator): RedirectResponse
  248.     {
  249.         $message = new Email();
  250.         $message->from($email)
  251.             ->to($email)
  252.             ->subject("Nouvelle demande de Verrière")
  253.             ->attachFromPath($this->getPdf($canopy$session$projectDir$pathPackage$company$translator), 'Verrière');
  254.         $mailer->send($message);
  255.         return $this->redirectToRoute("canopy_final");
  256.     }
  257.     /**
  258.      * Generate PDF
  259.      *
  260.      * @param Canopy $canopy
  261.      * @param Session $session
  262.      * @param $projectDir
  263.      * @param Packages $pathPackage
  264.      * @param Company $company
  265.      * @return string
  266.      * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
  267.      * @throws \setasign\Fpdi\PdfParser\Filter\FilterException
  268.      * @throws \setasign\Fpdi\PdfParser\PdfParserException
  269.      * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
  270.      * @throws \setasign\Fpdi\PdfReader\PdfReaderException
  271.      */
  272.     private function getPdf(Canopy $canopySession $session$projectDirPackages $pathPackageCompany $companyTranslatorInterface $translator): string
  273.     {
  274.         $token $session->get('token');
  275.         $user $this->getUser();
  276.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  277.         $config Yaml::parseFile($configFile);
  278.         $dayDate = (new \DateTime())->format("Y-m-d");
  279.         // Remove unnecessary keys
  280.         unset($config['time']);
  281.         $dispositionIndex $config['disposition'];
  282.         unset($config['disposition']);
  283.         unset($config['xml']);
  284.         // Unlink previously created file if there is one
  285.         if(array_key_exists("pdf"$config)) {
  286.             if(file_exists($config['pdf'])) {
  287.                 unlink($config['pdf']);
  288.             }
  289.             unset($config['pdf']);
  290.         }
  291.         $dispositions $canopy->getDispositions();
  292.         $disposition $dispositions[$dispositionIndex];
  293.         $pannels = [];
  294.         foreach($config as $pannel) {
  295.             $pannels[intval($pannel['step'])] = $pannel;
  296.             // Only show prices if activated
  297.             if($user->isTarif()) {
  298.                 $pannels[intval($pannel['step'])]['price'] = $canopy->handleAjax("price"$pannel);
  299.             } else {
  300.                 $pannels[intval($pannel['step'])]['price'] = ['prices' => [], 'price' => null];
  301.             }
  302.             $pannels[intval($pannel['step'])]['name'] = $canopy->getPanelNameByDisposition($dispositionIndexintval($pannel['step']));
  303.             $options = &$pannels[intval($pannel['step'])]['options'];
  304.             $keys = ['frameType''color''rempl'];
  305.             foreach($keys as $key) {
  306.                 if (array_key_exists($key$options) && gettype($options[$key]) == "string") {
  307.                     $options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
  308.                 }
  309.             }
  310.         }
  311.         ksort($pannels);
  312.         $pdf = new Fpdi();
  313.         // Fetch specific theme colors if there is one, otherwise use seed
  314.         $profil $session->get('profil');
  315.         $file sprintf("%s/config/theme/%s.php"$projectDir$profil);
  316.         if(!file_exists($file)) {
  317.             $file sprintf("%s/config/theme/seed.php"$projectDir);
  318.         }
  319.         $profil = require($file);
  320.         foreach($pannels as $pannel) {
  321.             $pack $canopy->handleAjax("getComponents"$pannel);
  322.             $dest sprintf("%s/var/cache/pdf/%s_canopy_%s.pdf"$projectDir$dayDateuniqid());
  323.             $page = \App\Pdf\Canopy::generate($company->getCompany(), $profil$pack$pannel$disposition,
  324.                 sprintf(
  325.                     "%s/public/uploads/%s",
  326.                     $projectDir,
  327.                     $company->getCompany()->getLogo()
  328.                 ), $translator
  329.             );
  330.             if(!file_exists(dirname($dest))) {
  331.                 mkdir(dirname($dest));
  332.             }
  333.             // Generate pdf to temporary file and merge it to the main pdf
  334.             $page->Output("F"$dest);
  335.             $pages $pdf->setSourceFile($dest);
  336.             for($page 0$page $pages$page++) {
  337.                 $pdf->AddPage();
  338.                 $i $pdf->importPage($page+1);
  339.                 $pdf->useTemplate($i00200);
  340.             }
  341.             unlink($dest);
  342.         }
  343.         $dest sprintf("%s/var/cache/pdf/%s_canopy_%s.pdf"$projectDir$dayDateuniqid());
  344.         if(!file_exists(dirname($dest))) {
  345.             mkdir(dirname($dest));
  346.         }
  347.         $pdf->Output("F"$dest);
  348.         $config Yaml::parseFile($configFile);
  349.         $config['pdf'] = $dest;
  350.         file_put_contents($configFileYaml::dump($config4));
  351.         return $dest;
  352.     }
  353.     private function getXML(Canopy $canopySession $session$projectDirPackages $pathPackageCompany $companyTranslatorInterface $translator): string
  354.     {
  355.         $token $session->get('token');
  356.         $user $this->getUser();
  357.         $configFile sprintf("%s/var/tokens/%s.yaml"$projectDir$token);
  358.         $config Yaml::parseFile($configFile);
  359.         $dayDate = (new \DateTime())->format("Y-m-d");
  360.         // Remove unnecessary keys
  361.         unset($config['time']);
  362.         $dispositionIndex $config['disposition'];
  363.         unset($config['disposition']);
  364.         unset($config['pdf']);
  365.         // Unlink previously created file if there is one
  366.         if(array_key_exists("xml"$config)) {
  367.             if(file_exists($config['xml'])) {
  368.                 unlink($config['xml']);
  369.             }
  370.             unset($config['xml']);
  371.         }
  372.         $dispositions $canopy->getDispositions();
  373.         $disposition $dispositions[$dispositionIndex];
  374.         $pannels = [];
  375.         foreach($config as $pannel) {
  376.             $pannels[intval($pannel['step'])] = $pannel;
  377.             // Only show prices if activated
  378.             if($user->isTarif()) {
  379.                 $pannels[intval($pannel['step'])]['price'] = $canopy->handleAjax("price"$pannel);
  380.             } else {
  381.                 $pannels[intval($pannel['step'])]['price'] = ['prices' => [], 'price' => null];
  382.             }
  383.             $pannels[intval($pannel['step'])]['name'] = $canopy->getPanelNameByDisposition($dispositionIndexintval($pannel['step']));
  384.             $options = &$pannels[intval($pannel['step'])]['options'];
  385.             $keys = ['frameType''color''rempl'];
  386.             foreach($keys as $key) {
  387.                 if (array_key_exists($key$options) && gettype($options[$key]) == "string") {
  388.                     $options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
  389.                 }
  390.             }
  391.         }
  392.         ksort($pannels);
  393.         $xmlBuilder = new CanopyXml($company->getCompany(), $pannels$disposition$translator$canopy);
  394.         $xmlBuilder->build();
  395.         $xml $xmlBuilder->getXml();
  396.         $dest sprintf("%s/var/cache/xml/%s_canopy_%s.xml"$projectDir$dayDateuniqid());
  397.         if(!file_exists(dirname($dest))) {
  398.             mkdir(dirname($dest));
  399.         }
  400.         $dom = new \DOMDocument("1.0""UTF-8");
  401.         $dom->preserveWhiteSpace false;
  402.         $dom->formatOutput true;
  403.         $dom->loadXML($xml);
  404.         $xmlFormatted $dom->saveXML();
  405.         $xmlFormatted mb_convert_encoding($xmlFormatted'UTF-8''HTML-ENTITIES');
  406.         file_put_contents($dest$xmlFormatted);
  407.         $config Yaml::parseFile($configFile);
  408.         $config['xml'] = $dest;
  409.         file_put_contents($configFileYaml::dump($config4));
  410.         return $dest;
  411.     }
  412. }