-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportActivityRecordsController.php
More file actions
107 lines (86 loc) · 2.82 KB
/
Copy pathExportActivityRecordsController.php
File metadata and controls
107 lines (86 loc) · 2.82 KB
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
<?php
namespace Drupal\entity_activity_tracker\Controller;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* Class ExportActivityRecordsController.
*/
class ExportActivityRecordsController extends ControllerBase {
/**
* Database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The filesystem settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $filesystemSettings;
/**
* Constructs a new GroupMembershipController.
*
* @param \Drupal\Core\Database\Connection $database
* The active database connection.
* @param \Drupal\Core\Config\ImmutableConfig $filesystem_settings
* The 'system.file' config.
*/
public function __construct(Connection $database, ImmutableConfig $filesystem_settings) {
$this->database = $database;
$this->filesystemSettings = $filesystem_settings;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database'),
$container->get('config.factory')->get('system.file')
);
}
/**
* Export.
*
* @return \Symfony\Component\HttpFoundation\Response
* Return Activity export in CSV.
*/
public function export() {
// @TODO: Move it to ActivityRecords storage.
// Get everything from activity table;.
$query = $this->database->select('entity_activity_tracker', 'fa')
->fields('fa');
if ($first_row = $query->execute()->fetchAssoc()) {
$temp_dir = $this->filesystemSettings->get('path.temporary');
$temp_file = tempnam($temp_dir, 'activity_csv_export_');
// Export to .CSV.
$fp = fopen($temp_file, 'w');
$headers = array_keys((array) $first_row);
// Put the headers.
fputcsv($fp, $headers);
$rows = $query->execute()->fetchAllAssoc('activity_id', \PDO::FETCH_ASSOC);
foreach ($rows as $row) {
// Push the rest.
fputcsv($fp, array_values((array) $row));
}
fclose($fp);
$file_content = file_get_contents($temp_file);
unlink($temp_file);
$time = date('d_m_Y');
$filename = "activity_export_{$time}.csv";
$response = new Response($file_content);
$content_disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', $content_disposition);
return $response;
}
return [
'#type' => 'markup',
'#markup' => $this->t('Nothing to export.'),
];
}
}