Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

FieldNotesController::indexAction()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 22
nc 4
nop 1
dl 0
loc 40
rs 8.439
c 2
b 1
f 0
1
<?php
2
3
namespace Oc\FieldNotes\Controller;
4
5
use Oc\AbstractController;
6
use Oc\FieldNotes\Form\UploadFormDataFactory;
7
use Oc\FieldNotes\Import\ImportService;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
8
use Oc\FieldNotes\Form\UploadType;
9
use Oc\FieldNotes\Persistence\FieldNoteService;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
/**
17
 * Class FieldNotesController
18
 *
19
 * @package Oc\FieldNotes\Controller
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
20
 */
21
class FieldNotesController extends AbstractController
22
{
23
    /**
24
     * @var ImportService
0 ignored issues
show
introduced by
ImportService => \Oc\FieldNotes\Import\ImportService
Loading history...
25
     */
26
    private $importService;
27
28
    /**
29
     * @var FieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
30
     */
31
    private $fieldNoteService;
32
33
    /**
34
     * @var TranslatorInterface
0 ignored issues
show
introduced by
TranslatorInterface => \Symfony\Component\Translation\TranslatorInterface
Loading history...
35
     */
36
    private $translator;
37
    /**
38
     * @var UploadFormDataFactory
0 ignored issues
show
introduced by
UploadFormDataFactory => \Oc\FieldNotes\Form\UploadFormDataFactory
Loading history...
39
     */
40
    private $formDataFactory;
0 ignored issues
show
introduced by
Expected 1 blank line before member var with comment; 0 found
Loading history...
41
42
    /**
43
     * FieldNotesController constructor.
44
     *
45
     * @param ImportService $importService
0 ignored issues
show
introduced by
ImportService => \Oc\FieldNotes\Import\ImportService
Loading history...
46
     * @param FieldNoteService $fieldNoteService
0 ignored issues
show
introduced by
FieldNoteService => \Oc\FieldNotes\Persistence\FieldNoteService
Loading history...
47
     * @param UploadFormDataFactory $formDataFactory
0 ignored issues
show
introduced by
UploadFormDataFactory => \Oc\FieldNotes\Form\UploadFormDataFactory
Loading history...
48
     * @param TranslatorInterface $translator
0 ignored issues
show
introduced by
TranslatorInterface => \Symfony\Component\Translation\TranslatorInterface
Loading history...
49
     */
50
    public function __construct(
51
        ImportService $importService,
52
        FieldNoteService $fieldNoteService,
53
        UploadFormDataFactory $formDataFactory,
54
        TranslatorInterface $translator
55
    ) {
56
        $this->importService = $importService;
57
        $this->fieldNoteService = $fieldNoteService;
58
        $this->translator = $translator;
59
        $this->formDataFactory = $formDataFactory;
60
    }
61
62
    /**
63
     * Index action for field-notes.
64
     *
65
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
66
     *
67
     * @return Response
0 ignored issues
show
introduced by
Response => \Symfony\Component\HttpFoundation\Response
Loading history...
68
     *
69
     * @Route("/field-notes/", name="field-notes")
70
     */
71
    public function indexAction(Request $request)
72
    {
73
        $this->denyAccessUnlessGranted('ROLE_USER');
74
75
        $this->setMenu(MNU_MYPROFILE_FIELD_NOTES);
76
        $this->setTitle($this->translator->trans('field_notes.field_notes'));
77
78
        $user = $this->getUser();
79
80
        $fieldNotes = $this->fieldNoteService->getUserListing($user->getId());
81
82
        $fieldNoteFormData = $this->formDataFactory->create($user->getId());
83
84
        $form = $this->createForm(UploadType::class, $fieldNoteFormData);
85
        $form->handleRequest($request);
86
87
        if ($form->isSubmitted() && $form->isValid()) {
88
            $formHandleContext = $this->importService->handleFormData($fieldNoteFormData);
89
90
            if (!$formHandleContext->isSuccess()) {
91
                foreach ($formHandleContext->getErrors() as $error) {
92
                    $this->addErrorMessage($error);
93
                }
94
95
                return $this->redirectToRoute('field-notes');
96
            }
97
98
            $this->addSuccessMessage(
99
                $this->translator->trans('field_notes.upload.success')
100
            );
101
102
            return $this->redirectToRoute('field-notes');
103
        }
104
105
        return $this->render('field-notes/index.html.twig', [
106
            'user' => $user,
107
            'form' => $form->createView(),
108
            'fieldNotes' => $fieldNotes
109
        ]);
110
    }
111
112
    /**
113
     * Action to delete one field-note.
114
     *
115
     * @param int $id
116
     *
117
     * @return RedirectResponse
0 ignored issues
show
introduced by
RedirectResponse => \Symfony\Component\HttpFoundation\RedirectResponse
Loading history...
118
     *
119
     * @Route("/field-notes/delete/{id}", name="field-notes.delete")
120
     */
121
    public function deleteAction($id)
122
    {
123
        $this->denyAccessUnlessGranted('ROLE_USER');
124
        $user = $this->getUser();
125
126
        $fieldNote = $this->fieldNoteService->fetchOneBy([
127
            'id' => $id,
128
            'user_id' => $user->getId()
129
        ]);
130
131
        if ($fieldNote === null) {
132
            return $this->redirectToRoute('field-notes');
133
        }
134
135
        $this->fieldNoteService->remove($fieldNote);
136
137
        $this->addSuccessMessage(
138
            $this->translator->trans('field_notes.success.deleted')
139
        );
140
141
        return $this->redirectToRoute('field-notes');
142
    }
143
144
    /**
145
     * Action to delete multiple field-notes.
146
     *
147
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
148
     *
149
     * @return RedirectResponse
0 ignored issues
show
introduced by
RedirectResponse => \Symfony\Component\HttpFoundation\RedirectResponse
Loading history...
150
     *
151
     * @Route("/field-notes/delete-multiple/", name="field-notes.delete-multiple")
152
     */
153
    public function deleteMultipleAction(Request $request)
154
    {
155
        $this->denyAccessUnlessGranted('ROLE_USER');
156
        $user = $this->getUser();
157
158
        $selectedFieldNotes = $request->get('selected-field-notes');
159
        if (!is_array($selectedFieldNotes)) {
160
            return $this->redirectToRoute('field-notes');
161
        }
162
163
        foreach ($selectedFieldNotes as $fieldNoteId) {
164
            $fieldNote = $this->fieldNoteService->fetchOneBy([
165
                'id' => $fieldNoteId,
166
                'user_id' => $user->getId()
167
            ]);
168
169
            if ($fieldNote === null) {
170
                continue;
171
            }
172
173
            $this->fieldNoteService->remove($fieldNote);
174
        }
175
176
        $this->addSuccessMessage(
177
            $this->translator->trans('field_notes.success.deleted_multiple')
178
        );
179
180
        return $this->redirectToRoute('field-notes');
181
    }
182
}
183