add tag button
This commit is contained in:
47
index.php
47
index.php
@@ -187,6 +187,53 @@ if ($action === 'api') {
|
||||
exit;
|
||||
}
|
||||
|
||||
if($api_action === 'add_task_tag') {
|
||||
if(!isset($_SESSION['user_id'])) die(json_encode(['success'=>false,'error'=>'unauthorized']));
|
||||
$task_id = $_POST['task_id'] ?? null;
|
||||
$tag = trim($_POST['tag'] ?? '');
|
||||
$user_id = $_SESSION['user_id'];
|
||||
|
||||
if($task_id && $tag) {
|
||||
$stmt = $pdo->prepare("SELECT tags FROM tasks WHERE id = ?");
|
||||
$stmt->execute([$task_id]);
|
||||
$row = $stmt->fetch();
|
||||
$current_tags = $row ? ($row['tags'] ?? '') : '';
|
||||
|
||||
$tags_array = $current_tags ? array_filter(array_map('trim', explode(',', $current_tags))) : [];
|
||||
if(!in_array($tag, $tags_array)) {
|
||||
$tags_array[] = $tag;
|
||||
$new_tags = implode(',', $tags_array);
|
||||
$pdo->prepare("UPDATE tasks SET tags = ? WHERE id = ?")->execute([$new_tags, $task_id]);
|
||||
}
|
||||
echo json_encode(['success'=>true]);
|
||||
} else {
|
||||
echo json_encode(['success'=>false,'error'=>'Missing task_id or tag']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if($api_action === 'remove_task_tag') {
|
||||
if(!isset($_SESSION['user_id'])) die(json_encode(['success'=>false,'error'=>'unauthorized']));
|
||||
$task_id = $_POST['task_id'] ?? null;
|
||||
$tag = trim($_POST['tag'] ?? '');
|
||||
|
||||
if($task_id && $tag) {
|
||||
$stmt = $pdo->prepare("SELECT tags FROM tasks WHERE id = ?");
|
||||
$stmt->execute([$task_id]);
|
||||
$row = $stmt->fetch();
|
||||
$current_tags = $row ? ($row['tags'] ?? '') : '';
|
||||
|
||||
$tags_array = $current_tags ? array_filter(array_map('trim', explode(',', $current_tags))) : [];
|
||||
$tags_array = array_diff($tags_array, [$tag]);
|
||||
$new_tags = implode(',', $tags_array);
|
||||
$pdo->prepare("UPDATE tasks SET tags = ? WHERE id = ?")->execute([$new_tags, $task_id]);
|
||||
echo json_encode(['success'=>true]);
|
||||
} else {
|
||||
echo json_encode(['success'=>false,'error'=>'Missing task_id or tag']);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode(['status'=>'error','msg'=>'Unknown api_action']);
|
||||
exit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user