<?php
/**
* Copyright (c) 2019-2022, MND Next GmbH - www.mndnext.de
*/
namespace App\Controller;
use App\Entity\Company;
use App\Entity\Employee;
use App\Entity\Machine;
use App\Entity\User;
use App\Entity\WinWOP\LicenseRequest;
use App\Entity\WinWOP\ModulesRequest;
use App\Entity\WinWOP\SmcRequest;
use App\Form\EmployeeType;
use App\Form\LicenseRequestType;
use App\Form\ModulesRequestType;
use App\Form\NewMachineType;
use App\Form\SMCRequestType;
use App\Services\MailService;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting
*/
class ProfileController extends ExtendedAbstractController
{
/** @var UserManagerInterface */
private $userManager;
/** @var TokenGeneratorInterface */
private $tokenGenerator;
/** @var MailService */
private $mailer;
private $parent;
/** @var TranslatorInterface */
private $translator;
public function __construct(\FOS\UserBundle\Controller\ProfileController $controller, UserManagerInterface $userManager, MailService $mailer, TokenGeneratorInterface $tokenGenerator, TranslatorInterface $translator)
{
$this->parent = $controller;
$this->userManager = $userManager;
$this->tokenGenerator = $tokenGenerator;
$this->mailer = $mailer;
$this->translator = $translator;
}
/**
* @Route("/profile/", methods={"GET","POST"})
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function showAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
if ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_NEW_ADMIN')) {
if (!$user->hasRole('ROLE_ADMIN')) {
if (!$user->getConfirmationToken()) {
$user->addRole('ROLE_ADMIN');
$user->removeRole('ROLE_NEW_ADMIN');
$this->userManager->updateUser($user);
}
}
return new RedirectResponse('/admin');
}
$form = null;
$employees = null;
if ($this->isGranted('ROLE_CONTACT')) {
$employees = $user->getCompany()->getEmployees();
$employee = new Employee();
$employee->setCompany($user->getCompany());
$employee->setRegisterState(USER::REGISTERED_SELF);
$form = $this->createForm(EmployeeType::class, $employee, [
//'action' => $this->generateUrl('license_request')
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$employee->setRandomSalt();
$employee->setPassword($this->tokenGenerator->generateToken());
$employee->setPasswordRequestedAt(new \DateTime());
$employee->setConfirmationToken($this->tokenGenerator->generateToken());
$employee->addRole('ROLE_NEW_EMPLOYEE');
$this->userManager->updateUser($employee);
$em = $this->getDoctrine()->getManager();
$em->persist($employee);
$em->flush();
$formData = $request->get('employee');
$this->addFlash('success', $this->translator->trans('employees.msg.success', [], 'app'));
if (array_key_exists('sendmail', $formData) && $formData['sendmail']) {
$this->mailer->sendRegisterConfirmEmployee($employee);
$this->addFlash('info', $this->translator->trans('employees.msg.mailsend', [], 'app'));
}
}
}
return $this->render('@FOSUser/Profile/show.html.twig', array(
'user' => $user,
'employees' => $employees,
'employeeForm' => ($form?$form->createView():null),
));
}
/**
* @Route("/profile/files")
*/
public function companyFiles(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_EMPLOYEE');
if ($this->isGranted('ROLE_CONTACT')) {
$files = $this->getUser()->getCompany()->getContactFiles();
} else {
$files = $this->getUser()->getCompany()->getEmployeeFiles();
}
return $this->render('profile/company_files.html.twig', ['files' => $files]);
}
/**
* @Route("/profile/licenserequest")
*/
public function licenseRequest(Request $request)
{
$machineId = $request->get('machineId');
if (!$machineId) {
$formData = $request->get('license_request');
if ($formData && array_key_exists('machine', $formData)) {
$machineId = $formData['machine'];
}
}
if ($machineId) {
$this->denyAccessUnlessGranted(['ROLE_NEW_CONTACT', 'ROLE_EMPLOYEE']);
try {
$machine = $this->getDoctrine()->getRepository(Machine::class)->find($machineId);
if ($machine->getCompany() != $this->getUser()->getCompany()) {
return new RedirectResponse('/profile/licenserequest');
}
if ($machine->getLicenseRequest()) {
return new RedirectResponse('/profile/machine/' . $machine->getId() . '?error=license_request_processing');
}
} catch (EntityNotFoundException $e) {
return new RedirectResponse('fos_user_profile_show');
}
} else {
$machine = null;
}
$machines = [];
/** @var $company Company */
if ($this->getUser()) {
$company = $this->getUser()->getCompany();
if ($company) {
$machines = $company->getMachines();
}
}
$licenseRequest = new LicenseRequest();
$licenseRequest->setMachine($machine);
$requestForm = $this->createForm(LicenseRequestType::class, $licenseRequest, ['machines' => $machines]);
$requestForm->handleRequest($request);
if ($requestForm->isSubmitted() && $requestForm->isValid()) {
if (!$machine) {
$machine = new Machine();
$machine->setName($licenseRequest->getName());
$machine->setCompany($this->getUser()->getCompany());
//$last = $this->getDoctrine()->getRepository(Machine::class)->findBy([], ['id' => 'DESC'], 1);
//$machine->getCompany()->getName() . '-' . ($last[0]->getId() + 1)
$machine->setSerial($licenseRequest->getSerial());
$machine->setControl($licenseRequest->getControl());
$machine->setManufacturerName($licenseRequest->getManufacturerName());
$licenseRequest->setMachine($machine);
} else {
$licenseRequest->setName($machine->getName());
$licenseRequest->setSerial($machine->getSerial());
$licenseRequest->setControl(($machine->getControl()?$machine->getControl():''));
$licenseRequest->setManufacturerName(($machine->getManufacturerName()?$machine->getManufacturerName():''));
}
$em = $this->getDoctrine()->getManager();
$em->persist($machine);
$em->persist($licenseRequest);
$em->flush();
$this->mailer->noticeLicenseRequest($licenseRequest, $this->getUser());
$requestSaved = true;
} else {
$requestSaved = false;
}
return $this->render('profile/license_request.html.twig', [
'machine' => $machine,
'form' => $requestForm->createView(),
'saved' => $requestSaved]
);
}
/**
* @Route("/profile/modulesrequest")
*/
public function modulesRequest(Request $request)
{
$this->denyAccessUnlessGranted(['ROLE_NEW_CONTACT', 'ROLE_EMPLOYEE']);
$machineId = $request->get('machineId');
if (!$machineId) {
$formData = $request->get('licenserequest');
if ($formData && array_key_exists('machine', $formData)) {
$machineId = $formData['machine'];
}
}
if ($machineId) {
try {
/** @var Machine $machine */
$machine = $this->getDoctrine()->getRepository(Machine::class)->find($machineId);
if ($machine->getCompany() != $this->getUser()->getCompany()) {
return new RedirectResponse('fos_user_profile_show');
}
if ($machine->getModulesRequest()) {
return new RedirectResponse('/profile/machine/' . $machine->getId() . '?error=modules_request_processing');
}
} catch (EntityNotFoundException $e) {
return new RedirectResponse('fos_user_profile_show');
}
} else {
return new RedirectResponse('fos_user_profile_show');
}
$modulesRequest = new ModulesRequest();
$modulesRequest->setMachine($machine);
$options = [];
if ($machine->getCurrentLicense()) {
$options = ['modules' => $machine->getCurrentLicensedModules()];
}
$requestForm = $this->createForm(ModulesRequestType::class, $modulesRequest, $options);
$requestForm->handleRequest($request);
if ($requestForm->isSubmitted() && $requestForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($machine);
$em->persist($modulesRequest);
$em->flush();
$this->mailer->noticeModuleLicenseRequest($modulesRequest, $this->getUser());
$requestSaved = true;
} else {
$requestSaved = false;
}
return $this->render('profile/modules_request.html.twig', [
'machine' => $machine,
'form' => $requestForm->createView(),
'saved' => $requestSaved]
);
}
/**
* @Route("/profile/smcrequest")
*/
public function smcRequest(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_NEW_CONTACT');
$machineId = $request->get('machineId');
if (!$machineId) {
$formData = $request->get('licenserequest');
if ($formData && array_key_exists('machine', $formData)) {
$machineId = $formData['machine'];
}
}
if ($machineId) {
$this->denyAccessUnlessGranted('ROLE_NEW_CONTACT');
try {
$machine = $this->getDoctrine()->getRepository(Machine::class)->find($machineId);
if ($machine->getCompany() != $this->getUser()->getCompany()) {
return new RedirectResponse('fos_user_profile_show');
}
if ($machine->getSmcRequest()) {
return new RedirectResponse('/profile/machine/' . $machine->getId() . '?error=smc_request_processing');
}
} catch (EntityNotFoundException $e) {
return new RedirectResponse('fos_user_profile_show');
}
} else {
return new RedirectResponse('fos_user_profile_show');
}
$smcRequest = new SmcRequest();
$smcRequest->setMachine($machine);
$requestForm = $this->createForm(SMCRequestType::class, $smcRequest);
$requestForm->handleRequest($request);
if ($requestForm->isSubmitted() && $requestForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($machine);
$em->persist($smcRequest);
$em->flush();
$this->mailer->noticeSmcRequest($smcRequest, $this->getUser());
$requestSaved = true;
} else {
$requestSaved = false;
}
return $this->render('profile/smc_request.html.twig', [
'machine' => $machine,
'form' => $requestForm->createView(),
'saved' => $requestSaved]
);
}
/**
* @Route("/profile/newmachine")
*/
public function newMachine(Request $request)
{
$this->denyAccessUnlessGranted(['ROLE_NEW_CONTACT', 'ROLE_EMPLOYEE']);
if (!$this->getUser()) {
return new RedirectResponse('/');
}
$machines = [];
/** @var $company Company */
$company = $this->getUser()->getCompany();
if ($company) {
$machines = $company->getMachines();
}
$machine = new Machine();
$form = $this->createForm(NewMachineType::class, $machine);
$form->handleRequest($request);
$requestSaved = false;
$duplicated = false;
if ($form->isSubmitted() && $form->isValid()) {
foreach ($machines as $oldMachine) {
if (mb_strtolower($oldMachine->getserial()) === mb_strtolower($machine->getSerial())) {
$duplicated = true;
$form->get('serial')->addError(new FormError($this->translator->trans('request.msg.duplicated', [], 'app')));
}
}
if (!$duplicated) {
$machine->setCompany($company);
$em = $this->getDoctrine()->getManager();
$em->persist($machine);
$em->flush();
//mail to admins
$this->mailer->noticeNewMachine($machine, $this->getUser());
$requestSaved = true;
}
}
return $this->render('profile/new_machine.html.twig', [
'machine' => $machine,
'form' => $form->createView(),
'saved' => $requestSaved]
);
}
public function editAction(Request $request)
{
return $this->parent->editAction($request);
}
}