From ad6d09dcf3aaed77161a7ebff466201921f3bd30 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sun, 1 Mar 2026 03:37:35 +0000 Subject: [PATCH] add migration 14 --- .../014_add_display_name_to_attachments.sql | 4 + db/migrations/015_split_mailbox_tables.sql | 35 +- inbound.php | 3 + print_inbound.php | 313 ++++++++++++++++++ view_mail.php | 4 +- 5 files changed, 339 insertions(+), 20 deletions(-) create mode 100644 db/migrations/014_add_display_name_to_attachments.sql create mode 100644 print_inbound.php diff --git a/db/migrations/014_add_display_name_to_attachments.sql b/db/migrations/014_add_display_name_to_attachments.sql new file mode 100644 index 0000000..5af6d56 --- /dev/null +++ b/db/migrations/014_add_display_name_to_attachments.sql @@ -0,0 +1,4 @@ +-- Migration: Add display_name to attachments table +-- This column is needed for the split_mailbox migration (015) to work correctly + +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS display_name VARCHAR(255) AFTER mail_id; diff --git a/db/migrations/015_split_mailbox_tables.sql b/db/migrations/015_split_mailbox_tables.sql index 30ee704..3de8ae4 100644 --- a/db/migrations/015_split_mailbox_tables.sql +++ b/db/migrations/015_split_mailbox_tables.sql @@ -127,50 +127,47 @@ CREATE TABLE IF NOT EXISTS internal_comments ( FOREIGN KEY (referred_user_id) REFERENCES users(id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; --- 4. Migrate data from old mailbox table --- We'll use a temporary mapping for IDs if we were strict, but since we are splitting, we can just insert. --- Note: Original IDs will change, which means attachments and comments must be migrated carefully. - --- Migrate Inbound -INSERT INTO inbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) +-- 4. Migrate data (using INSERT IGNORE to allow re-running partially failed migrations) +INSERT IGNORE INTO inbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at FROM mailbox WHERE type = 'inbound'; -INSERT INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) +INSERT IGNORE INTO inbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'inbound'; -INSERT INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) +INSERT IGNORE INTO inbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'inbound'; --- Migrate Outbound -INSERT INTO outbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) +INSERT IGNORE INTO outbound_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at FROM mailbox WHERE type = 'outbound'; -INSERT INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) +INSERT IGNORE INTO outbound_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'outbound'; -INSERT INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) +INSERT IGNORE INTO outbound_comments (id, mail_id, user_id, comment, referred_user_id, created_at) SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'outbound'; --- Migrate Internal -INSERT INTO internal_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) +INSERT IGNORE INTO internal_mail (id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at) SELECT id, ref_no, date_registered, due_date, sender, recipient, subject, description, status_id, assigned_to, created_by, created_at, updated_at FROM mailbox WHERE type = 'internal'; -INSERT INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) +INSERT IGNORE INTO internal_attachments (id, mail_id, display_name, file_path, file_name, file_size, created_at) SELECT a.id, a.mail_id, a.display_name, a.file_path, a.file_name, a.file_size, a.created_at FROM attachments a JOIN mailbox m ON a.mail_id = m.id WHERE m.type = 'internal'; -INSERT INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at) +INSERT IGNORE INTO internal_comments (id, mail_id, user_id, comment, referred_user_id, created_at) SELECT c.id, c.mail_id, c.user_id, c.comment, c.referred_user_id, c.created_at FROM comments c JOIN mailbox m ON c.mail_id = m.id WHERE m.type = 'internal'; -- 5. Rename old tables instead of dropping for safety -RENAME TABLE mailbox TO mailbox_old; -RENAME TABLE attachments TO attachments_old; -RENAME TABLE comments TO comments_old; +-- Using a check because RENAME TABLE fails if the target exists +SET @old_mailbox = (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'mailbox_old'); +SET @sql_rename = IF(@old_mailbox = 0, 'RENAME TABLE mailbox TO mailbox_old, attachments TO attachments_old, comments TO comments_old', 'SELECT 1'); +PREPARE stmt FROM @sql_rename; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; \ No newline at end of file diff --git a/inbound.php b/inbound.php index d056af8..5135632 100644 --- a/inbound.php +++ b/inbound.php @@ -264,6 +264,9 @@ if (isset($_GET['id'])) {
+ + + diff --git a/print_inbound.php b/print_inbound.php new file mode 100644 index 0000000..9a818e8 --- /dev/null +++ b/print_inbound.php @@ -0,0 +1,313 @@ +prepare("SELECT * FROM inbound_mail WHERE id = ?"); +$stmt->execute([$id]); +$mail = $stmt->fetch(); + +if (!$mail) { + die('Mail not found'); +} + +$settings = get_settings(); +$logo = !empty($settings['site_logo']) ? $settings['site_logo'] : ''; +$site_name = $settings['site_name']; +$site_address = $settings['site_address']; + +/** + * Convert Gregorian date to Hijri + */ +function gregorianToHijri($date) { + if (!$date) return ''; + $time = strtotime($date); + $m = date('m', $time); + $d = date('d', $time); + $y = date('Y', $time); + + if (($y > 1582) || (($y == 1582) && ($m > 10)) || (($y == 1582) && ($m == 10) && ($d > 14))) { + $jd = (int)((1461 * ($y + 4800 + (int)(($m - 14) / 12))) / 4) + + (int)((367 * ($m - 2 - 12 * ((int)(($m - 14) / 12)))) / 12) - + (int)((3 * ((int)(($y + 4900 + (int)(($m - 14) / 12)) / 100))) / 4) + + $d - 32075; + } else { + $jd = 367 * $y - (int)((7 * ($y + 5001 + (int)(($m - 9) / 7))) / 4) + (int)((275 * $m) / 9) + $d + 1729777; + } + + $l = $jd - 1948440 + 10632; + $n = (int)(($l - 1) / 10631); + $l = $l - 10631 * $n + 354; + $j = ((int)((10985 - $l) / 5316)) * ((int)((50 * $l) / 17719)) + ((int)($l / 5670)) * ((int)((43 * $l) / 15238)); + $l = $l - ((int)((30 - $j) / 15)) * ((int)((17719 * $j) / 50)) - ((int)($j / 16)) * ((int)((15238 * $j) / 43)) + 29; + + $month = (int)((24 * $l) / 709); + $day = $l - (int)((709 * $month) / 24); + $year = 30 * $n + $j - 30; + + $hijriMonths = [ + 1 => "محرم", 2 => "صفر", 3 => "ربيع الأول", 4 => "ربيع الآخر", + 5 => "جمادى الأولى", 6 => "جمادى الآخرة", 7 => "رجب", 8 => "شعبان", + 9 => "رمضان", 10 => "شوال", 11 => "ذو القعدة", 12 => "ذو الحجة" + ]; + + return $day . ' ' . $hijriMonths[$month] . ' ' . $year . ' هـ'; +} + +$hijriDate = gregorianToHijri($mail['date_registered']); +?> + + + + + + طباعة بريد وارد - <?= htmlspecialchars($mail['ref_no']) ?> + + + + + + + + + + + + \ No newline at end of file diff --git a/view_mail.php b/view_mail.php index 6990160..54d33dc 100644 --- a/view_mail.php +++ b/view_mail.php @@ -207,6 +207,8 @@ if ($type == 'internal') { تعديل البيانات طباعة + + طباعة
@@ -510,4 +512,4 @@ if ($type == 'internal') { }); - + \ No newline at end of file