<?php
namespace App\Controller;
use App\Entity\Employee;
use App\Entity\Enterprise;
use App\Entity\Reporting;
use App\Entity\Town;
use App\Repository\CommuneRepository;
use App\Repository\DepartmentRepository;
use App\Repository\EmployeeRepository;
use App\Repository\EnterpriseRepository;
use App\Repository\ReportingRepository;
use App\Repository\ReportingTableRepository;
use App\Repository\TownRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use JMS\Serializer\SerializerInterface;
class DefaultController extends AbstractController
{
public const ID_MENU = 1;
public const TABLE = 'table';
public const GRAPHIC = 'graphic';
#[Route('/', name: 'app_default')]
public function index(
Request $request,
ReportingRepository $reportingRepository,
ReportingTableRepository $reportingTableRepository,
UserRepository $userRepository,
EnterpriseRepository $enterpriseRepository,
EmployeeRepository $employeeRepository
): Response {
$id = $request->get('id') ?? 0;
$lastReporting = $reportingRepository->findOneBy(['state' => 0], ['id' => 'DESC']);
$numInitiated = $enterpriseRepository->countFolderByState($this->getUser(), $lastReporting);
$numValidate = $enterpriseRepository->countFolderByState($this->getUser(), $lastReporting, Enterprise::VALIDATED_STATUS);
$numRejected = $enterpriseRepository->countFolderByState($this->getUser(), $lastReporting, Enterprise::REJECTED_STATUS);
$numSubmited = $enterpriseRepository->countFolderByState($this->getUser(), $lastReporting, Enterprise::SUBMITED_STATUS);
return $this->render('default/index.html.twig', [
'numInitiated' => $numInitiated,
'numValidated' => $numValidate,
'numSubmited' => $numSubmited,
'numRejected' => $numRejected,
'reportings' => $reportingRepository->findAll(),
'tables' => $reportingTableRepository->findByType(self::TABLE),
'graphics' => $reportingTableRepository->findByType(self::GRAPHIC),
'reporting' => $reportingRepository->find($id),
'lastReporting' => $lastReporting,
'nbUsers' => sizeof($userRepository->findAll()),
'stats' => $lastReporting ? $reportingRepository->findStatsByTown($lastReporting->getId()) : [],
'nbEnterprises' => $enterpriseRepository->findBy([
'reporting' => $lastReporting,
'state' => Enterprise::VALIDATED_STATUS
]),
'nbEmployees' => $employeeRepository->findEmployees($lastReporting->getId(), null, null)
]);
}
#[Route('/departments', name: 'department_list', methods: ['GET'])]
public function listDepartment(
Request $request,
SerializerInterface $serializerInterface,
TownRepository $townRepository,
DepartmentRepository $repos
): Response {
$town = $townRepository->find($request->get('town'));
return new Response(
$serializerInterface->serialize($repos->findByTown($town), 'json'),
200,
['Content-Type' => 'application/json']
);
}
#[Route('/employee/{id}/{name}', name: 'employee_info', methods: ['GET'])]
public function employee(
Enterprise $enterprise,
SerializerInterface $serializerInterface,
string $name,
EmployeeRepository $employeeRepository
): Response {
$employee = $employeeRepository->findByName($name, $enterprise->getId());
if ($employee) {
$employee->getNom();
}
return new Response(
$serializerInterface->serialize($employee, 'json'),
200,
['Content-Type' => 'application/json']
);
}
#[Route('/communes', name: 'commune_list', methods: ['GET'])]
public function listCommune(
Request $request,
SerializerInterface $serializerInterface,
DepartmentRepository $departmentRepository,
CommuneRepository $repos
): Response {
$dep = $departmentRepository->find($request->get('department'));
return new Response(
$serializerInterface->serialize($repos->findByDepartment($dep), 'json'),
200,
['Content-Type' => 'application/json']
);
}
}