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