Issues (18)

src/Controller/Admin/WanderController.php (4 issues)

1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Entity\Wander;
6
use App\Form\WanderType;
7
use App\Repository\ImageRepository;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\File\Exception\FileException;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\HttpException;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\String\Slugger\SluggerInterface;
16
use App\Repository\WanderRepository;
17
use App\Service\GpxService;
18
use App\Service\UploadHelper;
19
use Doctrine\ORM\Mapping\OrderBy;
20
use Knp\Component\Pager\PaginatorInterface;
21
22
/**
23
 * @Route("/admin/wanders", name="admin_wanders_")
24
 */
25
class WanderController extends AbstractController
26
{
27
    /**
28
     * @Route("/", name="index", methods={"GET"})
29
     */
30
    public function index(
31
        Request $request,
32
        WanderRepository $wanderRepository,
33
        PaginatorInterface $paginator
34
        ): Response
35
    {
36
        $filterHasImages = null;
37
38
        // Customise the query to add an imageCount built-in so we can efficiently
39
        // (and at all :) ) sort it in our paginator.
40
        $qb = $wanderRepository->wandersWithImageCountQueryBuilder();
41
42
        if ($request->query->has('hasImages')) {
43
            $filterHasImages = $request->query->getBoolean('hasImages');
44
            $wanderRepository->addWhereHasImages($qb, $filterHasImages);
45
        }
46
47
        $query = $qb->getQuery();
48
49
        $pagination = $paginator->paginate(
50
            $query,
51
            $request->query->getInt('page', 1),
52
            20
53
        );
54
55
        return $this->render('admin/wander/index.html.twig', [
56
            'pagination' => $pagination,
57
            'filter_has_images' => $filterHasImages
58
        ]);
59
    }
60
61
    /**
62
     * @Route(
63
     *  "/backlog.{!_format}",
64
     *  name="backlog",
65
     *  methods={"GET"},
66
     *  format="html",
67
     *  requirements={
68
     *      "_format": "html|txt"
69
     *  }
70
     * )
71
     */
72
    public function backlog(
73
        Request $request,
74
        WanderRepository $wanderRepository
75
    ): Response
76
    {
77
        $qb = $wanderRepository
78
            ->standardQueryBuilder()
79
            ->OrderBy('w.startTime');
80
        $wanderRepository->addWhereHasImages($qb, false);
81
        $wanders = $qb->getQuery()->getResult();
82
        $response = new Response();
83
        $format = $request->getRequestFormat();
84
        return $this->render(
85
            'admin/wander/backlog.'.$format.'.twig',
86
            [
87
                'wanders' => $wanders
88
            ],
89
            $response
90
        );
91
    }
92
93
    /**
94
     * @Route("/new", name="new", methods={"GET","POST"})
95
     */
96
    public function new(
97
            Request $request,
98
            GpxService $gpxService,
99
            UploadHelper $uploadHelper
100
        ): Response
101
    {
102
        $wander = new Wander();
103
        // https://symfony.com/doc/current/controller/upload_file.html
104
105
        $form = $this->createForm(WanderType::class, $wander, ['type' => 'new']);
106
107
        $form->handleRequest($request);
108
        if ($form->isSubmitted() && $form->isValid()) {
109
110
            /** @var UploadedFile|null $gpxFile */
111
            $gpxFile = $form->get('gpxFilename')->getData();
112
113
            if ($gpxFile) {
114
                $wander->setGpxFilename($uploadHelper->uploadGpxFile($gpxFile));
115
            }
116
117
            $wander = $form->getData();
118
            $gpxService->updateWanderFromGpx($wander);
119
120
            $entityManager = $this->getDoctrine()->getManager();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

120
            $entityManager = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
121
            $entityManager->persist($wander);
122
            $entityManager->flush();
123
            return $this->redirectToRoute('admin_wanders_show', ['id' => $wander->getId()]);
124
        }
125
126
        return $this->render('admin/wander/new.html.twig', [
127
            'form' => $form->createView(),
128
        ]);
129
    }
130
131
    /**
132
     * @Route("/{id}", name="show", methods={"GET"})
133
     */
134
    public function show(Wander $wander): Response // Uses “param converter” to find the Wander in db through the {id}
135
    {
136
        return $this->render('admin/wander/show.html.twig', [
137
            'wander' => $wander
138
        ]);
139
    }
140
141
    /**
142
     * @Route("/{id}/edit", name="edit", methods={"GET","POST"})
143
     */
144
    public function edit(Request $request, Wander $wander): Response
145
    {
146
        $form = $this->createForm(WanderType::class, $wander);
147
        $form->handleRequest($request);
148
149
        if ($form->isSubmitted() && $form->isValid()) {
150
            $this->getDoctrine()->getManager()->flush();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

150
            /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager()->flush();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
151
152
            // It seems to be safe to redirect to show with an ID even after
153
            // deletion.
154
            return $this->redirectToRoute('admin_wanders_show', ['id' => $wander->getId()]);
155
        }
156
157
        return $this->render('admin/wander/edit.html.twig', [
158
            'wander' => $wander,
159
            'form' => $form->createView(),
160
        ]);
161
    }
162
163
    /**
164
     * @Route("/{id}", name="delete", methods={"DELETE"})
165
     */
166
    public function delete(Request $request, Wander $wander): Response
167
    {
168
        if ($this->isCsrfTokenValid('delete'.$wander->getId(), $request->request->get('_token'))) {
169
            $entityManager = $this->getDoctrine()->getManager();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

169
            $entityManager = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
170
            $entityManager->remove($wander);
171
            $entityManager->flush();
172
        }
173
174
        return $this->redirectToRoute('admin_wanders_index');
175
    }
176
177
    /**
178
     * @Route("/{id}/delete_images", name="delete_images", methods={"POST"})
179
     */
180
    public function deleteImages(Request $request, Wander $wander): Response
181
    {
182
        if ($this->isCsrfTokenValid('delete_images'.$wander->getId(), $request->request->get('_token'))) {
183
            $entityManager = $this->getDoctrine()->getManager();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Bundle\Framework...ntroller::getDoctrine() has been deprecated: since Symfony 5.4, inject an instance of ManagerRegistry in your controller instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

183
            $entityManager = /** @scrutinizer ignore-deprecated */ $this->getDoctrine()->getManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
184
            $images = $wander->getImages();
185
            foreach ($images as $image) {
186
                $entityManager->remove($image);
187
            }
188
            $entityManager->flush();
189
        }
190
191
        return $this->redirectToRoute('admin_wanders_show', ['id' => $wander->getId()]);
192
    }
193
}
194