From 627842bf5c6a9efcd93cf8bd5e4be206ddf236f1 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 28 Feb 2026 18:32:41 +0000 Subject: [PATCH] adding editor --- inbound.php | 759 ++++++++++++++++++++------------------------ includes/footer.php | 8 +- internal_outbox.php | 69 ++-- outbound.php | 751 ++++++++++++++++++------------------------- print_outbound.php | 277 ++++++++++++++++ view_mail.php | 3 + 6 files changed, 986 insertions(+), 881 deletions(-) create mode 100644 print_outbound.php diff --git a/inbound.php b/inbound.php index 79d984c..9de8786 100644 --- a/inbound.php +++ b/inbound.php @@ -1,391 +1,301 @@ query("SELECT * FROM mailbox_statuses ORDER BY id ASC")->fetchAll(); -$default_status_id = db()->query("SELECT id FROM mailbox_statuses WHERE is_default = 1 LIMIT 1")->fetchColumn() ?: 1; - -// Function to send assignment notification -function sendAssignmentNotification($assigned_to_id, $ref_no, $subject) { - if (!$assigned_to_id) return; - - $stmt = db()->prepare("SELECT full_name, email FROM users WHERE id = ?"); - $stmt->execute([$assigned_to_id]); - $user = $stmt->fetch(); - - if ($user && !empty($user['email'])) { - $to = $user['email']; - $email_subject = "تنبيه: تم تعيين بريد جديد لك (رقم القيد: $ref_no)"; - $htmlBody = " -
-

مرحباً " . htmlspecialchars($user['full_name']) . "

-

لقد تم تعيين مهمة بريد جديد لك في النظام.

- - - - - - - - - -
رقم القيد" . htmlspecialchars($ref_no) . "
الموضوع" . htmlspecialchars($subject) . "
-

يرجى الدخول للنظام لمتابعة المهمة.

-
-

هذا تنبيه تلقائي، يرجى عدم الرد.

