src/Controller/LiftController.php line 90

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