<?php
namespace App\Daikin\BaseBundle\Controller;
use App\Daikin\BaseBundle\Entity\Company;
use App\Daikin\BaseBundle\Entity\Staticpages;
use App\Service\Security\UserCredentialsService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Daikin\BaseBundle\Entity\User;
use App\Daikin\BaseBundle\Entity\Logs;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use App\Daikin\BaseBundle\Form\UserType;
use App\Daikin\BaseBundle\Form\CompanyAdminType;
class DefaultController extends AbstractDaikinBaseController
{
/**
* IndexAction
*
* @Route("/", name="homepage")
*/
public function indexAction()
{
$this->init();
//Company Administrator
if ($this->authChecker->isGranted('ROLE_ADMIN_COMPANY')) {
return $this->redirect($this->generateUrl('frontend_home'));
}
// Stadtbote Eingangsverwaltung Administrator
if ($this->authChecker->isGranted('ROLE_SPARE_PARTS_WAREHOUSE')) {
return $this->redirect($this->generateUrl('spare_parts_warehouse_admin'));
}
//Editor
if ($this->authChecker->isGranted('ROLE_EDITOR')) {
// return $this->redirect($this->generateUrl('editor_home'));
return $this->redirect($this->generateUrl('frontend_home'));
}
//Admin
if ($this->authChecker->isGranted('ROLE_ADMIN')) {
// return $this->redirect($this->generateUrl('admin_home'));
return $this->redirect($this->generateUrl('frontend_home'));
}
if ($this->authChecker->isGranted('ROLE_HELPDESK_READER') &&
!$this->authChecker->isGranted('ROLE_HELPDESK_WRITER') &&
!$this->authChecker->isGranted('ROLE_APP_USER')) {
return $this->redirect($this->generateUrl('frontend_home'));
}
// Stadtbote Eingangsverwaltung Administrator
if ($this->authChecker->isGranted('ROLE_SPARE_PARTS_FOR_CUSTOMER_ORDER')) {
return $this->redirect($this->generateUrl('spare_part_order_new'));
}
if ($this->authChecker->isGranted('ROLE_APP_USER')) {
return $this->redirect($this->generateUrl('frontend_home'));
}
return $this->render('@DaikinBase/default/index.html.twig');
}
/**
* Myprofile
*
* @Route("/my_profile", name="_my_profile")
* @param Request $request
* @param UserCredentialsService $credentialsService
* @return array|RedirectResponse
* @Template()
*/
public function myProfileAction(Request $request, UserCredentialsService $credentialsService)
{
$user = $this->em->getRepository(User::class)
->find($this->getUser()->getId());
$form = $this->createForm(UserType::class, $user, [
'change_password' => true,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($user->getPPassword()) {
$credentialsService->updatePasswordForUser($user, $user->getPPassword());
}
$logs = new Logs($user->getId(), 'User', 'Update profile information.');
$this->em->persist($logs);
$this->em->persist($user);
$this->em->flush();
$this->addFlashMessage('Profile successfully changed!');
return $this->redirectToRoute('homepage');
}
if ($request->getMethod() == 'POST') {
$this->addFlashMessage('Profile was not changed!');
}
return [
'form'=>$form->createView(),
'user' => $user
];
}
/**
* Company profile
*
* @Route("/my_company", name="_my_company")
* @Template()
* @param Request $request
* @return array|RedirectResponse
*/
public function myCompanyAction(Request $request)
{
$user = $this->getUser();
/** @var Company $company */
$company = $user->getCompany();
$company->setAddMissingPlusPhone(false);
$form = $this->createForm(CompanyAdminType::class, $company);
$form->handleRequest($request);
$may_edit = false;
if ($user->hasRole(array( 'ROLE_ADMIN', 'ROLE_ADMIN_COMPANY'))) {
if ($form->isSubmitted() && $form->isValid()) {
$log = new Logs($user->getId(),'Company','Update company information.');
$this->em->persist( $log );
$this->em->persist( $form->getData() );
$this->em->flush();
$this->addFlashMessage('Company information updated.');
return $this->redirect($this->generateUrl('_my_company'));
}
$may_edit = true;
}
return [
'form' => $form->createView(),
'company' => $company,
'may_edit' => $may_edit,
];
}
public function pagesmenuAction(Request $request, $v2 = false): Response
{
$locale = $request->getLocale() ?: 'en';
$pg = $this->em->getRepository(Staticpages::class)->findBy([
'lang' => $locale,
'showInFooterMenu' => true
]);
return $this->render('@DaikinBase/default/pagesmenu.html.twig', ['page' => $pg, 'v2' => $v2]);
}
/**
* @Route("/page/{alias}", name="staticpages")
* @param Request $request
* @param $alias
* @return array
* @Template()
*/
public function staticpagesAction(Request $request, $alias)
{
$locale = $request->getLocale() ?: 'en';
/** @var Staticpages $pg */
$pg = $this->em->getRepository(Staticpages::class)->findOneBy(['alias'=>$alias,'lang'=>$locale]);
if ($pg instanceof Staticpages) {
if ($pg->isShowDescriptionOnly()) {
die($pg->getDescription());
}
} else {
$pg = array(
'name' => 'No page',
'description' => 'Page not found.'
);
}
return [
'page' => $pg,
];
}
}