1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fmaj\LaposteDatanovaBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Fmaj\LaposteDatanovaBundle\Model\Download; |
6
|
|
|
use Fmaj\LaposteDatanovaBundle\Model\Search; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
class RecordsController extends Controller |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $dataset |
14
|
|
|
* @param string $query |
15
|
|
|
* @param string $sort |
16
|
|
|
* @param int $rows |
17
|
|
|
* @param int $start |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function searchAction($dataset, $query, $sort, $rows, $start) |
22
|
|
|
{ |
23
|
|
|
$search = new Search($dataset); |
24
|
|
|
$search |
25
|
|
|
->setFilter($query) |
26
|
|
|
->setStart($start) |
27
|
|
|
->setSort($sort) |
28
|
|
|
->setRows($rows); |
29
|
|
|
$results = $this->search($search); |
30
|
|
|
|
31
|
|
|
return new Response(json_encode($results)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $dataset |
36
|
|
|
* @param string $_format |
37
|
|
|
* @param string $query |
38
|
|
|
* |
39
|
|
|
* @return Response |
40
|
|
|
*/ |
41
|
|
|
public function downloadAction($dataset, $_format, $query) |
42
|
|
|
{ |
43
|
|
|
$response = new Response(); |
44
|
|
|
$download = new Download($dataset, $_format); |
45
|
|
|
$download->setFilter($query); |
46
|
|
|
$local = $this->getLocalDataset($download); |
47
|
|
|
if (null !== $local) { |
48
|
|
|
$results = $local; |
49
|
|
|
} else { |
50
|
|
|
$results = $this->download($download); |
51
|
|
|
} |
52
|
|
|
switch (strtolower($_format)) { |
53
|
|
|
case 'json': |
54
|
|
|
$results = json_encode($results); |
55
|
|
|
break; |
56
|
|
|
case 'csv': |
57
|
|
|
$response->headers->set('Content-Type', 'text/csv'); |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
$response->setContent($results); |
61
|
|
|
$response->headers->set( |
62
|
|
|
'Content-Disposition', |
63
|
|
|
sprintf('attachment; filename="%s.%s"', $dataset, $_format) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Search $search |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
private function search(Search $search) |
75
|
|
|
{ |
76
|
|
|
return $this |
77
|
|
|
->get('data_nova.manager.records') |
78
|
|
|
->search($search); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Download $download |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function download(Download $download) |
87
|
|
|
{ |
88
|
|
|
return $this |
89
|
|
|
->get('data_nova.manager.records') |
90
|
|
|
->download($download); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Download $download |
95
|
|
|
* |
96
|
|
|
* @return null|string |
97
|
|
|
*/ |
98
|
|
|
private function getLocalDataset(Download $download) |
99
|
|
|
{ |
100
|
|
|
return $this |
101
|
|
|
->get('data_nova.manager.records') |
102
|
|
|
->getLocalDatasetContent($download); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|