1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller\Admin; |
6
|
|
|
|
7
|
|
|
use App\Controller\BaseController; |
8
|
|
|
use App\Entity\Photo; |
9
|
|
|
use App\Entity\Property; |
10
|
|
|
use App\Service\FileUploader; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
|
16
|
|
|
final class PhotoController extends BaseController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @Route("/admin/photo/{id<\d+>}/edit", name="admin_photo_edit") |
20
|
|
|
*/ |
21
|
|
|
public function edit(Request $request, Property $property): Response |
22
|
|
|
{ |
23
|
|
|
$photos = $property->getPhotos(); |
24
|
|
|
|
25
|
|
|
return $this->render('admin/photo/edit.html.twig', [ |
26
|
|
|
'site' => $this->site($request), |
27
|
|
|
'photos' => $photos, |
28
|
|
|
'property_id' => $property->getId(), |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Deletes a Photo entity. |
34
|
|
|
* |
35
|
|
|
* @Route("/property/{property_id<\d+>}/photo/{id<\d+>}/delete", methods={"POST"}, name="admin_photo_delete") |
36
|
|
|
* @IsGranted("ROLE_ADMIN") |
37
|
|
|
*/ |
38
|
|
|
public function delete(Request $request, Photo $photo, FileUploader $fileUploader): Response |
39
|
|
|
{ |
40
|
|
|
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) { |
41
|
|
|
return $this->redirectToRoute( |
42
|
|
|
'admin_photo_edit', |
43
|
|
|
['id' => $request->attributes->get('property_id')] |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Delete from db |
48
|
|
|
$em = $this->doctrine->getManager(); |
49
|
|
|
$em->remove($photo); |
50
|
|
|
$em->flush(); |
51
|
|
|
|
52
|
|
|
// Delete file from folder |
53
|
|
|
$fileUploader->remove($photo->getPhoto()); |
54
|
|
|
|
55
|
|
|
$this->addFlash('success', 'message.deleted'); |
56
|
|
|
|
57
|
|
|
return $this->redirectToRoute( |
58
|
|
|
'admin_photo_edit', |
59
|
|
|
['id' => $request->attributes->get('property_id')] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|