Files
2026-02-16 11:11:56 +01:00

254 lines
12 KiB
PHP

<?php
/**
* app/modules/locations/list.php
*
* Uebersicht aller Standorte inkl. Gebaeude, Stockwerke und Raeume.
*/
$search = trim((string)($_GET['search'] ?? ''));
$where = '';
$types = '';
$params = [];
if ($search !== '') {
$where = "WHERE l.name LIKE ? OR l.comment LIKE ?";
$types = 'ss';
$params = ["%$search%", "%$search%"];
}
$locations = $sql->get(
"SELECT l.*, COUNT(b.id) AS building_count
FROM locations l
LEFT JOIN buildings b ON b.location_id = l.id
$where
GROUP BY l.id
ORDER BY l.name",
$types,
$params
);
$buildings = $sql->get(
"SELECT b.id, b.location_id, b.name, b.comment
FROM buildings b
ORDER BY b.location_id, b.name",
'',
[]
);
$floors = $sql->get(
"SELECT f.id, f.building_id, f.name, f.level,
COUNT(r.id) AS room_count
FROM floors f
LEFT JOIN rooms r ON r.floor_id = f.id
GROUP BY f.id
ORDER BY f.building_id, f.level, f.name",
'',
[]
);
$rooms = $sql->get(
"SELECT r.id, r.floor_id, r.name, r.number, r.comment,
COUNT(no.id) AS outlet_count
FROM rooms r
LEFT JOIN network_outlets no ON no.room_id = r.id
GROUP BY r.id
ORDER BY r.floor_id,
CASE WHEN r.number IS NULL OR r.number = '' THEN 1 ELSE 0 END,
r.number,
r.name",
'',
[]
);
$buildingsByLocation = [];
foreach ($buildings as $building) {
$buildingsByLocation[(int)$building['location_id']][] = $building;
}
$floorsByBuilding = [];
foreach ($floors as $floor) {
$floorsByBuilding[(int)$floor['building_id']][] = $floor;
}
$roomsByFloor = [];
foreach ($rooms as $room) {
$roomsByFloor[(int)$room['floor_id']][] = $room;
}
?>
<div class="locations-container">
<link rel="stylesheet" href="/assets/css/locations-list.css">
<script src="/assets/js/locations-list.js" defer></script>
<h1>Standorte</h1>
<div class="filter-form">
<form method="GET" class="locations-filter-form">
<input type="hidden" name="module" value="locations">
<input type="hidden" name="action" value="list">
<input type="text" name="search" placeholder="Suche nach Name..."
value="<?php echo htmlspecialchars($search); ?>" class="search-input">
<button type="submit" class="button">Filter</button>
<a href="?module=locations&action=list" class="button">Reset</a>
<a href="?module=locations&action=edit" class="button button-primary locations-filter-add">+ Neuer Standort</a>
</form>
</div>
<?php if (!empty($locations)): ?>
<table class="locations-list">
<thead>
<tr>
<th>Name</th>
<th>Gebaeude</th>
<th>Beschreibung</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($locations as $location): ?>
<tr>
<td><strong><?php echo htmlspecialchars((string)$location['name']); ?></strong></td>
<td><?php echo (int)$location['building_count']; ?></td>
<td><small><?php echo htmlspecialchars((string)($location['comment'] ?? '')); ?></small></td>
<td class="actions">
<a href="?module=locations&action=edit&id=<?php echo (int)$location['id']; ?>" class="button button-small">Bearbeiten</a>
<button type="button" class="button button-small button-danger js-delete-location" data-location-id="<?php echo (int)$location['id']; ?>">Loeschen</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<div class="empty-state">
<p>Keine Standorte gefunden.</p>
<p><a href="?module=locations&action=edit" class="button button-primary">Ersten Standort anlegen</a></p>
</div>
<?php endif; ?>
</div>
<section class="hierarchy-section">
<h2>Gebaeude, Stockwerke und Raeume</h2>
<?php if (!empty($locations)): ?>
<table class="hierarchy-table">
<thead>
<tr>
<th>Standort</th>
<th>Gebaeude</th>
<th>Stockwerk</th>
<th>Raum</th>
<th>Details</th>
<th class="actions">Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($locations as $location): ?>
<?php $locationBuildings = $buildingsByLocation[(int)$location['id']] ?? []; ?>
<tr class="hierarchy-row hierarchy-row--location">
<td class="hierarchy-cell hierarchy-cell--location" colspan="4">
<strong><?php echo htmlspecialchars((string)$location['name']); ?></strong>
<span class="hierarchy-meta">(<?php echo (int)$location['building_count']; ?> Gebaeude)</span>
</td>
<td></td>
<td class="actions hierarchy-actions">
<a href="?module=buildings&action=edit&location_id=<?php echo (int)$location['id']; ?>" class="button button-small">+ Gebaeude</a>
</td>
</tr>
<?php if (!empty($locationBuildings)): ?>
<?php foreach ($locationBuildings as $building): ?>
<?php $buildingFloors = $floorsByBuilding[(int)$building['id']] ?? []; ?>
<tr class="hierarchy-row hierarchy-row--building">
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--building" colspan="3"><?php echo htmlspecialchars((string)$building['name']); ?></td>
<td>
<?php if (!empty($building['comment'])): ?>
<span class="hierarchy-meta"><?php echo htmlspecialchars((string)$building['comment']); ?></span>
<?php else: ?>
<span class="hierarchy-meta hierarchy-meta--muted">Kein Kommentar</span>
<?php endif; ?>
</td>
<td class="actions hierarchy-actions">
<a href="?module=buildings&action=edit&id=<?php echo (int)$building['id']; ?>" class="button button-small">Bearbeiten</a>
<button type="button" class="button button-small button-danger js-delete-building" data-building-id="<?php echo (int)$building['id']; ?>">Loeschen</button>
<a href="?module=floors&action=edit&building_id=<?php echo (int)$building['id']; ?>" class="button button-small">+ Stockwerk</a>
</td>
</tr>
<?php if (!empty($buildingFloors)): ?>
<?php foreach ($buildingFloors as $floor): ?>
<?php $floorRooms = $roomsByFloor[(int)$floor['id']] ?? []; ?>
<tr class="hierarchy-row hierarchy-row--floor">
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--floor" colspan="2"><?php echo htmlspecialchars((string)$floor['name']); ?></td>
<td>
<?php if ($floor['level'] !== null): ?>
<span class="hierarchy-meta">Ebene <?php echo htmlspecialchars((string)$floor['level']); ?></span>
<?php else: ?>
<span class="hierarchy-meta hierarchy-meta--muted">Keine Ebene</span>
<?php endif; ?>
<span class="hierarchy-meta"> | <?php echo (int)$floor['room_count']; ?> Raeume</span>
</td>
<td class="actions hierarchy-actions">
<a href="?module=floors&action=edit&id=<?php echo (int)$floor['id']; ?>" class="button button-small">Bearbeiten</a>
<button type="button" class="button button-small button-danger js-delete-floor" data-floor-id="<?php echo (int)$floor['id']; ?>">Loeschen</button>
<a href="?module=rooms&action=edit&floor_id=<?php echo (int)$floor['id']; ?>" class="button button-small">+ Raum</a>
</td>
</tr>
<?php if (!empty($floorRooms)): ?>
<?php foreach ($floorRooms as $room): ?>
<tr class="hierarchy-row hierarchy-row--room">
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--room">
<?php echo htmlspecialchars((string)$room['name']); ?>
<?php if (!empty($room['number'])): ?>
<span class="hierarchy-meta">(<?php echo htmlspecialchars((string)$room['number']); ?>)</span>
<?php endif; ?>
</td>
<td>
<span class="hierarchy-meta"><?php echo (int)$room['outlet_count']; ?> Dosen</span>
<?php if (!empty($room['comment'])): ?>
<span class="hierarchy-meta"> | <?php echo htmlspecialchars((string)$room['comment']); ?></span>
<?php endif; ?>
</td>
<td class="actions hierarchy-actions">
<a href="?module=rooms&action=edit&id=<?php echo (int)$room['id']; ?>" class="button button-small">Bearbeiten</a>
<button type="button" class="button button-small button-danger js-delete-room" data-room-id="<?php echo (int)$room['id']; ?>">Loeschen</button>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr class="hierarchy-row hierarchy-row--empty">
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--room hierarchy-meta hierarchy-meta--muted" colspan="2">Keine Raeume</td>
<td><span class="hierarchy-meta hierarchy-meta--muted">Fuer dieses Stockwerk sind noch keine Raeume angelegt.</span></td>
<td></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php else: ?>
<tr class="hierarchy-row hierarchy-row--empty">
<td class="hierarchy-cell hierarchy-cell--empty">&nbsp;</td>
<td class="hierarchy-cell hierarchy-cell--empty" colspan="3">Keine Gebaeude</td>
<td><span class="hierarchy-meta hierarchy-meta--muted">Fuer diesen Standort sind noch keine Gebaeude vorhanden.</span></td>
<td></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Keine Standorte gefunden.</p>
<?php endif; ?>
</section>