<?php
/*
* Disclaimer: This source code is protected by copyright law and by
* international conventions.
*
* Any reproduction or partial or total distribution of the source code, by any
* means whatsoever, is strictly forbidden.
*
* Anyone not complying with these provisions will be guilty of the offense of
* infringement and the penal sanctions provided for by law.
*
* © 2022 All rights reserved.
*/
namespace App\Controller;
use App\Entity\Parametre\Parametre;
use App\Entity\Parametre\Societe;
use App\Entity\Parametre\Utilisateur;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* AbstractAppController.
*
* @author John Doe <john.doe@projetx.io>
* @package App\Controller
*/
class AbstractAppController extends AbstractController {
/** @var array */
private $cacheParameters = [];
/**
* Get the entity manager.
*
* @return \Doctrine\Persistence\ObjectManager
*/
public function em() {
return $this->getDoctrine()->getManager();
}
/**
* Get application parameter.
*
* @param $key
* @return mixed|null
*/
public function getAppParameter($key) {
if (true === array_key_exists($key, $this->cacheParameters)) {
return $this->cacheParameters [$key];
}
// Find parameter.
$findParameter = $this->em()->getRepository(Parametre::class)->findOneBy([
"cle" => $key,
"platformId" => getenv("PLATFORM_ID"),
]);
// If parameter was not found.
if (null === $findParameter) {
return null;
}
// Build cache.
$this->cacheParameters[$key] = $findParameter->getValeur();
// Return value.
return $this->cacheParameters[$key];
}
/**
* @return string
* @throws \Exception
*/
public function getLogoApplicationDataBase64() {
$filePath = \Kernel::getRoot() . "/public/assets/images/logo-dark.png";
if (getenv("PLATFORM_CODE") !== false) {
$logo = $this->getAppParameter("APP_LOGO");
if (null !== $logo && true === file_exists(\Kernel::getStoragePath() . "/template/{$logo}")) {
$filePath = \Kernel::getStoragePath() . "/template/{$logo}";
}
}
return "data:image/png;base64," . base64_encode(file_get_contents($filePath));
}
/**
* @return Societe|object|null
*/
public function getSocieteSelectedByUser() {
/** @var Utilisateur $user */
$user = $this->getUser();
$selected = $user->getSocieteSelected();
if ($selected !== null) {
return $selected;
}
$request = $this->get("request_stack");
if ($request->getSession()->has("companySelected")) {
$societe = $this->em()->getRepository(Societe::class)->findOneBy([
"id" => $request->getSession()->get("companySelected"),
"platformId" => getenv("PLATFORM_ID"),
]);
if ($societe !== null) {
return $societe;
}
}
return null;
}
/**
* @return Utilisateur|UserInterface
*/
public function getUser() {
return parent::getUser();
}
/**
* @param $hasPermission
*/
public static function handlePermissionResponse($hasPermission) {
if (true === $hasPermission) {
return;
}
exit("Permission denied.");
}
/**
* @param false $asJson
*/
public function requireSocieteSelected($asHtml = false) {
if ($this->getUser()->getSocieteSelected() !== null) {
return;
}
if (true === $asHtml) {
exit($this->render("Layout/page/500-content.html.twig", [
"message" => "Votre compte utilisateur doit être rattaché à un établissement.<br/><smalL>Veuillez contacter votre administrateur afin de résoudre le problème.</smalL>",
"showButtonHome" => false,
])->getContent());
}
header('Content-type: application/json');
exit(json_encode([
"success" => false,
"message" => "Vous devez sélectionner une structure.",
]));
}
}