-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_reports.admin.inc
225 lines (198 loc) · 7.02 KB
/
custom_reports.admin.inc
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
// Uncomment the following line to include some sample reports:
// include 'custom_reports.sample.inc';
/**
* Lists available reports.
*/
function custom_reports_list_form(&$form_state) {
$options = $storable = $elements = array();
foreach (custom_reports_get_reports() as $report) {
if (!$report['active']) {
continue;
}
// Add report to list
$options[$report['id']] = $report['title'];
// Add link to report history if stored
if ($report['store']) {
$options[$report['id']] .= ' - ' . l('history', "admin/reports/custom/history/{$report['id']}");
}
// Keep track of storable reports, so we can toggle the "run as test" checkbox
if ($report['store']) {
$storable[] = $report['id'];
}
// Add report's custom form elements (using a relevant namespace)
if (!empty($report['form'])) {
foreach ($report['form'] as $key => $element) {
$form["{$report['id']}_{$key}"] = $element;
$elements[$report['id']][] = "{$report['id']}_{$key}";
}
}
}
$form['report'] = array(
'#type' => 'radios',
'#title' => t('Report'),
'#options' => $options,
'#required' => TRUE,
'#weight' => -1,
);
$form['test'] = array(
'#type' => 'checkbox',
'#title' => t('Run this report as a test'),
'#description' => t('When checked, the report will not be saved to the file system.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Generate'),
);
// Add javascript to toggle certain form fields
drupal_add_js(drupal_get_path('module', 'custom_reports') . '/custom_reports.js');
drupal_add_js(array('custom_reports_storable' => $storable, 'custom_reports_elements' => $elements), 'setting');
return $form;
}
/**
* Generates chosen report.
*/
function custom_reports_list_form_submit($form, &$form_state) {
$reports = custom_reports_get_reports();
$report = $reports[$form_state['values']['report']];
// Make sure callback exists
if (!function_exists($report['callback'])) {
form_set_error('report', t('The callback for the !title report does not exist.', array('!title' => $report['title'])));
return;
}
// Prepare callback args
$args[0] =& $report;
foreach ($report['form'] as $key => $element) {
$args[] = $report['args'][$key] = $form_state['values']["{$report['id']}_{$key}"];
}
// Generate results from report's callback function
$report['results'] = call_user_func_array($report['callback'], $args);
// Make sure report generated results
if (empty($report['results'])) {
$message = 'The !title report generated no results.';
$args['!title'] = $report['title'];
if ($report['store']) {
$message .= ' Please check the <a href="@link">report history</a>.';
$args['@link'] = url("admin/reports/custom/history/{$report['id']}");
}
form_set_error('report', t($message, $args));
return;
}
// Convert results into CSV
$report['csv'] = custom_reports_array_to_csv($report['results'], $report['header']);
// Replace tokens in filename
$report['filename'] = token_replace($report['filename'], 'custom_reports', $report);
// If report is storaable, and not running as a test
if ($report['store'] and !$form_state['values']['test']) {
// Get/create "custom_reports" directory
$directory = file_create_path('custom_reports');
file_check_directory($directory, FILE_CREATE_DIRECTORY);
// Protect stored reports from public access using .htaccess file
$htaccess_file = $directory . '/.htaccess';
$htaccess_lines = "order allow,deny\ndeny from all\n";
if (!is_file($htaccess_file) or strpos(file_get_contents($htaccess_file), $htaccess_lines) === FALSE) {
file_save_data($htaccess_lines, $htaccess_file);
chmod($htaccess_file, 0644);
}
// Get/create specific report directory
$directory .= '/' . $report['id'];
file_check_directory($directory, FILE_CREATE_DIRECTORY);
// Save file and verify it was saved before proceeding
$path = file_save_data($report['csv'], $directory . '/' . $report['filename'], FILE_EXISTS_RENAME);
if (!$path) {
form_set_error('report', t('The report could not be saved to !path.', array('!path' => $directory . '/' . $report['filename'])));
return;
}
// Let modules perform post-save processing of report
module_invoke_all('custom_reports_stored', $report);
}
// Download!
custom_reports_download_csv($report['csv'], $report['filename']);
}
/**
* Returns list of available reports.
*/
function custom_reports_get_reports() {
$reports = array();
// Invoke hook_custom_reports_info()
foreach (module_invoke_all('custom_reports_info') as $report) {
// // Id must be defined
if (empty($report['id'])) {
continue;
}
// Callback defaults to id
if (!isset($report['callback'])) {
$report['callback'] = $report['id'];
}
// Title defaults to id
if (!isset($report['title'])) {
$report['title'] = $report['id'];
}
// Associative defaults to TRUE
if (!isset($report['associative'])) {
$report['associative'] = TRUE;
}
// Header rows are only available if associative is TRUE
if (!$report['associative']) {
$report['header'] = FALSE;
}
// Header defaults to TRUE (except if associative)
if (!isset($report['header'])) {
$report['header'] = TRUE;
}
// Filename defaults to something like "YYYY-MM-DD Report Title.csv"
if (!isset($report['filename'])) {
$report['filename'] = '[date] [title].csv';
}
// Set default date format.
if (!isset($report['date_format'])) {
$report['date_format'] = 'Y-m-d';
}
// Store defaults to FALSE
if (!isset($report['store'])) {
$report['store'] = FALSE;
}
// Form defaults to an empty array
if (!isset($report['form'])) {
$report['form'] = array();
}
// Active defaults to TRUE
if (!isset($report['active'])) {
$report['active'] = TRUE;
}
$report['args'] = array();
$reports[$report['id']] = $report;
}
return $reports;
}
/**
* Displays list of previously generated reports.
* Downloadable via system file download only.
*/
function custom_reports_history($report = NULL) {
$reports = custom_reports_get_reports();
// Determine path
$directories[] = 'custom_reports';
if ($report) {
$directories[] = $report;
}
$path = file_create_path(join('/', $directories));
// Get files in path
$files = file_scan_directory($path, '.*');
if (empty($files)) {
return t('No history is available for this report.');
}
krsort($files);
// Table header
$header = array(t('Report'), t('Filename'));
// Table rows
foreach ($files as $file) {
$directory = basename(dirname($file->filename));
$directory_name = isset($reports[$directory]) ? $reports[$directory]['title'] : $directory;
$rows[] = array(
l($directory_name, "admin/reports/custom/history/{$directory}"),
l($file->basename, "system/files/{$file->filename}"),
);
}
return theme('table', $header, $rows);
}