mirror of
https://github.com/JMP-MS/ASPIC.git
synced 2026-08-28 23:33:13 +00:00
80 lines
3.0 KiB
PHP
80 lines
3.0 KiB
PHP
<?php
|
|
/**
|
|
* ASPIC — professions.php
|
|
* Table de référence ASP_Professions (liée à ASP_Contacts via
|
|
* CNT_CodeProfession = PRF_Incr, lien applicatif, sans contrainte FOREIGN KEY
|
|
* SQL — voir journal de développement, section 2).
|
|
*
|
|
* GET : liste des professions, triée par libellé.
|
|
* POST : création d'une nouvelle profession (utilisée par le formulaire de
|
|
* détail du contact, option "+ Nouvelle profession…" — exception à la
|
|
* règle "table gérée à la main" décidée le 23 août 2026, car
|
|
* ASP_Professions est une liste ouverte destinée à grandir avec
|
|
* l'usage, contrairement à ASP_Statuts/ASP_Actions).
|
|
*
|
|
* ⚠️ Session utilisateur intégrée le 24 août 2026 (voir journal, section 14) :
|
|
* accès réservé aux utilisateurs identifiés (`$_SESSION['USR_Code']`, ouverte
|
|
* par `aspic.php`).
|
|
*/
|
|
|
|
session_start();
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
require __DIR__ . '/db.php';
|
|
|
|
if (empty($_SESSION['USR_Code'])) {
|
|
http_response_code(401);
|
|
echo json_encode(['error' => 'Non identifié — veuillez vous connecter.']);
|
|
exit;
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
try {
|
|
$pdo = getDbConnection();
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Connexion base de données impossible']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
switch ($method) {
|
|
case 'GET':
|
|
$stmt = $pdo->query('SELECT PRF_Incr, PRF_Libelle FROM ASP_Professions ORDER BY PRF_Libelle ASC');
|
|
echo json_encode(['professions' => $stmt->fetchAll()]);
|
|
break;
|
|
|
|
case 'POST':
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$libelle = trim($data['PRF_Libelle'] ?? '');
|
|
if ($libelle === '') {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Le libellé de la profession est obligatoire']);
|
|
exit;
|
|
}
|
|
|
|
// Pas de contrainte UNIQUE en base sur PRF_Libelle à ce stade (voir
|
|
// journal) : on évite ici un doublon évident et strictement identique,
|
|
// sans garantie absolue en cas de création concurrente.
|
|
$verif = $pdo->prepare('SELECT PRF_Incr FROM ASP_Professions WHERE PRF_Libelle = ?');
|
|
$verif->execute([$libelle]);
|
|
$existant = $verif->fetch();
|
|
if ($existant) {
|
|
echo json_encode(['id' => (int)$existant['PRF_Incr'], 'existant' => true]);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare('INSERT INTO ASP_Professions (PRF_Libelle) VALUES (?)');
|
|
$stmt->execute([$libelle]);
|
|
echo json_encode(['id' => (int)$pdo->lastInsertId(), 'existant' => false]);
|
|
break;
|
|
|
|
default:
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Méthode non autorisée']);
|
|
}
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Erreur base de données : ' . $e->getMessage()]);
|
|
}
|