src/Controller/App/CompanyController.php line 52

  1. <?php
  2. namespace App\Controller\App;
  3. use App\Entity\ExpenseCategory;
  4. use App\Entity\Section;
  5. use App\Entity\Service;
  6. use DateTime;
  7. use App\Entity\Season;
  8. use App\Entity\Company;
  9. use App\Form\CompanyType;
  10. use App\Form\NewCompanyType;
  11. use App\Service\FileUploader;
  12. use App\Form\CompanyTypeAddress;
  13. use App\Form\CompanyTypeBasicInfos;
  14. use App\Form\CompanyTypeLocalization;
  15. use App\Form\CompanyTypeOtherDetails;
  16. use App\Form\CompanyTypeSocialMedias;
  17. use App\Repository\CompanyRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Bundle\SecurityBundle\Security;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. #[Route('/app/company')]
  27. #[IsGranted('IS_AUTHENTICATED_FULLY')]
  28. class CompanyController extends AbstractController
  29. {
  30.     public function __construct(private TranslatorInterface $translator)
  31.     {
  32.     }
  33.     #[Route('/'name'app_company_index'methods: ['GET'])]
  34.     #[IsGranted('ROLE_SUPER_ADMIN')]
  35.     public function index(EntityManagerInterface $entityManager): Response
  36.     {
  37.         $companies $entityManager
  38.             ->getRepository(Company::class)
  39.             ->findAll();
  40.         return $this->render('app/company/index.html.twig', [
  41.             'companies' => $companies,
  42.         ]);
  43.     }
  44.     #[Route('/{id}/my'name'app_company_my'methods: ['GET''POST'])]
  45.     public function my(Company $companyRequest $requestEntityManagerInterface $entityManagerFileUploader $fileUploader): Response
  46.     {
  47.         $formBasicInfos $this->createForm(CompanyTypeBasicInfos::class, $company);
  48.         $formBasicInfos->handleRequest($request);
  49.         if ($formBasicInfos->isSubmitted() && $formBasicInfos->isValid()) {
  50.             $logo $formBasicInfos->get('logo')->getData();
  51.             if ($logo) {
  52.                 $uploadedFileName $fileUploader->upload($logo'companies_dir');
  53.                 $company->setLogo($uploadedFileName);
  54.             }
  55.             $entityManager->persist($company);
  56.             $entityManager->flush();
  57.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  58.             return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
  59.         }
  60.         $formSocilaMedias $this->createForm(CompanyTypeSocialMedias::class, $company);
  61.         $formSocilaMedias->handleRequest($request);
  62.         if ($formSocilaMedias->isSubmitted() && $formSocilaMedias->isValid()) {
  63.             $entityManager->persist($company);
  64.             $entityManager->flush();
  65.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  66.             return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
  67.         }
  68.         $formAddress $this->createForm(CompanyTypeAddress::class, $company);
  69.         $formAddress->handleRequest($request);
  70.         if ($formAddress->isSubmitted() && $formAddress->isValid()) {
  71.             $entityManager->persist($company);
  72.             $entityManager->flush();
  73.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  74.             return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
  75.         }
  76.         $formOtherDetails $this->createForm(CompanyTypeOtherDetails::class, $company);
  77.         $formOtherDetails->handleRequest($request);
  78.         if ($formOtherDetails->isSubmitted() && $formOtherDetails->isValid()) {
  79.             $entityManager->persist($company);
  80.             $entityManager->flush();
  81.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  82.             return $this->redirectToRoute('app_company_my', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
  83.         }
  84.         return $this->render('app/company/my.html.twig', [
  85.             'company' => $company,
  86.             'form_basic_infos' => $formBasicInfos->createView(),
  87.             'form_social_medias' => $formSocilaMedias->createView(),
  88.             'form_address' => $formAddress->createView(),
  89.             'form_other_details' => $formOtherDetails->createView(),
  90.         ]);
  91.     }
  92.     #[Route('/{id}/localization'name'app_company_localization'methods: ['GET''POST'])]
  93.     public function localization(Company $companyRequest $requestEntityManagerInterface $entityManagerFileUploader $fileUploader): Response
  94.     {
  95.         $form $this->createForm(CompanyTypeLocalization::class, $company);
  96.         $form->handleRequest($request);
  97.         if ($form->isSubmitted() && $form->isValid()) {
  98.             $entityManager->persist($company);
  99.             $entityManager->flush();
  100.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  101.             return $this->redirectToRoute('app_company_localization', ['id' => $company->getId()], Response::HTTP_SEE_OTHER);
  102.         }
  103.         return $this->render('app/company/localization.html.twig', [
  104.             'company' => $company,
  105.             'form' => $form->createView()
  106.         ]);
  107.     }
  108.     #[Route('/new'name'app_company_new'methods: ['GET''POST'])]
  109.     #[IsGranted('ROLE_SUPER_ADMIN')]
  110.     public function new(Request $requestEntityManagerInterface $entityManagerFileUploader $fileUploader): Response
  111.     {
  112.         $company = new Company();
  113.         $form $this->createForm(CompanyType::class, $company);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $company->addUser($this->getUser());
  117.             $logo $form->get('logo')->getData();
  118.             if ($logo) {
  119.                 $uploadedFileName $fileUploader->upload($logo'companies_dir');
  120.                 $company->setLogo($uploadedFileName);
  121.             }
  122.             $entityManager->persist($company);
  123.             $entityManager->flush();
  124.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  125.             return $this->redirectToRoute('app_company_index', [], Response::HTTP_SEE_OTHER);
  126.         }
  127.         return $this->renderForm('app/company/new.html.twig', [
  128.             'company' => $company,
  129.             'form' => $form,
  130.         ]);
  131.     }
  132.     #[Route('/new/company'name'app_company_new_company'methods: ['GET''POST'])]
  133.     public function new_company(Request $requestSecurity $securityEntityManagerInterface $entityManagerFileUploader $fileUploader): Response
  134.     {
  135.         $company = new Company();
  136.         $form $this->createForm(NewCompanyType::class, $company);
  137.         $form->handleRequest($request);
  138.         if ($form->isSubmitted() && $form->isValid()) {
  139.             /** @var $user App\Entity\User */
  140.             $user $this->getUser();
  141.             $user->setCompany($company);
  142.             $user->addRole('ROLE_OWNER');
  143.             $company->addUser($this->getUser());
  144.             $season = new Season();
  145.             $season->setName('Season ' date("Y"))
  146.                 ->setDescription('Season par defaut')
  147.                 ->setStartDate(new DateTime())
  148.                 ->setIsActive(true)
  149.                 ->setEndDate(new DateTime('+1 years'))->setIsActive(true);
  150.             $season->setOwner($company);
  151.             $entityManager->persist($season);
  152.             $entityManager->persist($company);
  153.             $entityManager->persist($user);
  154.             $category = new ExpenseCategory();
  155.             $category
  156.                 ->setName('Autre')
  157.                 ->setDescription('Objets divers')
  158.                 ->setColor('#8a0da0')
  159.                 ->setOwner($company);
  160.             $entityManager->persist($category);
  161.             $section = new Section();
  162.             $section
  163.                 ->setName('Section-1')
  164.                 ->setDescription('Section par defaut')
  165.                 ->setAgeRange('4 ans et plus')
  166.                 ->setOwner($company);
  167.             $entityManager->persist($section);
  168.             $service = new Service();
  169.             $service
  170.                 ->setName('Guard occasionnel')
  171.                 ->setDescription('Service par defaut')
  172.                 ->setPrice(1000)
  173.                 ->setOwner($company);
  174.             $entityManager->persist($service);
  175.             $entityManager->flush();
  176.             $security->login($user'security.authenticator.form_login.main');
  177.             $this->addFlash('success'$this->translator->trans('Succès !!'));
  178.             // TODO:: ask to confirm email page
  179.             
  180.             return $this->redirectToRoute('app_main');
  181.         }
  182.         return $this->renderForm('auth/registration/business.html.twig', [
  183.             'company' => $company,
  184.             'form' => $form,
  185.         ]);
  186.     }
  187.     #[Route('/{id}'name'app_company_show'methods: ['GET'])]
  188.     public function show(Company $company): Response
  189.     {
  190.         return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
  191.         // return $this->render('app/company/show.html.twig', [
  192.         //     'company' => $company,
  193.         // ]);
  194.     }
  195.     #[Route('/{id}/edit'name'app_company_edit'methods: ['GET''POST'])]
  196.     public function edit(Request $requestCompany $companyEntityManagerInterface $entityManagerFileUploader $fileUploader): Response
  197.     {
  198.         return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
  199.         // $form = $this->createForm(CompanyType::class, $company);
  200.         // $form->handleRequest($request);
  201.         // if ($form->isSubmitted() && $form->isValid()) {
  202.         //     $logo = $form->get('logo')->getData();
  203.         //     if ($logo) {
  204.         //         $uploadedFileName = $fileUploader->upload($logo, 'companies_dir');
  205.         //         $company->setLogo($uploadedFileName);
  206.         //     }
  207.         //     $entityManager->flush();
  208.         //     $this->addFlash('success', $this->translator->trans('Succès !!'));
  209.         //     return $this->redirectToRoute('app_company_index', [], Response::HTTP_SEE_OTHER);
  210.         // }
  211.         // return $this->renderForm('app/company/edit.html.twig', [
  212.         //     'company' => $company,
  213.         //     'form' => $form,
  214.         // ]);
  215.     }
  216.     #[Route('/{id}'name'app_company_delete'methods: ['POST'])]
  217.     public function delete(Request $requestCompany $companyEntityManagerInterface $entityManager): Response
  218.     {
  219.         if ($this->isCsrfTokenValid('delete' $company->getId(), $request->request->get('_token'))) {
  220.             try {
  221.                 $entityManager->remove($company);
  222.                 $entityManager->flush();
  223.                 $this->addFlash('success'$this->translator->trans('Succès !!'));
  224.             } catch (\Exception $e) {
  225.                 $errorMessage $e->getMessage();
  226.                 $result explode(':'$errorMessage);
  227.                 $this->addFlash('error'$result[0]);
  228.             }
  229.         }
  230.         return $this->redirectToRoute('app_company_index', [], Response::HTTP_SEE_OTHER);
  231.     }
  232.     #[Route('/online/registration/{id}'name'app_company_online_registration'methods: ['GET'])]
  233.     public function company_online_subscription(Request $requestCompany $company): Response
  234.     {
  235.         $url $request->getSchemeAndHttpHost() . $this->generateUrl('front_online_registration', ['company' => $company->getId()]);
  236.         return $this->render('app/company/online-registration.html.twig', [
  237.             'company' => $company,
  238.             'url' => $url
  239.         ]);
  240.     }
  241. }