Passed
Pull Request — master (#347)
by Mirko
09:07
created

FieldNotesController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 122
rs 10
wmc 14
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
C indexAction() 0 50 8
A deleteAction() 0 19 2
B deleteMultipleAction() 0 27 4
1
<?php
2
3
namespace AppBundle\Controller\FieldNotes;
4
5
use AppBundle\Controller\AbstractController;
6
use AppBundle\Form\DataProvider\UploadFieldNotesDataProvider;
7
use AppBundle\Form\UploadFieldNotesType;
8
use AppBundle\Util\DateUtil;
9
use DateTime;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class FieldNotesController extends AbstractController
14
{
15
    /**
16
     * @Route("/field-notes/", name="field-notes")
17
     *
18
     * @param \Symfony\Component\HttpFoundation\Request $request
19
     *
20
     * @return \Symfony\Component\HttpFoundation\Response
21
     */
22
    public function indexAction(Request $request)
23
    {
24
        $this->denyAccessUnlessGranted('ROLE_USER');
25
        $user = $this->getUser();
26
27
        $fieldNoteService = $this->get('app.service.field_note');
28
        $dataProvider = $this->get('app.dataprovider.upload_field_note');
29
30
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
31
        $fieldNotes = $repository->findBy(['user' => $user->getId()], ['date' => 'ASC', 'id' => 'ASC']);
32
33
        $form = $this->createForm(UploadFieldNotesType::class, $dataProvider->getData($user->getId()));
34
        $form->handleRequest($request);
35
        if ($form->isSubmitted() && $form->isValid()) {
36
            /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
37
            $file = $form->getData()[UploadFieldNotesType::FIELD_FILE];
38
            try {
39
                $ignoreDate = null;
40
                if ($form->getData()[UploadFieldNotesType::FIELD_IGNORE] && $form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE]) {
41
                    $ignoreDate = DateUtil::dateTimeFromMySqlFormat($form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE]);
42
                }
43
                $fieldNoteService->importFromFile($file->getRealPath(), $user->getId(), $ignoreDate);
44
            } catch (\Exception $e) {
45
                $this->addErrorMessage($e->getMessage());
46
47
                return $this->redirectToRoute('field-notes');
48
            }
49
            if ($fieldNoteService->hasErrors()) {
50
                foreach ($fieldNoteService->getErrors() as $error) {
51
                    $this->addErrorMessage($error);
52
                }
53
54
                return $this->redirectToRoute('field-notes');
55
            }
56
            $this->addSuccessMessage(
57
                $this->get('translator')->trans('field_notes.upload.success')
58
            );
59
60
            return $this->redirectToRoute('field-notes');
61
        }
62
63
        $this->setMenu(MNU_MYPROFILE_FIELD_NOTES);
64
        $this->setTitle($this->get('translator')->trans('field_notes.field_notes'));
65
66
        return $this->render('field-notes/index.html.twig', [
67
            'user' => $user,
68
            'form' => $form->createView(),
69
            'fieldNotes' => $fieldNotes,
70
        ]);
71
    }
72
73
    /**
74
     * @Route("/field-notes/delete/{id}", name="field-notes.delete")
75
     *
76
     * @param int $id
77
     *
78
     * @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...
79
     */
80
    public function deleteAction($id)
81
    {
82
        $this->denyAccessUnlessGranted('ROLE_USER');
83
        $user = $this->getUser();
84
85
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
86
        $fieldNote = $repository->findOneBy(['user' => $user->getId(), 'id' => $id]);
87
        if (!$fieldNote) {
88
            return $this->redirectToRoute('field-notes');
89
        }
90
        $em = $this->getDoctrine()->getEntityManager();
91
        $em->remove($fieldNote);
92
        $em->flush();
93
        $this->addSuccessMessage(
94
            $this->get('translator')->trans('field_notes.success.deleted')
95
        );
96
97
        return $this->redirectToRoute('field-notes');
98
    }
99
100
    /**
101
     * @Route("/field-notes/delete-multiple/", name="field-notes.delete-multiple")
102
     *
103
     * @param \Symfony\Component\HttpFoundation\Request $request
104
     *
105
     * @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...
106
     */
107
    public function deleteMultipleAction(Request $request)
108
    {
109
        $this->denyAccessUnlessGranted('ROLE_USER');
110
        $user = $this->getUser();
111
112
        $selectedFieldNotes = $request->get('selected-field-notes');
113
        if (!is_array($selectedFieldNotes)) {
114
            return $this->redirectToRoute('field-notes');
115
        }
116
117
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
118
        $em = $this->getDoctrine()->getEntityManager();
119
        foreach ($selectedFieldNotes as $fieldNoteId) {
120
            $fieldNote = $repository->findOneBy(['user' => $user->getId(), 'id' => $fieldNoteId]);
121
            if (!$fieldNote) {
122
                continue;
123
            }
124
            $em->remove($fieldNote);
125
        }
126
        $em->flush();
127
128
        $this->addSuccessMessage(
129
            $this->get('translator')->trans('field_notes.success.deleted_multiple')
130
        );
131
132
        return $this->redirectToRoute('field-notes');
133
    }
134
}
135