1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\ActivityLogBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use FOS\RestBundle\Controller\Annotations\Get; |
16
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
17
|
|
|
use FOS\RestBundle\Routing\ClassResourceInterface; |
18
|
|
|
use Sulu\Bundle\ActivityLogBundle\Compatibility\FieldDescriptor; |
19
|
|
|
use Sulu\Component\ActivityLog\ActivityLogger; |
20
|
|
|
use Sulu\Component\ActivityLog\ActivityLoggerInterface; |
21
|
|
|
use Sulu\Component\Rest\Exception\EntityNotFoundException; |
22
|
|
|
use Sulu\Component\Rest\ListBuilder\ListRepresentation; |
23
|
|
|
use Sulu\Component\Rest\ListBuilder\ListRestHelper; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
27
|
|
|
|
28
|
|
|
class ActivityLogController extends FOSRestController implements ClassResourceInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
const EXPORT_COLUMN_DELIMITER = ';'; |
32
|
|
|
const EXPORT_FILENAME = 'acitivity-log-export'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns all fields that can be used by list. |
36
|
|
|
* |
37
|
|
|
* @Get("activity-log/fields") |
38
|
|
|
* |
39
|
|
|
* @param Request $request |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function getFieldsAction(Request $request) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
// default contacts list |
46
|
|
|
return $this->handleView( |
47
|
|
|
$this->view( |
|
|
|
|
48
|
|
|
array_values( |
49
|
|
|
$this->getFieldDescriptors() |
50
|
|
|
), |
51
|
|
|
200 |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create field-descriptor array. |
58
|
|
|
* |
59
|
|
|
* @return FieldDescriptor[] |
60
|
|
|
*/ |
61
|
|
|
private function getFieldDescriptors() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
'uuid' => new FieldDescriptor('id', 'public.id', true, false), |
65
|
|
|
'data' => new FieldDescriptor('data', 'public.data', false, true), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Shows all activity-log-items |
71
|
|
|
* |
72
|
|
|
* @param Request $request |
73
|
|
|
* |
74
|
|
|
* @Get("activity-log") |
75
|
|
|
* |
76
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
77
|
|
|
*/ |
78
|
|
|
public function cgetAction(Request $request) |
79
|
|
|
{ |
80
|
|
|
$list = $this->getActivityLogs($request); |
81
|
|
|
|
82
|
|
|
$view = $this->view($list, 200); |
83
|
|
|
|
84
|
|
|
return $this->handleView($view); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* returns datagrid list of activity-log for export |
89
|
|
|
* |
90
|
|
|
* @param Request $request |
91
|
|
|
* |
92
|
|
|
* @Get("activity-log/export") |
93
|
|
|
* |
94
|
|
|
* @return Response |
95
|
|
|
*/ |
96
|
|
|
public function getActivityLogExportAction(Request $request) |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$list = $this->getActivityLogs($request); |
100
|
|
|
|
101
|
|
|
return $this->generateCsvResponse($this->listToCsv($list, self::EXPORT_COLUMN_DELIMITER)); |
102
|
|
|
} catch (Exception $e) { |
103
|
|
|
$view = $this->view(array($e->getMessage()), 400); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->handleView($view); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* returns view of files |
111
|
|
|
* |
112
|
|
|
* @param Request $request |
113
|
|
|
* |
114
|
|
|
* @throws EntityNotFoundException |
115
|
|
|
* |
116
|
|
|
* @return ListRepresentation |
117
|
|
|
*/ |
118
|
|
|
private function getActivityLogs(Request $request) |
119
|
|
|
{ |
120
|
|
|
$restHelper = new ListRestHelper($request); |
|
|
|
|
121
|
|
|
|
122
|
|
|
/** @var ActivityLogger $activityLogger */ |
123
|
|
|
$activityLogger = $this->get('sulu_activity_log.activity_logger'); |
124
|
|
|
|
125
|
|
|
$page = (int)$restHelper->getPage(); |
126
|
|
|
$limit = (int)$restHelper->getLimit(); |
127
|
|
|
|
128
|
|
|
$list = $activityLogger->findAll($page, $limit); |
129
|
|
|
$list = array_values($list); |
130
|
|
|
|
131
|
|
|
$list = new ListRepresentation( |
132
|
|
|
$list, |
133
|
|
|
'activity-log-items', |
134
|
|
|
'get_activity_logs', |
135
|
|
|
$request->query->all(), |
136
|
|
|
$page, |
137
|
|
|
$limit, |
138
|
|
|
count($list) |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
return $list; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ListRepresentation $list |
146
|
|
|
* @param string $delimiter |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
private function listToCsv($list, $delimiter) |
151
|
|
|
{ |
152
|
|
|
$data = $list->getInline()->getResources(); |
153
|
|
|
$csv = ''; |
154
|
|
|
$headers = array_keys(reset($data)); |
155
|
|
|
foreach ($headers as $header) { |
156
|
|
|
$csv .= $header . $delimiter; |
157
|
|
|
} |
158
|
|
|
$csv = rtrim($csv, $delimiter) . PHP_EOL; |
159
|
|
|
|
160
|
|
|
// iterate over data array |
161
|
|
|
foreach ($data as $dataline) { |
162
|
|
|
$csv .= $this->addLine($dataline, $delimiter); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $csv; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param array $dataline |
170
|
|
|
* @param string $delimiter |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
private function addLine($dataline, $delimiter) |
175
|
|
|
{ |
176
|
|
|
$csvLine = ''; |
177
|
|
|
foreach ($dataline as $datafield) { |
178
|
|
|
if ($datafield instanceof DateTime) { |
|
|
|
|
179
|
|
|
$csvLine .= $datafield->format(DateTime::ISO8601); |
180
|
|
|
} elseif (is_scalar($datafield)) { |
181
|
|
|
$csvLine .= $datafield; |
182
|
|
|
} |
183
|
|
|
$csvLine .= $delimiter; |
184
|
|
|
} |
185
|
|
|
$csvLine = rtrim($csvLine, $delimiter) . PHP_EOL; |
186
|
|
|
|
187
|
|
|
return $csvLine; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $csv |
192
|
|
|
* |
193
|
|
|
* @return Response |
194
|
|
|
*/ |
195
|
|
|
private function generateCsvResponse($csv) |
196
|
|
|
{ |
197
|
|
|
$response = new Response(); |
198
|
|
|
$response->setContent($csv); |
199
|
|
|
|
200
|
|
|
$name = self::EXPORT_FILENAME . '-' . date('Ymd') . '.csv'; |
201
|
|
|
$disponent = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $name); |
202
|
|
|
$response->headers->set('Content-Disposition', $disponent); |
203
|
|
|
$response->headers->set('Content-Type', 'text/csv'); |
204
|
|
|
|
205
|
|
|
return $response; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.