src/App/Controller/AbstractAppController.php line 50

Open in your IDE?
  1. <?php
  2. /*
  3. * Disclaimer: This source code is protected by copyright law and by
  4. * international conventions.
  5. *
  6. * Any reproduction or partial or total distribution of the source code, by any
  7. * means whatsoever, is strictly forbidden.
  8. *
  9. * Anyone not complying with these provisions will be guilty of the offense of
  10. * infringement and the penal sanctions provided for by law.
  11. *
  12. * © 2022 All rights reserved.
  13. */
  14. namespace App\Controller;
  15. use App\Entity\Parametre\Parametre;
  16. use App\Entity\Parametre\Societe;
  17. use App\Entity\Parametre\Utilisateur;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. /**
  21.  * AbstractAppController.
  22.  *
  23.  * @author John Doe <john.doe@projetx.io>
  24.  * @package App\Controller
  25.  */
  26. class AbstractAppController extends AbstractController {
  27.     /** @var array */
  28.     private $cacheParameters = [];
  29.     /**
  30.      * Get the entity manager.
  31.      *
  32.      * @return \Doctrine\Persistence\ObjectManager
  33.      */
  34.     public function em() {
  35.         return $this->getDoctrine()->getManager();
  36.     }
  37.     /**
  38.      * Get application parameter.
  39.      *
  40.      * @param $key
  41.      * @return mixed|null
  42.      */
  43.     public function getAppParameter($key) {
  44.         if (true === array_key_exists($key$this->cacheParameters)) {
  45.             return $this->cacheParameters [$key];
  46.         }
  47.         // Find parameter.
  48.         $findParameter $this->em()->getRepository(Parametre::class)->findOneBy([
  49.             "cle"        => $key,
  50.             "platformId" => getenv("PLATFORM_ID"),
  51.         ]);
  52.         // If parameter was not found.
  53.         if (null === $findParameter) {
  54.             return null;
  55.         }
  56.         // Build cache.
  57.         $this->cacheParameters[$key] = $findParameter->getValeur();
  58.         // Return value.
  59.         return $this->cacheParameters[$key];
  60.     }
  61.     /**
  62.      * @return string
  63.      * @throws \Exception
  64.      */
  65.     public function getLogoApplicationDataBase64() {
  66.         $filePath = \Kernel::getRoot() . "/public/assets/images/logo-dark.png";
  67.         if (getenv("PLATFORM_CODE") !== false) {
  68.             $logo $this->getAppParameter("APP_LOGO");
  69.             if (null !== $logo && true === file_exists(\Kernel::getStoragePath() . "/template/{$logo}")) {
  70.                 $filePath = \Kernel::getStoragePath() . "/template/{$logo}";
  71.             }
  72.         }
  73.         return "data:image/png;base64," base64_encode(file_get_contents($filePath));
  74.     }
  75.     /**
  76.      * @return Societe|object|null
  77.      */
  78.     public function getSocieteSelectedByUser() {
  79.         /** @var Utilisateur $user */
  80.         $user     $this->getUser();
  81.         $selected $user->getSocieteSelected();
  82.         if ($selected !== null) {
  83.             return $selected;
  84.         }
  85.         $request $this->get("request_stack");
  86.         if ($request->getSession()->has("companySelected")) {
  87.             $societe $this->em()->getRepository(Societe::class)->findOneBy([
  88.                 "id"         => $request->getSession()->get("companySelected"),
  89.                 "platformId" => getenv("PLATFORM_ID"),
  90.             ]);
  91.             if ($societe !== null) {
  92.                 return $societe;
  93.             }
  94.         }
  95.         return null;
  96.     }
  97.     /**
  98.      * @return Utilisateur|UserInterface
  99.      */
  100.     public function getUser() {
  101.         return parent::getUser();
  102.     }
  103.     /**
  104.      * @param $hasPermission
  105.      */
  106.     public static function handlePermissionResponse($hasPermission) {
  107.         if (true === $hasPermission) {
  108.             return;
  109.         }
  110.  
  111.         exit("Permission denied.");
  112.     }
  113.     /**
  114.      * @param false $asJson
  115.      */
  116.     public function requireSocieteSelected($asHtml false) {
  117.         if ($this->getUser()->getSocieteSelected() !== null) {
  118.             return;
  119.         }
  120.         if (true === $asHtml) {
  121.             exit($this->render("Layout/page/500-content.html.twig", [
  122.                 "message"        => "Votre compte utilisateur doit être rattaché à un établissement.<br/><smalL>Veuillez contacter votre administrateur afin de résoudre le problème.</smalL>",
  123.                 "showButtonHome" => false,
  124.             ])->getContent());
  125.         }
  126.         header('Content-type: application/json');
  127.         exit(json_encode([
  128.             "success" => false,
  129.             "message" => "Vous devez sélectionner une structure.",
  130.         ]));
  131.     }
  132. }