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