<?php
namespace App\Controller;
use App\Entity\EventData;
use App\Entity\Farm;
use App\Entity\Animal;
use App\Repository\EventDataRepository;
use App\Repository\FarmRepository;
use App\Repository\OrganizationRepository;
use App\Service\DashboardService;
use App\Service\EventDataService;
use App\Repository\LotRepository;
use App\Repository\TreatmentRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use App\Controller\Common\MetadataBuilder;
use Symfony\Contracts\Translation\TranslatorInterface;
class DashboardController extends AbstractController
{
use MetadataBuilder;
private $security;
private TranslatorInterface $translator;
public function __construct(Security $security, TranslatorInterface $translator )
{
$this->security = $security;
$this->translator = $translator;
}
/**
* @Route("/", name="app_home")
*/
public function home(): Response
{/*{{{*/
$route_name = ( $this->isGranted('ROLE_ADMIN') ) ? 'organization_index' : 'app_dashboard';
return $this->redirectToRoute($route_name);
}/*}}}*/
/**
* @Route("/dashboard", name="app_dashboard")
*/
public function index(
EventDataRepository $eventDataRepository,
OrganizationRepository $organizationRepository,
FarmRepository $farmRepository,
LotRepository $lotRepository,
Request $request
): Response
{/*{{{*/
$organization = null;
$organizationId = null;
$addressList = [];
$listFarm = [];
$userAux = null;
/* user login */
$animalRepository = $this->getDoctrine()->getRepository(Animal::class);
if ( ! $this->isGranted('ROLE_ADMIN') ) {
$this->isGranted('ROLE_MANAGER') or $userAux = $this->getUser();
$organization = $this->getUser()->getOrganization();
$organizationId = $organization->getId();
}
$listFarm = $this->getDoctrine()->getRepository(Farm::class)->getListQuery( $organizationId )->getQuery()->getResult();
// dd( $listFarm );
foreach ($listFarm as $farm) {
if ( isset($farm['latitude']) and isset($farm['longitude']) ) {
$addressList[] = [
'name' => $farm['name'],
'farmId' => $farm['id'],
'position' => [
'lat' => $farm['latitude'],
'lng' => $farm['longitude']
]];
}
}
$lots = null;
$stats = $organizationRepository->getStatsQuery( $organizationId )->getQuery()->getSingleResult();
// dd( $stats );
$stockMatrix = $animalRepository->getAnimalsByCategory($organizationId);
$allData = [
'stats' => $stats,
'stockMatrix' => $stockMatrix,
'listFarm' => $listFarm,
'addressList' => $addressList,
'gridConfig' => [
'farm' => $this->getGridConfig($farmRepository, $farmRepository->getListQuery($organizationId), 'farm-dashboard'),
'event_data' => $this->getGridConfig($eventDataRepository, $eventDataRepository->getListQuery($organizationId), 'event_data-dashboard'),
'lot' => $this->getGridConfig($lotRepository, $lotRepository->getListQuery($organizationId), 'lot-dashboard')
]
];
// dd( $allData );
$request->get('dd') and dd( $allData );
return $this->render('dashboard/index.html.twig', $allData );
}/*}}}*/
/**
* @Route("/ajax/dashboardData", name="ajax_get_dashboard_data")
*/
public function getDashboardData(Request $request, DashboardService $dashboardService)
{/*{{{*/
$user = null;
if ( ! $this->isGranted('ROLE_ADMIN') ) {
$user = $this->getUser();
}
$filters = $request->query->get('filters');
$dashboardData = $dashboardService->getDashboardData($user, $filters);
return $this->json($dashboardData);
}/*}}}*/
}