-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_reports.module
98 lines (93 loc) · 2.61 KB
/
custom_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
<?php
/**
* Implements hook_menu().
*/
function custom_reports_menu() {
$items['admin/reports/custom'] = array(
'title' => t('Custom reports'),
'description' => t('Download custom reports exposed by various modules on your site.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_reports_list_form'),
'access arguments' => array('access site reports'),
'file' => 'custom_reports.admin.inc',
);
$items['admin/reports/custom/list'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => t('List'),
);
$items['admin/reports/custom/history'] = array(
'type' => MENU_LOCAL_TASK,
'title' => t('History'),
'page callback' => 'custom_reports_history',
'access arguments' => array('access site reports'),
'file' => 'custom_reports.admin.inc',
'weight' => 1,
);
return $items;
}
/**
* Implements hook_file_download().
*/
function custom_reports_file_download($filepath) {
// Serve up reports to users with proper permission
if (file_check_location($filepath, file_create_path('custom_reports'))) {
if (user_access('access site reports')) {
return array(
'Content-Type: text/csv',
'Content-Length: ' . filesize($filepath),
'Content-Disposition: attachment; filename="' . basename($filepath) . '"',
);
} else {
return -1;
}
}
return null;
}
/**
* Implements hook_token_values().
*/
function custom_reports_token_values($type, $object = NULL) {
if ($type == 'custom_reports') {
$report = $object;
$tokens['date'] = date($report['date_format']);
$tokens['title'] = $report['title'];
foreach ($report['args'] as $key => $value) {
if (is_array($value)) {
$value = join('-', $value);
}
$tokens[$key] = $value;
}
return $tokens;
}
}
/**
* Converts array to CSV.
*/
function custom_reports_array_to_csv($rows, $header) {
$output = '';
if ($header) {
array_unshift($rows, array_keys($rows[0]));
}
foreach ($rows as $key1 => $value1) {
$row = array();
foreach ($value1 as $key2 => $value2) {
if (preg_match("/^ |\n|,|\"| $/", $value2)) {
$value2 = '"' . str_replace('"', '""', $value2) . '"';
}
$row[] = $value2;
}
$output .= implode(',', $row) . "\n";
}
return $output;
}
/**
* Downloads a CSV file.
*/
function custom_reports_download_csv($output, $filename) {
// Download CSV
drupal_set_header('Content-Type: text/csv');
drupal_set_header('Content-Disposition: attachment; filename="' . str_replace('"', '', $filename) . '"');
drupal_set_header('Cache-Control: private');
echo $output;
exit;
}