-
- "; - MailService::sendMail($to, $email_subject, $htmlBody); - } -} - -// Handle actions -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $action = $_POST['action'] ?? ''; - - // Permission checks for POST actions - if (($action === 'add' && !canAdd('inbound')) || ($action === 'edit' && !canEdit('inbound'))) { - $error = 'عذراً، ليس لديك الصلاحية للقيام بهذا الإجراء'; +// Handle CRUD operations +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { + if (!canEdit('inbound') && !canAdd('inbound')) { + $error = 'ليس لديك صلاحية للقيام بهذا الإجراء.'; } else { - $type = 'inbound'; + $action = $_POST['action']; + $id = $_POST['id'] ?? 0; $ref_no = $_POST['ref_no'] ?? ''; $date_registered = $_POST['date_registered'] ?? date('Y-m-d'); - $due_date = !empty($_POST['due_date']) ? $_POST['due_date'] : null; + $due_date = $_POST['due_date'] ?? null; $sender = $_POST['sender'] ?? ''; $recipient = $_POST['recipient'] ?? ''; $subject = $_POST['subject'] ?? ''; $description = $_POST['description'] ?? ''; - $status_id = $_POST['status_id'] ?? $default_status_id; - $assigned_to = !empty($_POST['assigned_to']) ? $_POST['assigned_to'] : null; - $id = $_POST['id'] ?? 0; + $status_id = $_POST['status_id'] ?? null; + $assigned_to = $_POST['assigned_to'] ?? null; - if ($ref_no && $subject) { - $should_notify = false; + if ($action === 'add' || $action === 'edit') { try { db()->beginTransaction(); - if ($action === 'add') { - $stmt = db()->prepare("INSERT INTO inbound_mail (ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute([$ref_no, $date_registered, $due_date, $sender, $recipient, $subject, $description, $status_id, $assigned_to, $user_id]); - $mail_id = db()->lastInsertId(); - - if ($assigned_to) { - $should_notify = true; - } - - $_SESSION['success'] = 'تمت إضافة البريد بنجاح'; - } elseif ($action === 'edit') { - $mail_id = $id; - // Get previous assigned_to to check if it changed - $stmt_old = db()->prepare("SELECT assigned_to FROM inbound_mail WHERE id = ?"); - $stmt_old->execute([$id]); - $old_assigned_to = $stmt_old->fetchColumn(); - $stmt = db()->prepare("UPDATE inbound_mail SET ref_no = ?, date_registered = ?, due_date = ?, sender = ?, recipient = ?, subject = ?, description = ?, status_id = ?, assigned_to = ? WHERE id = ? "); + if ($action === 'add') { + if (!canAdd('inbound')) throw new Exception('ليس لديك صلاحية الإضافة.'); + $stmt = db()->prepare("INSERT INTO inbound_mail (ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$ref_no, $date_registered, $due_date, $sender, $recipient, $subject, $description, $status_id, $assigned_to, $_SESSION['user_id']]); + $id = db()->lastInsertId(); + $success = 'تم إضافة البريد الوارد بنجاح.'; + } else { + if (!canEdit('inbound')) throw new Exception('ليس لديك صلاحية التعديل.'); + $stmt = db()->prepare("UPDATE inbound_mail SET ref_no = ?, date_registered = ?, due_date = ?, sender = ?, recipient = ?, subject = ?, description = ?, status_id = ?, assigned_to = ? WHERE id = ?"); $stmt->execute([$ref_no, $date_registered, $due_date, $sender, $recipient, $subject, $description, $status_id, $assigned_to, $id]); - - if ($assigned_to && $assigned_to != $old_assigned_to) { - $should_notify = true; - } - - $_SESSION['success'] = 'تم تحديث البيانات بنجاح'; + $success = 'تم تحديث بيانات البريد الوارد بنجاح.'; } - // Handle Attachments - if (!empty($_FILES['attachments']['name'][0])) { + // Handle file uploads + if (isset($_FILES['attachments']) && !empty($_FILES['attachments']['name'][0])) { $upload_dir = 'uploads/attachments/'; if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true); - foreach ($_FILES['attachments']['name'] as $key => $name) { - if ($_FILES['attachments']['error'][$key] === 0) { - $file_name = time() . '_' . basename($name); - $target_path = $upload_dir . $file_name; - if (move_uploaded_file($_FILES['attachments']['tmp_name'][$key], $target_path)) { + for ($i = 0; $i < count($_FILES['attachments']['name']); $i++) { + if ($_FILES['attachments']['error'][$i] === 0) { + $filename = time() . '_' . $_FILES['attachments']['name'][$i]; + $filepath = $upload_dir . $filename; + if (move_uploaded_file($_FILES['attachments']['tmp_name'][$i], $filepath)) { $stmt = db()->prepare("INSERT INTO inbound_attachments (mail_id, display_name, file_path, file_name, file_size) VALUES (?, ?, ?, ?, ?)"); - $stmt->execute([$mail_id, $name, $target_path, $name, $_FILES['attachments']['size'][$key]]); + $stmt->execute([$id, $_FILES['attachments']['name'][$i], $filepath, $_FILES['attachments']['name'][$i], $_FILES['attachments']['size'][$i]]); } } } } db()->commit(); - - // Notify after commit to avoid holding locks during email sending - if ($should_notify) { - sendAssignmentNotification($assigned_to, $ref_no, $subject); - } - - redirect('inbound.php'); - } catch (PDOException $e) { - if (db()->inTransaction()) db()->rollBack(); - if ($e->getCode() == 23000) { - $error = 'رقم القيد مستخدم مسبقاً'; - } else { - $error = 'حدث خطأ: ' . $e->getMessage(); - } + } catch (Exception $e) { + db()->rollBack(); + $error = 'خطأ: ' . $e->getMessage(); + } + } elseif ($action === 'delete') { + if (!canDelete('inbound')) { + $error = 'ليس لديك صلاحية الحذف.'; + } else { + $stmt = db()->prepare("DELETE FROM inbound_mail WHERE id = ?"); + $stmt->execute([$id]); + $success = 'تم حذف البريد الوارد بنجاح.'; } - } else { - $error = 'يرجى ملء الحقول المطلوبة (رقم القيد، الموضوع)'; } } } -// Delete action -if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) { - if (!canDelete('inbound')) { - $error = 'عذراً، ليس لديك الصلاحية لحذف السجلات'; - } else { - $id = $_GET['id']; - $stmt = db()->prepare("DELETE FROM inbound_mail WHERE id = ? "); - $stmt->execute([$id]); - $_SESSION['success'] = 'تم حذف البريد بنجاح'; - redirect('inbound.php'); - } -} +// Fetch stats +$total_stmt = db()->query("SELECT COUNT(*) FROM inbound_mail"); +$total_inbound = $total_stmt->fetchColumn(); -// Get session messages -if (isset($_SESSION['success'])) { - $success = $_SESSION['success']; - unset($_SESSION['success']); -} -if (isset($_SESSION['error'])) { - $error = $_SESSION['error']; - unset($_SESSION['error']); -} +$pending_stmt = db()->prepare("SELECT COUNT(*) FROM inbound_mail WHERE status_id IN (SELECT id FROM mailbox_statuses WHERE is_default = 1 OR name LIKE '%قيد%')"); +$pending_stmt->execute(); +$pending_inbound = $pending_stmt->fetchColumn(); -$search = $_GET['search'] ?? ''; -$my_tasks = isset($_GET['my_tasks']) && $_GET['my_tasks'] == 1; - -// 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 = ["1=1"]; +// Search and Filter +$where = "WHERE 1=1"; $params = []; -if ($search) { - $where_clauses[] = "(m.ref_no LIKE ? OR m.sender LIKE ? OR m.subject LIKE ?)"; - $params[] = "%$search%"; - $params[] = "%$search%"; - $params[] = "%$search%"; +if (isset($_GET['search']) && !empty($_GET['search'])) { + $where .= " AND (m.ref_no LIKE ? OR m.subject LIKE ? OR m.sender LIKE ? OR m.recipient LIKE ?)"; + $search = "%" . $_GET['search'] . "%"; + $params = array_merge($params, [$search, $search, $search, $search]); } -if ($my_tasks) { - $where_clauses[] = "m.assigned_to = ?"; - $params[] = $user_id; +if (isset($_GET['status_id']) && !empty($_GET['status_id'])) { + $where .= " AND m.status_id = ?"; + $params[] = $_GET['status_id']; } -$where_sql = implode(" AND ", $where_clauses); +if (isset($_GET['my_tasks'])) { + $where .= " AND m.assigned_to = ?"; + $params[] = $_SESSION['user_id']; +} -// Get total records for pagination -$count_query = "SELECT COUNT(*) FROM inbound_mail 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, (SELECT GROUP_CONCAT(display_name SEPARATOR '|||') FROM inbound_attachments WHERE mail_id = m.id) as attachment_names FROM inbound_mail 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"; + $where + ORDER BY m.date_registered DESC, m.id DESC"; $stmt = db()->prepare($query); $stmt->execute($params); $mails = $stmt->fetchAll(); -$users_list = db()->query("SELECT id, full_name FROM users ORDER BY full_name")->fetchAll(); +$statuses = db()->query("SELECT * FROM mailbox_statuses ORDER BY id ASC")->fetchAll(); +$users = db()->query("SELECT id, full_name, username FROM users ORDER BY full_name ASC")->fetchAll(); +$default_status_id = db()->query("SELECT id FROM mailbox_statuses WHERE is_default = 1 LIMIT 1")->fetchColumn() ?: ($statuses[0]['id'] ?? null); -// Handle Deep Link for Edit $deepLinkData = null; -if (isset($_GET['action']) && $_GET['action'] === 'edit' && isset($_GET['id'])) { - if (canEdit('inbound')) { - $stmt = db()->prepare("SELECT m.*, (SELECT GROUP_CONCAT(display_name SEPARATOR '|||') FROM inbound_attachments WHERE mail_id = m.id) as attachment_names FROM inbound_mail m WHERE m.id = ? "); - $stmt->execute([$_GET['id']]); - $deepLinkData = $stmt->fetch(); - } -} - -function getStatusBadgeInList($mail) { - $status_name = $mail['status_name'] ?? 'غير معروف'; - $status_color = $mail['status_color'] ?? '#6c757d'; - - // Translation for default statuses - $display_name = $status_name; - if ($status_name == 'received') $display_name = 'تم الاستلام'; - if ($status_name == 'in_progress') $display_name = 'قيد المعالجة'; - if ($status_name == 'closed') $display_name = 'مكتمل'; - - return '' . htmlspecialchars($display_name) . ''; +if (isset($_GET['id'])) { + $dlStmt = db()->prepare("SELECT m.*, (SELECT GROUP_CONCAT(display_name SEPARATOR '|||') FROM inbound_attachments WHERE mail_id = m.id) as attachment_names FROM inbound_mail m WHERE m.id = ?"); + $dlStmt->execute([$_GET['id']]); + $deepLinkData = $dlStmt->fetch(); } ?> -
-

البريد الوارد

- - - -
- - - - - - - - - -
-
-
-
- -
-
-
- onchange="this.form.submit()"> - -
-
-
- -
- - +
+
+
+

البريد الوارد

+

إدارة جميع المراسلات الواردة والمهام المسندة.

+
+
+ + - +
-
+ + +
+
+
+
+
+
+ +
+
+
إجمالي الوارد
+

+
+
+
+
+
+
+
+
+
+
+ +
+
+
قيد المعالجة
+

+
+
+
+
+
+
+ + +
+ + +
+ + + +
+
+
+
+
+ + +
+
+
+ +
+
+
+ onchange="this.form.submit()"> + +
+
+
+ +
+
+
+
+ + +
- - - - + - + + - + - - - - - - - - - - - - - + + + + + + + + + + +
رقم القيد التاريخالموعد النهائي الموضوعالمرسلالمرفقاتالمسؤولالجهة المرسلة الحالةالإجراءاتالمسؤولالإجراءات
- - - - - - - - - - - + + +

لا يوجد بريد وارد حالياً.

- - - - - - - - - - - غير معين - - - - - - - - - - - -
لا يوجد بريد وارد مسجل حالياً
+
+ + + مرفقات + + +
+ + + + +
+
+ +
+ +
+
+
+ + + + + + + +
+ + + +
+ +
+
- 1): ?> - -
- - +