src/Controller/App/CompanyController.php line 112
<?php
namespace App\Controller\App;
use App\Entity\ExpenseCategory;
use App\Entity\Section;
use App\Entity\Service;
use DateTime;
use App\Entity\Season;
use App\Entity\Company;
use App\Form\CompanyType;
use App\Form\NewCompanyType;
use App\Service\FileUploader;
use App\Form\CompanyTypeAddress;
use App\Form\CompanyTypeBasicInfos;
use App\Form\CompanyTypeLocalization;
use App\Form\CompanyTypeOtherDetails;
use App\Form\CompanyTypeSocialMedias;
use App\Repository\CompanyRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route('/app/company')]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
class CompanyController extends AbstractController
{
public function __construct(private TranslatorInterface $translator)
{
}
#[Route('/', name: 'app_company_index', methods: ['GET'])]
#[IsGranted('ROLE_SUPER_ADMIN')]
public function index(EntityManagerInterface $entityManager): Response
{
$companies = $entityManager
->getRepository(Company::class)
->findAll();
return $this->render('app/company/index.html.twig', [
'companies' => $companies,
]);
}
#[Route('/{id}/my', name: 'app_company_my', methods: ['GET', 'POST'])]
public function my(Company $company, Request $request, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$formBasicInfos = $this->createForm(CompanyTypeBasicInfos::class, $company);
$formBasicInfos->handleRequest($request);
if ($formBasicInfos->isSubmitted() && $formBasicInfos->isValid()) {
$logo = $formBasicInfos->get('logo')->getData();
if ($logo) {
$uploadedFileName = $fileUploader->upload($logo, 'companies_dir');
$company->setLogo($uploadedFileName);
}
$entityManager->persist($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
}
$formSocilaMedias = $this->createForm(CompanyTypeSocialMedias::class, $company);
$formSocilaMedias->handleRequest($request);
if ($formSocilaMedias->isSubmitted() && $formSocilaMedias->isValid()) {
$entityManager->persist($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
}
$formAddress = $this->createForm(CompanyTypeAddress::class, $company);
$formAddress->handleRequest($request);
if ($formAddress->isSubmitted() && $formAddress->isValid()) {
$entityManager->persist($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
}
$formOtherDetails = $this->createForm(CompanyTypeOtherDetails::class, $company);
$formOtherDetails->handleRequest($request);
if ($formOtherDetails->isSubmitted() && $formOtherDetails->isValid()) {
$entityManager->persist($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
}
return $this->render('app/company/my.html.twig', [
'company' => $company,
'form_basic_infos' => $formBasicInfos->createView(),
'form_social_medias' => $formSocilaMedias->createView(),
'form_address' => $formAddress->createView(),
'form_other_details' => $formOtherDetails->createView(),
]);
}
#[Route('/{id}/localization', name: 'app_company_localization', methods: ['GET', 'POST'])]
public function localization(Company $company, Request $request, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$form = $this->createForm(CompanyTypeLocalization::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_company_localization', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
}
return $this->render('app/company/localization.html.twig', [
'company' => $company,
'form' => $form->createView()
]);
}
#[Route('/new', name: 'app_company_new', methods: ['GET', 'POST'])]
#[IsGranted('ROLE_SUPER_ADMIN')]
public function new(Request $request, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$company = new Company();
$form = $this->createForm(CompanyType::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$company->addUser($this->getUser());
$logo = $form->get('logo')->getData();
if ($logo) {
$uploadedFileName = $fileUploader->upload($logo, 'companies_dir');
$company->setLogo($uploadedFileName);
}
$entityManager->persist($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
return $this->redirectToRoute('app_company_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('app/company/new.html.twig', [
'company' => $company,
'form' => $form,
]);
}
#[Route('/new/company', name: 'app_company_new_company', methods: ['GET', 'POST'])]
public function new_company(Request $request, Security $security, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$company = new Company();
$form = $this->createForm(NewCompanyType::class, $company);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $user App\Entity\User */
$user = $this->getUser();
$user->setCompany($company);
$user->addRole('ROLE_OWNER');
$company->addUser($this->getUser());
$season = new Season();
$season->setName('Season ' . date("Y"))
->setDescription('Season par defaut')
->setStartDate(new DateTime())
->setIsActive(true)
->setEndDate(new DateTime('+1 years'))->setIsActive(true);
$season->setOwner($company);
$entityManager->persist($season);
$entityManager->persist($company);
$entityManager->persist($user);
$category = new ExpenseCategory();
$category
->setName('Autre')
->setDescription('Objets divers')
->setColor('#8a0da0')
->setOwner($company);
$entityManager->persist($category);
$section = new Section();
$section
->setName('Section-1')
->setDescription('Section par defaut')
->setAgeRange('4 ans et plus')
->setOwner($company);
$entityManager->persist($section);
$service = new Service();
$service
->setName('Guard occasionnel')
->setDescription('Service par defaut')
->setPrice(1000)
->setOwner($company);
$entityManager->persist($service);
$entityManager->flush();
$security->login($user, 'security.authenticator.form_login.main');
$this->addFlash('success', $this->translator->trans('Succès !!'));
// TODO:: ask to confirm email page
return $this->redirectToRoute('app_main');
}
return $this->renderForm('auth/registration/business.html.twig', [
'company' => $company,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_company_show', methods: ['GET'])]
public function show(Company $company): Response
{
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
// return $this->render('app/company/show.html.twig', [
// 'company' => $company,
// ]);
}
#[Route('/{id}/edit', name: 'app_company_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Company $company, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
// $form = $this->createForm(CompanyType::class, $company);
// $form->handleRequest($request);
// if ($form->isSubmitted() && $form->isValid()) {
// $logo = $form->get('logo')->getData();
// if ($logo) {
// $uploadedFileName = $fileUploader->upload($logo, 'companies_dir');
// $company->setLogo($uploadedFileName);
// }
// $entityManager->flush();
// $this->addFlash('success', $this->translator->trans('Succès !!'));
// return $this->redirectToRoute('app_company_index', [], Response::HTTP_SEE_OTHER);
// }
// return $this->renderForm('app/company/edit.html.twig', [
// 'company' => $company,
// 'form' => $form,
// ]);
}
#[Route('/{id}', name: 'app_company_delete', methods: ['POST'])]
public function delete(Request $request, Company $company, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete' . $company->getId(), $request->request->get('_token'))) {
try {
$entityManager->remove($company);
$entityManager->flush();
$this->addFlash('success', $this->translator->trans('Succès !!'));
} catch (\Exception $e) {
$errorMessage = $e->getMessage();
$result = explode(':', $errorMessage);
$this->addFlash('error', $result[0]);
}
}
return $this->redirectToRoute('app_company_index', [], Response::HTTP_SEE_OTHER);
}
#[Route('/online/registration/{id}', name: 'app_company_online_registration', methods: ['GET'])]
public function company_online_subscription(Request $request, Company $company): Response
{
$url = $request->getSchemeAndHttpHost() . $this->generateUrl('front_online_registration', ['company' => $company->getId()]);
return $this->render('app/company/online-registration.html.twig', [
'company' => $company,
'url' => $url
]);
}
}