Initial commit
This commit is contained in:
+112
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/config.php';
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
|
||||
|
||||
$acc = require_auth(true);
|
||||
$action = $_GET['action'] ?? '';
|
||||
$body = json_decode(file_get_contents('php://input'), true) ?? [];
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
$week = current_week();
|
||||
|
||||
switch ($action) {
|
||||
|
||||
case 'init':
|
||||
$tasks = resolve_visible_tasks($db, $acc);
|
||||
$categories = [];
|
||||
$cat_ids = array_unique(array_column($tasks, 'category_id'));
|
||||
if ($cat_ids) {
|
||||
$in = implode(',', array_map('intval', $cat_ids));
|
||||
$categories = $db->query("SELECT * FROM categories WHERE id IN ($in) ORDER BY sort_order")->fetchAll();
|
||||
}
|
||||
|
||||
// Récupérer les completions de la période courante pour chaque fréquence
|
||||
$period_keys = all_current_period_keys();
|
||||
$unique_keys = array_unique(array_values($period_keys));
|
||||
$placeholders = implode(',', array_map(fn($k) => "'{$k}'", $unique_keys));
|
||||
|
||||
$comp = $db->query("
|
||||
SELECT c.task_id, c.account_id, c.checked, c.completed_at,
|
||||
c.period_key, t.frequency,
|
||||
a.display_name, a.color, a.avatar
|
||||
FROM completions c
|
||||
JOIN accounts a ON a.id = c.account_id
|
||||
JOIN tasks t ON t.id = c.task_id
|
||||
WHERE c.period_key IN ({$placeholders})
|
||||
AND (
|
||||
(t.frequency = 'quotidien' AND c.period_key = '{$period_keys['quotidien']}')
|
||||
OR (t.frequency = 'hebdomadaire' AND c.period_key = '{$period_keys['hebdomadaire']}')
|
||||
OR (t.frequency = 'mensuel' AND c.period_key = '{$period_keys['mensuel']}')
|
||||
OR (t.frequency = 'trimestriel' AND c.period_key = '{$period_keys['trimestriel']}')
|
||||
OR (t.frequency = 'saisonnier' AND c.period_key = '{$period_keys['saisonnier']}')
|
||||
OR (t.frequency = 'biannuel' AND c.period_key = '{$period_keys['biannuel']}')
|
||||
OR (t.frequency = 'annuel' AND c.period_key = '{$period_keys['annuel']}')
|
||||
)
|
||||
ORDER BY c.completed_at ASC
|
||||
")->fetchAll();
|
||||
|
||||
$accounts_list = $db->query("
|
||||
SELECT id, display_name, color, avatar FROM accounts ORDER BY id
|
||||
")->fetchAll();
|
||||
|
||||
json_response([
|
||||
'account' => $acc,
|
||||
'tasks' => $tasks,
|
||||
'categories' => $categories,
|
||||
'completions' => $comp,
|
||||
'accounts' => $accounts_list,
|
||||
'week' => current_week(),
|
||||
'period_keys' => $period_keys,
|
||||
]);
|
||||
|
||||
case 'toggle':
|
||||
$task_id = (int)($body['task_id'] ?? 0);
|
||||
$checked = (int)($body['checked'] ?? 1);
|
||||
if (!$task_id) json_response(['error' => 'task_id requis'], 400);
|
||||
|
||||
// Récupérer la fréquence de la tâche pour calculer le bon period_key
|
||||
$stmt = $db->prepare("SELECT frequency FROM tasks WHERE id = :id");
|
||||
$stmt->execute([':id' => $task_id]);
|
||||
$task_row = $stmt->fetch();
|
||||
if (!$task_row) json_response(['error' => 'Tâche introuvable'], 404);
|
||||
|
||||
// Vérifier que la tâche est visible pour ce compte
|
||||
$visible = resolve_visible_tasks($db, $acc);
|
||||
$allowed = array_column($visible, 'id');
|
||||
if ($acc['role'] !== 'admin' && !in_array($task_id, $allowed))
|
||||
json_response(['error' => 'Tâche non autorisée'], 403);
|
||||
|
||||
$period_key = period_key_for($task_row['frequency']);
|
||||
|
||||
$db->prepare("
|
||||
INSERT INTO completions (task_id, account_id, checked, period_key)
|
||||
VALUES (:tid, :aid, :chk, :pk)
|
||||
")->execute([':tid'=>$task_id, ':aid'=>$acc['id'], ':chk'=>$checked, ':pk'=>$period_key]);
|
||||
|
||||
json_response(['success' => true, 'period_key' => $period_key]);
|
||||
|
||||
case 'history':
|
||||
$weeks = min((int)($_GET['weeks'] ?? 8), 52);
|
||||
$stmt = $db->prepare("
|
||||
SELECT c.period_key, c.completed_at, c.checked,
|
||||
a.display_name, a.color, a.avatar,
|
||||
t.label as task_label, t.frequency,
|
||||
cat.name as category_name, cat.icon as category_icon
|
||||
FROM completions c
|
||||
JOIN accounts a ON a.id = c.account_id
|
||||
JOIN tasks t ON t.id = c.task_id
|
||||
JOIN categories cat ON cat.id = t.category_id
|
||||
WHERE c.completed_at >= DATE_SUB(NOW(), INTERVAL :w WEEK)
|
||||
ORDER BY c.completed_at DESC
|
||||
LIMIT 500
|
||||
");
|
||||
$stmt->execute([':w' => $weeks]);
|
||||
json_response(['history' => $stmt->fetchAll()]);
|
||||
|
||||
default:
|
||||
json_response(['error' => 'Action inconnue'], 404);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
json_response(['error' => 'DB: ' . $e->getMessage()], 500);
|
||||
}
|
||||
Reference in New Issue
Block a user