src/Controller/ReportProcessController.php line 342

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\AdminController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use App\Service\ReportService;
  8. use App\Service\ValidationService;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Knp\Snappy\Pdf;
  11. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. // generar pdf
  14. use App\Entity\{
  15.     Result,
  16.     Answer,
  17.     Gene,
  18.     Report,
  19.     ReportGenerator,
  20.     ReportGeneratorSecond,
  21.     ResetPasswordRequest,
  22.     ResultReport,
  23.     ResultSection,
  24.     ResultSubsection,
  25.     Section,
  26.     SectionReport,
  27.     Subsection,
  28.     User
  29. };
  30. class ReportProcessController extends AdminController
  31. {
  32.     public static function getSubscribedServices(): array
  33.     {
  34.         return array_merge(parent::getSubscribedServices(), [
  35.             'report_service' => ReportService::class,
  36.             'validation_service' => ValidationService::class,
  37.             'translations' => TranslatorInterface::class,
  38.         ]);
  39.     }
  40.     public function persistReportGeneratorEntity($entity)
  41.     {
  42.         $validation $this->step1ReportValidation($entity);
  43.         if ($validation['validation']) {
  44.             $this->em->persist($entity);
  45.             $this->em->flush();
  46.         } else {
  47.             $this->addFlash("danger"'Some errors ocurred in the processing of the reports.Please try again.');
  48.         }
  49.         $this->step2Report($entity);
  50.     }
  51.     public function step1ReportValidation($entity)
  52.     {
  53.         //make the validations of the step1 persist
  54.         $validation true;
  55.         $errors = [];
  56.         return ['validation' => $validation'errors' => $errors];
  57.     }
  58.     public function step2Report($report_generator)
  59.     {
  60.         $tiempo_recorrido=$this->microtime_float() ;
  61.         $process $this->get('report_service')->processFile($report_generator);
  62.        
  63.         $trans $this->get('translations');
  64.         if ($process['state']) {
  65.             $report_generator_second $process['result'];
  66.             $reports_result $report_generator_second->getClientReports();
  67.             if (!empty($reports_result)) {
  68.                 $session = new Session();
  69.                 $session->set('report_pos'0);
  70.                 $first_report $reports_result[0];
  71.                 $this->request->query->set('referer''editResultReport/' $first_report->getId());
  72.             } else {
  73.                 $this->addFlash("danger"'Some errors ocurred in the processing of the reports in the step 2.Please try again.');
  74.                 $query = array(
  75.                     'entity' => 'ReportGenerator',
  76.                     'action' => 'new'
  77.                 );
  78.                 $this->request->query->set('referer''?' http_build_query($query));
  79.             }
  80.         } else {
  81.             $errors $process['errors'];
  82.             foreach ($errors as $error) {
  83.                 if ($error['error'] == 'marker_not_found_import') {
  84.                     $this->addFlash('danger'$trans->trans($error['error']) . '. ' $trans->trans('marker') . ': ' $error['marker'] . '. ' $trans->trans('subsection_title') . ':' $trans->trans($error['title']));
  85.                 } elseif ($error['error'] == 'number_clients_size') {
  86.                     $this->addFlash('danger'$trans->trans($error['error']) . '. ' $trans->trans('line') . ': ' $error['line']);
  87.                 } elseif ($error['error'] == 'file_not_found') {
  88.                     $this->addFlash('danger'$trans->trans($error['error']) . ' - Ruta: ' $error['path']);
  89.                 } else {
  90.                     $this->addFlash('warning'$error['error']);
  91.                 }
  92.             }
  93.             $this->addFlash("danger"'Some errors ocurred in the processing of the reports in the step 2.Please try again.');
  94.             $query = array(
  95.                 'entity' => 'ReportGenerator',
  96.                 'action' => 'new'
  97.             );
  98.             $this->request->query->set('referer''?' http_build_query($query));
  99.         }
  100.     }
  101.     /**
  102.      * @Route("/currentClientView", name="currentClientView")
  103.      */
  104.     public function current_client_view($current_result_client)
  105.     {
  106.         $query = array(
  107.             'entity' => 'ResultReport',
  108.             'action' => 'edit',
  109.             'id' => $current_result_client->getId()
  110.         );
  111.         $this->request->query->set('referer''?' http_build_query($query));
  112.     }
  113.     /**
  114.      * @Route("/editResultReport/{id}", name="editResultReport")
  115.      */
  116.     public function editResultReport($id)
  117.     {
  118.         $session = new Session();
  119.         $report_pos $session->get('report_pos');
  120.         $actual $id;
  121.         $result_report $this->getDoctrine()->getRepository(ResultReport::class)->find($id);
  122.         $only_automatic true;
  123.         
  124.         
  125.         foreach ($result_report->getResultSections() as $key_1 => $result_section) {
  126.             foreach ($result_section->getResultSubsections() as $key_2 => $result_subsection) {
  127.                 if (!$result_subsection->getSubsection()->getIsAutomatic()) {
  128.                     $only_automatic false;
  129.                     break;
  130.                 }
  131.             }
  132.         }
  133.         if (!$only_automatic) {
  134.             return $this->render('resultReport/form.html.twig', [
  135.                 'result_report' => $result_report,
  136.             ]);
  137.         } else {
  138.             //check if more report
  139.             $reportGeneratorSecond $result_report->getReportGeneratorSecond();
  140.             $process_report_id $reportGeneratorSecond->getId();
  141.             $reports $reportGeneratorSecond->getClientReports();
  142.             $reports_array = [];
  143.             foreach ($reports as $key => $report_elem) {
  144.                 $reports_array[$key] = $report_elem;
  145.             }
  146.             $session = new Session();
  147.             $report_pos $session->get('report_pos');
  148.             $report_pos $report_pos 1;
  149.             $session->set('report_pos'$report_pos);
  150.             if (array_key_exists($report_pos$reports_array)) {
  151.                 //else go to list of reports to download
  152.                 $result_report $reports[$report_pos];
  153.                 return $this->redirectToRoute(
  154.                     'editResultReport',
  155.                     ['id' => $result_report->getId()
  156.                     ]);
  157.             } else {
  158.                 //else go to list of reports to download
  159.                 return $this->redirectToRoute(
  160.                     'print_reports',
  161.                     ['id' => $process_report_id
  162.                     ]);
  163.             }
  164.         }
  165.     }
  166.     /**
  167.      * @Route("/editResultReportClient/{id}/{plan}", name="editResultReportClient")
  168.      */
  169.     public function editResultReportClient($idAnswer $plan)
  170.     {
  171.         $session = new Session();
  172.         $report_pos $session->get('report_pos');
  173.         $actual $id;
  174.         $result_report $this->getDoctrine()->getRepository(ResultReport::class)->find($id);
  175.         $only_automatic true;
  176.         
  177.         
  178.         foreach ($result_report->getResultSections() as $key_1 => $result_section) {
  179.             foreach ($result_section->getResultSubsections() as $key_2 => $result_subsection) {
  180.                 if (!$result_subsection->getSubsection()->getIsAutomatic()) {
  181.                     $only_automatic false;
  182.                     break;
  183.                 }
  184.             }
  185.         }
  186.         if (!$only_automatic) {
  187.             return $this->render('resultReport/form.html.twig', [
  188.                 'result_report' => $result_report,
  189.                 'pre_set' => $plan,
  190.             ]);
  191.         } else {
  192.             //check if more report
  193.             $reportGeneratorSecond $result_report->getReportGeneratorSecond();
  194.             $process_report_id $reportGeneratorSecond->getId();
  195.             $reports $reportGeneratorSecond->getClientReports();
  196.             $reports_array = [];
  197.             foreach ($reports as $key => $report_elem) {
  198.                 $reports_array[$key] = $report_elem;
  199.             }
  200.             $session = new Session();
  201.             $report_pos $session->get('report_pos');
  202.             $report_pos $report_pos 1;
  203.             $session->set('report_pos'$report_pos);
  204.             if (array_key_exists($report_pos$reports_array)) {
  205.                 //else go to list of reports to download
  206.                 $result_report $reports[$report_pos];
  207.                 return $this->redirectToRoute(
  208.                     'editResultReport',
  209.                     ['id' => $result_report->getId()
  210.                     ]);
  211.             } else {
  212.                 //else go to list of reports to download
  213.                 return $this->redirectToRoute(
  214.                     'print_reports',
  215.                     ['id' => $process_report_id
  216.                     ]);
  217.             }
  218.         }
  219.     }
  220.     public function salt_automatic($result_report$report_pos$reports_array)
  221.     {
  222.     }
  223.     /**
  224.      * @Route("/change_report_result", name="change_report_result")
  225.      */
  226.     public function change_report_result(Request $request)
  227.     {
  228.         $entityManager $this->getDoctrine()->getManager();
  229.         $result_subsection false;
  230.         $process_report_id $request->request->get('process_report_id');
  231.         $reportGeneratorSecond $this->getDoctrine()->getRepository(ReportGeneratorSecond::class)->find($process_report_id);
  232.         if ($request->request->all()) {
  233.             $valores_enviados $request->request->all();
  234.             foreach ($valores_enviados as $key => $value) {
  235.                 $array_aux explode('aditional_text_'$key);
  236.                 if (!empty($array_aux[1])) {
  237.                     $id_subsection $array_aux[1];
  238.                     $result_subsection $this->getDoctrine()->getRepository(ResultSubSection::class)->find($id_subsection);
  239.                     $result_subsection->setAditionalText($value);
  240.                     $entityManager->flush($result_subsection);
  241.                 }
  242.                 $array_aux explode('answer_'$key);
  243.                 if (!empty($array_aux[1])) {
  244.                     $answer_descriptive explode('|'$value);
  245.                     $id_subsection $array_aux[1];
  246.                     $result_subsection $this->getDoctrine()->getRepository(ResultSubSection::class)->find($id_subsection);
  247.                     $result_subsection->setAnswers([$answer_descriptive]);
  248.                     $entityManager->flush($result_subsection);
  249.                 }
  250.             }
  251.         }
  252.         $reports $reportGeneratorSecond->getClientReports();
  253.         $reports_array = [];
  254.         foreach ($reports as $key => $report_elem) {
  255.             $reports_array[$key] = $report_elem;
  256.         }
  257.         $session = new Session();
  258.         $report_pos $session->get('report_pos');
  259.         $report_pos $report_pos 1;
  260.         $session->set('report_pos'$report_pos);
  261.         if (array_key_exists($report_pos$reports_array)) {
  262.             $result_report $reports[$report_pos];
  263.             return $this->redirectToRoute(
  264.                 'editResultReport',
  265.                 ['id' => $result_report->getId()
  266.                 ]);
  267.         } else {
  268.             return $this->redirectToRoute(
  269.                 'print_reports',
  270.                 ['id' => $process_report_id
  271.                 ]);
  272.         }
  273.     }
  274.     /**
  275.      * @Route("/print_reports/{id}", name="print_reports")
  276.      */
  277.     public function print_reports($id)
  278.     {
  279.         $process $this->getDoctrine()->getRepository(ReportGeneratorSecond::class)->find($id);
  280.         return $this->render('resultReport/download_pdfs_reports.html.twig', [
  281.             'process' => $process
  282.         ]);
  283.     }
  284.     /**
  285.      * @Route("/download_report/{id}/{pre}", name="download_report")
  286.      */
  287.     public function download_report($id$pre falsePdf $pdf)
  288.     {
  289.         $result_report $this->getDoctrine()->getRepository(ResultReport::class)->find($id);
  290.         $body $this->renderView('pdf/reportArt2.html.twig', ['result_report' => $result_report'pre' => $pre]);
  291.         $header $this->renderView('pdf/header.html.twig', ['result_report' => $result_report]);
  292.         $footer $this->renderView('pdf/footer.html.twig', ['result_report' => $result_report]);
  293.         $name preg_replace('/[^\x20-\x7E]/'''$result_report->getClient());
  294.         $filename "Reporte_" $name "_" date('d') . date('m') . date('Y') . ".pdf";
  295.         //DAR = Darwin = MacOS
  296.         if (strtoupper(substr(PHP_OS03)) === 'WIN' or strtoupper(substr(PHP_OS03)) === "DAR") {
  297.             $response = new PdfResponse(
  298.                 $pdf->getOutputFromHtml($body, [
  299.                     'images' => true,
  300.                     'enable-javascript' => true,
  301.                     'page-size' => 'A4',
  302.                     'print-media-type' => true,
  303.                     'image-dpi' => '300',
  304.                     'load-error-handling' => 'ignore',
  305.                     'load-media-error-handling' => 'ignore',
  306.                     'margin-left' => 1,
  307.                     'margin-right' => 1,
  308.                     'header-html' => $header,
  309.                     'footer-html' => $footer,
  310.                 ]),
  311.                 $filename
  312.             );
  313.         } else {
  314.             $response = new PdfResponse(
  315.                 $pdf->getOutputFromHtml($body, [
  316.                     'images' => true,
  317.                     'enable-javascript' => true,
  318.                     'page-size' => 'A4',
  319.                     'print-media-type' => true,
  320.                     'image-dpi' => '300',
  321.                     'load-error-handling' => 'ignore',
  322.                     'load-media-error-handling' => 'ignore',
  323.                     'margin-left' => 1,
  324.                     'margin-right' => 1,
  325.                     'header-html' => $header,
  326.                     'footer-html' => $footer,
  327.                     'disable-smart-shrinking' => true,
  328.                     'zoom' => 0.78
  329.                 ]),
  330.                 $filename
  331.             );
  332.         }
  333.         $response->headers->set('Set-Cookie''fileDownload=true');
  334.         $response->headers->set('path''/');
  335.         return $response;
  336.         // $entityManager = $this->getDoctrine()->getManager();
  337.         // $product = 
  338.         // // $this->getDoctrine()
  339.         // ->getRepository(Product::class)
  340.         // ->findBy(['title'=>'titulo']);
  341.         // $pdfOptions = new Options();
  342.         // $pdfOptions->set('defaultFont', 'Arial');
  343.         // $pdfOptions->set('isRemoteEnabled', TRUE);
  344.         // $pdfOptions->set('debugKeepTemp', TRUE);
  345.         // $pdfOptions->set('isHtml5ParserEnabled', true);
  346.         // $pdfOptions->set('enable_remote', true);
  347.         // // Instantiate Dompdf with our options
  348.         // $dompdf = new Dompdf($pdfOptions);
  349.         // // Retrieve the HTML generated in our twig file
  350.         // // return $html;die;
  351.         // // Load HTML to Dompdf
  352.         // $dompdf->loadHtml($html);
  353.         // // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  354.         // $dompdf->setPaper('A4', 'portrait');
  355.         // // Render the HTML as PDF
  356.         // $dompdf->render();
  357.         // // Output the generated PDF to Browser (force download)
  358.         // $dompdf->stream("reportArt2.pdf", [
  359.         //     "Attachment" => true
  360.         // ]);
  361.         // die;
  362.         //magic for pdf generate
  363.     }
  364.     private function microtime_float()
  365.     {
  366.         list($useg$seg) = explode(" "microtime());
  367.         return ((float)$useg + (float)$seg);
  368.     }
  369. }