Files
ASPIC/missions.php

85 lines
3.2 KiB
PHP

<?php
/**
* ASPIC — missions.php
*
* Endpoint interne (11 septembre 2026, chantier Compétences/Missions) —
* création/liste des missions. Volontairement séparé de mission_agent.php
* (qui ne fait que dialoguer sur une mission déjà ouverte) : ouvrir une
* mission est un acte de gestion (qui, quoi, avec quelle compétence), pas un
* tour de dialogue.
*
* GET : liste les missions (agent, compétence, site éventuel, statut).
* POST { "MIS_AgentIncr": 13, "MIS_CompetenceCode": "SITE_WEB",
* "MIS_Titre": "...", "MIS_Objectif": "..." }
* Vérifie que le collaborateur possède bien la compétence demandée avant
* toute création (ASP_AgentsCompetences) — refus explicite sinon, jamais
* une mission incohérente créée silencieusement.
*/
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;
}
try {
$pdo = getDbConnection();
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Connexion base de données impossible']);
exit;
}
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
$missions = $pdo->query(
"SELECT m.MIS_Incr, m.MIS_Titre, m.MIS_Objectif, m.MIS_Statut, m.MIS_DateOuverture, m.MIS_DateCloture,
a.AGN_Prenom, c.CMP_Libelle, m.MIS_SiteIncr, s.SIT_Nom, s.SIT_Statut AS SIT_Statut
FROM ASP_Missions m
JOIN ASP_Agents a ON a.AGN_Incr = m.MIS_AgentIncr
JOIN ASP_Competences c ON c.CMP_Code = m.MIS_CompetenceCode
LEFT JOIN ASP_SitesWeb s ON s.SIT_Incr = m.MIS_SiteIncr
ORDER BY m.MIS_DateOuverture DESC"
)->fetchAll();
echo json_encode(['missions' => $missions]);
exit;
}
if ($method === 'POST') {
$data = json_decode(file_get_contents('php://input'), true) ?? [];
$agentIncr = (int) ($data['MIS_AgentIncr'] ?? 0);
$competenceCode = trim((string) ($data['MIS_CompetenceCode'] ?? ''));
$titre = trim((string) ($data['MIS_Titre'] ?? ''));
$objectif = trim((string) ($data['MIS_Objectif'] ?? ''));
if ($agentIncr <= 0 || $competenceCode === '' || $titre === '' || $objectif === '') {
http_response_code(400);
echo json_encode(['error' => 'MIS_AgentIncr, MIS_CompetenceCode, MIS_Titre et MIS_Objectif sont obligatoires.']);
exit;
}
$stmt = $pdo->prepare('SELECT 1 FROM ASP_AgentsCompetences WHERE AGC_AgentIncr = ? AND AGC_CompetenceCode = ?');
$stmt->execute([$agentIncr, $competenceCode]);
if (!$stmt->fetch()) {
http_response_code(400);
echo json_encode(['error' => "Ce collaborateur n'a pas la compétence $competenceCode — mission refusée."]);
exit;
}
$stmt = $pdo->prepare(
"INSERT INTO ASP_Missions (MIS_AgentIncr, MIS_CompetenceCode, MIS_Titre, MIS_Objectif, MIS_Statut)
VALUES (?, ?, ?, ?, 'ouverte')"
);
$stmt->execute([$agentIncr, $competenceCode, $titre, $objectif]);
echo json_encode(['success' => true, 'MIS_Incr' => (int) $pdo->lastInsertId()]);
exit;
}
http_response_code(405);
echo json_encode(['error' => 'Méthode non autorisée']);