<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\Session;
use App\Services\UserAttendanceManager;
use App\Entity\UserAttendance;
use Doctrine\ORM\EntityManagerInterface;
//use App\Message\MailNotification;
use App\Services\SignManager;
use DateTime;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Mime\Email;
use App\Config\Config;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
//use Symfony\Component\Validator\Constraints\DateTime;
class SignController extends AbstractController
{
public function __construct(
UserAttendanceManager $uam,
EntityManagerInterface $entityManager,
MailerInterface $mailer,
SignManager $sm, Config $config
) {
$this->uam = $uam;
$this->entityManager = $entityManager;
$this->mailer = $mailer;
$this->sm = $sm;
$this->config = $config;
}
#[
Route(
"/sign/{id}/{idSession}/{startTime}/{firstName}/{lastName}/{company}/{title}/{location}",
name: "app_sign"
)
]
public function index(
$id,
$idSession,
$startTime,
$firstName,
$lastName,
$company,
$location,
$title
) {
$session = new Session();
$session->remove("user_for_sign");
$session->start();
$session->set("user_for_sign", $id);
$session->set("session_to_sign", $idSession);
$monthNum = date("m", $startTime);
$dateObj = \DateTime::createFromFormat("!m", $monthNum);
$monthName = $dateObj->format("F");
return $this->render("acoa/sign/index.html.twig", [
"data" => [
"startTime" =>
date("d", $startTime) .
" " .
$this->setToLocale($monthName) .
" " .
date("Y", $startTime) .
" " .
date(" à " . "H:i", $startTime + 6*3600 ),
"firstName" => $firstName,
"location" => $location,
"lastName" => $lastName,
"title" => str_replace(" ", " ", $title),
"company" => $company,
"signed" => $this->sm->signStatus($id, $idSession),
],
]);
}
#[Route("/postsign", name: "postsign")]
public function post_sign()
{
$session = new Session();
if (
file_get_contents("php://input") !== "" &&
$session->get("user_for_sign") &&
$session->get("session_to_sign")
) {
$imageData = file_get_contents("php://input");
$filteredData = substr($imageData, strpos($imageData, ",") + 1);
$unencodedData = base64_decode($filteredData);
$imageName =
"sign_" .
$session->get("user_for_sign") .
"_" .
$session->get("session_to_sign") .
".png";
$filepath =
$this->getParameter("kernel.project_dir") .
"/public/signature/" .
$imageName;
if (!file_exists($filepath)) {
$fp = fopen($filepath, "wb");
fwrite($fp, $unencodedData);
fclose($fp);
$userId = $session->get("user_for_sign");
$idSessionStage = $session->get("session_to_sign");
$this->uam->updateAttendance($userId, "Y", $idSessionStage);
}
}
$session->remove("user_for_sign");
$session->remove("session_to_sign");
return $this->redirectToRoute("home");
}
#[Route("/requestsign", name: "requestsign")]
public function requestSign()
{
return $this->render("acoa/sign/request.html.twig", [
"controller_name" => "SignController",
]);
}
private function setToLocale($string): string
{
switch ($string) {
case "January":
return str_replace("January", "Janvier", $string);
break;
case "February":
return str_replace("February", "Février", $string);
break;
case "March":
return str_replace("March", "Mars", $string);
break;
case "April":
return str_replace("April", "Avril", $string);
break;
case "May":
return str_replace("May", "Mai", $string);
break;
case "June":
return str_replace("June", "Juin", $string);
break;
case "July":
return str_replace("July", "Juillet", $string);
break;
case "August":
return str_replace("August", "Août", $string);
break;
case "September":
return str_replace("September", "Septembre", $string);
break;
case "October":
return str_replace("October", "Octobre", $string);
break;
case "November":
return str_replace("November", "Novembre", $string);
break;
case "December":
return str_replace("December", "Décembre", $string);
break;
default:
return $string;
break;
}
}
private function _date($date)
{
$getKeys = preg_split("/[\s:]+/", $date);
$buildArray = [$getKeys[0], $getKeys[1], $getKeys[2], $getKeys[3]];
$dateArray = date_parse_from_format("Y/m/d H:i:s", $date);
$year = $dateArray["year"];
$day = $dateArray["day"];
$hour = $buildArray[1];
$minutes = $buildArray[2];
$seconds = $buildArray[3];
$month = \DateTime::createFromFormat("!m", $dateArray["month"])->format(
"F"
);
return strtotime(
"$day $month $year $hour hours $minutes minutes $seconds seconds"
);
}
#[Route("/requestsign/email", name: "requestsign_email")]
public function sendMail()
{
$details = $this->sm->getDetailsForSignature();
//dd($details);
foreach ($details as $aDetail) {
if (!$aDetail["first_name"]) {
$aDetail["first_name"] = "no_first_name";
}
if (!$aDetail["last_name"]) {
$aDetail["last_name"] = "no_last_name";
}
if (!$aDetail["title"]) {
$aDetail["title"] = "no_title";
}
$stageDate = $aDetail["stage_date"]
->setTimezone(new \DateTimeZone("America/Guadeloupe"))
->format("Y-m-d H:i:s");
// dd($stageDate);
$monthNum = gmdate("m", $this->_date($stageDate));
$dateObj = \DateTime::createFromFormat("!m", $monthNum);
$monthName = $this->setToLocale($dateObj->format("F"));
$_stageDate = " " .
$monthName .
" " .
gmdate("Y", $this->_date($stageDate));
// dd( $aDetail["start_time"]);
if ($aDetail["start_time"]) {
$startTime = $aDetail["start_time"]->setTimezone(new \DateTimeZone("America/Guadeloupe"))->format("Y-m-d H:i:s");
// dd($startTime);
$monthNum = date("m", $this->_date($startTime));
$dateObj = \DateTime::createFromFormat("!m", $monthNum);
$monthName = $this->setToLocale($dateObj->format("F"));
$_startTime = date(" à " . "H:i", $this->_date($startTime));
$dateStr = $aDetail["stage_date"]->setTimezone(new \DateTimeZone("America/Guadeloupe"))->format("Y-m-d") .
" " .
$aDetail["start_time"]->setTimezone(new \DateTimeZone("America/Guadeloupe"))->format("H:i:s");
$aDetail["start_time"] = $this->_date($dateStr) - 6* 3600;
$aDetail["title"] = str_replace(" ", " ", $aDetail["title"]);
$url =
$_SERVER["SERVER_NAME"] .
"/sign/" .
$aDetail["id"] .
"/" .
$aDetail["id_session_stage"] .
"/" .
$aDetail["start_time"] .
"/" .
$aDetail["first_name"] .
"/" .
$aDetail["last_name"] .
"/" .
$aDetail["company"] .
"/" .
$aDetail["title"] .
"/" .
$aDetail["location"];
$link = "https://" . $url;
$emailContent =
"Bonjour " .
$aDetail["last_name"] .
",<br>
Afin d'attester votre présence à la seance " .
$aDetail["title"] .
" du " .
$_stageDate .
$_startTime .
",<br>
Veuillez cliquer sur le lien ci-contre : " .
$link .
" <br>
L'équipe ACOA";
$em = (new TemplatedEmail())
->from($this->config->getMailSender())
->priority(Email::PRIORITY_HIGH)
->to($aDetail["email"])
->cc("kalidougattaba@gmail.com","pascal.liatard@abware.fr")
->subject(
"Attestation de présence du " .
$_stageDate .
$_startTime
) //->html($emailContent);
->htmlTemplate("acoa/email/signature.html.twig")
->context([
"time" => $_stageDate . $_startTime,
"firstname" => $aDetail["first_name"],
"lastname" => $aDetail["last_name"],
"link" => $link,
"title" => $aDetail["title"],
]);
$this->mailer->send($em);
$this->sm->setEmailToSend(
$aDetail["id"],
$aDetail["id_session_stage"]
);
// $this->dispatchMessage(new MailNotification($aDetail["email"], "Signature", $link));
}
}
return new Response();
}
}