@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
// modules/holidays/includes/api/delete_attachment.php
|
||||
require dirname(__DIR__, 4) . '/includes/auth.php';
|
||||
require dirname(__DIR__, 4) . '/includes/db.php';
|
||||
require_login();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode(['success' => false, 'error' => 'Méthode non autorisée']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$file_id = isset($_POST['file_id']) ? (int)$_POST['file_id'] : 0;
|
||||
$holiday_id = isset($_POST['holiday_id']) ? (int)$_POST['holiday_id'] : 0;
|
||||
|
||||
if ($file_id === 0 || $holiday_id === 0) {
|
||||
echo json_encode(['success' => false, 'error' => 'Paramètres manquants']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 1. On récupère le chemin exact du fichier pour le supprimer du NAS
|
||||
$stmt = $pdo->prepare("SELECT file_path FROM pf_holidays_attachments WHERE id = ? AND holiday_id = ?");
|
||||
$stmt->execute([$file_id, $holiday_id]);
|
||||
$file = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($file) {
|
||||
$absolutePath = dirname(__DIR__, 4) . '/' . $file['file_path'];
|
||||
|
||||
// 2. On supprime physiquement le fichier s'il existe
|
||||
if (file_exists($absolutePath)) {
|
||||
unlink($absolutePath);
|
||||
}
|
||||
|
||||
// 3. On nettoie la base de données
|
||||
$del = $pdo->prepare("DELETE FROM pf_holidays_attachments WHERE id = ?");
|
||||
$del->execute([$file_id]);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => 'Fichier introuvable ou non autorisé']);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
// modules/holidays/includes/api/get_attachments.php
|
||||
require dirname(__DIR__, 4) . '/includes/auth.php';
|
||||
require dirname(__DIR__, 4) . '/includes/db.php';
|
||||
require_login();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$holiday_id = isset($_GET['holiday_id']) ? (int)$_GET['holiday_id'] : 0;
|
||||
$item_id = isset($_GET['item_id']) ? (int)$_GET['item_id'] : 0;
|
||||
|
||||
if ($holiday_id === 0) {
|
||||
echo json_encode(['success' => false, 'error' => 'ID manquant']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// On récupère les documents de cette étape spécifique
|
||||
$stmt = $pdo->prepare("SELECT id, file_name, file_path FROM pf_holidays_attachments WHERE holiday_id = ? AND item_id = ? ORDER BY uploaded_at DESC");
|
||||
$stmt->execute([$holiday_id, $item_id]);
|
||||
$files = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo json_encode(['success' => true, 'files' => $files]);
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// modules/holidays/includes/api/upload_attachment.php
|
||||
require dirname(__DIR__, 4) . '/includes/auth.php';
|
||||
require dirname(__DIR__, 4) . '/includes/db.php';
|
||||
require_login();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode(['success' => false, 'error' => 'Méthode non autorisée']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$holiday_id = isset($_POST['holiday_id']) ? (int)$_POST['holiday_id'] : 0;
|
||||
$item_id = (isset($_POST['item_id']) && (int)$_POST['item_id'] > 0) ? (int)$_POST['item_id'] : null;
|
||||
|
||||
if ($holiday_id === 0 || empty($_FILES['file'])) {
|
||||
echo json_encode(['success' => false, 'error' => 'Données manquantes ou fichier invalide']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$file = $_FILES['file'];
|
||||
|
||||
// On range les fichiers dans un sous-dossier par voyage pour rester propre sur le NAS
|
||||
$uploadDir = dirname(__DIR__, 4) . '/uploads/holidays/' . $holiday_id . '/';
|
||||
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0777, true);
|
||||
}
|
||||
|
||||
// Sécurisation du nom de fichier (retrait des accents et caractères spéciaux)
|
||||
$fileName = basename($file['name']);
|
||||
$safeFileName = preg_replace("/[^a-zA-Z0-9.-]/", "_", $fileName);
|
||||
$uniqueFileName = time() . '_' . $safeFileName;
|
||||
$destination = $uploadDir . $uniqueFileName;
|
||||
|
||||
if (move_uploaded_file($file['tmp_name'], $destination)) {
|
||||
// Enregistrement en base de données
|
||||
$stmt = $pdo->prepare("INSERT INTO pf_holidays_attachments (holiday_id, item_id, file_name, file_path) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([$holiday_id, $item_id, $fileName, 'uploads/holidays/' . $holiday_id . '/' . $uniqueFileName]);
|
||||
|
||||
$attachmentId = $pdo->lastInsertId();
|
||||
echo json_encode(['success' => true, 'id' => $attachmentId, 'file_name' => $fileName]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => 'Erreur lors de l\'écriture du fichier sur le NAS']);
|
||||
}
|
||||
Reference in New Issue
Block a user