<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace App\Controller;
use App\Entity\User;
use App\Repository\ContextRepository;
use App\Services\BluueLogo;
use App\Repository\UserRepository;
use App\Event\DashboardWidgetsEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class DashboardController extends AbstractController
{
/**
* @param EventDispatcherInterface $eventDispatcher
* @param RequestStack $requestStack
* @param UserRepository $userRepo
* @param EntityManagerInterface $em
* @param ContextRepository $contextRepo
* @return Response
* @Route("/", name="app_dashboard", host="%app.default_host%")
*/
public function index(
EventDispatcherInterface $eventDispatcher,
RequestStack $requestStack,
UserRepository $userRepo,
EntityManagerInterface $em,
ContextRepository $contextRepo
): Response {
$request = $requestStack->getMainRequest();
$query = $request->query;
/** @var ?User $user */
$user = $this->getUser();
$userConfig = $user->getConfig();
$settings = $userConfig['core']['dashboard'] ?? [
'dateBegin' => null,
'dateEnd' => null
];
$dateNoFilter = $query->get('date_no_filter');
$userNoFilter = $query->get('user_no_filter');
$dateBegin = $query->get('date_begin');
if (!$dateBegin && !$dateNoFilter) {
$dateBegin = $settings['dateBegin'];
$query->set('date_begin', $dateBegin);
} else {
$settings['dateBegin'] = $dateBegin;
}
$dateEnd = $query->get('date_end');
if (!$dateEnd && !$dateNoFilter) {
$dateEnd = $settings['dateEnd'];
$query->set('date_end', $dateEnd);
} else {
$settings['dateEnd'] = $dateEnd;
}
$usersFilter = [];
if (!$userNoFilter) {
foreach ($query->all() as $key => $el) {
if (str_starts_with($key, 'user_id')) {
$usersFilter[] = $el;
}
}
if (empty($usersFilter)) {
foreach ($settings as $key => $setting) {
if (str_starts_with($key, 'user_id')) {
$query->set($key, $setting);
}
}
}
$usersFilter = [];
foreach ($query->all() as $key => $el) {
if (str_starts_with($key, 'user_id')) {
$usersFilter[] = $el;
}
}
foreach ($settings as $key => $setting) {
if (str_starts_with($key, 'user_id')) {
unset($settings[$key]);
}
}
foreach ($query->all() as $key => $el) {
if (str_starts_with($key, 'user_id')) {
$settings[$key] = $el;
}
}
}
$contextId = null;
foreach ($query->all() as $key => $el) {
if (str_starts_with($key, 'contextId')) {
$settings[$key] = $el;
$contextId = $el;
}
}
$userConfig['core']['dashboard'] = $settings;
$user->setConfig($userConfig);
$em->flush();
if ($dateBegin) {
$dateRange = date("d-m-Y", strtotime($dateBegin)) . ' - ' . date("d-m-Y", strtotime($dateEnd));
} else {
$dateRange = null;
}
$dashboardWidgetsEvent = new DashboardWidgetsEvent([]);
$eventDispatcher->dispatch(
$dashboardWidgetsEvent,
DashboardWidgetsEvent::NAME
);
$widgets = $dashboardWidgetsEvent->getWidgets();
return $this->render(
'dashboard/index.html.twig',
[
'widgets' => $widgets,
'users' => $userRepo->findAllActive(),
'usersFilter' => $usersFilter,
'dateRange' => $dateRange,
'userNote' => $user->getNote(),
'contexts' => $contextRepo->findByCurrentUser(),
'contextId' => $contextId,
]
);
}
/**
* @param BluueLogo $bluueLogo
* @param string $name
* @param string|null $contextId
* @return Response
* @Route("/display_bluue_logo/{name}/{contextId}", name="app_display_bluue_logo")
*/
public function displayBluueLogo(BluueLogo $bluueLogo, string $name, ?string $contextId = null): Response
{
$bluueLogo->searchFile($name, false, true, false, $contextId);
die;
}
/**
* @param SessionInterface $session
* @param string $collapsed
* @return JsonResponse
* @Route("/change_menu_collapsed/{collapsed}", name="change_menu_collapsed")
*/
public function changeMenuCollapsed(
SessionInterface $session,
string $collapsed
): JsonResponse {
$session->set('_app_menu_collapsed', $collapsed);
return new JsonResponse(['result' => true]);
}
}