|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/emp.outerorbittech.in/admin/ | |
|
Path: /home2/outerorb/emp.outerorbittech.in/admin/announcements.php
Size: 11.65 KB
Permissions: 0666
<?php
require __DIR__ . '/../includes/helpers.php';
require_admin();
ensure_announcements_table();
$roleLabel = 'Admin';$pdo = db();
$flash = flash();
$admin = current_admin();
$errors = [];
// Departments for restriction
$deptRows = $pdo->query("SELECT DISTINCT department FROM employees WHERE deleted_at IS NULL ORDER BY department")->fetchAll();
// POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!verify_csrf($_POST['csrf_token'] ?? '')) {
redirect_with_message('announcements.php', 'Session expired.', 'error');
}
$action = $_POST['action'] ?? '';
if (in_array($action, ['add', 'edit'])) {
$id = (int)($_POST['id'] ?? 0);
$title = sanitize_text($_POST['title'] ?? '');
$body = sanitize_text($_POST['body'] ?? '');
$depts = isset($_POST['departments']) && is_array($_POST['departments'])
? implode(',', array_map('sanitize_text', $_POST['departments'])) : null;
$pinned = isset($_POST['pinned']) ? 1 : 0;
$publishedAt = sanitize_text($_POST['published_at'] ?? '') ?: date('Y-m-d H:i:s');
$expiresAt = sanitize_text($_POST['expires_at'] ?? '') ?: null;
if (!$title) $errors[] = 'Title is required.';
if (!$body) $errors[] = 'Body is required.';
if (empty($errors)) {
if ($id) {
$pdo->prepare(
'UPDATE announcements SET title=?,body=?,departments=?,pinned=?,published_at=?,expires_at=?,updated_at=NOW() WHERE id=?'
)->execute([$title, $body, $depts, $pinned, $publishedAt, $expiresAt, $id]);
} else {
$pdo->prepare(
'INSERT INTO announcements (title,body,departments,pinned,published_at,expires_at,created_by) VALUES (?,?,?,?,?,?,?)'
)->execute([$title, $body, $depts, $pinned, $publishedAt, $expiresAt, $admin['id'] ?? null]);
}
redirect_with_message('announcements.php', $id ? 'Announcement updated.' : 'Announcement posted.');
}
}
if ($action === 'delete') {
$id = (int)($_POST['id'] ?? 0);
if ($id) { $pdo->prepare('DELETE FROM announcements WHERE id=?')->execute([$id]); }
redirect_with_message('announcements.php', 'Announcement deleted.');
}
if ($action === 'toggle_pin') {
$id = (int)($_POST['id'] ?? 0);
if ($id) { $pdo->prepare('UPDATE announcements SET pinned = NOT pinned WHERE id=?')->execute([$id]); }
redirect_with_message('announcements.php', 'Pin status toggled.');
}
}
// Load announcements
$announcements = $pdo->query(
'SELECT a.*, u.username AS created_by_name
FROM announcements a
LEFT JOIN admin_users u ON u.id = a.created_by
ORDER BY a.is_pinned DESC, a.published_at DESC'
)->fetchAll();
$editItem = null;
if (isset($_GET['edit'])) {
$es = $pdo->prepare('SELECT * FROM announcements WHERE id=? LIMIT 1');
$es->execute([(int)$_GET['edit']]);
$editItem = $es->fetch() ?: null;
}
function e(string $v): string { return htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Announcements — HRMS</title>
<link rel="stylesheet" href="../assets/css/style.css?v=<?php echo filemtime(__DIR__.'/../assets/css/style.css'); ?>" />
<link rel="stylesheet" href="../assets/css/polish.css">
<style>
.ann-card { border:1px solid #e5e7eb; border-radius:10px; padding:16px 20px; margin-bottom:12px; }
.ann-card.pinned { border-left:4px solid #f59e0b; }
.ann-body { white-space:pre-wrap; font-size:.875rem; color:#374151; margin-top:8px; line-height:1.6; }
</style>
</head>
<body class="dashboard-layout">
<div class="app-wrapper">
<?php require '_sidebar.php'; ?>
<div class="main-content">
<div class="top-bar">
<div class="top-bar-left">
<button class="sidebar-toggle" id="sidebarToggle" aria-label="Toggle sidebar">☰</button>
<div>
<h1 class="page-title">Announcements</h1>
<div class="breadcrumb-nav"><span>Post notices and updates for employees.</span></div>
</div>
</div>
<div class="top-bar-right">
<a class="badge" href="dashboard.php">Dashboard</a>
<a class="badge" href="logout.php">Logout</a>
</div>
</div>
<div class="dashboard-container">
<?php if ($flash): ?>
<div class="alert <?php echo $flash['type']==='error' ? 'alert-error' : 'alert-success'; ?>"><?php echo e($flash['message']); ?></div>
<?php endif; ?>
<?php foreach ($errors as $err): ?><div class="alert alert-error"><?php echo e($err); ?></div><?php endforeach; ?>
<!-- Form -->
<div class="card" style="margin-bottom:16px;">
<h4 style="margin:0 0 14px; font-size:.85rem; text-transform:uppercase; color:#6b7280;">
<?php echo $editItem ? 'Edit Announcement' : 'New Announcement'; ?>
</h4>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="<?php echo $editItem ? 'edit' : 'add'; ?>" />
<?php if ($editItem): ?><input type="hidden" name="id" value="<?php echo (int)$editItem['id']; ?>" /><?php endif; ?>
<div class="form-group">
<label>Title *</label>
<input type="text" name="title" class="form-control" required
value="<?php echo e($editItem ? $editItem['title'] : ''); ?>" />
</div>
<div class="form-group">
<label>Body *</label>
<textarea name="body" class="form-control" rows="5" required><?php echo e($editItem ? $editItem['body'] : ''); ?></textarea>
</div>
<div style="display:grid; grid-template-columns:1fr 1fr 1fr; gap:16px;">
<div class="form-group" style="margin:0;">
<label>Published At</label>
<input type="datetime-local" name="published_at" class="form-control"
value="<?php echo $editItem ? date('Y-m-d\TH:i', strtotime($editItem['published_at'])) : date('Y-m-d\TH:i'); ?>" />
</div>
<div class="form-group" style="margin:0;">
<label>Expires At (optional)</label>
<input type="datetime-local" name="expires_at" class="form-control"
value="<?php echo ($editItem && $editItem['expires_at']) ? date('Y-m-d\TH:i', strtotime($editItem['expires_at'])) : ''; ?>" />
</div>
<div class="form-group" style="margin:0;">
<label style="display:flex; align-items:center; gap:6px; margin-top:24px;">
<input type="checkbox" name="pinned" <?php echo ($editItem && $editItem['pinned']) ? 'checked' : ''; ?> />
Pin to top
</label>
</div>
</div>
<?php if (!empty($deptRows)): ?>
<div class="form-group" style="margin-top:12px;">
<label>Restrict to departments (leave blank for all)</label>
<div style="display:flex; flex-wrap:wrap; gap:10px; margin-top:6px;">
<?php
$editDepts = [];
if ($editItem && $editItem['departments']) {
$editDepts = array_map('trim', explode(',', $editItem['departments']));
}
foreach ($deptRows as $dr): ?>
<label style="display:flex; align-items:center; gap:5px; font-size:.85rem;">
<input type="checkbox" name="departments[]" value="<?php echo e($dr['department']); ?>"
<?php echo in_array($dr['department'], $editDepts) ? 'checked' : ''; ?> />
<?php echo e($dr['department']); ?>
</label>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<button type="submit" class="btn btn-primary" style="margin-top:14px;">
<?php echo $editItem ? 'Update' : 'Post Announcement'; ?>
</button>
<?php if ($editItem): ?><a href="announcements.php" style="margin-left:10px; font-size:.85rem; color:#6b7280;">Cancel</a><?php endif; ?>
</form>
</div>
<!-- List -->
<?php if (empty($announcements)): ?>
<div class="card" style="text-align:center; padding:40px; color:#6b7280;">No announcements yet.</div>
<?php else: ?>
<?php foreach ($announcements as $ann):
$isExpired = $ann['expires_at'] && strtotime($ann['expires_at']) < time();
?>
<div class="ann-card <?php echo $ann['pinned'] ? 'pinned' : ''; ?>" style="<?php echo $isExpired ? 'opacity:.55;' : ''; ?>">
<div style="display:flex; justify-content:space-between; align-items:flex-start; gap:12px;">
<div>
<div style="font-weight:700;">
<?php if ($ann['pinned']): ?><span style="color:#f59e0b;">📌 </span><?php endif; ?>
<?php echo e($ann['title']); ?>
</div>
<div style="font-size:.75rem; color:#9ca3af; margin-top:2px;">
Posted <?php echo date('d M Y H:i', strtotime($ann['published_at'])); ?>
<?php if ($ann['created_by_name']): ?> by <?php echo e($ann['created_by_name']); ?><?php endif; ?>
<?php if ($ann['departments']): ?> • <em><?php echo e($ann['departments']); ?></em><?php endif; ?>
<?php if ($isExpired): ?> • <span style="color:#ef4444;">Expired</span><?php endif; ?>
</div>
</div>
<div style="display:flex; gap:8px; flex-shrink:0;">
<form method="POST" style="display:inline;">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="toggle_pin" />
<input type="hidden" name="id" value="<?php echo (int)$ann['id']; ?>" />
<button type="submit" style="background:none; border:none; cursor:pointer; font-size:.78rem; color:#6b7280;"><?php echo $ann['pinned'] ? 'Unpin' : 'Pin'; ?></button>
</form>
<a href="announcements.php?edit=<?php echo (int)$ann['id']; ?>" style="font-size:.78rem; color:#2563eb;">Edit</a>
<form method="POST" style="display:inline;" onsubmit="return confirm('Delete this announcement?');">
<input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?php echo (int)$ann['id']; ?>" />
<button type="submit" style="background:none; border:none; cursor:pointer; font-size:.78rem; color:#ef4444;">Delete</button>
</form>
</div>
</div>
<div class="ann-body"><?php echo e($ann['body']); ?></div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>