added new export for buildings

This commit is contained in:
Luca Haid
2025-04-23 10:32:39 +02:00
parent a9918eb229
commit c95a835820
2 changed files with 102 additions and 1 deletions

View File

@@ -479,5 +479,102 @@ class BuildingController extends mfBaseController {
return ["count" => count($buildings), "buildings" => $results];
}
protected function exportXLSXAction()
{
if (!$this->me->isAdmin()) $this->redirect("Building");
$requestedNetworkId = $this->request->network_id;
if (!is_numeric($requestedNetworkId) || $requestedNetworkId <= 0) {
$this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
$this->redirect("Building");
}
$targetNetwork = new Network($requestedNetworkId);
if (!$targetNetwork->id) {
$this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
$this->redirect("Building");
}
$buildingRecords = BuildingModel::search(['network_id' => $targetNetwork->id]);
if (empty($buildingRecords)) {
$this->layout()->setFlash("Keine Objekte gefunden", "error");
$this->redirect("Building");
}
$exportFilename = sprintf("buildings_%s_%s.xlsx", $targetNetwork->name, date("Y-m-d"));
$columnHeaders = [
'id' => 'ID',
'network_name' => 'Netzgebiet',
'pop_name' => 'POP',
'type_name' => 'Objekttyp',
'code' => 'Objektcode',
'oan_id' => 'OA-ID',
'street' => 'Strasse',
'zip' => 'PLZ',
'city' => 'Ort',
'units' => 'Anzahl Einheiten',
'status_name' => 'Status',
'create' => 'Erstellt am',
'create_by' => 'Erstellt von',
'edit' => 'Bearbeitet am',
'edit_by' => 'Bearbeitet von'
];
$headerKeys = array_keys($columnHeaders);
$formattedData = array_map(function($building) use ($targetNetwork, $headerKeys) {
$rowData = [
'id' => $building->id,
'network_name' => $targetNetwork->name,
'pop_name' => $building->pop_id ? ($building->pop->name ?? '-') : "-",
'type_name' => $building->type->name ?? '-',
'code' => $building->code,
'oan_id' => $building->oaid,
'street' => $building->street,
'zip' => $building->zip,
'city' => $building->city,
'units' => $building->units,
'status_name' => $building->status ? $this->__($building->status->name."-b") : '-',
'create' => $building->create ? date('d.m.Y H:i:s', $building->create) : '-',
'create_by' => $building->creator->name ?? '-',
'edit' => $building->edit ? date('d.m.Y H:i:s', $building->edit) : '-',
'edit_by' => $building->editor->name ?? '-',
];
$orderedRow = [];
foreach ($headerKeys as $key) {
$orderedRow[] = $rowData[$key] ?? '';
}
return $orderedRow;
}, $buildingRecords);
$spreadsheet = new Spreadsheet();
$activeSheet = $spreadsheet->getActiveSheet();
$activeSheet->fromArray(array_values($columnHeaders), null, 'A1');
if (!empty($formattedData)) {
$activeSheet->fromArray($formattedData, null, 'A2');
}
foreach (range('A', $activeSheet->getHighestDataColumn()) as $col) {
$activeSheet->getColumnDimension($col)->setAutoSize(true);
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $exportFilename . '"');
header('Cache-Control: max-age=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
$xlsxWriter = new Xlsx($spreadsheet);
if (ob_get_level()) {
ob_end_clean();
}
$xlsxWriter->save('php://output');
exit;
}
}