src/Controller/BaseController.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactFormType;
  4. use App\Service\PrintJobBackend;
  5. use App\Utils\SessionUtils;
  6. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
  17. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  18. /**
  19.  * BaseController
  20.  */
  21. class BaseController extends AbstractController
  22. {
  23.     use SessionUtils;
  24.     const DASHBOARD_RENDER 'base/dashboard.html.twig';
  25.     /**
  26.      * This function reoresente the landing page (after Login)
  27.      * If the User (not SA) has many bubble, we only give his the first Bubble
  28.      * And after, when user change his bubble, we come here to change it
  29.      *
  30.      * Any bubble we display, we get all the information of profiles (complete Object with relation mapping) and store it in Session
  31.      * Asswel, the role of user is check > Store in DB > as the refresh process can refresh the firewall/token/rights
  32.      *
  33.      * Redirect to dashboard at the dashboard
  34.      *
  35.      * @param $b_key
  36.      * @param Request $request
  37.      * @return RedirectResponse
  38.      *
  39.      * @Route("/load/{b_key}", name="load_bubble")
  40.      */
  41.     public function loadBubble(
  42.         $b_key,
  43.         Request $request
  44.     ): RedirectResponse {
  45.         $session $this->sessionByRequestStack();
  46.         /** Set user profile key  */
  47.         $session->set('b_key'$b_key);
  48.         $referer $request->headers->get('referer');
  49.         return new RedirectResponse($referer);
  50.     }
  51.     /**
  52.      * dashboard
  53.      *
  54.      * @return Response
  55.      *
  56.      * @Route("/", name="dashboard")
  57.      */
  58.     public function dashboard(): Response
  59.     {
  60.         $session $this->sessionByRequestStack();
  61.         $user $this->getuser();
  62.         $profiles $user->getProfiles();
  63.         $b_key $session->get('b_key'0);
  64.         $profile $profiles[$b_key];
  65.         // User is SA
  66.         if ($user->getIsSA() == || $profile['dashboard']['read'] == 1) {
  67.             $bubbles_infos $user->getBubblesInfo();
  68.             if (!empty($bubbles_infos)) {
  69.                 return $this->render(self::DASHBOARD_RENDER, [
  70.                     'bubbles_infos' => $bubbles_infos
  71.                 ]);
  72.             }
  73.         }
  74.         return $this->render(self::DASHBOARD_RENDER);
  75.     }
  76.     /**
  77.      * documentation
  78.      *
  79.      * @Route("/documentation", name="documentation")
  80.      */
  81.     public function documentation(TranslatorInterface $translator)
  82.     {
  83.         $this->addFlash('warning'sprintf($translator->trans("La documentation sera prochainement disponible")));
  84.         return $this->redirectToRoute('dashboard');
  85.     }
  86.     /**
  87.      * pageIntrouvable
  88.      *
  89.      * @param TranslatorInterface $translator
  90.      * @return Response
  91.      *
  92.      * @Route("/page_introuvale/404", name="404")
  93.      */
  94.     public function pageIntrouvable(TranslatorInterface $translator)
  95.     {
  96.         $this->addFlash('empty'$translator->trans('Voici le texte de l\'erreur rencontrée. <br/>Vous pouvez faire <a href="">une action</a>'));
  97.         return $this->render('base/404.html.twig');
  98.     }
  99.     /**
  100.      *
  101.      * @Route("contact", name="contact")
  102.      *
  103.      */
  104.     public function contact(
  105.         Request $request,
  106.         MailerInterface $mailer,
  107.         TranslatorInterface $translator
  108.     ) {
  109.         $user $this->getuser();
  110.         $form $this->createForm(ContactFormType::class, NULL, [
  111.             'user' => $user,
  112.         ]);
  113.         $form->handleRequest($request);
  114.         if ($form->isSubmitted() && $form->isValid()) {
  115.             $data $form->getData();
  116.             // $data contient le prenom, le nom, l'objet, l'entreprise, l'email de l'expediteur, le message... (le $_POST finalement)
  117.             try {
  118.                 $this->sendEmail($mailer$data);
  119.                 $this->addFlash('success'$translator->trans('Votre email a été envoyé avec succès.'));
  120.             } catch (TransportExceptionInterface $e) {
  121.                 $this->addFlash('errors'$translator->trans('Erreur dans l\'envoie de l\'email'));
  122.             }
  123.         }
  124.         return $this->render('base/contact.html.twig', [
  125.             'form' => $form->createView()
  126.         ]);
  127.     }
  128.     /**
  129.      * Send mail
  130.      *
  131.      * @param $mailer
  132.      * @param $data
  133.      * @return mixed
  134.      */
  135.     public function sendEmail($mailer$data)
  136.     {
  137.         // $transport = new EsmtpTransport('localhost');
  138.         $mail = (new TemplatedEmail())
  139.         ->subject('Support Client MainChain : ' $data['request'] . ' - ' $data['objet'])
  140.             ->htmlTemplate('mail/contact_mail.html.twig')
  141.             ->from('support@main-chain.org')
  142.             ->to(new Address($data['email']))
  143.             ->context(['data' => $data])
  144.         ;
  145.         return $mailer->send($mail);
  146.     }
  147.     /**
  148.      * dashboard Export
  149.      *
  150.      * @param PrintJobBackend $jobBackend
  151.      * @param TranslatorInterface $translator
  152.      * @return RedirectResponse|Response|void
  153.      *
  154.      * @Route("/dashboard/export", name="dashboard_export")
  155.      */
  156.     public function dashboardExport (
  157.         PrintJobBackend $jobBackend,
  158.         TranslatorInterface $translator
  159.     ) {
  160.         $session $this->sessionByRequestStack();
  161.         $user $this->getuser();
  162.         $profiles $user->getProfiles();
  163.         $b_key $session->get('b_key'0);
  164.         $profile $profiles[$b_key];
  165.         $bubbleId $profile['b_id'];
  166.         if ($profile['dashboard']['read'] == 1) {
  167.             $user $this->getUser();
  168.             $date = ['from' => 0'to' => 0];
  169.             $resp $jobBackend->getDashboardLog($bubbleId$user$date);
  170.             if ($resp->getSTatusCode() == '200') {
  171.                 $response json_decode($resp->getContent(), true)['contents'];
  172.                 // Stats data set creation
  173.                 $datas = [];
  174.                 $i 1;
  175.                 foreach($response as $r) {
  176.                     foreach($r as $indice => $value) {
  177.                         $row['row'] = $i;
  178.                         $row['full_date'] = date('d-m-Y'strtotime($r['job_start_time']));
  179.                         $row['day'] = date('d'strtotime($r['job_start_time']));
  180.                         $row['month'] = date('m'strtotime($r['job_start_time']));
  181.                         $row['year'] = date('Y'strtotime($r['job_start_time']));
  182.                         if (
  183.                             $indice == 'job_start_time' ||
  184.                             $indice == 'job_end_time' ||
  185.                             $indice == 'job_deleted_at' ||
  186.                             $indice == 'job_created_at'
  187.                         ) {
  188.                             if ($value) {
  189.                                 $row[$indice '_date'] = date('d-m-Y'strtotime($value));
  190.                                 $row[$indice] = date('H:i:s'strtotime($value));
  191.                             }
  192.                             else{
  193.                                 $row[$indice] = '-NC-';
  194.                             }
  195.                         }
  196.                         if ($indice == 'job_status') {
  197.                             if (
  198.                                 $value == 'finished' ||
  199.                                 $value == 'Success' ||
  200.                                 $value == 'success' ||
  201.                                 $value == 'Finished'
  202.                             ) {
  203.                                 $row[$indice] = 'finished';
  204.                             }
  205.                             else{
  206.                                 $row[$indice] = $value;
  207.                             }
  208.                         }
  209.                         if ($indice == 's_name') {
  210.                             $row['site'] = $value;
  211.                         }
  212.                         if ($indice == 'all_piece_controlled') {
  213.                             if ($value == true) {
  214.                                 $row['all_piece_controlled'] = 'yes';
  215.                             }
  216.                             else{
  217.                                 $row['all_piece_controlled'] = 'no';
  218.                             }
  219.                         }
  220.                         if ($indice == 'job_time_total') {
  221.                             $row[$indice . (' (sec)')] = ($value 1000);
  222.                         }
  223.                         if (
  224.                             $indice != 'us_pro_id' &&
  225.                             $indice != 's_id' &&
  226.                             $indice != 's_name' &&
  227.                             $indice != 'job_id' &&
  228.                             $indice != 'job_build_plate' &&
  229.                             $indice != 'job_start_time' &&
  230.                             $indice != 'job_end_time' &&
  231.                             $indice != 'job_estimated_time_total' &&
  232.                             $indice != 'job_deleted_at' &&
  233.                             $indice != 'job_created_at' &&
  234.                             $indice != 'job_forced' &&
  235.                             $indice != 'job_time_total' &&
  236.                             $indice != 'job_owner' &&
  237.                             $indice != 'job_status' &&
  238.                             $indice != 'job_index' &&
  239.                             $indice != 'job_req_id' &&
  240.                             $indice != 'job_print_core_1' &&
  241.                             $indice != 'job_print_core_2' &&
  242.                             $indice != 'job_log_filename' &&
  243.                             $indice != 'job_req_id' &&
  244.                             $indice != 'b_id' &&
  245.                             $indice != 's_id' &&
  246.                             $indice != 'p_job_history_creation_date' &&
  247.                             $indice != 'p_job_history_update_date' &&
  248.                             $indice != 'job_programmed_count' &&
  249.                             $indice != 'job_printed_count' &&
  250.                             $indice != 'job_conf_count' &&
  251.                             $indice != 'printer_job_id' &&
  252.                             $indice != 'printer_model' &&
  253.                             $indice != 'printer_brand' &&
  254.                             $indice != 'all_piece_controlled'
  255.                         ) {
  256.                             $row[$indice] = $value;
  257.                         }
  258.                     }
  259.                     $i++;
  260.                     $datas[] = $row;
  261.                 }
  262.                 // CSV GENERATION
  263.                 $csvContent '';
  264.                 foreach($datas[0] as $indice => $title) {
  265.                     $csvContent .= $indice ";";
  266.                 }
  267.                 $csvContent .= "\n";
  268.                 foreach($datas as $row) {
  269.                     foreach($row as $data) {
  270.                         $csvContent .= $data ";";
  271.                     }
  272.                     $csvContent .= "\n";
  273.                 }
  274.                 $csvName 'dashboard_' strtolower(str_replace(' ''_'$profile['b_name'])) . '_' date('d_m_Y')  .'.csv';
  275.                 return new Response($csvContent,200, [
  276.                         'Content-Type' => 'application/vnd.ms-excel',
  277.                         "Content-disposition" => "attachment; filename=$csvName"
  278.                     ]
  279.                 );
  280.             } else {
  281.                 if ($resp->getStatusCode() == '204') {
  282.                     $this->addFlash("errors"sprintf($translator->trans("Cette bubble ne dispose pas de sufisamment de données pour effectuer un export")));
  283.                 } else {
  284.                     $this->addFlash("errors"sprintf($translator->trans("Une erreur s'est produite; Veuillez ré-essayer plus tard")));
  285.                 }
  286.                 return $this->redirectToRoute('dashboard');
  287.             }
  288.         }
  289.     }
  290. }