Skip to content

Commit

Permalink
Merge pull request #60 from kiloutyg/58-add-a-closing-function-that-w…
Browse files Browse the repository at this point in the history
…ill-archived-ncf

added the closing function, changed a few things about the views, and…
  • Loading branch information
kiloutyg authored Mar 15, 2024
2 parents 358a599 + 03930ab commit 266cde0
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 60 deletions.
13 changes: 11 additions & 2 deletions src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ public function productVersionCreation(Request $request): Response
}
}


#[Route('admin/archive/{entityType}/{id}', name: 'archive_entity')]
public function archiveEntity(Request $request, string $entityType, int $id): Response
{
Expand All @@ -355,9 +356,15 @@ public function archiveEntity(Request $request, string $entityType, int $id): Re
return $this->redirect($originUrl);
} else {
$this->addFlash('success', 'L\'élément a bien été archivé');
return $this->redirect($originUrl);
if ($entityType == "efnc") {
return $this->redirectToRoute('app_base', []);
} else {
return $this->redirect($originUrl);
}
}
}


#[Route('admin/delete/{entityType}/{id}', name: 'delete_entity')]
public function deleteEntity(Request $request, string $entityType, int $id): Response
{
Expand All @@ -371,6 +378,8 @@ public function deleteEntity(Request $request, string $entityType, int $id): Res
return $this->redirect($originUrl);
}
}


#[Route('admin/unarchive/{entityType}/{id}', name: 'unarchive_entity')]
public function unarchiveEntity(Request $request, string $entityType, int $id): Response
{
Expand All @@ -384,4 +393,4 @@ public function unarchiveEntity(Request $request, string $entityType, int $id):
return $this->redirect($originUrl);
}
}
}
}
61 changes: 49 additions & 12 deletions src/Controller/EFNCController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,32 @@ public function formModificationDisplay(int $efncID, Request $request): Response
'EFNC' => $efnc,
]);
} else if ($request->getMethod() == 'POST') {
if ($this->getUser() !== null) {
if ($this->authChecker->isGranted('ROLE_ADMIN')) {
$user = $this->getUser();
$form1->handleRequest($request);
if ($form1->isValid() && $form1->isSubmitted()) {
$result = $this->formModificationService->modifyNCForm(
$efnc,
$request,
$form1,
$user
);
}
if ($result === true) {

$form1->handleRequest($request);
if ($form1->isValid() && $form1->isSubmitted()) {
$result = $this->formModificationService->modifyNCForm(
$efnc,
$request,
$form1
);
}
if ($result === true) {
$this->addFlash('success', 'Fiche correctement modifiée!');
return $this->redirectToRoute('app_base', []);
$this->addFlash('success', 'Fiche correctement modifiée!');
return $this->redirectToRoute('app_base', []);
} else {
$this->addFlash('error', 'Erreur lors de la modification de la fiche!');
return $this->redirectToRoute('app_base', []);
}
} else {
$this->addFlash('error', 'Vous n\'avez pas les droits pour modifier cette fiche!');
return $this->redirectToRoute('app_base', []);
}
} else {
$this->addFlash('error', 'Erreur lors de la modification de la fiche!');
$this->addFlash('error', 'Vous devez êtres connecté pour modifier cette fiche!');
return $this->redirectToRoute('app_base', []);
}
}
Expand All @@ -131,4 +143,29 @@ public function pictureView(int $pictureID): Response
);
return $response;
}

#[Route('admin/close/{entityType}/{id}', name: 'close_entity')]
public function closeEntity(Request $request, string $entityType, int $id): Response
{
$originUrl = $request->headers->get('referer');

if ($this->getUser() !== null) {
if ($this->authChecker->isGranted('ROLE_MASTER_ADMIN')) {
$result = $this->entityDeletionService->closeEntity($entityType, $id); // Implement this method in your service
if ($result == false) {
$this->addFlash('danger', 'L\'élément n\'a pas pu être clôturé');
return $this->redirectToRoute('app_base', []);
} else {
$this->addFlash('success', 'L\'élément a bien été clôturé');
return $this->redirectToRoute('app_base', []);
}
} else {
$this->addFlash('error', 'Vous n\'avez pas les droits pour clôturer un élément');
return $this->redirectToRoute('app_base', []);
}
} else {
$this->addFlash('error', 'Vous devez être connecté pour clôturer un élément');
return $this->redirectToRoute('app_base', []);
}
}
}
20 changes: 17 additions & 3 deletions src/Entity/EFNC.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class EFNC
#[ORM\OneToMany(mappedBy: 'EFNC', targetEntity: ImmediateConservatoryMeasures::class, cascade: ['persist', 'remove'])]
private Collection $immediateConservatoryMeasures;


