cmtracker/src/Controller/AdminController.php

229 lines
7.8 KiB
PHP
Raw Normal View History

2024-11-29 21:46:33 -05:00
<?php
namespace App\Controller;
use App\Entity\Supervision;
2024-11-29 21:46:33 -05:00
use App\Entity\User;
use App\Form\EditUserFormType;
use App\Form\SupervisorFormType;
2024-11-29 21:46:33 -05:00
use App\Form\UserFormType;
use App\Repository\UserRepository;
2024-11-29 21:46:33 -05:00
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[IsGranted('ROLE_ADMIN')]
class AdminController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly UserPasswordHasherInterface $userPasswordHasher,
private array $navLinks = []
) {
$this->navLinks = [
'admin_dashboard' => 'nav-link text-dark',
'user_dashboard' => 'nav-link text-dark',
'profile' => 'nav-link text-dark',
'user_list' => 'nav-link text-dark',
'staff_dashboard' => 'nav-link text-dark',
2024-11-29 21:46:33 -05:00
];
}
#[Route('/admin-dashboard', name: 'app_admin_dashboard')]
public function adminDashboard(#[CurrentUser()] User $user): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$this->navLinks['admin_dashboard'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/admin/admin-dashboard.html.twig',
2024-11-29 21:46:33 -05:00
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Admin Dashboard'
],
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/list-users', name: 'app_list_users')]
public function listUsers(#[CurrentUser()] User $user): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
/** @var UserRepository $repo */
$repo = $this->entityManager->getRepository(User::class);
$users = $repo->getCompanyUsers($user->getCompany());
/** @var SupervisionRepository $supRepo */
$supRepo = $this->entityManager->getRepository(Supervision::class);
foreach ($users as $idx => $user) {
$supervisor = $supRepo->getSupervisorByWorker($user);
$users[$idx]->setSupervisor($supervisor);
}
2024-11-29 21:46:33 -05:00
$this->navLinks['user_list'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/admin/list-users.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'User List'
],
'users' => $users,
'notifications' => $user->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/add-user', name: 'app_add_user')]
public function addUser(Request $request, #[CurrentUser()] User $admin): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$user = new User();
$form = $this->createForm(UserFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$plainPassword = $form->get('password')->getData();
$user->setUsername($form->get('username')->getData());
$user->setPassword(
$this->userPasswordHasher->hashPassword(
$user,
$plainPassword
)
);
$user->setRoles(['ROLE_USER']);
$user->setName($form->get('name')->getData());
$user->setEmail($form->get('email')->getData());
$user->setJob($form->get('job')->getData());
$user->setRateType($form->get('rateType')->getData());
$user->setRate($form->get('rate')->getData());
$user->setLevel($form->get('level')->getData());
$user->setCompany($admin->getCompany());
$this->entityManager->persist($user);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_users');
}
$this->navLinks['user_list'] = 'nav-link text-white active bg-gradient-dark';
return $this->render(
'internal/admin/add-user.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Add User'
],
'form' => $form,
'notifications' => $admin->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/edit-user/{id}', name: 'app_edit_user')]
public function editUser(string $id, Request $request, #[CurrentUser()] User $admin): Response
{
/** @var UserRepository $userRepo */
$userRepo = $this->entityManager->getRepository(User::class);
2024-11-29 21:46:33 -05:00
/** @var User $user */
$user = $userRepo->find($id);
2024-11-29 21:46:33 -05:00
$form = $this->createForm(EditUserFormType::class, $user);
2024-11-29 21:46:33 -05:00
$form->handleRequest($request);
$this->navLinks['user_list'] = 'nav-link text-white active bg-gradient-dark';
if ($form->isSubmitted() && $form->isValid()) {
$user->setName($form->get('name')->getData())
->setEmail($form->get('email')->getData())
->setJob($form->get('job')->getData())
->setRateType($form->get('rateType')->getData())
->setRate($form->get('rate')->getData())
->setLevel($form->get('level')->getData());
$this->entityManager->persist($user);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_users');
}
return $this->render(
'internal/admin/edit-user.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Edit User'
],
'data' => $user,
'form' => $form,
'notifications' => $admin->retrieveUnreadNotifications(),
]
)
);
}
#[Route('/assign-supervisor/{id}', name: 'app_assign_supervisor')]
public function assignSupervisor(string $id, Request $request, #[CurrentUser()] User $admin): Response
{
/** @var UserRepository $userRepo */
$userRepo = $this->entityManager->getRepository(User::class);
/** @var User $user */
$user = $userRepo->find($id);
$form = $this->createForm(SupervisorFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$supervisor = $form->get('supervisor')->getData();
$sup = new Supervision();
$sup->setSupervisor($supervisor);
$sup->setWorker($user);
$this->entityManager->persist($sup);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_users');
}
return $this->render(
'internal/admin/assign-supervisor.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [
'Assign Supervisor'
],
'data' => $user,
'form' => $form,
'supervisors' => $userRepo->getCaseManagers($admin->getCompany()),
'notifications' => $admin->retrieveUnreadNotifications(),
]
)
);
}
2024-11-29 21:46:33 -05:00
}