refactor: replace global WAHA notifications with granular user and group-specific templates and dispatch logic
This commit is contained in:
@@ -38,9 +38,16 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_task'])) {
|
||||
}
|
||||
|
||||
if($can_delete) {
|
||||
$task_to_del = query("SELECT title FROM tasks WHERE id=?", [$del_id])->fetch();
|
||||
$task_to_del = query("SELECT title, assignee_type, assignee_id FROM tasks WHERE id=?", [$del_id])->fetch();
|
||||
query("DELETE FROM tasks WHERE id=?", [$del_id]);
|
||||
if($task_to_del) notify_waha('Deleted', 'Task', $task_to_del['title']);
|
||||
if($task_to_del) {
|
||||
$ctx = ['action' => 'Deleted', 'module' => 'Task', 'item' => $task_to_del['title']];
|
||||
if($task_to_del['assignee_type'] === 'user' && $task_to_del['assignee_id']) {
|
||||
notify_user($task_to_del['assignee_id'], "Task '{$task_to_del['title']}' has been deleted.", $ctx);
|
||||
} elseif($task_to_del['assignee_type'] === 'group' && $task_to_del['assignee_id']) {
|
||||
notify_group($task_to_del['assignee_id'], "Group task '{$task_to_del['title']}' has been deleted.", $ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,17 +155,16 @@ if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_task'])) {
|
||||
}
|
||||
|
||||
// Handle notifications
|
||||
$ctx = ['action' => 'Created', 'module' => 'Task', 'item' => $title, 'assignor' => $_SESSION['username'] ?? 'System', 'due_date' => $due_date ?: 'None'];
|
||||
foreach($to_list as $rec) {
|
||||
$u = query("SELECT id FROM users WHERE username=?", [$rec])->fetch();
|
||||
if($u) notify_user($u['id'], "You have been assigned a new task: $title");
|
||||
if($u) notify_user($u['id'], "You have been assigned a new task: $title", $ctx);
|
||||
else {
|
||||
$g = query("SELECT id FROM groups WHERE name=?", [$rec])->fetch();
|
||||
if($g) notify_group($g['id'], "Your group has been assigned a new task: $title");
|
||||
if($g) notify_group($g['id'], "Your group has been assigned a new task: $title", $ctx);
|
||||
}
|
||||
}
|
||||
|
||||
notify_waha('Created', 'Task', $title);
|
||||
|
||||
if(isset($_POST['ajax_submit'])) {
|
||||
echo json_encode(['success'=>true, 'redirect'=>'?action=tasks&msg_title='.urlencode('Task Created').'&msg_body='.urlencode('Task successfully created.')]);
|
||||
exit;
|
||||
@@ -194,7 +200,12 @@ $custom_like = $custom_tokens ? implode(' OR ', array_fill(0, count($custom_toke
|
||||
|
||||
$params = array_merge([$user_id], $my_group_ids, $my_group_ids, [$user_id], $custom_tokens, $custom_tokens);
|
||||
|
||||
$sql = "SELECT t.*, u.username as assignor_user_name, u.profile_pic as assignor_profile_pic, g.name as assignor_group_name, au.username as assignee_user_name, au.profile_pic as assignee_profile_pic, ag.name as assignee_group_name, cu.username as completed_by_username, aru.username as archived_by_username
|
||||
$sql = "SELECT t.*,
|
||||
u.username as assignor_user_name, u.profile_pic as assignor_user_pic,
|
||||
g.name as assignor_group_name, g.profile_pic as assignor_group_pic,
|
||||
au.username as assignee_user_name, au.profile_pic as assignee_user_pic,
|
||||
ag.name as assignee_group_name, ag.profile_pic as assignee_group_pic,
|
||||
cu.username as completed_by_username, aru.username as archived_by_username
|
||||
FROM tasks t
|
||||
LEFT JOIN users u ON t.assignor_id = u.id
|
||||
LEFT JOIN groups g ON t.assignor_group_id = g.id
|
||||
@@ -237,7 +248,7 @@ if($t_ids) {
|
||||
}
|
||||
}
|
||||
|
||||
// Pre-fetch profile pics for all potential assignees in the view
|
||||
// Pre-fetch profile pics for users
|
||||
$all_usernames = [];
|
||||
foreach($tasks as $t) {
|
||||
if($t['assignee_type'] == 'user' || $t['assignee_type'] == 'custom') {
|
||||
@@ -256,6 +267,21 @@ if($all_usernames) {
|
||||
$user_pics[$row['username']] = $row['profile_pic'];
|
||||
}
|
||||
}
|
||||
// Pre-fetch group pics
|
||||
$all_groupnames = [];
|
||||
foreach($tasks as $t) {
|
||||
if($t['assignee_type'] == 'group') $all_groupnames[] = $t['assignee_group_name'];
|
||||
if($t['assignor_type'] == 'group') $all_groupnames[] = $t['assignor_group_name'];
|
||||
}
|
||||
if($all_groupnames) {
|
||||
$all_groupnames = array_unique($all_groupnames);
|
||||
$in_g = str_repeat('?,', count($all_groupnames) - 1) . '?';
|
||||
$stmt_g = $pdo->prepare("SELECT name, profile_pic FROM groups WHERE name IN ($in_g)");
|
||||
$stmt_g->execute(array_values($all_groupnames));
|
||||
foreach($stmt_g->fetchAll() as $row) {
|
||||
$user_pics[$row['name']] = $row['profile_pic'];
|
||||
}
|
||||
}
|
||||
|
||||
$kanban = ['pending' => [], 'in_progress' => [], 'completed' => [], 'archived' => []];
|
||||
foreach($tasks as $t) {
|
||||
@@ -683,7 +709,7 @@ body.dark-mode .kanban-task { border: 1px solid rgba(255,255,255,0.05); border-l
|
||||
}
|
||||
}
|
||||
|
||||
$from_profile_pic = ($t['assignor_type'] == 'user') ? ($user_pics[$from_name] ?? '') : '';
|
||||
$from_profile_pic = $user_pics[$from_name] ?? '';
|
||||
?>
|
||||
<div style="font-size:0.8rem; display:flex; justify-content:space-between; align-items:center;">
|
||||
<span style="color:var(--text-light);">Assignments:</span>
|
||||
|
||||
Reference in New Issue
Block a user