add company controller for company actions

This commit is contained in:
Ryan Prather 2025-01-10 14:49:08 +00:00
parent cd8cbcf6ba
commit 75cffad2ea
2 changed files with 185 additions and 1 deletions

View File

@ -0,0 +1,182 @@
<?php
namespace App\Controller;
use App\Entity\Company;
use App\Entity\CompanyDocument;
use App\Entity\User;
use App\Form\CompanyDocumentFormType;
use App\Form\InternalCompanyFormType;
use App\Libs\Breadcrumb;
use App\Libs\NavList;
use App\Libs\Libs;
use DateTime;
use DateTimeZone;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\String\Slugger\SluggerInterface;
#[IsGranted('ROLE_ADMIN')]
class CompanyController extends AbstractController
{
private array $msgs;
public function __construct(
private EntityManagerInterface $entityManager,
private array $navLinks = []
){
$this->navLinks = NavList::LIST;
}
#[Route('/company', name: 'app_company')]
public function editCompanyInfo(
#[CurrentUser()] User $user,
Request $request,
SluggerInterface $slugger
): Response {
$this->navLinks['company_nav'] = NavList::PRESENT_LINK;
$this->msgs = Libs::getMessages($user, $this->entityManager);
$company = $user->getCompany();
$form = $this->createForm(InternalCompanyFormType::class, $company);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
/** @var Company $company */
$company = $form->getData();
if ($form->get('companyLogo')->getData()) {
$file = $form['companyLogo']->getData();
$destination = $this->getParameter('kernel.project_dir').'/public/uploads/company/';
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$newFilename = $slugger->slug($originalFilename).'-'.uniqid().'.'.$file->guessExtension();
$file->move(
$destination,
$newFilename
);
$company->setCompanyLogo($newFilename);
}
$this->entityManager->flush();
return $this->redirectToRoute('app_admin_dashboard');
}
return $this->render(
'internal/admin/company/company-info.html.twig',
array_merge(
$this->navLinks,
[
'form' => $form,
'company' => $company,
'breadcrumbs' => [
new Breadcrumb($this->generateUrl('app_admin_dashboard'), "Admin Dashboard"),
],
'msgs' => $this->msgs,
]
)
);
}
#[Route('/docs/list', name: 'app_list_documents')]
public function listDocs(#[CurrentUser()] User $user): Response
{
$this->navLinks['company_nav'] = NavList::PRESENT_LINK;
$this->msgs = Libs::getMessages($user, $this->entityManager);
$companyDocs = $this->entityManager->getRepository(CompanyDocument::class)->findBy(['company' => $user->getCompany()], ['title' => 'ASC']);
return $this->render(
'internal/admin/company/docs/list-documents.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [],
'notifications' => $this->msgs,
'docs' => $companyDocs,
]
)
);
}
#[Route('/docs/add', name: 'app_add_doc')]
public function addCompanyDocument(Request $request, #[CurrentUser()] User $user): Response
{
$this->navLinks['company_nav'] = NavList::PRESENT_LINK;
$this->msgs = Libs::getMessages($user, $this->entityManager);
$form = $this->createForm(CompanyDocumentFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var CompanyDocument $doc */
$doc = $form->getData();
$doc->setCompany($user->getCompany());
$doc->setUpdated(new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE'])));
$this->entityManager->persist($doc);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_documents');
}
return $this->render(
'internal/admin/company/docs/add-document.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [],
'notifications' => $this->msgs,
'form' => $form,
]
)
);
}
#[Route('/docs/edit/{docId}', name: 'app_edit_doc')]
public function editCompanyDocument(string $docId, Request $request, #[CurrentUser()] User $user): Response
{
$companyDoc = $this->entityManager->getRepository(CompanyDocument::class)->find($docId);
$this->navLinks['company_nav'] = NavList::PRESENT_LINK;
$this->msgs = Libs::getMessages($user, $this->entityManager);
$form = $this->createForm(CompanyDocumentFormType::class, $companyDoc);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var CompanyDocument $doc */
$doc = $form->getData();
$doc->setUpdated(new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE'])));
$this->entityManager->persist($doc);
$this->entityManager->flush();
return $this->redirectToRoute('app_list_documents');
}
return $this->render(
'internal/admin/company/docs/edit-document.html.twig',
array_merge(
$this->navLinks,
[
'breadcrumbs' => [],
'notifications' => $this->msgs,
'form' => $form,
'doc' => $companyDoc,
'docText' => str_replace("\r\n", "", $companyDoc->getText())
]
)
);
}
}

View File

@ -5,6 +5,7 @@ namespace App\Form;
use App\DataTransferObject\CompanyDetailsDto;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
@ -43,6 +44,7 @@ class CompanyFormType extends AbstractType
'required' => true
])
->add('url', UrlType::class)
->add('companyLogo', FileType::class)
;
}