<?php
namespace App\Controller\Admin\User;
use App\Entity\User\User;
use App\Form\User\UserType;
use App\Service\User\StatusManager;
use App\Service\Manager\AdminManager;
use App\Repository\User\UserRepository;
use App\Controller\Admin\CoreController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Form\Search\Custom\UserType as CustomUserType;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[Route("/admin/users")]
class UserController extends CoreController
{
#[Route('/', name: 'admin_users_index')]
public function index(Request $request,
UserRepository $repository): Response
{
$form = $this->createForm(CustomUserType::class, null, [
'method' => 'GET',
'idLangue' => $this->getCurrentLanguage(),
]);
$form->handleRequest($request);
$formParams = $form->getData() ?? [];
return $this->render('admin/user/index.html.twig', [
'lists' => $repository->paginateAllVisible(
$this->getCurrentLanguage(),
$request->query->getInt('page', 1),
$formParams,
),
'form' => $form->createView(),
'table' => $this->getTable(),
]);
}
public function getTable()
{
return [
'id' => 'u',
'fields' => [
[
'field' => 'firstName',
'order' => 'u.firstName'
],
[
'field' => 'lastname',
'order' => 'u.lastname'
],
[
'field' => 'email',
'order' => 'u.email'
],
[
'field' => 'statusName',
'order' => 'statusName',
'type' => 'status',
],
],
'actions'=> [
[
'label' => 'edit',
'icon' => 'edit',
'path' => 'admin_users_edit',
],
[
'label' => 'delete',
'type' => 'delete',
'path' => 'admin_users_delete',
]
]
];
}
#[Route('/new', name: 'admin_users_new')]
#[Route('/edit/{id}', name: 'admin_users_edit')]
public function new(
Request $request,
AdminManager $adminManager,
StatusManager $statusManager,
UserPasswordHasherInterface $passwordHasher,
User $user = null)
{
if(!$user) {
$user = new User();
$oldStatut = null;
}
else {
$oldStatut = $user->getStatus();
}
$form = $this->createForm(UserType::class, $user, [
'idLangue' => $this->getCurrentLanguage(),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** Manage Password */
if ($form->get('plainPassword')->getData()) {
$user->setPassword (
$passwordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
}
// if(isset(ProfileRole::LISTS[$user->getProfile()])) {
// $user->setRoles([ProfileRole::LISTS[$user->getProfile()]]);
// }
$adminManager->saveEntity($user);
return $this->redirectToRoute('admin_users_index');
}
return $this->render('admin/user/form.html.twig', [
'field' => $user,
'form' => $form->createView(),
]);
}
#[Route('/delete/{id}', name: 'admin_users_delete')]
public function delete(Request $request,
User $user,
AdminManager $adminManager): Response
{
if ($this->isCsrfTokenValid('delete'. $user->getId(), $request->query->get('_token'))) {
$user->setEmail($user->getEmail().uniqid());
$adminManager->deleteEntity($user);
}
return $this->redirectToRoute('admin_users_index');
}
}