_user["rowid"]; switch ($Route->_action) { case "load_contacts": // Charge tous les contacts d'un client $data = json_decode(file_get_contents("php://input")); if (isset($data->fk_client)) { $fk_client = nettoie_input($data->fk_client); $fkClientSafe = intval($fk_client); try { $db = Database::getInstance(); $sql = 'SELECT rowid, nom, prenom, fonction, telephone, mobile, email, principal, active FROM clients_contacts WHERE fk_client = :fk_client AND active = 1 ORDER BY principal DESC, nom, prenom'; $contacts = $db->fetchAll($sql, [':fk_client' => $fkClientSafe]); echo json_encode($contacts); } catch (Exception $e) { error_log("Erreur load_contacts : " . $e->getMessage()); echo json_encode(array('ret' => 'ko', 'msg' => 'Erreur lors du chargement des contacts')); } } else { echo json_encode(array('ret' => 'ko', 'msg' => 'Client non spécifié')); } break; case "load_contact": // Charge un contact spécifique $data = json_decode(file_get_contents("php://input")); if (isset($data->rowid)) { $rowid = nettoie_input($data->rowid); $rowidSafe = intval($rowid); try { $db = Database::getInstance(); $sql = 'SELECT rowid, fk_client, nom, prenom, fonction, telephone, mobile, email, principal, active FROM clients_contacts WHERE rowid = :rowid'; $contact = $db->fetchAll($sql, [':rowid' => $rowidSafe]); if (count($contact) == 1) { echo json_encode($contact[0]); } else { echo json_encode(array('ret' => 'ko', 'msg' => 'Contact introuvable')); } } catch (Exception $e) { error_log("Erreur load_contact : " . $e->getMessage()); echo json_encode(array('ret' => 'ko', 'msg' => 'Erreur lors du chargement du contact')); } } else { echo json_encode(array('ret' => 'ko', 'msg' => 'Contact non spécifié')); } break; case "save_contact": // Crée ou met à jour un contact if ($_POST) { $rowid = nettoie_input($_POST["rowid"]); $fk_client = nettoie_input($_POST["fk_client"]); $nom = nettoie_input($_POST["nom"]); $prenom = nettoie_input($_POST["prenom"]); $fonction = nettoie_input($_POST["fonction"]); $telephone = formattel(nettoie_input($_POST["telephone"])); $mobile = formattel(nettoie_input($_POST["mobile"])); $email = nettoie_input($_POST["email"]); $principal = 0; if (isset($_POST["principal"]) && $_POST["principal"] == "1") { $principal = 1; } $fkClientSafe = intval($fk_client); $fkUserSafe = intval($fk_user); try { $db = Database::getInstance(); if ($rowid == 0) { // Création d'un nouveau contact // Si ce contact est marqué comme principal, on retire le flag principal des autres contacts du client if ($principal == 1) { $sqlUpdate = 'UPDATE clients_contacts SET principal = 0 WHERE fk_client = :fk_client'; $db->query($sqlUpdate, [':fk_client' => $fkClientSafe]); } $data = [ 'fk_client' => $fkClientSafe, 'nom' => $nom, 'prenom' => $prenom, 'fonction' => $fonction, 'telephone' => $telephone, 'mobile' => $mobile, 'email' => $email, 'principal' => $principal, 'active' => 1, 'date_creat' => date("Y-m-d H:i:s"), 'fk_user_creat' => $fkUserSafe ]; $newId = $db->insert('clients_contacts', $data); eLog("Contact créé avec l'ID : " . $newId); echo json_encode(array('ret' => 'ok', 'msg' => 'Contact créé avec succès', 'rowid' => $newId)); } else { // Mise à jour d'un contact existant $rowidSafe = intval($rowid); // Si ce contact est marqué comme principal, on retire le flag principal des autres contacts du client if ($principal == 1) { $sqlUpdate = 'UPDATE clients_contacts SET principal = 0 WHERE fk_client = :fk_client AND rowid != :rowid'; $db->query($sqlUpdate, [':fk_client' => $fkClientSafe, ':rowid' => $rowidSafe]); } $sql = 'UPDATE clients_contacts SET nom = :nom, prenom = :prenom, fonction = :fonction, telephone = :telephone, mobile = :mobile, email = :email, principal = :principal, date_modif = :date_modif, fk_user_modif = :fk_user_modif WHERE rowid = :rowid'; $params = [ ':nom' => $nom, ':prenom' => $prenom, ':fonction' => $fonction, ':telephone' => $telephone, ':mobile' => $mobile, ':email' => $email, ':principal' => $principal, ':date_modif' => date("Y-m-d H:i:s"), ':fk_user_modif' => $fkUserSafe, ':rowid' => $rowidSafe ]; $db->query($sql, $params); eLog("Contact mis à jour : " . $rowidSafe); echo json_encode(array('ret' => 'ok', 'msg' => 'Contact mis à jour avec succès', 'rowid' => $rowidSafe)); } } catch (Exception $e) { error_log("Erreur save_contact : " . $e->getMessage()); echo json_encode(array('ret' => 'ko', 'msg' => 'Erreur lors de l\'enregistrement du contact')); } } break; case "delete_contact": // Supprime (désactive) un contact $data = json_decode(file_get_contents("php://input")); if (isset($data->rowid)) { $rowid = nettoie_input($data->rowid); $rowidSafe = intval($rowid); try { $db = Database::getInstance(); // Vérifier qu'il reste au moins un autre contact actif pour ce client $sqlCheck = 'SELECT cc.fk_client, COUNT(*) as nb_contacts FROM clients_contacts cc WHERE cc.rowid = :rowid'; $result = $db->fetchAll($sqlCheck, [':rowid' => $rowidSafe]); if (count($result) == 1) { $fkClient = $result[0]['fk_client']; // Compter les contacts actifs restants $sqlCount = 'SELECT COUNT(*) as nb FROM clients_contacts WHERE fk_client = :fk_client AND active = 1 AND rowid != :rowid'; $countResult = $db->fetchAll($sqlCount, [':fk_client' => $fkClient, ':rowid' => $rowidSafe]); if ($countResult[0]['nb'] == 0) { echo json_encode(array('ret' => 'ko', 'msg' => 'Impossible de supprimer le dernier contact actif du client')); } else { // Désactiver le contact $fkUserSafe = intval($fk_user); $sql = 'UPDATE clients_contacts SET active = 0, date_modif = :date_modif, fk_user_modif = :fk_user_modif WHERE rowid = :rowid'; $params = [ ':date_modif' => date("Y-m-d H:i:s"), ':fk_user_modif' => $fkUserSafe, ':rowid' => $rowidSafe ]; $db->query($sql, $params); eLog("Contact désactivé : " . $rowidSafe); echo json_encode(array('ret' => 'ok', 'msg' => 'Contact supprimé avec succès')); } } else { echo json_encode(array('ret' => 'ko', 'msg' => 'Contact introuvable')); } } catch (Exception $e) { error_log("Erreur delete_contact : " . $e->getMessage()); echo json_encode(array('ret' => 'ko', 'msg' => 'Erreur lors de la suppression du contact')); } } else { echo json_encode(array('ret' => 'ko', 'msg' => 'Contact non spécifié')); } break; case "set_principal": // Définit un contact comme principal $data = json_decode(file_get_contents("php://input")); if (isset($data->rowid) && isset($data->fk_client)) { $rowid = nettoie_input($data->rowid); $fk_client = nettoie_input($data->fk_client); $rowidSafe = intval($rowid); $fkClientSafe = intval($fk_client); try { $db = Database::getInstance(); $fkUserSafe = intval($fk_user); // Retirer le flag principal de tous les contacts du client $sqlReset = 'UPDATE clients_contacts SET principal = 0 WHERE fk_client = :fk_client'; $db->query($sqlReset, [':fk_client' => $fkClientSafe]); // Définir le contact comme principal $sql = 'UPDATE clients_contacts SET principal = 1, date_modif = :date_modif, fk_user_modif = :fk_user_modif WHERE rowid = :rowid'; $params = [ ':date_modif' => date("Y-m-d H:i:s"), ':fk_user_modif' => $fkUserSafe, ':rowid' => $rowidSafe ]; $db->query($sql, $params); eLog("Contact principal défini : " . $rowidSafe); echo json_encode(array('ret' => 'ok', 'msg' => 'Contact principal défini avec succès')); } catch (Exception $e) { error_log("Erreur set_principal : " . $e->getMessage()); echo json_encode(array('ret' => 'ko', 'msg' => 'Erreur lors de la définition du contact principal')); } } else { echo json_encode(array('ret' => 'ko', 'msg' => 'Données manquantes')); } break; default: echo json_encode(array('ret' => 'ko', 'msg' => 'Action inconnue')); break; } exit();