<?php
namespace App\Controller;
use App\Activity\Canopy;
use App\Activity\Door;
use App\Entity\Log;
use App\Entity\Option;
use App\Entity\User;
use App\Helper\Company;
use App\Xml\DoorXml;
use setasign\Fpdi\Fpdi;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\Translation\TranslatorInterface;
class DoorController extends AbstractController
{
/**
* @Route("/porte-battante/", name="door_tool")
*/
public function selectDisposition(Canopy $canopy): Response
{
return $this->render("home/index.html.twig", ["popup" => "popup_select_door"]);
}
/**
* @Route("/porte-battante/standard", name="doorb_tool")
* @Route("/porte-battante/{pos}", requirements={"pos": "\d+"}, name="door_tool_pos")
*/
public function battante(Session $session, $projectDir, $pos = null): Response
{
return $this->showTool($session, $projectDir, 'b', $pos);
}
/**
* @Route("/porte-battante/sur-mesure", name="door_tool_custom")
*/
public function battanteSurMesure(Session $session, $projectDir): Response
{
return $this->showTool($session, $projectDir, 'bs', null);
}
/**
* @Route("/porte-suspendue/", name="dooro_tool")
*/
public function ouvrante(Session $session, $projectDir, $pos = null): Response
{
return $this->render("home/index.html.twig", ["popup" => "popup_select_dooro"]);
}
/**
* @Route("/porte-suspendue-40kg/", name="dooro40_tool")
*/
public function ouvrante40(Session $session, $projectDir, $pos = null): Response
{
if($session->has("has_door")) {
$session->clear();
}
return $this->showTool($session, $projectDir, 'o40', $pos);
}
/**
* @Route("/porte-suspendue-70kg/", name="dooro70_tool")
*/
public function ouvrante70(Session $session, $projectDir, $pos = null): Response
{
if($session->has("has_door")) {
$session->clear();
}
return $this->showTool($session, $projectDir, 'o70', $pos);
}
public function showTool(Session $session, $projectDir, $type, $pos = null): Response
{
if(!$session->has("door_token")) {
$token = uniqid();
$session->set('door_token', $token);
if($session->has("has_door")) {
if($session->get('has_door') === $session->get('configured_door')) {
$session->remove('has_door');
$session->remove('configured_door');
}
}
}
if($pos !== null) {
$session->set('door', $pos);
}
$token = $session->get('door_token');
$configFile = sprintf("%s/var/tokens/%s.yaml", $projectDir, $token);
if(file_exists($configFile)) {
unlink($configFile);
}
if ($type == 'b') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_STANDARD_DOOR');
}
else if ($type == 'bs') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_CUSTOM_DOOR');
}
else if ($type == 'o40') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_SLIDING_DOOR_40');
}
else if ($type == 'o70') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'START_SLIDING_DOOR_70');
}
$modules = $type == 'o40' ? ['door_sliding_40'] : ($type == 'o70' ? ['door_sliding_70'] : ['door_swinging']);
$options = [];
foreach ($this->getUser()->getCompany()->getOptions() as $option) {
foreach ($option->getModules() as $module) {
if (in_array($module->getName(), $modules)) {
$options[$option->getType()][] = $option;
break;
}
}
}
return $this->render('activities/door/tool.html.twig', [
'token' => $token,
'type' => $type,
'pos' => $pos,
"options" => $options,
]);
}
/**
* @Route("/porte/resultat", name="door_final")
*/
public function finalStep(Door $door, Session $session, $projectDir): Response
{
$token = $session->get('door_token');
if($session->has("has_door")) {
$session->set(sprintf("module_door_%d", $session->get('door')), $token);
$session->set("configured_door", $session->get('configured_door')+1);
$session->remove("door_token");
return $this->redirectToRoute("cloison_final");
}
$configFile = sprintf("%s/var/tokens/%s.yaml", $projectDir, $token);
$config = Yaml::parseFile($configFile);
unset($config['time']);
if(array_key_exists("pdf", $config)) {
unset($config['pdf']);
}
$pannel = $config[0];
if ($pannel['options']) {
$options = &$pannel['options'];
$keys = ['dormant', 'sens', 'serrure', 'profile', 'handleProfile', 'jointvit', 'jointbut', 'color', 'colorCrutch'];
foreach($keys as $key) {
if (array_key_exists($key, $options) && gettype($options[$key]) == "string") {
$options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
}
}
}
return $this->render("activities/door/final.html.twig", ['pannel' => $pannel, 'type' => $pannel['type']]);
}
/**
* @Route("/porte/pdf", name="door_print")
*/
public function printCloison(TranslatorInterface $translator, Company $company, Door $door, Session $session, $projectDir, Packages $pathPackage): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
$token = $session->get('door_token');
$configFile = sprintf("%s/var/tokens/%s.yaml", $projectDir, $token);
$config = Yaml::parseFile($configFile);
$pannel = $config[0];
$type = $pannel['type'];
$dest = self::getPdf($translator, $this->getUser(), $company, $token, $door, $session, $projectDir, $pathPackage);
if(!array_key_exists('pdf', $config)) {
if ($type == 'b') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_STANDARD_DOOR');
}
else if ($type == 'bs') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_CUSTOM_DOOR');
}
else if ($type == 'o40') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_40');
}
else if ($type == 'o70') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_70');
}
}
return $this->file($dest, "porte.pdf", ResponseHeaderBag::DISPOSITION_INLINE);
}
public function getPdf(TranslatorInterface $translator, User $user, Company $company, $token, Door $door, Session $session, $projectDir, Packages $pathPackage): string
{
$configFile = sprintf("%s/var/tokens/%s.yaml", $projectDir, $token);
$config = Yaml::parseFile($configFile);
$dayDate = (new \DateTime())->format("Y-m-d");
unset($config['time']);
unset($config['pdf']);
if(array_key_exists("pdf", $config)) {
if(file_exists($config['pdf'])) {
unlink($config['pdf']);
}
unset($config['pdf']);
}
$pannel = $config[0];
$options = &$pannel['options'];
$keys = ['dormant', 'sens', 'serrure', 'profile', 'handle_profile', 'jointvit', 'jointbut', 'color', 'colorCrutch'];
foreach($keys as $key) {
if (array_key_exists($key, $options) && gettype($options[$key]) == "string") {
$options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
}
}
if($user->isTarif()) {
$pannel['price'] = $door->handleAjax("price", $pannel['config']);
} else{
$pannel['price'] = [
'price' => 0,
'prices' => []
];
}
$pdf = new Fpdi();
// Fetch specific theme colors if there is one, otherwise use seed
$profil = $session->get('profil');
$file = sprintf("%s/config/theme/%s.php", $projectDir, $profil);
if(!file_exists($file)) {
$file = sprintf("%s/config/theme/seed.php", $projectDir);
}
$profil = require($file);
$pack = $door->handleAjax("getComponents", $pannel);
$dest = sprintf("%s/var/cache/pdf/%s_door_%s.pdf", $projectDir, $dayDate, uniqid());
$page = \App\Pdf\Door::generate($translator, $company->getCompany(), $profil, $pack, $pannel,
sprintf(
"%s/public/uploads/%s",
$projectDir,
$company->getCompany()->getLogo()
)
);
if(!file_exists(dirname($dest))) {
mkdir(dirname($dest));
}
// Generate pdf to temporary file and merge it to the main pdf
$page->Output("F", $dest);
$pages = $pdf->setSourceFile($dest);
for($page = 0; $page < $pages; $page++) {
$pdf->AddPage();
$i = $pdf->importPage($page+1);
$pdf->useTemplate($i, 0, 0, 200);
}
unlink($dest);
$dest = sprintf("%s/var/cache/pdf/%s.pdf", $projectDir, uniqid());
if (!file_exists(dirname($dest))) {
mkdir(dirname($dest), 0777, true);
}
$pdf->Output("F", $dest);
$config = Yaml::parseFile($configFile);
$config['pdf'] = $dest;
file_put_contents($configFile, Yaml::dump($config, 4));
return $dest;
}
/**
* @Route("/porte/xml", name="door_xml")
*/
public function printXml(TranslatorInterface $translator, Company $company, Door $door, Session $session, $projectDir, Packages $pathPackage): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
$token = $session->get('door_token');
$configFile = sprintf("%s/var/tokens/%s.yaml", $projectDir, $token);
$config = Yaml::parseFile($configFile);
$pannel = $config[0];
$type = $pannel['type'];
$dest = self::getXml($translator, $this->getUser(), $company, $token, $door, $session, $projectDir, $pathPackage);
if(!array_key_exists('xml', $config)) {
if ($type == 'b') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_STANDARD_DOOR');
}
else if ($type == 'bs') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_CUSTOM_DOOR');
}
else if ($type == 'o40') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_40');
}
else if ($type == 'o70') {
$this->getDoctrine()->getRepository(Log::class)->addLog($this->getUser(), 'END_SLIDING_DOOR_70');
}
}
return $this->file($dest, "porte.xml", ResponseHeaderBag::DISPOSITION_ATTACHMENT);
}
public function getXml(TranslatorInterface $translator, User $user, Company $company, $token, Door $door, Session $session, $projectDir, Packages $pathPackage): string
{
$configFile = sprintf("%s/var/tokens/%s.yaml", $projectDir, $token);
$config = Yaml::parseFile($configFile);
$dayDate = (new \DateTime())->format("Y-m-d");
unset($config['time']);
unset($config['pdf']);
if(array_key_exists("xml", $config)) {
if(file_exists($config['xml'])) {
unlink($config['xml']);
}
unset($config['xml']);
}
$pannel = $config[0];
$options = &$pannel['options'];
$keys = ['dormant', 'sens', 'serrure', 'profile', 'handleProfile', 'jointvit', 'jointbut', 'color', 'colorCrutch'];
foreach($keys as $key) {
if (array_key_exists($key, $options) && gettype($options[$key]) == "string") {
$options[$key] = $this->getDoctrine()->getRepository(Option::class)->find($options[$key]);
}
}
if($user->isTarif()) {
$pannel['price'] = $door->handleAjax("price", $pannel['config']);
} else{
$pannel['price'] = [
'price' => 0,
'prices' => []
];
}
$xmlBuilder = new DoorXml($company->getCompany(), [$pannel], $translator, $door);
$xmlBuilder->build();
$xml = $xmlBuilder->getXml();
$dest = sprintf("%s/var/cache/xml/%s.xml", $projectDir, uniqid());
if (!file_exists(dirname($dest))) {
mkdir(dirname($dest), 0777, true);
}
$dom = new \DOMDocument("1.0", "UTF-8");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);
$xmlFormatted = $dom->saveXML();
$xmlFormatted = mb_convert_encoding($xmlFormatted, 'UTF-8', 'HTML-ENTITIES');
file_put_contents($dest, $xmlFormatted);
$config = Yaml::parseFile($configFile);
$config['xml'] = $dest;
file_put_contents($configFile, Yaml::dump($config, 4));
return $dest;
}
}