src/Controller/HomeController.php line 32

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Hook;
  4. use App\Repository\HookRepository;
  5. use App\Repository\ModuleRepository;
  6. use App\Repository\ParameterRepository;
  7. use App\Repository\PriceRepository;
  8. use App\Repository\RoleRepository;
  9. use App\Repository\WidgetRepository;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Exception;
  12. use ReflectionClass;
  13. use ReflectionException;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Mailer\Bridge\Google\Transport\GmailSmtpTransport;
  16. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Mailer\Transport\TransportInterface;
  19. use Symfony\Component\Mime\Email;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. class HomeController extends AbstractController
  23. {
  24.     /**
  25.      * @throws ReflectionException|TransportExceptionInterface
  26.      * @throws Exception
  27.      */
  28.     #[Route('/'name'app_home')]
  29.     public function index(HookRepository $hookRepositoryWidgetRepository $widgetRepositoryManagerRegistry $registryPriceRepository $priceRepositoryParameterRepository $parameterRepository): Response
  30.     {
  31.         if ($this->container->has('security.token_storage')) {
  32.             $current_user $this->getUser();
  33.             if (!$current_user) {
  34.                 return $this->redirectToRoute('app_login');
  35.             }
  36.         } else {
  37.             return $this->redirectToRoute('app_login');
  38.         }
  39.         $moduleRepo = new ModuleRepository($registry);
  40.         $header_widgets $widgetRepository->findByHook($hookRepository->findOneBy(['alias' => 'HEADER']));
  41.         $home_widgets = [];
  42.         foreach ($widgetRepository->findByHook($hookRepository->findOneBy(['alias' => 'HOME_PAGE'])) as $w) {
  43.             $elm = [
  44.                 'libelle' => $w->getLibelle(),
  45.                 'alias' => $w->getAlias(),
  46.                 'hook' => $w->getHook(),
  47.                 'mode' => $w->getMode(),
  48.                 'iconColor' => $w->getIconColor(),
  49.                 'bgColor' => $w->getBgColor(),
  50.                 'textColor' => $w->getTextColor(),
  51.                 'isActivated' => $w->isIsActivated()
  52.             ];
  53.             $elm["module"] = "";
  54.             $elm["moduleName"] = "";
  55.             $elm["icon"] = "";
  56.             $elm["classRepoName"] = "";
  57.             $elm["elements"] = "";
  58.             $elm["elementsCount"] = "";
  59.             $elm["elementsEnableCount"] = "";
  60.             $elm["elementsDisableCount"] = "";
  61.             $elm["elementsRate"] = "";
  62.             if ($w->getModule() != "") {
  63.                 if ($w->getMode() == 'ed') {
  64.                     $classRepoName 'App\Repository\\' ucfirst($w->getModule()->getNameInDb()) . 'Repository';
  65.                     $ref = new ReflectionClass($classRepoName);
  66.                     $classRepoObject $ref->newInstanceArgs([$registry]);
  67.                     $elements $classRepoObject->findAll();
  68.                     $elements_enable $classRepoObject->findBy(['isActivated' => true]);
  69.                     $elements_disable $classRepoObject->findBy(['isActivated' => false]);
  70.                     $elm["module"] = $w->getModule()->getLibelle();
  71.                     $elm["moduleName"] = $w->getModule()->getNameInDb();
  72.                     $elm["icon"] = $w->getModule()->getIcon();
  73.                     $elm["classRepoName"] = $classRepoName;
  74.                     $elm["elements"] = $elements;
  75.                     $elm["elementsCount"] = count($elements);
  76.                     $elm["elementsEnableCount"] = count($elements_enable);
  77.                     $elm["elementsDisableCount"] = count($elements_disable);
  78.                     $rate count($elements) > floatval((count($elements_enable) / (count($elements_enable) + count($elements_disable))) * 100) : 0;
  79.                     $elm["elementsRate"] = number_format($rate2);
  80.                 } elseif ($w->getMode() == 'listing') {
  81.                     $classRepoName 'App\Repository\\' ucfirst($w->getModule()->getNameInDb()) . 'Repository';
  82.                     $ref = new ReflectionClass($classRepoName);
  83.                     $classRepoObject $ref->newInstanceArgs([$registry]);
  84.                     $className 'App\Entity\\' ucfirst($w->getModule()->getNameInDb());
  85.                     $ref = new ReflectionClass($className);
  86.                     $classObject $ref->newInstanceArgs();
  87.                     $elements $classRepoObject->findBy(['isActivated' => true], null5);
  88.                     $form $this->createForm('App\Form\\' ucfirst($w->getModule()->getNameInDb()) . 'Type'$classObject);
  89.                     $fieldNames = ['#'];
  90.                     $attributes = ['id'];
  91.                     foreach ($form->all() as $column) {
  92.                         if ($column->getName() != "password") {
  93.                             $fieldNames[] = $column->getConfig()->getOptions()['label'];
  94.                             $attributes[] = $column->getName();
  95.                         }
  96.                     }
  97.                     $elm["module"] = $w->getModule()->getLibelle();
  98.                     $elm["moduleName"] = $w->getModule()->getNameInDb();
  99.                     $elm["icon"] = $w->getModule()->getIcon();
  100.                     $elm["classRepoName"] = $classRepoName;
  101.                     $elm["fieldNames"] = $fieldNames;
  102.                     $elm["attributes"] = $attributes;
  103.                     $elm["elements"] = $elements;
  104.                 } elseif ($w->getMode() == 'shortcut') {
  105.                     $elm["module"] = $w->getModule()->getLibelle();
  106.                     $elm["moduleName"] = $w->getModule()->getNameInDb();
  107.                     $elm["icon"] = $w->getModule()->getIcon();
  108.                 }
  109.             }
  110.             $home_widgets[] = $elm;
  111.         }
  112.         $recent_activities = [];
  113.         $parameter $parameterRepository->findOneByAlias(['alias' => 'DAY_LIMIT_FOR_LISTING']);
  114.         $prices $priceRepository->getPriceGreaterThan($parameter->getValue(), null);
  115.         $limit intval($parameterRepository->findOneByAlias(['alias' => 'RECENT_ACTIVITIES_LIMIT'])->getValue());
  116.         $i 0;
  117.         foreach ($prices as $price) {
  118.             if ($i $limit) {
  119.                 $elm = [];
  120.                 $elm["status"] = $price->getPriceStatus();
  121.                 $elm["gasStation"] = $price->getGasStation();
  122.                 $elm["priceRows"] = $price->getPriceRows();
  123.                 $elm["appliedAt"] = $price->getAppliedAt();
  124.                 $recent_activities[] = $elm;
  125.                 $i++;
  126.             }
  127.         }
  128.         return $this->render('home/index.html.twig', [
  129.             'root_uri' => 'Accueil',
  130.             'title' => 'Tableau de bord',
  131.             'module_mod_menu' => $moduleRepo->findByParam("isModule"),
  132.             'dictionnary_mod_menu' => $moduleRepo->findByParam("isDictionnary"),
  133.             'parameter_mod_menu' => $moduleRepo->findByParam("isParameter"),
  134.             'user' => $current_user,
  135.             'user_role' => $current_user->getRole(),
  136.             'header_widgets' => $header_widgets,
  137.             'home_widgets' => $home_widgets,
  138.             'recent_activities' => $recent_activities,
  139.         ]);
  140.     }
  141. }