Corriger les dossiers animaux et le suivi médical
This commit is contained in:
parent
a55d27935f
commit
7b89fd5dfa
34 changed files with 440 additions and 39 deletions
|
|
@ -1227,6 +1227,68 @@ final class SettingsController
|
|||
header('X-Content-Type-Options: nosniff');
|
||||
readfile($path);
|
||||
}
|
||||
public static function catalogs(): void
|
||||
{
|
||||
self::admin();
|
||||
$db = DB::pdo();
|
||||
render('settings_catalogs.php', [
|
||||
'title' => t('settings.catalogs'),
|
||||
'catalogs' => [
|
||||
'medication' => $db->query('SELECT id,name,molecule extra FROM ref_medications ORDER BY name COLLATE NOCASE')->fetchAll(PDO::FETCH_ASSOC),
|
||||
'vaccine' => $db->query('SELECT id,name,code extra FROM ref_vaccines ORDER BY name COLLATE NOCASE')->fetchAll(PDO::FETCH_ASSOC),
|
||||
'dewormer' => $db->query('SELECT id,name,active_ingredient extra FROM ref_dewormers ORDER BY name COLLATE NOCASE')->fetchAll(PDO::FETCH_ASSOC),
|
||||
'manufacturer' => $db->query('SELECT id,name,NULL extra FROM ref_vaccine_manufacturers ORDER BY name COLLATE NOCASE')->fetchAll(PDO::FETCH_ASSOC),
|
||||
],
|
||||
'saved' => isset($_GET['saved']),
|
||||
'error' => (string) ($_GET['error'] ?? ''),
|
||||
]);
|
||||
}
|
||||
public static function saveCatalogItem(): void
|
||||
{
|
||||
self::admin(); self::postOnly();
|
||||
$type = (string) ($_POST['type'] ?? '');
|
||||
$id = (int) ($_POST['id'] ?? 0);
|
||||
$name = trim((string) ($_POST['name'] ?? ''));
|
||||
$extra = trim((string) ($_POST['extra'] ?? '')) ?: null;
|
||||
$map = [
|
||||
'medication' => ['ref_medications','molecule'], 'vaccine' => ['ref_vaccines','code'],
|
||||
'dewormer' => ['ref_dewormers','active_ingredient'], 'manufacturer' => ['ref_vaccine_manufacturers',null],
|
||||
];
|
||||
if ($name === '' || !isset($map[$type])) {
|
||||
header('Location: /settings/catalogs?error=' . rawurlencode(t('catalog.invalid'))); exit();
|
||||
}
|
||||
[$table,$extraColumn] = $map[$type];
|
||||
try {
|
||||
if ($id) {
|
||||
$sql = "UPDATE $table SET name=?" . ($extraColumn ? ",$extraColumn=?" : '') . ' WHERE id=?';
|
||||
DB::pdo()->prepare($sql)->execute($extraColumn ? [$name,$extra,$id] : [$name,$id]);
|
||||
} else {
|
||||
$sql = "INSERT INTO $table(name" . ($extraColumn ? ",$extraColumn" : '') . ') VALUES(?' . ($extraColumn ? ',?' : '') . ')';
|
||||
DB::pdo()->prepare($sql)->execute($extraColumn ? [$name,$extra] : [$name]);
|
||||
$id = (int) DB::pdo()->lastInsertId();
|
||||
}
|
||||
AuditService::log('catalog_saved', '/settings/catalogs/save', 'Catalogue modifié : ' . $name, $type, $id);
|
||||
header('Location: /settings/catalogs?saved=1');
|
||||
} catch (Throwable $e) {
|
||||
header('Location: /settings/catalogs?error=' . rawurlencode(SecurityService::publicError($e)));
|
||||
}
|
||||
exit();
|
||||
}
|
||||
public static function deleteCatalogItem(): void
|
||||
{
|
||||
self::admin(); self::postOnly();
|
||||
$type = (string) ($_POST['type'] ?? ''); $id = (int) ($_POST['id'] ?? 0);
|
||||
$tables = ['medication'=>'ref_medications','vaccine'=>'ref_vaccines','dewormer'=>'ref_dewormers','manufacturer'=>'ref_vaccine_manufacturers'];
|
||||
if (!$id || !isset($tables[$type])) { http_response_code(400); return; }
|
||||
try {
|
||||
DB::pdo()->prepare('DELETE FROM ' . $tables[$type] . ' WHERE id=?')->execute([$id]);
|
||||
AuditService::log('catalog_deleted', '/settings/catalogs/delete', 'Élément de catalogue supprimé', $type, $id);
|
||||
header('Location: /settings/catalogs?saved=1');
|
||||
} catch (Throwable) {
|
||||
header('Location: /settings/catalogs?error=' . rawurlencode(t('catalog.in_use')));
|
||||
}
|
||||
exit();
|
||||
}
|
||||
private static function postOnly(): void
|
||||
{
|
||||
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
|
||||
|
|
|
|||
Loading…
Reference in a new issue