src/Controller/CompanyController.php line 99
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\Company;
use App\Entity\Contact;
use App\Entity\ProspectFile;
use App\Entity\Hardware;
use App\Entity\Feature;
use App\Entity\FeatureModel;
use App\Entity\CompanyType;
use App\Entity\CompanyLead;
use App\Form\CompanyForm;
use App\Form\ContactForm;
use App\Form\HardwareForm;
use App\Repository\CompanyRepository;
use App\Repository\ContactRepository;
use App\Repository\HardwareTypeRepository;
use App\Repository\HardwareRepository;
use App\Repository\ProspectFileRepository;
use App\Repository\FeatureModelRepository;
use App\Repository\UserRepository;
use App\Repository\CompanyTypeRepository;
use App\Repository\CompanyLeadRepository;
class CompanyController extends AbstractController
{
#[Route(path: '/companies', name: 'companies')]
public function companies(CompanyRepository $companyRepository): Response
{
$company = new Company();
$form = $this->createForm(CompanyForm::class, $company , [
'user' => $this->getUser() ,
]);
if ( in_array("MANAGER", $this->getUser()->getRoles() ) ) {
$companies = $companyRepository->findBy( ['userGroup' => $this->getUser()->getUserGroup()] );
}else{
$companies = $companyRepository->findByUser( $this->getUser()->getId() );
}
return $this->render('company/companies.html.twig', ["companies" => $companies, "form" => $form ]);
}
#[Route(path: '/addCompany', name: 'add_company')]
public function addCompany(CompanyRepository $companyRepository, Request $request, ManagerRegistry $doctrine): Response
{
if( $request->get('company_form_id') == "" ){
$company = new Company();
}else{
$company = $companyRepository->find( $request->get('company_form_id') );
}
$form = $this->createForm(CompanyForm::class, $company, [
'user' => $this->getUser() ,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$company = $form->getData();
$company->setUserGroup( $this->getUser()->getUserGroup() );
$company->addUser( $this->getUser() );
$em = $doctrine->getManager();
$em->persist($company);
$em->flush();
return $this->redirectToRoute('companies');
}
return $this->redirectToRoute('companies');
}
#[Route(path: '/deleteCompany/{id}', name: 'delete_company')]
public function deleteCompany(Request $request, Company $company, ManagerRegistry $doctrine ): Response
{
$em = $doctrine->getManager();
$em->remove($company);
$em->flush();
return $this->redirectToRoute('companies');
}
#[Route(path: '/infoCompany/{id}/{navlink?}', name: 'info_company')]
public function infoCompany(Request $request, Company $company, $navlink = null ,
HardwareTypeRepository $hardwareTypeRepository, ContactRepository $contactRepository): Response
{
$contact = new Contact();
$contactForm = $this->createForm(ContactForm::class, $contact);
$hardwareTypes = $hardwareTypeRepository->findAll();
return $this->render('company/infoCompany.html.twig', ["company" => $company,
"hardwareTypes" => $hardwareTypes, "contactForm" => $contactForm, "navlink" => $navlink]);
}
#[Route('/addContact', name: 'add_contact')]
public function addContact(Request $request, ManagerRegistry $doctrine,
HardwareRepository $hardwareRepository, CompanyRepository $companyRepository, ContactRepository $contactRepository): Response
{
$contact = $request->get('contactId') == "" ? new Contact() : $contactRepository->find( $request->get('contactId') );
$company = $companyRepository->find( $request->get('companyId') );
$contactForm = $this->createForm(ContactForm::class, $contact);
$contactForm->handleRequest($request);
if ($contactForm->isSubmitted() && $contactForm->isValid()) {
$contact = $contactForm->getData();
$contact->setCompany($company);
$em = $doctrine->getManager();
$em->persist($contact);
$em->flush();
}
return $this->redirectToRoute('info_company', ['id' => $company->getId(), 'navlink'=> 'contacts']);
}
#[Route('/newHardware', name: 'new_hardware')]
public function newHardware(Request $request, ManagerRegistry $doctrine, CompanyRepository $companyRepository,
FeatureModelRepository $featureModelRepository,HardwareTypeRepository $hardwareTypeRepository): Response
{
$hardware = new Hardware();
$company = $companyRepository->find( $request->get('company_id') );
$hardwareType = $hardwareTypeRepository->find( $request->get('type_id') );
$hardware->setCompany($company);
$hardware->setHardwareType($hardwareType);
$featureModels = $featureModelRepository->findBy(
['userGroup' => $this->getUser()->getUserGroup()],
);
$hardwareForm = $this->createForm(HardwareForm::class, $hardware);
$contact = new Contact();
$contactForm = $this->createForm(ContactForm::class, $contact);
$typeId = $request->get('type_id');
$hardwareTypes = $hardwareTypeRepository->findAll();
return $this->render('company/hardwareForm.html.twig', ['hardwareForm' => $hardwareForm, 'contactForm' => $contactForm,
'hardware' => $hardware, "hardwareTypes" => $hardwareTypes , 'featureModels' => $featureModels, 'navlink'=> 'editHardware']);
}
#[Route('/editHardware/{id}', name: 'edit_hardware')]
public function editHardware(Request $request, ManagerRegistry $doctrine, CompanyRepository $companyRepository ,
Hardware $hardware, FeatureModelRepository $featureModelRepository,HardwareTypeRepository $hardwareTypeRepository ): Response
{
$hardwareForm = $this->createForm(HardwareForm::class, $hardware);
$featureModels = $featureModelRepository->findBy(
['userGroup' => $this->getUser()->getUserGroup()],
);
$company = $hardware->getCompany();
$contact = new Contact();
$contactForm = $this->createForm(ContactForm::class, $contact);
$hardwareForm->handleRequest($request);
if ($hardwareForm->isSubmitted() && $hardwareForm->isValid()) {
$hardware = $hardwareForm->getData();
$em = $doctrine->getManager();
foreach ( $request->get('feature') as $key => $value) {
$feature = $hardware->getFeatureByName($key);
$feature->setValue($value);
// ------- CHECK NEW MODEL ---------
$key = str_contains($key, '-MARQUE')? "MARQUE": $key;
$newFeature = 1;
foreach ($featureModels as $featureModel) {
if( ( ($featureModel->getName() === $key) && ($featureModel->getValue() === $value) )|| str_contains($key, 'DATE') ){
$newFeature = 0;
}
}
if($newFeature){
$featureModel = new FeatureModel();
$featureModel->setName($key)->setValue($value);
$featureModel->setUserGroup( $this->getUser()->getUserGroup() );
$value == "" ? "" :$em->persist($featureModel);
}
}
$em->persist($hardware);
$em->flush();
return $this->redirectToRoute('info_company', ['id' => $company->getId(), 'navlink'=> 'hardwares']);
}
$hardwareTypes = $hardwareTypeRepository->findAll();
return $this->render('company/hardwareForm.html.twig', ['hardwareForm' => $hardwareForm, 'contactForm' => $contactForm,
'hardware' => $hardware, "hardwareTypes" => $hardwareTypes, 'featureModels' => $featureModels, 'navlink'=> 'editHardware']);
}
#[Route('/addHardware', name: 'add_hardware')]
public function addHardware(Request $request, ManagerRegistry $doctrine, CompanyRepository $companyRepository,
HardwareTypeRepository $hardwareTypeRepository): Response
{
$hardware = new Hardware();
$company = $companyRepository->find( $request->get('companyId') );
$hardwareForm = $this->createForm(HardwareForm::class, $hardware);
$hardwareForm->handleRequest($request);
$hardware->setCompany($company);
$hardwareType = $hardwareTypeRepository->find( $request->get('hardwareTypeId') );
$hardware->setHardwareType( $hardwareType );
if ($hardwareForm->isSubmitted() && $hardwareForm->isValid()) {
$hardware = $hardwareForm->getData();
$em = $doctrine->getManager();
foreach ( $request->get('feature') as $key => $value) {
$feature = new Feature();
$feature->setHardware($hardware);
$feature->setName($key);
$feature->setValue($value);
$em->persist($feature);
}
$em->persist($hardware);
$em->flush();
}
return $this->redirectToRoute('info_company', ['id' => $company->getId(), 'navlink'=> 'hardwares']);
}
#[Route('/addCompanyUser', name: 'add_company_user', methods:"POST")]
public function addCompanyUser(Request $request, ManagerRegistry $doctrine, CompanyRepository $companyRepository, UserRepository $userRepository): Response
{
$company = $companyRepository->find( $request->get('companyId') );
$user = $userRepository->find( $request->get('companyUserSelect') );
$company->addUser($user);
$em = $doctrine->getManager();
$em->persist($company);
$em->flush();
return $this->redirectToRoute('info_company', ['id' => $company->getId()]);
}
#[Route('/deleteCompanyUser', name: 'delete_company_user', methods:"POST")]
public function deleteCompanyUser(Request $request, ManagerRegistry $doctrine, CompanyRepository $companyRepository, UserRepository $userRepository): Response
{
$company = $companyRepository->find( $request->get('companyId') );
$user = $userRepository->find( $request->get('companyUserSelect') );
$company->removeUser($user);
$em = $doctrine->getManager();
$em->persist($company);
$em->flush();
return $this->redirectToRoute('info_company', ['id' => $company->getId()]);
}
#[Route('/addCompanyType', name: 'add_companyType', methods:"POST")]
public function addCompanyType(Request $request, ManagerRegistry $doctrine): Response
{
$companyType = new CompanyType();
$companyType->setName( $request->get('companyTypeName') );
$companyType->setUserGroup( $this->getUser()->getUserGroup() );
$em = $doctrine->getManager();
$em->persist( $companyType );
$em->flush();
return $this->redirectToRoute("settings");
}
#[Route('/deleteCompanyType', name: 'delete_companyType', methods:"POST")]
public function deleteCompanyType(Request $request, CompanyTypeRepository $companyTypeRepository, ManagerRegistry $doctrine): Response
{
$companyType = $companyTypeRepository->find( $request->get('companyTypeId') );
if( $companyType->getUserGroup() !== $this->getUser()->getUserGroup() ){
return $this->redirectToRoute("settings");
}
if( $companyType->getCompanies()->count() == 0 ){
$em = $doctrine->getManager();
$em->remove($companyType);
$em->flush();
}else{
$this->addFlash("warning", "Des entreprises sont reliées. Impossible de supprimer");
}
return $this->redirectToRoute("settings");
}
#[Route('/addCompanyLead', name: 'add_companyLead', methods:"POST")]
public function addCompanyLead(Request $request, ManagerRegistry $doctrine): Response
{
$companyLead = new CompanyLead();
$companyLead->setName( $request->get('companyLeadName') );
$companyLead->setUserGroup( $this->getUser()->getUserGroup() );
$em = $doctrine->getManager();
$em->persist( $companyLead );
$em->flush();
return $this->redirectToRoute("settings");
}
#[Route('/deleteCompanyLead', name: 'delete_companyLead', methods:"POST")]
public function deleteCompanyLead(Request $request, CompanyLeadRepository $companyLeadRepository, ManagerRegistry $doctrine): Response
{
$companyLead = $companyLeadRepository->find( $request->get('companyLeadId') );
if( $companyLead->getUserGroup() !== $this->getUser()->getUserGroup() ){
return $this->redirectToRoute("settings");
}
if( $companyLead->getCompanies()->count() == 0 ){
$em = $doctrine->getManager();
$em->remove($companyLead);
$em->flush();
}else{
$this->addFlash("warning", "Des entreprises sont reliées. Impossible de supprimer");
}
return $this->redirectToRoute("settings");
}
}