<?php
namespace App\Controller\User;
use App\Entity\Partner\Partner;
use App\Entity\User\AdminUser;
use App\Manager\Rca\CapsuleManager;
use Sylius\Bundle\UserBundle\Controller\UserController as BaseUserController;
use Sylius\RbacPlugin\Entity\AdministrationRole;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class UserController extends BaseUserController
{
public function createAction(Request $request): Response
{
$oldData = $request->request->all();
if (!isset($oldData['email'])) {
$oldData = CapsuleManager::getDefaultAdminUserCapsule();
}
$entityManager = $this->container->get('doctrine.orm.entity_manager');
return $this->render('@templates/AdminCustom/AdminUser/create.html.twig', [
'roles' => $entityManager
->getRepository(AdministrationRole::class)->findAll(),
'partners' => $entityManager->getRepository(Partner::class)->findAll(),
'oldData' => $oldData,
]);
}
public function updateAction(Request $request): Response
{
$entityManager = $this->container->get('doctrine.orm.entity_manager');
return $this->render('@templates/AdminCustom/AdminUser/update.html.twig', [
'roles' => $entityManager
->getRepository(AdministrationRole::class)->findAll(),
'partners' => $entityManager->getRepository(Partner::class)->findAll(),
'admin' => $entityManager->getRepository(AdminUser::class)->find($request->get('id')),
]);
}
public function createAdmin(Request $request)
{
$data = $request->request->all();
$validator = $this->container->get('app.validator.admin_user_form')->validate($data);
if (!$validator['status']) {
$this->addFlash('error', $validator['message']);
return $this->redirectToRoute('sylius_admin_admin_user_create', ['oldData' => $data]);
}
$this->container->get('app.service.user.admin_user')->createOrUpdateAdminUser($data);
$this->addFlash('success', 'Admin adaugat cu succes!');
return $this->redirectToRoute('sylius_admin_admin_user_index');
}
public function updateAdmin(Request $request)
{
$data = $request->request->all();
$validator = $this->container->get('app.validator.admin_user_form')->validate($data);
if (!$validator['status']) {
$this->addFlash('error', $validator['message']);
return $this->redirectToRoute('sylius_admin_admin_user_create', ['oldData' => $data]);
}
$this->container->get('app.service.user.admin_user')->createOrUpdateAdminUser($data);
$this->addFlash('success', 'Admin editat cu succes!');
return $this->redirectToRoute('sylius_admin_admin_user_index');
}
}