|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller\Admin; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\Photo; |
|
8
|
|
|
use App\Entity\Property; |
|
9
|
|
|
use App\Service\FileUploader; |
|
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
17
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
18
|
|
|
|
|
19
|
|
|
final class PhotoController extends AbstractController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @Route("/admin/photo/{id<\d+>}/upload", name="admin_photo_upload", methods={"POST"}) |
|
23
|
|
|
*/ |
|
24
|
|
|
public function upload(Request $request, FileUploader $fileUploader): Response |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var UploadedFile $uploadedFile */ |
|
27
|
|
|
$uploadedFile = $request->files->get('file'); |
|
28
|
|
|
$violations = $fileUploader->validate($uploadedFile); |
|
29
|
|
|
|
|
30
|
|
|
if ($violations->count() > 0) { |
|
31
|
|
|
/** @var ConstraintViolation $violation */ |
|
32
|
|
|
$violation = $violations[0]; |
|
33
|
|
|
|
|
34
|
|
|
return new JsonResponse(['status' => 'error', 'message' => $violation->getMessage()]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$fileName = $fileUploader->upload($uploadedFile); |
|
38
|
|
|
|
|
39
|
|
|
$property = $this->getDoctrine() |
|
40
|
|
|
->getRepository(Property::class) |
|
41
|
|
|
->find($request->attributes->get('id')); |
|
42
|
|
|
|
|
43
|
|
|
$photo = new Photo(); |
|
44
|
|
|
$photo->setProperty($property) |
|
45
|
|
|
->setSortOrder(0) |
|
46
|
|
|
->setPhoto($fileName); |
|
47
|
|
|
|
|
48
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
49
|
|
|
$em->persist($photo); |
|
50
|
|
|
$em->flush(); |
|
51
|
|
|
|
|
52
|
|
|
return new JsonResponse(['status' => 'ok']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @Route("/admin/photo/{id<\d+>}/edit", name="admin_photo_edit") |
|
57
|
|
|
*/ |
|
58
|
|
|
public function edit(Request $request): Response |
|
59
|
|
|
{ |
|
60
|
|
|
$property = $this->getDoctrine() |
|
61
|
|
|
->getRepository(Property::class) |
|
62
|
|
|
->find($request->attributes->get('id')); |
|
63
|
|
|
|
|
64
|
|
|
$photos = $property->getPhotos(); |
|
65
|
|
|
|
|
66
|
|
|
return $this->render('admin/photo/edit.html.twig', [ |
|
67
|
|
|
'photos' => $photos, |
|
68
|
|
|
'property_id' => $property->getId(), |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sort photos. |
|
74
|
|
|
* |
|
75
|
|
|
* @Route("/admin/photo/sort",methods={"POST"}, name="admin_photo_sort") |
|
76
|
|
|
*/ |
|
77
|
|
|
public function sort(Request $request) |
|
78
|
|
|
{ |
|
79
|
|
|
$ids = $request->request->get('ids'); |
|
80
|
|
|
$repository = $this->getDoctrine()->getRepository(Photo::class); |
|
81
|
|
|
$repository->reorderPhotos($ids); |
|
82
|
|
|
|
|
83
|
|
|
return new JsonResponse(['status' => 'ok']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Deletes a Photo entity. |
|
88
|
|
|
* |
|
89
|
|
|
* @Route("/property/{property_id<\d+>}/photo/{id<\d+>}/delete", methods={"POST"}, name="admin_photo_delete") |
|
90
|
|
|
* @IsGranted("ROLE_ADMIN") |
|
91
|
|
|
*/ |
|
92
|
|
|
public function delete(Request $request, Photo $photo, FileUploader $fileUploader): Response |
|
93
|
|
|
{ |
|
94
|
|
|
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) { |
|
95
|
|
|
return $this->redirectToRoute( |
|
96
|
|
|
'admin_photo_edit', |
|
97
|
|
|
['id' => $request->attributes->get('property_id')] |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Delete from db |
|
102
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
103
|
|
|
$em->remove($photo); |
|
104
|
|
|
$em->flush(); |
|
105
|
|
|
|
|
106
|
|
|
// Delete file from folder |
|
107
|
|
|
$fileUploader->remove($photo->getPhoto()); |
|
108
|
|
|
|
|
109
|
|
|
$this->addFlash('success', 'message.deleted'); |
|
110
|
|
|
|
|
111
|
|
|
return $this->redirectToRoute( |
|
112
|
|
|
'admin_photo_edit', |
|
113
|
|
|
['id' => $request->attributes->get('property_id')] |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|