1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\TransfertPrestataireSiege;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Geocoder\Provider\Nominatim\Nominatim;
use Geocoder\Query\GeocodeQuery;
class AdminController extends Controller
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @Route("/admin/setenable/{type}/{id}", name="setenable")
* @IsGranted("ROLE_ADMIN")
*/
public function setEnableAction($type, $id, Request $request)
{
$referer = $request->headers->get('referer');
if ($referer && !$request->isXmlHttpRequest()) {
return $this->redirect($referer);
} elseif (!$request->isXmlHttpRequest()) {
return new Response('', Response::HTTP_BAD_REQUEST);
}
$status = 'success';
try {
$object = $this->em->getRepository(str_replace('.', ':', $type))->findOneById($id);
if ($object && method_exists($object, 'setEnabled')) {
$object->setEnabled(!$object->isEnabled());
$this->em->persist($object);
$this->em->flush();
} else {
$status = 'error';
$this->get('session')->getFlashBag()->add(
'notice',
'Erreur : veuillez recharger la page et réessayer !'
);
}
} catch (\Exception $e) {
$status = 'error';
$this->get('session')->getFlashBag()->add(
'notice',
'Erreur : veuillez recharger la page et réessayer !'
);
}
return new JsonResponse(array('status' => $status, 'newvalue' => ($object->isEnabled()?'true':'false')));
}
/**
* @Route("/admin/setreconverti/{id}", name="setreconverti")
* @IsGranted({"ROLE_TRESORIER", "ROLE_ADMIN_TRANSFERT_GERER_VIEW"})
*/
public function setReconvertiAction(TransfertPrestataireSiege $object, Request $request)
{
$referer = $request->headers->get('referer');
if ($referer && !$request->isXmlHttpRequest()) {
return $this->redirect($referer);
} elseif (!$request->isXmlHttpRequest()) {
return new Response('', Response::HTTP_BAD_REQUEST);
}
$status = 'success';
try {
if ($object && method_exists($object, 'setReconverti')) {
$object->setReconverti(!$object->getReconverti());
$this->em->persist($object);
$this->em->flush();
} else {
$status = 'error';
}
} catch (\Exception $e) {
$status = 'error';
}
return new JsonResponse(array('status' => $status, 'newvalue' => ($object->getReconverti()?'true':'false')));
}
/**
* @Route("/admin/getcsv", name="getcsv")
* @IsGranted({"ROLE_ADMIN_IMPORT_EDIT", "ROLE_ADMIN_IMPORT_LIST", "ROLE_ADMIN_IMPORT_CREATE", "ROLE_ADMIN_IMPORT_VIEW", "ROLE_ADMIN_IMPORT_DELETE", "ROLE_ADMIN_IMPORT_EXPORT", "ROLE_ADMIN_IMPORT_ALL"})
*/
public function getCsvAction(Request $request)
{
$data = $request->get('data');
$header = $request->get('header');
$response = new StreamedResponse();
$response->setCallback(function () use ($data, $header) {
$handle = fopen('php://output', 'w+');
// Nom des colonnes du CSV
fputcsv($handle, explode(';', $header), ';');
//Champs
foreach ($data as $row) {
// fwrite($handle, $row);
fputcsv($handle, explode(';', $row), ';');
}
fclose($handle);
});
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
return $response;
}
/**
* @Route("/admin/geoloc", name="geolocAdresse")
* @IsGranted("ROLE_ADMIN")
*/
public function geoLocAction(Request $request)
{
$referer = $request->headers->get('referer');
if ($referer && !$request->isXmlHttpRequest()) {
return $this->redirect($referer);
} elseif (!$request->isXmlHttpRequest()) {
return new Response('', Response::HTTP_BAD_REQUEST);
}
$status = 'success';
$return = null;
if (!empty($request->get('cpostal')) && !empty($request->get('ville'))) {
try {
// GEOCODING ADDRESS :
$httpClient = new \Http\Adapter\Guzzle6\Client();
$provider = Nominatim::withOpenStreetMapServer($httpClient, 'Mozilla/5.0');
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'fr');
// Query geocoding from complete address
$result = $geocoder->geocodeQuery(GeocodeQuery::create($request->get('adresse').' '.$request->get('cpostal').' '.$request->get('ville')));
if (count($result) > 0) {
$coords = $result->first()->getCoordinates();
$return = ['lat' => $coords->getLatitude(), 'lon' => $coords->getLongitude()];
} else {
$result = $geocoder->geocodeQuery(GeocodeQuery::create($request->get('cpostal').' '.$request->get('ville')));
if (count($result) > 0) {
$coords = $result->first()->getCoordinates();
$return = ['lat' => $coords->getLatitude(), 'lon' => $coords->getLongitude()];
}
}
} catch (\Exception $e) {
$status = 'error';
}
} else {
$status = 'error';
}
return new JsonResponse(array('status' => $status, 'data' => $return));
}
}