Passed
Pull Request — master (#347)
by Mirko
08:35
created

FieldNotesController::deleteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace AppBundle\Controller\FieldNotes;
4
5
use AppBundle\Controller\AbstractController;
6
use AppBundle\Form\UploadFieldNotesType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class FieldNotesController extends AbstractController
11
{
12
    /**
13
     * @Route("/field-notes/", name="field-notes")
14
     *
15
     *
16
     * @param \Symfony\Component\HttpFoundation\Request $request
17
     *
18
     * @return \Symfony\Component\HttpFoundation\Response
19
     */
20
    public function indexAction(Request $request)
21
    {
22
        $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
23
        $user = $this->getUser();
24
25
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
26
        $fieldNotes = $repository->findBy(['user' => $user->getId()], ['date' => 'ASC']);
27
28
        $form = $this->createForm(UploadFieldNotesType::class);
29
        $form->handleRequest($request);
30
        if ($form->isSubmitted() && $form->isValid()) {
31
            /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
32
            $file = $form->getData()[UploadFieldNotesType::FIELD_FILE];
33
            $fieldNoteService = $this->get('app.service.field_note');
34
            try {
35
                $fieldNoteService->importFromFile($file->getRealPath(), $user->getId());
36
            } catch (\Exception $e) {
37
                $this->addErrorMessage($e->getMessage());
38
39
                return $this->redirectToRoute('field-notes');
40
            }
41
            if ($fieldNoteService->hasErrors()) {
42
                foreach ($fieldNoteService->getErrors() as $error) {
43
                    $this->addErrorMessage($error);
44
                }
45
46
                return $this->redirectToRoute('field-notes');
47
            }
48
            $this->addSuccessMessage($this->get('translator')->trans('Field Notes successfully uploaded.'));
49
50
            return $this->redirectToRoute('field-notes');
51
        }
52
53
        $this->setMenu(MNU_MYPROFILE_FIELD_NOTES);
54
        $this->setTitle($this->get('translator')->trans('Field Notes'));
55
56
        return $this->render('field-notes/index.html.twig', [
57
            'user' => $user,
58
            'form' => $form->createView(),
59
            'fieldNotes' => $fieldNotes,
60
        ]);
61
    }
62
63
    /**
64
     * @Route("/field-notes/delete/{id}", name="field-notes.delete")
65
     * @param int $id
66
     *
67
     * @return \Symfony\Component\HttpFoundation\RedirectResponse;
0 ignored issues
show
Documentation introduced by
The doc-type \Symfony\Component\HttpF...ation\RedirectResponse; could not be parsed: Expected "|" or "end of type", but got ";" at position 50. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
68
     */
69
    public function deleteAction($id)
70
    {
71
        $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
72
        $user = $this->getUser();
73
74
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
75
        $fieldNote = $repository->findOneBy(['user' => $user->getId(), 'id' => $id]);
76
        $em = $this->getDoctrine()->getEntityManager();
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Bundle\Doctrine...try::getEntityManager() has been deprecated.

This method has been deprecated.

Loading history...
77
        $em->remove($fieldNote);
78
        $em->flush();
79
        $this->addSuccessMessage($this->get('translator')->trans('Field note deleted.'));
80
81
        return $this->redirectToRoute('field-notes');
82
    }
83
}
84