-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanifest-export.php
54 lines (49 loc) · 1.69 KB
/
manifest-export.php
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
<?php
/** @var \ORCA\OrcaSpecimenTracking\OrcaSpecimenTracking $module */
try {
// get system configuration
$system_config = $module->getConfiguration($module->getProjectId());
// if any errors
if (!empty($system_config["errors"])) {
// end the request and send back the error(s)
$module->sendError($system_config["errors"]);
} else {
// establish context
$module->setConfigProjectContext($system_config);
}
// ensure a shipment record_id was provided
if (!isset($_GET["id"]) || !is_numeric($_GET["id"])) {
$module->sendError("Cannot export manifest - shipment_id is invalid or missing.");
}
// get the module config
$module_config = $module->getModuleConfig() ?? [];
// get the data
[ $shipment, $data ] = $module->getShipmentManifestData($_GET["id"]);
// dump data to csv
$temp_path = $module->generateTempFileName(5);
$temp_output = fopen($temp_path, 'c');
// headers
$file_headers = array_map(function($k) use ($module_config) {
$tmp = $k;
// TODO switch to use defined values in module config
// switch ($k) {
// case "volume":
// $tmp = "$sample_type ($sample_unit)";
// break;
// }
return $tmp;
}, array_keys(reset($data)));
fputcsv($temp_output, $file_headers);
// rows
foreach ($data as $i => $row) {
fputcsv($temp_output, $row);
}
// close the stream
fclose($temp_output);
// filename
$file_name = "manifest-" . date("Ymd_His") . ".csv";
// output file
$module->downloadFile($temp_path, $file_name);
} catch (Exception $ex) {
$module->sendError($ex->getMessage());
}