Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BO - Signalement] Rapport de visite non modifiable #2135

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/Back/SignalementFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function deleteFileSignalement(
): JsonResponse {
$this->denyAccessUnlessGranted('FILE_DELETE', $signalement);
if ($this->isCsrfTokenValid('signalement_delete_file_'.$signalement->getId(), $request->get('_token'))) {
if ($uploadHandlerService->deleteFile($signalement, $type, $filename, $fileRepository)) {
if ($uploadHandlerService->deleteSignalementFile($signalement, $type, $filename, $fileRepository)) {
return $this->json(['response' => 'success']);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/Controller/Back/SignalementVisitesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller\Back;

use App\Dto\Request\Signalement\VisiteRequest;
use App\Entity\Intervention;
use App\Entity\Signalement;
use App\Entity\User;
use App\Event\InterventionCreatedEvent;
Expand All @@ -13,6 +14,7 @@
use App\Repository\InterventionRepository;
use App\Service\Files\FilenameGenerator;
use App\Service\UploadHandlerService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -330,6 +332,26 @@ public function editVisiteFromSignalement(
return $this->redirectToRoute('back_signalement_view', ['uuid' => $signalement->getUuid()]);
}

#[Route('/{uuid}/visites/{intervention}/delete-rapport', name: 'back_signalement_visite_deleterapport')]
public function deleteRapportVisiteFromSignalement(
Signalement $signalement,
Intervention $intervention,
Request $request,
EntityManagerInterface $entityManager,
UploadHandlerService $uploadHandlerService,
): Response {
if (!$this->isCsrfTokenValid('delete_rapport', $request->get('_token')) || $intervention->getSignalement()->getId() !== $signalement->getId() || $intervention->getFiles()->isEmpty()) {
return $this->redirectToRoute('back_signalement_view', ['uuid' => $signalement->getUuid()]);
}
$file = $intervention->getFiles()->first();
$uploadHandlerService->deleteFileInBucket($file);
$entityManager->remove($file);
$entityManager->flush();
$this->addFlash('success', 'Le rapport de visite a bien été supprimé.');

return $this->redirectToRoute('back_signalement_view', ['uuid' => $signalement->getUuid()]);
}

private function validateRequest(VisiteRequest $visiteRequest, ValidatorInterface $validator): string
{
$errorMessage = '';
Expand Down
28 changes: 16 additions & 12 deletions src/Service/UploadHandlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function movePhotoVariants(string $filename): void
$this->moveFilePath($distantFolder.$variantNames[ImageManipulationHandler::SUFFIX_THUMB]);
}

public function deleteFile(Signalement $signalement, string $type, string $filename, FileRepository $fileRepository)
public function deleteSignalementFile(Signalement $signalement, string $type, string $filename, FileRepository $fileRepository): bool
{
$fileType = 'documents' === $type ? File::FILE_TYPE_DOCUMENT : File::FILE_TYPE_PHOTO;

Expand All @@ -133,17 +133,7 @@ function (File $file) use ($fileType, $filename) {

if (!$fileCollection->isEmpty()) {
$file = $fileCollection->current();
if ($this->fileStorage->fileExists($file->getFilename())) {
$this->fileStorage->delete($file->getFilename());
}
$variantNames = ImageManipulationHandler::getVariantNames($filename);
if ($this->fileStorage->fileExists($variantNames[ImageManipulationHandler::SUFFIX_RESIZE])) {
$this->fileStorage->delete($variantNames[ImageManipulationHandler::SUFFIX_RESIZE]);
}
if ($this->fileStorage->fileExists($variantNames[ImageManipulationHandler::SUFFIX_THUMB])) {
$this->fileStorage->delete($variantNames[ImageManipulationHandler::SUFFIX_THUMB]);
}

$this->deleteFileInBucket($file);
$fileRepository->remove($file, true);

return true;
Expand All @@ -152,6 +142,20 @@ function (File $file) use ($fileType, $filename) {
return false;
}

public function deleteFileInBucket(File $file): void
{
if ($this->fileStorage->fileExists($file->getFilename())) {
$this->fileStorage->delete($file->getFilename());
}
$variantNames = ImageManipulationHandler::getVariantNames($file->getFilename());
if ($this->fileStorage->fileExists($variantNames[ImageManipulationHandler::SUFFIX_RESIZE])) {
$this->fileStorage->delete($variantNames[ImageManipulationHandler::SUFFIX_RESIZE]);
}
if ($this->fileStorage->fileExists($variantNames[ImageManipulationHandler::SUFFIX_THUMB])) {
$this->fileStorage->delete($variantNames[ImageManipulationHandler::SUFFIX_THUMB]);
}
}

public function uploadFromFilename(string $filename): ?string
{
$this->logger->info($filename);
Expand Down
16 changes: 9 additions & 7 deletions templates/back/signalement/view/visites/visite-item.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@
{% endif %}
{% endif %}
</div>
<div class="fr-mb-3v">
<strong>Rapport :</strong>
<div class="fr-mb-3v fr-display-inline-flex">
<strong>Rapport : </strong>
{% if signalement.interventions is empty or intervention.files is empty %}
<span class="fr-badge fr-badge--no-icon" title="Non disponible">Non disponible</span>
<span class="fr-badge fr-badge--no-icon" title="Non disponible">Non disponible</span>
{% else %}
<a href="{{ asset('_up/'~intervention.files[0].filename)~'/' ~ signalement.uuid }}"
class="fr-btn fr-btn--sm fr-fi-file-line fr-btn--icon-left"
title="Afficher le document"
rel="noopener"
target="_blank">Voir le rapport de visite</a>
class="fr-btn fr-btn--sm"
title="Afficher le document"
rel="noopener"
target="_blank">
<span aria-hidden="true" class="fr-fi-file-line fr-icon--sm"></span> Voir le rapport de visite
</a>
{% endif %}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,16 @@
</legend>
<input type="file" accept="application/*" name="visite-{{ formType }}[rapport]">
</fieldset>
{% elseif intervention is defined and intervention.files|length %}
<fieldset class="fr-fieldset fr-fieldset--inline fr-mb-5v">
<ul class="fr-btns-group fr-btns-group--inline-sm fr-btns-group--sm">
<li>
<a href="{{ asset('_up/'~intervention.files[0].filename)~'/' ~ intervention.signalement.uuid }}" class="fr-btn" title="Afficher le document" rel="noopener" target="_blank">Voir le rapport de visite
</a>
</li>
<li>
<a href="{{ path('back_signalement_visite_deleterapport',{uuid:intervention.signalement.uuid, intervention:intervention.id}) }}?_token={{ csrf_token('delete_rapport') }}" title="Supprimer le rapport" class="fr-btn fr-btn--secondary fr-background--white fr-fi-delete-line "></a>
</li>
</ul>
</fieldset>
{% endif %}
Loading