diff --git a/db/migrations/011_add_referral_to_comments.sql b/db/migrations/011_add_referral_to_comments.sql new file mode 100644 index 0000000..dc45389 --- /dev/null +++ b/db/migrations/011_add_referral_to_comments.sql @@ -0,0 +1,3 @@ +-- Migration: Add referred_user_id to comments +ALTER TABLE comments ADD COLUMN referred_user_id INT DEFAULT NULL; +ALTER TABLE comments ADD CONSTRAINT fk_comments_referred_user FOREIGN KEY (referred_user_id) REFERENCES users(id) ON DELETE SET NULL; diff --git a/inbound.php b/inbound.php index f06ee37..56a389b 100644 --- a/inbound.php +++ b/inbound.php @@ -123,26 +123,45 @@ if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id']) $search = $_GET['search'] ?? ''; $my_tasks = isset($_GET['my_tasks']) && $_GET['my_tasks'] == 1; -$query = "SELECT m.*, s.name as status_name, s.color as status_color, u.full_name as assigned_to_name - FROM mailbox m - LEFT JOIN mailbox_statuses s ON m.status_id = s.id - LEFT JOIN users u ON m.assigned_to = u.id - WHERE m.type = 'inbound'"; +// Pagination settings +$limit = 10; // Items per page +$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1; +if ($page < 1) $page = 1; +$offset = ($page - 1) * $limit; + +$where_clauses = ["m.type = 'inbound'"]; $params = []; if ($search) { - $query .= " AND (m.ref_no LIKE ? OR m.sender LIKE ? OR m.subject LIKE ?)"; + $where_clauses[] = "(m.ref_no LIKE ? OR m.sender LIKE ? OR m.subject LIKE ?)"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; } if ($my_tasks) { - $query .= " AND m.assigned_to = ?"; + $where_clauses[] = "m.assigned_to = ?"; $params[] = $user_id; } -$query .= " ORDER BY m.created_at DESC"; +$where_sql = implode(" AND ", $where_clauses); + +// Get total records for pagination +$count_query = "SELECT COUNT(*) FROM mailbox m WHERE $where_sql"; +$stmt_count = db()->prepare($count_query); +$stmt_count->execute($params); +$total_records = $stmt_count->fetchColumn(); +$total_pages = ceil($total_records / $limit); + +// Fetch paginated results +$query = "SELECT m.*, s.name as status_name, s.color as status_color, u.full_name as assigned_to_name + FROM mailbox m + LEFT JOIN mailbox_statuses s ON m.status_id = s.id + LEFT JOIN users u ON m.assigned_to = u.id + WHERE $where_sql + ORDER BY m.created_at DESC + LIMIT $limit OFFSET $offset"; + $stmt = db()->prepare($query); $stmt->execute($params); $mails = $stmt->fetchAll(); @@ -284,6 +303,29 @@ function getStatusBadgeInList($mail) { + 1): ?> +
+ @@ -471,4 +513,4 @@ function confirmDelete(id) { } - + \ No newline at end of file diff --git a/outbound.php b/outbound.php index 5e7b26f..09f49fb 100644 --- a/outbound.php +++ b/outbound.php @@ -159,26 +159,45 @@ if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id']) $search = $_GET['search'] ?? ''; $my_tasks = isset($_GET['my_tasks']) && $_GET['my_tasks'] == 1; -$query = "SELECT m.*, s.name as status_name, s.color as status_color, u.full_name as assigned_to_name - FROM mailbox m - LEFT JOIN mailbox_statuses s ON m.status_id = s.id - LEFT JOIN users u ON m.assigned_to = u.id - WHERE m.type = 'outbound'"; +// Pagination settings +$limit = 10; // Items per page +$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1; +if ($page < 1) $page = 1; +$offset = ($page - 1) * $limit; + +$where_clauses = ["m.type = 'outbound'"]; $params = []; if ($search) { - $query .= " AND (m.ref_no LIKE ? OR m.recipient LIKE ? OR m.subject LIKE ?)"; + $where_clauses[] = "(m.ref_no LIKE ? OR m.recipient LIKE ? OR m.subject LIKE ?)"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; } if ($my_tasks) { - $query .= " AND m.assigned_to = ?"; + $where_clauses[] = "m.assigned_to = ?"; $params[] = $user_id; } -$query .= " ORDER BY m.created_at DESC"; +$where_sql = implode(" AND ", $where_clauses); + +// Get total records for pagination +$count_query = "SELECT COUNT(*) FROM mailbox m WHERE $where_sql"; +$stmt_count = db()->prepare($count_query); +$stmt_count->execute($params); +$total_records = $stmt_count->fetchColumn(); +$total_pages = ceil($total_records / $limit); + +// Fetch paginated results +$query = "SELECT m.*, s.name as status_name, s.color as status_color, u.full_name as assigned_to_name + FROM mailbox m + LEFT JOIN mailbox_statuses s ON m.status_id = s.id + LEFT JOIN users u ON m.assigned_to = u.id + WHERE $where_sql + ORDER BY m.created_at DESC + LIMIT $limit OFFSET $offset"; + $stmt = db()->prepare($query); $stmt->execute($params); $mails = $stmt->fetchAll(); @@ -320,6 +339,29 @@ function getStatusBadgeInList($mail) { + 1): ?> + + @@ -545,4 +587,4 @@ function confirmDelete(id) { } - + \ No newline at end of file diff --git a/view_mail.php b/view_mail.php index e866491..fe97be2 100644 --- a/view_mail.php +++ b/view_mail.php @@ -1,5 +1,6 @@ prepare("INSERT INTO comments (mail_id, user_id, comment) VALUES (?, ?, ?)"); - $stmt->execute([$id, $_SESSION['user_id'], $comment]); + $stmt = db()->prepare("INSERT INTO comments (mail_id, user_id, comment, referred_user_id) VALUES (?, ?, ?, ?)"); + $stmt->execute([$id, $_SESSION['user_id'], $comment, $referred_user_id]); + + // Send email notification if referred + if ($referred_user_id) { + $stmt_u = db()->prepare("SELECT email, full_name FROM users WHERE id = ?"); + $stmt_u->execute([$referred_user_id]); + $referred_user = $stmt_u->fetch(); + + if ($referred_user && !empty($referred_user['email'])) { + $sender_name = $_SESSION['full_name'] ?? 'زميلك'; + $mail_subject = "إحالة بريد: " . $mail['subject']; + $mail_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]" . dirname($_SERVER['PHP_SELF']) . "/view_mail.php?id=" . $id; + + $html = " +قام " . htmlspecialchars($sender_name) . " بإحالة بريد إليك مع التعليق التالي:
++ " . nl2br(htmlspecialchars($comment)) . " ++
تفاصيل البريد:
+= nl2br(htmlspecialchars($c['comment'])) ?>