added new export for buildings
This commit is contained in:
@@ -94,6 +94,10 @@
|
||||
<div class="col">
|
||||
<button type="submit" class="btn btn-primary">Filter anwenden</button>
|
||||
<a class="btn btn-secondary" href="<?=self::getUrl("Building")?>">Filter zurücksetzen</a>
|
||||
<?php if ($me->isAdmin() && is_array($filter) && array_key_exists("network_id", $filter) && !empty($filter["network_id"])): ?>
|
||||
<a class="btn btn-outline-success" href="<?=self::getUrl("Building", "exportXLSX", ["network_id" => ($filter["network_id"])])?>">
|
||||
<i class="fas fa-file-excel"></i> Exportieren</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<!--<div class="col">
|
||||
<button class="btn btn-info" type="button" onclick="refreshMap()"><i class="far fa-map"></i> Auf Karte anzeigen</button>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user