<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpClient\HttpClient;
class DefaultController extends AbstractController
{
public function admin(Request $request): Response
{
return $this->redirectToRoute('sonata_admin_dashboard');
}
public function index($slasecret = null, Request $request): Response
{
$user = $this->getUser();
if($user) {
$isLoggedIn = true;
} else {
$isLoggedIn = false;
}
$valid = null;
$CHEQContracts = null;
$others = null;
$changes = [];
if(!$slasecret && $isLoggedIn) {
$slasecret = $request->request->get('sla_botname', null);
} else {
$slasecret = "sla:".$slasecret;
}
if($slasecret) {
$slaChanges = [];
$em = $this->getDoctrine()->getManager();
$projectRepo = $em->getRepository("App\Entity\Project");
if(strpos($slasecret, "sla:") !== false) {
$project = $projectRepo->findOneBy(array("slasecret" => str_replace("sla:", "", $slasecret)));
} else {
$project = $projectRepo->findOneBy(array("botname" => $slasecret));
}
if($project) {
/*SLAs*/
$slas = $project->getSlaChanges();
if(!empty($slas)) {
foreach($slas as $sla) {
$slaChanges["changes"][$sla->getDate()->format("Y-m-d H:i:s")] = [
"name" => $sla->getCurrentSLA()->getName(),
"value" => $sla->getCurrentSLA()->getValue(),
"id" => $sla->getId(),
"security" => $sla->getSecurity(),
];
}
}
krsort($slaChanges["changes"]);
$changes = $slaChanges["changes"];
$valid = 1;
/*CHEQContracts*/
foreach($project->getCHEQContracts() AS $CHEQContract) {
if(!$CHEQContract->getFiles()->isEmpty()) {
foreach($CHEQContract->getFiles() AS $file) {
$CHEQContracts[$CHEQContract->getCreated()->format("Y-m-d H:i:s")][$file->getName()."-".$file->getCreated()->getTimestamp()] = [
"fileName" => $file->getName(),
"fileDescription" => $file->getDescription(),
"fileCreated" => $file->getCreated()->format("Y-m-d H:i:s"),
"fileId" => $file->getId(),
"fileSecurity" => $file->getSecurity(),
];
}
krsort($CHEQContracts[$CHEQContract->getCreated()->format("Y-m-d H:i:s")]);
}
}
/*Other files*/
if(!empty($project->getFiles())) {
foreach($project->getFiles() AS $file) {
if($file->getCHEQContract()) {
continue;
}
$extension = \explode(".", $file->getFilePath());
switch (\end($extension)) {
case "png":
case "jpg":
case "jpeg":
case "gif":
$fileType = "image";
break;
case "pdf":
$fileType = "pdf";
break;
default:
$fileType = "other";
}
$others[] = [
"fileName" => $file->getName(),
"fileDescription" => $file->getDescription(),
"fileCreated" => $file->getCreated()->format("Y-m-d H:i:s"),
"fileId" => $file->getId(),
"fileSecurity" => $file->getSecurity(),
"fileType" => $fileType,
];
}
if(!empty($others)) {
krsort($others);
}
}
}
}
return $this->render("index.html.twig", [
"valid" => $valid,
"botname" => $slasecret,
"changes" => $changes,
"isLoggedIn" => $isLoggedIn,
"CHEQContracts" => $CHEQContracts,
"others" => $others,
]);
}
}