mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Change CSV format to one animal per line, columns specific to date
This commit is contained in:
parent
ee480ec590
commit
37dacdb43e
1 changed files with 115 additions and 13 deletions
|
@ -206,10 +206,12 @@ function farm_livestock_weight_report_form_submit($form, &$form_state) {
|
|||
$start_date = strtotime($form_state['values']['start_date']);
|
||||
$end_date = strtotime($form_state['values']['end_date']);
|
||||
|
||||
// Prepare CSV file.
|
||||
drupal_add_http_header('Content-Type', 'text/csv');
|
||||
drupal_add_http_header('Content-Disposition', 'attachment; filename=animal_weights.csv');
|
||||
$csvdata = 'AssetID, AssetName, Weight, Units, Timestamp, GroupName' . PHP_EOL;
|
||||
// Array to store dates of animal weight logs for CSV export.
|
||||
$date_format = 'Y-m-d';
|
||||
$log_dates = array();
|
||||
|
||||
// Array to store animals and log data
|
||||
$animals = array();
|
||||
|
||||
// Loop through each group, its members, and all weight logs.
|
||||
foreach ($group_ids as $id) {
|
||||
|
@ -229,6 +231,9 @@ function farm_livestock_weight_report_form_submit($form, &$form_state) {
|
|||
continue;
|
||||
}
|
||||
|
||||
// Save the animal info
|
||||
$asset->group_name = $group_name;
|
||||
|
||||
// Load the group member's weights
|
||||
$time = REQUEST_TIME;
|
||||
if (!empty($end_date)) {
|
||||
|
@ -236,8 +241,13 @@ function farm_livestock_weight_report_form_submit($form, &$form_state) {
|
|||
}
|
||||
$logs = farm_quantity_log_asset($asset, 'weight', NULL, $time, TRUE, NULL, FALSE);
|
||||
|
||||
// Array to save data from animal logs.
|
||||
$all_log_data = array();
|
||||
|
||||
// Loop through weight logs.
|
||||
foreach ($logs as $log) {
|
||||
// Get the date of the log from the timestamp.
|
||||
$log_date = date($date_format, $log->timestamp);
|
||||
|
||||
// Check that the log timestamp fits the date parameters. Note that we
|
||||
// only need to check the start date bound, because end date is already
|
||||
|
@ -246,6 +256,11 @@ function farm_livestock_weight_report_form_submit($form, &$form_state) {
|
|||
continue;
|
||||
}
|
||||
|
||||
// Add the log date to the array if not already included.
|
||||
if (!in_array($log_date, $log_dates)){
|
||||
$log_dates[] = $log_date;
|
||||
}
|
||||
|
||||
// Extract quantity data from the log.
|
||||
$data = farm_quantity_log_data($log, 'weight');
|
||||
|
||||
|
@ -255,24 +270,111 @@ function farm_livestock_weight_report_form_submit($form, &$form_state) {
|
|||
$value = $quantity['value'];
|
||||
$units = $quantity['units'];
|
||||
|
||||
// Add a row of data.
|
||||
$row = array();
|
||||
$row[] = $asset->id;
|
||||
$row[] = $asset->name;
|
||||
$row[] = $value;
|
||||
$row[] = $units;
|
||||
$row[] = $log->timestamp;
|
||||
$row[] = $group_name;
|
||||
// Add the log data to array of logs.
|
||||
$log_data = array();
|
||||
$log_data['date'] = $log_date;
|
||||
$log_data['value'] = $value;
|
||||
$log_data['units'] = $units;
|
||||
|
||||
$all_log_data[]= $log_data;
|
||||
}
|
||||
}
|
||||
$csvdata .= implode(',', $row) . PHP_EOL;
|
||||
}
|
||||
// Save all log data with the animal.
|
||||
$asset->all_log_data = $all_log_data;
|
||||
|
||||
// Add animal data to array of all animals.
|
||||
$animals[] = $asset;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort all collected log_dates.
|
||||
asort($log_dates);
|
||||
|
||||
// Prepare CSV file.
|
||||
drupal_add_http_header('Content-Type', 'text/csv');
|
||||
drupal_add_http_header('Content-Disposition', 'attachment; filename=animal_weights.csv');
|
||||
|
||||
// Add the CSV header.
|
||||
$csvdata = 'AssetID, AssetName, Group, ';
|
||||
|
||||
// Add columns for each date collected.
|
||||
foreach ($log_dates as $date) {
|
||||
$csvdata .= 'Date ' . $date . ', Weight, Units,';
|
||||
}
|
||||
$csvdata .= PHP_EOL;
|
||||
|
||||
foreach ($animals as $animal) {
|
||||
// Add a row of data.
|
||||
$row = array();
|
||||
$row[] = $animal->id;
|
||||
$row[] = $animal->name;
|
||||
$row[] = $animal->group_name;
|
||||
|
||||
// Save the logs.
|
||||
$logs = $animal->all_log_data;
|
||||
|
||||
// Skip adding logs if the animal has no logs
|
||||
if (sizeof($logs) == 0) {
|
||||
// Add to Animal info to CSV string.
|
||||
$csvdata .= implode(',', $row) . PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sort the logs by date.
|
||||
usort($logs, "farm_livestock_weight_log_array_sort_date");
|
||||
|
||||
// Save a counter for which column to check against the log date.
|
||||
$curr_log_index = 0;
|
||||
|
||||
// Loop through each column and add log data if log date == column date.
|
||||
// Note that each date requires 3 columns in the CSV file to display the
|
||||
// date, value and units of recorded log weights.
|
||||
foreach($log_dates as $column_date) {
|
||||
// Conert the dates to times for easier comparison.
|
||||
$column_time = strtotime($column_date);
|
||||
$log_time = strtotime($logs[$curr_log_index]['date']);
|
||||
|
||||
// If the log_time is less than column_time, then there are multiple logs
|
||||
// on the same date for the animal. The first one has already been saved,
|
||||
// skip any additional logs with the same date.
|
||||
if ($column_time > $log_time) {
|
||||
$curr_log_index += 1;
|
||||
|
||||
// Set empty values if the times don't match.
|
||||
} else if ($column_time != $log_time) {
|
||||
$row[] = '';
|
||||
$row[] = '';
|
||||
$row[] = '';
|
||||
|
||||
// Save the log date, value and units if the times match.
|
||||
} else {
|
||||
$row[] = $logs[$curr_log_index]['date'];
|
||||
$row[] = $logs[$curr_log_index]['value'];
|
||||
$row[] = $logs[$curr_log_index]['units'];
|
||||
$curr_log_index += 1;
|
||||
}
|
||||
|
||||
// Exit if there are no more logs to check.
|
||||
if ($curr_log_index >= sizeof($logs)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Add to CSV string
|
||||
$csvdata .= implode(',', $row) . PHP_EOL;
|
||||
}
|
||||
print $csvdata;
|
||||
drupal_exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to sort an array of logs by timestamp
|
||||
*/
|
||||
function farm_livestock_weight_log_array_sort_date($a, $b) {
|
||||
return strtotime($a['date']) - strtotime($b['date']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_view_alter().
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue