Rend les salles et tournées configurables

Refs #10 #21
This commit is contained in:
Alexandre NOEL 2026-09-08 00:33:25 +02:00
commit e7176b6371
17 changed files with 189 additions and 25 deletions

View file

@ -215,6 +215,7 @@ final class SettingsController
render('settings_rooms.php', [
'title' => t('rooms.title'),
'rooms' => ShelterRoomService::all(),
'species' => SpeciesService::all(),
'saved' => isset($_GET['saved']),
'error' => (string) ($_GET['error'] ?? ''),
]);
@ -236,6 +237,18 @@ final class SettingsController
$type = (string) ($row['room_type'] ?? 'collective');
$status = (string) ($row['status_code'] ?? 'refuge');
$color = (string) ($row['color'] ?? '#6c757d');
$allowedSpecies = SpeciesService::normalize((string) ($row['allowed_species'] ?? ''));
if (!SpeciesService::exists($allowedSpecies, false)) {
$allowedSpecies = null;
}
$allowedSex = in_array($row['allowed_sex'] ?? '', ['M', 'F'], true) ? (string) $row['allowed_sex'] : null;
$requiredFiv = in_array($row['required_fiv'] ?? '', ['positive', 'negative'], true)
? (string) $row['required_fiv']
: 'any';
$periods = array_values(array_intersect(ShelterRoomService::PERIODS, (array) ($row['care_periods'] ?? [])));
if (!$periods) {
throw new RuntimeException(t('rooms.period_required'));
}
if (
$name === '' ||
mb_strlen($name) > 100 ||
@ -246,7 +259,7 @@ final class SettingsController
throw new RuntimeException(t('rooms.invalid'));
}
$db->prepare(
"UPDATE shelter_rooms SET name=?,room_type=?,status_code=?,color=?,bulk_validation=?,active=?,sort_order=?,updated_at=datetime('now') WHERE id=?",
"UPDATE shelter_rooms SET name=?,room_type=?,status_code=?,color=?,bulk_validation=?,active=?,sort_order=?,allowed_species=?,allowed_sex=?,required_fiv=?,care_periods=?,updated_at=datetime('now') WHERE id=?",
)->execute([
$name,
$type,
@ -255,6 +268,10 @@ final class SettingsController
isset($row['bulk_validation']) ? 1 : 0,
isset($row['active']) ? 1 : 0,
max(0, (int) ($row['sort_order'] ?? 100)),
$allowedSpecies,
$allowedSex,
$requiredFiv,
implode(',', $periods),
$id,
]);
self::syncBoxes($db, $id, (string) ($row['boxes'] ?? ''));
@ -278,14 +295,24 @@ final class SettingsController
$color = preg_match('/^#[0-9a-fA-F]{6}$/', (string) ($new['color'] ?? ''))
? (string) $new['color']
: '#6c757d';
$allowedSpecies = SpeciesService::normalize((string) ($new['allowed_species'] ?? ''));
if (!SpeciesService::exists($allowedSpecies, false)) {
$allowedSpecies = null;
}
$allowedSex = in_array($new['allowed_sex'] ?? '', ['M', 'F'], true) ? (string) $new['allowed_sex'] : null;
$requiredFiv = in_array($new['required_fiv'] ?? '', ['positive', 'negative'], true)
? (string) $new['required_fiv']
: 'any';
$periods = array_values(array_intersect(ShelterRoomService::PERIODS, (array) ($new['care_periods'] ?? [])));
$periods = $periods ?: ['morning', 'evening'];
$duplicate = $db->prepare('SELECT 1 FROM shelter_rooms WHERE code=? OR name=? COLLATE NOCASE');
$duplicate->execute([$code, $newName]);
if ($duplicate->fetchColumn()) {
throw new RuntimeException(t('rooms.duplicate'));
}
$db->prepare(
'INSERT INTO shelter_rooms(code,name,room_type,status_code,color,bulk_validation,sort_order) VALUES(?,?,?,?,?,?,?)',
)->execute([$code, $newName, $type, $status, $color, isset($new['bulk_validation']) ? 1 : 0, 100]);
'INSERT INTO shelter_rooms(code,name,room_type,status_code,color,bulk_validation,sort_order,allowed_species,allowed_sex,required_fiv,care_periods) VALUES(?,?,?,?,?,?,?,?,?,?,?)',
)->execute([$code, $newName, $type, $status, $color, isset($new['bulk_validation']) ? 1 : 0, 100, $allowedSpecies, $allowedSex, $requiredFiv, implode(',', $periods)]);
self::syncBoxes($db, (int) $db->lastInsertId(), (string) ($new['boxes'] ?? ''));
}
$db->commit();