<?php
namespace App\Controller;
use App\Entity\RefRole;
use App\Entity\RefFederation;
use App\Entity\RefAffectation;
use App\Entity\ParametrageTexte;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\DateRapportAnnuelActiviteType;
use App\Services\Lib\SpecializedJsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TextesController extends AbstractController
{
private ManagerRegistry $doctrine;
private SpecializedJsonResponse $specializedJsonResponse;
public function __construct(ManagerRegistry $doctrine, SpecializedJsonResponse $specializedJsonResponse)
{
$this->doctrine = $doctrine;
$this->specializedJsonResponse = $specializedJsonResponse;
}
#[Route(path: '/api/set_parametrage_texte_lm', name: 'cts_web_set_parametrage_texte_lm')]
public function SetParametrageTexteLm(Request $request)
{
$role = $this->getUser()->getUtilisateur()->getIdRole();
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$request->request->replace(is_array($data) ? $data : []);
$params = $request->request->all();
$entityManager = $this->doctrine->getManager();
try {
if (
RefRole::ROLE_FEDE === $role
|| RefRole::ROLE_FEDE_INVITE === $role
) {
$federation = $this->getUser()->getIdFederation();
$text_RefFede = $this->doctrine->getRepository(RefFederation::class)->find($federation->getIdFederation());
$text_RefFede->settext_DTG_dtn($params['textes_general_cts']['directiveTechDTN']);
$entityManager->persist($text_RefFede);
}
if (
RefRole::ROLE_DR === $role
|| RefRole::ROLE_DR_SIGNATAIRE === $role
) {
$affectation = $this->getUser()->getIdAffectation();
$text_RefAffe = $this->doctrine->getRepository(RefAffectation::class)->find($affectation->getIdAffectation());
$text_RefAffe->settext_DGCS_dr_ds($params['textes_general_cts']['directiveChefService']);
$entityManager->persist($text_RefAffe);
}
if (
RefRole::ROLE_DS_ADMIN === $role
|| RefRole::ROLE_DS_SIGNATAIRE === $role
) {
$text_RefAffe = $this->doctrine->getRepository(RefAffectation::class)->find(1);
$text_RefAffe->settext_DGCS_dr_ds($params['textes_general_cts']['directiveChefService']);
$entityManager->persist($text_RefAffe);
}
$entityManager->flush();
return new JsonResponse(
[
'success' => true
],
JsonResponse::HTTP_CREATED
);
} catch (\Exception $e) {
echo $e->getMessage();
$result = [
'success' => false,
'data' => $e->getMessage()
];
}
return new JsonResponse($result, JsonResponse::HTTP_BAD_REQUEST);
}
#[Route(path: '/api/get_parametrage_texte_lm', name: 'cts_web_get_parametrage_texte_lm')]
public function GetParametrageTexteLm()
{
$role = $this->getUser()->getUtilisateur()->getIdRole();
$data = ['directiveTechDTN' => null, 'directiveChefService' => null];
try {
if (
RefRole::ROLE_FEDE === $role
|| RefRole::ROLE_FEDE_INVITE === $role
) {
$federation = $this->getUser()->getIdFederation();
$text_RefFede = $this->doctrine->getRepository(RefFederation::class)->find($federation->getIdFederation());
$data['directiveTechDTN'] = $text_RefFede->gettext_DTG_dtn();
}
if (
RefRole::ROLE_DR === $role
|| RefRole::ROLE_DR_SIGNATAIRE === $role
) {
$affectation = $this->getUser()->getIdAffectation();
$text_RefAffe = $this->doctrine->getRepository(RefAffectation::class)->find($affectation->getIdAffectation());
$data['directiveChefService'] = $text_RefAffe->gettext_DGCS_dr_ds();
}
if (RefRole::ROLE_DS_SIGNATAIRE === $role || RefRole::ROLE_DS_ADMIN === $role) {
$affectation = $this->getUser()->getIdAffectation();
$text_RefAffe = $this->doctrine->getRepository(RefAffectation::class)->find(1); // NATIONAL
$data['directiveChefService'] = $text_RefAffe->gettext_DGCS_dr_ds();
}
$success = true;
} catch (\Exception $e) {
$success = false;
$data = $e->getMessage();
}
return $this->specializedJsonResponse->getJson($success, $data);
}
/**
* @throws \JsonException
*/
#[Route(path: '/api/set_parametrage_carriere', name: 'cts_web_set_parametrage_carriere')]
public function SetParametrageCarriere(Request $request)
{
$entityManager = $this->doctrine->getManager();
$repo = $this->doctrine->getRepository(DateRapportAnnuelActiviteType::class);
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$request->request->replace(is_array($data) ? $data : []);
$params = $request->request->all();
$t = $repo->getList();
if ((is_countable($t) ? count($t) : 0) > 0) {
$t = $t[0];
} else {
$t = new DateRapportAnnuelActiviteType();
}
$t->setNoteInfoEcranCarriere($params['NOTE_INFO_ECRAN_CARRIERE']);
$t->setNoteInfoRapportAnnuelActivite($params['NOTE_INFO_RAPPORT_ANNUEL_ACTIVITE']);
$entityManager->persist($t);
$entityManager->flush();
return $this->specializedJsonResponse->getJson(true, $data);
}
#[Route(path: '/api/get_parametrage_carriere', name: 'cts_web_get_parametrage_carriere')]
public function GetParametrageCarriere()
{
$result = [];
$repo = $this->doctrine->getRepository(DateRapportAnnuelActiviteType::class)->getList();
$text_info = array_map(function ($text) {
return [
'NOTE_INFO_ECRAN_CARRIERE' => $text->getNoteInfoEcranCarriere(),
'NOTE_INFO_RAPPORT_ANNUEL_ACTIVITE' => $text->getNoteInfoRapportAnnuelActivite()
];
}, $repo);
if (sizeof($text_info) > 0) {
$data = [
'status' => Response::HTTP_OK,
'data' => $text_info
];
} else {
$data = [
'status' => Response::HTTP_NOT_FOUND,
'data' => 'Aucun texte trouvé'
];
}
return new JsonResponse($data);
}
/**
* @throws \JsonException
*/
#[Route(path: '/api/set_parametrage_texte', name: 'cts_web_set_parametrage_texte')]
public function SetParametrageTexte(Request $request)
{
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$entityManager = $this->doctrine->getManager();
$repo = $this->doctrine->getRepository(ParametrageTexte::class);
foreach ($data as $code => $texte) {
$ta = $repo->getByCode($code);
if ((is_countable($ta) ? count($ta) : 0) > 0) {
$t = $ta[0];
$t->setTexte($texte);
} else {
$t = new ParametrageTexte();
$t->setCode($code);
$t->setTexte($texte);
}
$entityManager->persist($t);
}
$entityManager->flush();
return $this->specializedJsonResponse->getJson(true, $data);
}
#[Route(path: '/api/get_date_rapport', name: 'cts_web_get_date_rapport')]
public function GetDateRapport(Request $request, SpecializedJsonResponse $specializedJsonResponse)
{
if ($request->getContent() == '') {
$data = [];
} else {
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
}
$repo = $this->doctrine->getRepository(DateRapportAnnuelActiviteType::class);
$result = [];
$t = $repo->getList();
$result['DATE_DEBUT'] = $t[0]->getDateDebutSaisi();
$result['DATE_FIN'] = $t[0]->getDateFinSaisi();
// return $specializedJsonResponse->getJson(true, $result); // Crée le tableau items avec les valeurs récupérées
$annee =Date('Y');
$items = [
'annee' => $annee,
'dateDebutSaisi' => isset($result['DATE_DEBUT']) ? $result['DATE_DEBUT']->format('Y-m-d H:i:s') : null,
'dateFinSaisi' => isset( $result['DATE_FIN']) ? $result['DATE_FIN']->format('Y-m-d H:i:s') : null,
];
$jsonv['items'] = $items;
$json_merge = json_encode($jsonv);
return new JsonResponse($json_merge, JsonResponse::HTTP_OK, [], true);
}
#[Route(path: '/api/get_parametrage_texte', name: 'cts_web_get_parametrage_texte')]
public function GetParametrageTexte(Request $request, SpecializedJsonResponse $specializedJsonResponse)
{
//ajout controle
if ($request->getContent() == '') {
$data = [];
} else {
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
}
$repo = $this->doctrine->getRepository(ParametrageTexte::class);
$result = [];
foreach ($data as $code) {
$t = $repo->getByCode($code);
if ((is_countable($t) ? count($t) : 0) > 0)
$result[$code] = $t[0]->getTexte();
else
$result[$code] = '';
}
return $specializedJsonResponse->getJson(true, $result);
}
#[Route(path: '/api/saveTextInfoProfil', name: 'cts_web_set_texte_profil')]
public function SetTextInfoProfil(Request $request)
{
$role = $this->getUser()->getUtilisateur()->getIdRole();
if (RefRole::ROLE_DS_ADMIN !== $role) {
return new JsonResponse(null, JsonResponse::HTTP_BAD_REQUEST);
}
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$entityManager = $this->doctrine->getManager();
$repo = $this->doctrine->getRepository(ParametrageTexte::class);
foreach ($data as $code => $texte) {
$ta = $repo->getByCode($code);
if ((is_countable($ta) ? count($ta) : 0) > 0) {
$t = $ta[0];
$t->setTexte($texte);
} else {
$t = new ParametrageTexte();
$t->setCode($code);
$t->setTexte($texte);
}
$entityManager->persist($t);
}
$entityManager->flush();
return $this->specializedJsonResponse->getJson(true, $data);
}
#[Route(path: '/api/loadTextInfoProfil', name: 'cts_web_get_texte_profil')]
public function GetTextInfoProfil()
{
$data = ['textCts', 'textDs', 'textDtn', 'TextSh'];
$repo = $this->doctrine->getRepository(ParametrageTexte::class);
$result = [];
foreach ($data as $code) {
$t = $repo->getByCode($code);
if ((is_countable($t) ? count($t) : 0) > 0)
$result[$code] = $t[0]->getTexte();
else
$result[$code] = '';
}
return $this->specializedJsonResponse->getJson(true, $result);
}
}