#[ORM\OneToMany(mappedBy: 'EFNC', targetEntity: BoughtComponent::class)]
private Collection $boughtComponents;

Expand Down Expand Up @@ -96,6 +95,9 @@ class EFNC
#[ORM\Column(nullable: true)]
private ?bool $archived = null;

#[ORM\ManyToOne(inversedBy: 'eFNCs')]
private ?User $lastModifier = null;


public function __construct()
{
Expand Down Expand Up @@ -361,7 +363,7 @@ public function setStatus(?bool $Status): static
$this->Status = $Status;
// If the status is set to true, we set the ClosedDate to the current date
if ($Status === true) {
$this->ClosedDate = new \DateTimeInterface();
$this->ClosedDate = new \DateTime();
}
return $this;
}
Expand Down Expand Up @@ -524,4 +526,16 @@ public function setArchived(?bool $archived): static

return $this;
}
}

public function getLastModifier(): ?User
{
return $this->lastModifier;
}

public function setLastModifier(?User $lastModifier): static
{
$this->lastModifier = $lastModifier;

return $this;
}
}
40 changes: 40 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

use Doctrine\ORM\Mapping as ORM;

Expand Down Expand Up @@ -37,6 +39,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(length: 255, nullable: true)]
private ?string $emailAddress = null;

#[ORM\OneToMany(mappedBy: 'lastModifier', targetEntity: EFNC::class)]
private Collection $eFNCs;

public function __construct()
{
$this->eFNCs = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -119,4 +129,34 @@ public function setEmailAddress(?string $emailAddress): static

return $this;
}

/**
* @return Collection<int, EFNC>
*/
public function getEFNCs(): Collection
{
return $this->eFNCs;
}

public function addEFNC(EFNC $eFNC): static
{
if (!$this->eFNCs->contains($eFNC)) {
$this->eFNCs->add($eFNC);
$eFNC->setLastModifier($this);
}

return $this;
}

public function removeEFNC(EFNC $eFNC): static
{
if ($this->eFNCs->removeElement($eFNC)) {
// set the owning side to null (unless already changed)
if ($eFNC->getLastModifier() === $this) {
$eFNC->setLastModifier(null);
}
}

return $this;
}
}
30 changes: 30 additions & 0 deletions src/Service/EntityDeletionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,36 @@ public function unarchiveEntity(string $entityType, int $id): bool
}


$this->em->flush();

return true;
}


public function closeEntity(string $entityType, int $id): bool
{
$repository = null;
switch ($entityType) {
case "efnc":
$repository = $this->EFNCRepository;
break;
}

// If the repository is not found or the entity is not found in the database, return false
if (!$repository) {
return false;
}
// Get the entity from the database
$entity = $repository->find($id);
if (!$entity) {
return false;
}

if ($entityType === 'efnc') {
$entity->setArchived(true);
$entity->setStatus(true);
}

$this->em->flush();

return true;
Expand Down
8 changes: 5 additions & 3 deletions src/Service/FormModificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\ORM\EntityManagerInterface;

use App\Entity\EFNC;
use App\Entity\User;

use App\Service\PictureService;

Expand Down Expand Up @@ -49,7 +50,8 @@ public function __construct(
public function modifyNCForm(
EFNC $efnc,
Request $request,
FormInterface $form1
FormInterface $form1,
User $user
) {
$now = new \DateTime();

Expand Down Expand Up @@ -91,10 +93,10 @@ public function modifyNCForm(
$this->PictureService->pictureUpload($picture, $efnc, $efncFolderName, 'NC');
}
}

$efnc->setLastModifier($user);
$this->em->persist($efnc);
$this->em->flush();

return true;
}
}
}
Loading

0 comments on commit 266cde0

Please sign in to comment.