src/Controller/SignController.php line 187

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use App\Services\UserAttendanceManager;
  8. use App\Entity\UserAttendance;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. //use App\Message\MailNotification;
  11. use App\Services\SignManager;
  12. use DateTime;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
  15. use Symfony\Component\Mime\Email;
  16. use App\Config\Config;
  17. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  18. //use Symfony\Component\Validator\Constraints\DateTime;
  19. class SignController extends AbstractController
  20. {
  21.     public function __construct(
  22.         UserAttendanceManager $uam,
  23.         EntityManagerInterface $entityManager,
  24.         MailerInterface $mailer,
  25.         SignManager $smConfig $config
  26.     ) {
  27.         $this->uam $uam;
  28.         $this->entityManager $entityManager;
  29.         $this->mailer $mailer;
  30.         $this->sm $sm;
  31.         $this->config $config;
  32.     }
  33.     #[
  34.         Route(
  35.             "/sign/{id}/{idSession}/{startTime}/{firstName}/{lastName}/{company}/{title}/{location}",
  36.             name"app_sign"
  37.         )
  38.     ]
  39.     public function index(
  40.         $id,
  41.         $idSession,
  42.         $startTime,
  43.         $firstName,
  44.         $lastName,
  45.         $company,
  46.         $location,
  47.         $title
  48.     ) {
  49.         $session = new Session();
  50.         $session->remove("user_for_sign");
  51.         $session->start();
  52.         $session->set("user_for_sign"$id);
  53.         $session->set("session_to_sign"$idSession);
  54.     $monthNum date("m"$startTime);
  55.         $dateObj \DateTime::createFromFormat("!m"$monthNum);
  56.     $monthName $dateObj->format("F");
  57.     return $this->render("acoa/sign/index.html.twig", [
  58.             "data" => [
  59.                 "startTime" =>
  60.                     date("d"$startTime) .
  61.                     " " .
  62.                     $this->setToLocale($monthName) .
  63.                     " " .
  64.                     date("Y"$startTime) .
  65.                     " " .
  66.                     date(" à " "H:i"$startTime 6*3600 ),
  67.                 "firstName" => $firstName,
  68.                 "location" => $location,
  69.                 "lastName" => $lastName,
  70.                 "title" => str_replace(" "" "$title),
  71.                 "company" => $company,
  72.                 "signed" => $this->sm->signStatus($id$idSession),
  73.             ],
  74.         ]);
  75.     }
  76.     #[Route("/postsign"name"postsign")]
  77.     public function post_sign()
  78.     {
  79.         $session = new Session();
  80.         
  81.         if (
  82.             file_get_contents("php://input") !== "" &&
  83.             $session->get("user_for_sign") &&
  84.             $session->get("session_to_sign")
  85.         ) {
  86.             $imageData file_get_contents("php://input");
  87.             $filteredData substr($imageDatastrpos($imageData",") + 1);
  88.             $unencodedData base64_decode($filteredData);
  89.             $imageName =
  90.                 "sign_" .
  91.                 $session->get("user_for_sign") .
  92.                 "_" .
  93.                 $session->get("session_to_sign") .
  94.                 ".png";
  95.             $filepath =
  96.                 $this->getParameter("kernel.project_dir") .
  97.                 "/public/signature/" .
  98.                 $imageName;
  99.             if (!file_exists($filepath)) {
  100.                 $fp fopen($filepath"wb");
  101.                 fwrite($fp$unencodedData);
  102.                 fclose($fp);
  103.                 $userId $session->get("user_for_sign");
  104.         $idSessionStage $session->get("session_to_sign");
  105.                 $this->uam->updateAttendance($userId"Y"$idSessionStage);
  106.             }
  107.         }
  108.         $session->remove("user_for_sign");
  109.         $session->remove("session_to_sign");
  110.         return $this->redirectToRoute("home");
  111.     }
  112.     #[Route("/requestsign"name"requestsign")]
  113.     public function requestSign()
  114.     {
  115.         return $this->render("acoa/sign/request.html.twig", [
  116.             "controller_name" => "SignController",
  117.         ]);
  118.     }
  119.     private function setToLocale($string): string
  120.     {
  121.         switch ($string) {
  122.             case "January":
  123.                 return str_replace("January""Janvier"$string);
  124.                 break;
  125.             case "February":
  126.                 return str_replace("February""Février"$string);
  127.                 break;
  128.             case "March":
  129.                 return str_replace("March""Mars"$string);
  130.                 break;
  131.             case "April":
  132.                 return str_replace("April""Avril"$string);
  133.                 break;
  134.             case "May":
  135.                 return str_replace("May""Mai"$string);
  136.                 break;
  137.             case "June":
  138.                 return str_replace("June""Juin"$string);
  139.                 break;
  140.             case "July":
  141.                 return str_replace("July""Juillet"$string);
  142.                 break;
  143.             case "August":
  144.                 return str_replace("August""Août"$string);
  145.                 break;
  146.             case "September":
  147.                 return str_replace("September""Septembre"$string);
  148.                 break;
  149.             case "October":
  150.                 return str_replace("October""Octobre"$string);
  151.                 break;
  152.             case "November":
  153.                 return str_replace("November""Novembre"$string);
  154.                 break;
  155.             case "December":
  156.                 return str_replace("December""Décembre"$string);
  157.                 break;
  158.             default:
  159.                 return $string;
  160.                 break;
  161.         }
  162.     }
  163.     private function _date($date)
  164.     {
  165.         $getKeys preg_split("/[\s:]+/"$date);
  166.         $buildArray = [$getKeys[0], $getKeys[1], $getKeys[2], $getKeys[3]];
  167.     $dateArray date_parse_from_format("Y/m/d H:i:s"$date);
  168.         $year $dateArray["year"];
  169.         $day $dateArray["day"];
  170.     $hour $buildArray[1];
  171.         $minutes $buildArray[2];
  172.         $seconds $buildArray[3];
  173.         $month \DateTime::createFromFormat("!m"$dateArray["month"])->format(
  174.             "F"
  175.     );
  176.         return strtotime(
  177.             "$day $month $year  $hour hours $minutes minutes $seconds seconds"
  178.         );
  179.     }
  180.     #[Route("/requestsign/email"name"requestsign_email")]
  181.     public function sendMail()
  182.     {
  183.         $details $this->sm->getDetailsForSignature();
  184. //dd($details);
  185.         foreach ($details as $aDetail) {
  186.             if (!$aDetail["first_name"]) {
  187.                 $aDetail["first_name"] = "no_first_name";
  188.             }
  189.             if (!$aDetail["last_name"]) {
  190.                 $aDetail["last_name"] = "no_last_name";
  191.             }
  192.             if (!$aDetail["title"]) {
  193.                 $aDetail["title"] = "no_title";
  194.             }
  195.         $stageDate $aDetail["stage_date"]
  196.             ->setTimezone(new \DateTimeZone("America/Guadeloupe"))
  197.             ->format("Y-m-d H:i:s");
  198.        // dd($stageDate);
  199.             $monthNum gmdate("m"$this->_date($stageDate));
  200.             $dateObj \DateTime::createFromFormat("!m"$monthNum);
  201.             $monthName $this->setToLocale($dateObj->format("F"));
  202.             
  203.         $_stageDate =    " " .
  204.                 $monthName .
  205.                 " " .
  206.         gmdate("Y"$this->_date($stageDate));
  207.     //    dd( $aDetail["start_time"]);
  208.             if ($aDetail["start_time"]) {
  209.                 $startTime $aDetail["start_time"]->setTimezone(new \DateTimeZone("America/Guadeloupe"))->format("Y-m-d H:i:s");
  210.     //    dd($startTime);
  211.         $monthNum date("m"$this->_date($startTime));
  212.                 $dateObj \DateTime::createFromFormat("!m"$monthNum);
  213.                 $monthName $this->setToLocale($dateObj->format("F"));
  214.                 $_startTime date(" à " "H:i"$this->_date($startTime));
  215.         
  216.                 $dateStr $aDetail["stage_date"]->setTimezone(new \DateTimeZone("America/Guadeloupe"))->format("Y-m-d") .
  217.                         " " .
  218.             $aDetail["start_time"]->setTimezone(new \DateTimeZone("America/Guadeloupe"))->format("H:i:s");
  219.         
  220.         $aDetail["start_time"] = $this->_date($dateStr) - 63600;
  221.                 $aDetail["title"] = str_replace(" "" "$aDetail["title"]);
  222.                 $url =
  223.                     $_SERVER["SERVER_NAME"] .
  224.                     "/sign/" .
  225.                     $aDetail["id"] .
  226.                     "/" .
  227.                     $aDetail["id_session_stage"] .
  228.                     "/" .
  229.                     $aDetail["start_time"] .
  230.                     "/" .
  231.                     $aDetail["first_name"] .
  232.                     "/" .
  233.                     $aDetail["last_name"] .
  234.                     "/" .
  235.                     $aDetail["company"] .
  236.                     "/" .
  237.                     $aDetail["title"] .
  238.                     "/" .
  239.                     $aDetail["location"];
  240.         $link "https://" $url;
  241.                 $emailContent =
  242.                     "Bonjour " .
  243.                     $aDetail["last_name"] .
  244.                     ",<br>
  245.                 Afin d'attester votre présence à la seance " .
  246.                     $aDetail["title"] .
  247.                     " du " .
  248.                     $_stageDate .
  249.                     $_startTime .
  250.                     ",<br>
  251.                 Veuillez cliquer sur le lien ci-contre :  " .
  252.                     $link .
  253.                     " <br>
  254.                 L'équipe ACOA";
  255.                 $em = (new TemplatedEmail())
  256.                     ->from($this->config->getMailSender())
  257.                     ->priority(Email::PRIORITY_HIGH)
  258.                     ->to($aDetail["email"])
  259.                     ->cc("kalidougattaba@gmail.com","pascal.liatard@abware.fr")
  260.                     ->subject(
  261.                         "Attestation de présence du " .
  262.                             $_stageDate .
  263.                             $_startTime
  264.                     //->html($emailContent);
  265.                     ->htmlTemplate("acoa/email/signature.html.twig")
  266.                     ->context([
  267.                         "time" => $_stageDate $_startTime,
  268.                         "firstname" => $aDetail["first_name"],
  269.                         "lastname" => $aDetail["last_name"],
  270.                         "link" => $link,
  271.                         "title" => $aDetail["title"],
  272.                     ]);
  273.                 $this->mailer->send($em);
  274.                 $this->sm->setEmailToSend(
  275.                     $aDetail["id"],
  276.                     $aDetail["id_session_stage"]
  277.                 );
  278.                 // $this->dispatchMessage(new MailNotification($aDetail["email"], "Signature", $link));
  279.             }
  280.         }
  281.         return new Response();
  282.     }
  283. }