-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_formats_reports.module
128 lines (116 loc) · 3.94 KB
/
media_formats_reports.module
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
<?php
use Drupal\Core\Url;
/**
* @file
* Contains the media_formats_reports.module.
*/
/**
* Implements hook_theme().
*/
function media_formats_reports_theme($existing, $type, $theme, $path) {
return [
'media_formats_reports_chart' => [
'variables' => ['form' => null, 'show_csv_link' => null],
],
];
}
/**
* Default preprocessor function for the media_formats_reports_theme hook.
*/
function template_preprocess_media_formats_reports_chart(&$variables) {
$variables['attributes'] = [
'id' => ['media_formats_reports_chart'],
];
if ($variables['show_csv_link']) {
$files_path = \Drupal::service('file_system')->realpath(file_default_scheme() . "://");
$report_url = file_create_url(file_default_scheme() . "://media_types.csv");
$variables['csv_url'] = $report_url;
}
}
/**
* Implements hook_page_attachments().
*/
function media_formats_reports_page_attachments(array &$attachments) {
$current_path = \Drupal::service('path.current')->getPath();
if ($current_path == '/admin/reports/media_formats') {
$pie_chart_data = media_formats_reports_get_data();
$attachments['#attached']['library'][] = 'media_formats_reports/media_formats_reports_chart';
$attachments['#attached']['drupalSettings']['media_formats_reports']['chart_data'] = $pie_chart_data;
}
}
/**
* @return object
* A Chart.js dataset object.
*/
function media_formats_reports_get_data() {
if ($tempstore = \Drupal::service('user.private_tempstore')->get('media_formats_reports')) {
$report_type = $tempstore->get('media_formats_reports_report_type');
}
// Set safe default value.
if (empty($report_type)) {
$report_type = 'mimetype';
}
$config = \Drupal::config('media_formats_reports.settings');
$cache_data = $config->get('media_formats_reports_cache_report_data');
$format_counts = &drupal_static(__FUNCTION__);
$cid = 'media_formats_reports_format_counts_' . $report_type;
if ($cache_data && $cache = \Drupal::cache()->get($cid)) {
$format_counts = $cache->data;
}
else {
$data_source_service_id = 'media_formats_reports.datasource.' . $report_type;
$data_source = \Drupal::service($data_source_service_id);
$format_counts = $data_source->getData();
if ($cache_data) {
\Drupal::cache()->set($cid, $format_counts);
}
}
// Populate the Chart.js dataset object.
$num_formats = count($format_counts);
$colors = media_formats_reports_get_chart_colors($num_formats);
$dataset = new StdClass();
$dataset->data = array_values($format_counts);
$dataset->backgroundColor = media_formats_reports_get_chart_colors($num_formats);
$pie_chart_data = array(
'labels' => array_keys($format_counts),
'datasets' => array($dataset),
);
// Write out a CSV for download.
if ($tempstore->get('media_formats_reports_generate_csv')) {
$files_path = \Drupal::service('file_system')->realpath(file_default_scheme() . "://");
$csv_data = [['Format', 'Count']];
foreach ($format_counts as $format => $count) {
$csv_data[] = [$format, $count];
}
$fp = fopen($files_path . '/media_types.csv', 'w');
foreach ($csv_data as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
// We're finished with this session variable, so clear it for
// the next rendering of the report page.
$tempstore->delete('media_formats_reports_generate_csv');
return $pie_chart_data;
}
/**
* Generate a set of random colors to use in the pie chart.
*
* @param int $length
* The length of the array to generate.
*
* @return array
* An array of RGB values in the format required by Chart.js, e.g.,
* array('rgba(255, 99, 132)', 'rgba(54, 162, 235)', 'rgba(255, 206, 86)').
*/
function media_formats_reports_get_chart_colors($length) {
$colors = [];
for ($i = 1; $i <= $length; $i++) {
$rgb_color = [];
foreach (['r', 'g', 'b'] as $color) {
$rgb_color[$color] = rand(0, 255);
}
$colors[] = 'rgba(' . implode(',', $rgb_color) . ')';
}
return $colors;
}