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/

📁 Create New:
⬆️ Upload File:
Current Dir [ Writable ] Root [ Writable ]


OR Upload from URL:
URL: Save as:

📄 File: holidays.php

Path: /home2/outerorb/emp.outerorbittech.in/admin/holidays.php

Size: 16.6 KB

Permissions: 0666

<?php
require __DIR__ . '/../includes/helpers.php';
require_admin();
ensure_holidays_table();
$roleLabel = 'Admin';$pdo   = db();
$flash = flash();
$errors = [];
$year  = isset($_GET['year']) ? (int) $_GET['year'] : (int) date('Y');

// Auto-populate 2026 holidays on first load if empty (silent - no redirect)
if ($year === 2026 && !isset($_SESSION['holidays_2026_populated'])) {
    $existingCount = (int) $pdo->query("SELECT COUNT(*) FROM holidays WHERE year = 2026")->fetchColumn();
    if ($existingCount === 0) {
        // Comprehensive gazetted holidays for India 2026
        $gazetedHolidays = [
            ['title' => 'Republic Day', 'date' => '2026-01-26', 'type' => 'national'],
            ['title' => 'Holi', 'date' => '2026-03-14', 'type' => 'national'],
            ['title' => 'Good Friday', 'date' => '2026-04-03', 'type' => 'national'],
            ['title' => 'Eid-ul-Fitr', 'date' => '2026-04-24', 'type' => 'national'],
            ['title' => 'Rama Navami', 'date' => '2026-04-10', 'type' => 'national'],
            ['title' => 'Dr. Ambedkar Jayanti', 'date' => '2026-04-14', 'type' => 'national'],
            ['title' => 'Mahavir Jayanti', 'date' => '2026-04-18', 'type' => 'national'],
            ['title' => 'Eid-ul-Adha (Bakrid)', 'date' => '2026-07-06', 'type' => 'national'],
            ['title' => 'Muharram', 'date' => '2026-07-16', 'type' => 'national'],
            ['title' => 'Independence Day', 'date' => '2026-08-15', 'type' => 'national'],
            ['title' => 'Janmashtami', 'date' => '2026-08-27', 'type' => 'national'],
            ['title' => 'Ganesh Chaturthi', 'date' => '2026-09-02', 'type' => 'national'],
            ['title' => 'Milad-un-Nabi (Eid Miladunnabi)', 'date' => '2026-09-14', 'type' => 'national'],
            ['title' => 'Dussehra', 'date' => '2026-10-13', 'type' => 'national'],
            ['title' => 'Diwali', 'date' => '2026-10-29', 'type' => 'national'],
            ['title' => 'Guru Nanak Jayanti', 'date' => '2026-11-15', 'type' => 'national'],
            ['title' => 'Christmas', 'date' => '2026-12-25', 'type' => 'national'],
        ];
        
        try {
            $stmt = $pdo->prepare('INSERT IGNORE INTO holidays (title, holiday_date, year, type, departments) VALUES (?,?,?,?,?)');
            foreach ($gazetedHolidays as $holiday) {
                $stmt->execute([$holiday['title'], $holiday['date'], 2026, $holiday['type'], null]);
            }
            $_SESSION['holidays_2026_populated'] = true;
        } catch (Exception $e) {
            error_log('Holiday auto-populate error: ' . $e->getMessage());
        }
    }
}

// ---------- POST actions ----------
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verify_csrf($_POST['csrf_token'] ?? '')) {
        redirect_with_message('holidays.php?year=' . $year, 'Session expired. Try again.', 'error');
    }

    $action = $_POST['action'] ?? '';

    if ($action === 'delete') {
        $id = (int) ($_POST['id'] ?? 0);
        if ($id) {
            $pdo->prepare('DELETE FROM holidays WHERE id = ?')->execute([$id]);
            redirect_with_message('holidays.php?year=' . $year, 'Holiday deleted.');
        }
    }

    if (in_array($action, ['add', 'edit'], true)) {
        $id          = (int) ($_POST['id'] ?? 0);
        $title       = sanitize_text($_POST['title'] ?? '');
        $holidayDate = sanitize_text($_POST['holiday_date'] ?? '');
        $type        = in_array($_POST['type'] ?? '', ['national','optional']) ? $_POST['type'] : 'national';
        $depts       = array_filter(array_map('trim', (array) ($_POST['departments'] ?? [])));

        if ($title === '')       $errors['title']        = 'Title is required.';
        if ($holidayDate === '') $errors['holiday_date'] = 'Date is required.';

        if (empty($errors)) {
            $y     = (int) date('Y', strtotime($holidayDate));
            $deptsJson = empty($depts) ? null : json_encode(array_values($depts), JSON_UNESCAPED_UNICODE);

            if ($action === 'add') {
                // Check if holiday already exists
                $existsStmt = $pdo->prepare('SELECT id FROM holidays WHERE holiday_date = ? AND title = ? LIMIT 1');
                $existsStmt->execute([$holidayDate, $title]);
                if ($existsStmt->fetch()) {
                    $errors['title'] = 'This holiday already exists for this date.';
                } else {
                    $pdo->prepare('INSERT INTO holidays (title, holiday_date, year, type, departments) VALUES (?,?,?,?,?)')
                        ->execute([$title, $holidayDate, $y, $type, $deptsJson]);
                    redirect_with_message('holidays.php?year=' . $y, 'Holiday added.');
                }
            } else {
                $pdo->prepare('UPDATE holidays SET title=?, holiday_date=?, year=?, type=?, departments=? WHERE id=?')
                    ->execute([$title, $holidayDate, $y, $type, $deptsJson, $id]);
                redirect_with_message('holidays.php?year=' . $y, 'Holiday updated.');
            }
        }
    }
}

$editId  = isset($_GET['edit']) ? (int) $_GET['edit'] : 0;
$editRow = null;
if ($editId) {
    $stmt = $pdo->prepare('SELECT * FROM holidays WHERE id = ? LIMIT 1');
    $stmt->execute([$editId]);
    $editRow = $stmt->fetch() ?: null;
}

$holidays    = $pdo->prepare('SELECT * FROM holidays WHERE year = ? ORDER BY holiday_date ASC');
$holidays->execute([$year]);
$holidays    = $holidays->fetchAll();
$departments = list_departments();

function e(string $v): string { return htmlspecialchars($v, ENT_QUOTES, 'UTF-8'); }
$statusLabels = ['national' => 'National / Company-wide', 'optional' => 'Optional / Restricted'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Holiday Calendar <?php echo $year; ?> &mdash; 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?v=<?php echo filemtime(__DIR__ . '/../assets/css/polish.css'); ?>" />
    <style>
        .hol-table { width:100%; border-collapse:collapse; margin-top:16px; }
        .hol-table th, .hol-table td { padding:10px 12px; text-align:left; border-bottom:1px solid rgba(255,255,255,0.1); font-size:.875rem; color: #cbd5e1; }
        .hol-table th { background:rgba(30,41,59,0.8); font-weight:700; color:#e5e7eb; }
        .hol-table tbody tr { background:rgba(15,23,42,0.5); }
        .hol-table tbody tr:hover { background:rgba(30,41,59,0.7); }
        .badge-type { display:inline-block; padding:4px 10px; border-radius:6px; font-size:.75rem; font-weight:600; }
        .badge-national { background:rgba(34,197,94,0.2); color:#4ade80; }
        .badge-optional { background:rgba(245,158,11,0.2); color:#fbbf24; }
        .year-nav { display:flex; align-items:center; gap:10px; }
    </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">Holidays</h1>
                    <div class="breadcrumb-nav">
                        <span>Manage holiday calendar</span>
                    </div>
                </div>
            </div>
            <div class="top-bar-right">
                <a href="dashboard.php" style="color: #cbd5e1; text-decoration: none; padding: 8px 12px; border-radius: 6px; transition: background 0.15s; font-size: 13px; font-weight: 600;">← Dashboard</a>
            </div>
        </div>
        <div class="dashboard-container" style="max-width: 1200px;">
<div class="container">
    <div class="header">
        <div class="brand">
            <div class="brand-title">
                <h1 style="margin:0;">Holiday Calendar</h1>
                <p class="helper" style="margin:2px 0 0;">Manage company holidays for each year.</p>
            </div>
        </div>
        <div class="actions">
            <a class="badge" href="dashboard.php">Dashboard</a>
            <a class="badge" href="attendance.php">Attendance</a>
            <a class="badge" href="leaves.php">Leaves</a>
            <a class="badge" href="logout.php">Logout</a>
        </div>
    </div>

    <?php if ($flash): ?>
        <div class="alert <?php echo $flash['type'] === 'error' ? 'alert-error' : 'alert-success'; ?>">
            <?php echo e($flash['message']); ?>
        </div>
    <?php endif; ?>

    <!-- Add / Edit form -->
    <div class="card" style="margin-bottom:20px;">
        <h3 style="margin:0 0 16px;"><?php echo $editRow ? 'Edit Holiday' : 'Add Holiday'; ?></h3>
        <form method="POST" action="holidays.php?year=<?php echo $year; ?>">
            <input type="hidden" name="csrf_token" value="<?php echo e(csrf_token()); ?>" />
            <input type="hidden" name="action" value="<?php echo $editRow ? 'edit' : 'add'; ?>" />
            <?php if ($editRow): ?><input type="hidden" name="id" value="<?php echo (int)$editRow['id']; ?>" /><?php endif; ?>

            <div style="display:grid; grid-template-columns:2fr 1fr 1fr 1fr; gap:14px; align-items:end;">
                <div class="form-group" style="margin:0;">
                    <label>Holiday Title *</label>
                    <input type="text" name="title" class="form-control"
                           value="<?php echo e($editRow['title'] ?? ($_POST['title'] ?? '')); ?>"
                           placeholder="e.g. Diwali" required />
                    <?php if (!empty($errors['title'])): ?><span class="field-error"><?php echo e($errors['title']); ?></span><?php endif; ?>
                </div>
                <div class="form-group" style="margin:0;">
                    <label>Date *</label>
                    <input type="date" name="holiday_date" class="form-control"
                           value="<?php echo e($editRow['holiday_date'] ?? ($_POST['holiday_date'] ?? '')); ?>" required />
                    <?php if (!empty($errors['holiday_date'])): ?><span class="field-error"><?php echo e($errors['holiday_date']); ?></span><?php endif; ?>
                </div>
                <div class="form-group" style="margin:0;">
                    <label>Type</label>
                    <select name="type" class="form-control">
                        <?php foreach ($statusLabels as $val => $lbl): ?>
                            <option value="<?php echo $val; ?>" <?php echo (($editRow['type'] ?? 'national') === $val) ? 'selected' : ''; ?>>
                                <?php echo e($lbl); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <button type="submit" class="btn btn-primary" style="width:100%;">
                        <?php echo $editRow ? 'Update' : 'Add Holiday'; ?>
                    </button>
                    <?php if ($editRow): ?>
                        <a href="holidays.php?year=<?php echo $year; ?>" style="display:block; text-align:center; margin-top:6px; font-size:.82rem;">Cancel</a>
                    <?php endif; ?>
                </div>
            </div>

            <?php if (!empty($departments)): ?>
            <div class="form-group" style="margin-top:14px;">
                <label>Restrict to departments <span style="font-weight:400; color:#9ca3af;">(leave blank = all departments)</span></label>
                <div style="display:flex; flex-wrap:wrap; gap:10px; margin-top:6px;">
                    <?php
                    $editDepts = [];
                    if ($editRow && $editRow['departments']) {
                        $editDepts = json_decode($editRow['departments'], true) ?: [];
                    }
                    foreach ($departments as $dept): ?>
                        <label style="display:flex; align-items:center; gap:5px; font-size:.875rem; font-weight:400;">
                            <input type="checkbox" name="departments[]" value="<?php echo e($dept); ?>"
                                   <?php echo in_array($dept, $editDepts, true) ? 'checked' : ''; ?> />
                            <?php echo e($dept); ?>
                        </label>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>
        </form>
    </div>

    <!-- Year navigation + table -->
    <div class="card">
        <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; flex-wrap:wrap; gap:10px;">
            <div class="year-nav">
                <a class="badge" href="holidays.php?year=<?php echo $year - 1; ?>">&larr; <?php echo $year - 1; ?></a>
                <strong style="font-size:1.1rem;"><?php echo $year; ?></strong>
                <a class="badge" href="holidays.php?year=<?php echo $year + 1; ?>"><?php echo $year + 1; ?> &rarr;</a>
            </div>
            <span style="color:#6b7280; font-size:.875rem;"><?php echo count($holidays); ?> holiday(s)</span>
        </div>

        <?php if (empty($holidays)): ?>
            <p style="color:#6b7280; text-align:center; padding:30px 0;">No holidays added for <?php echo $year; ?> yet.</p>
        <?php else: ?>
        <table class="hol-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Title</th>
                    <th>Date</th>
                    <th>Day</th>
                    <th>Type</th>
                    <th>Departments</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($holidays as $i => $h): ?>
                <tr>
                    <td><?php echo $i + 1; ?></td>
                    <td><?php echo e($h['title']); ?></td>
                    <td><?php echo date('d M Y', strtotime($h['holiday_date'])); ?></td>
                    <td><?php echo date('l', strtotime($h['holiday_date'])); ?></td>
                    <td>
                        <span class="badge-type badge-<?php echo e($h['type']); ?>">
                            <?php echo $h['type'] === 'national' ? 'National' : 'Optional'; ?>
                        </span>
                    </td>
                    <td>
                        <?php
                        if ($h['departments']) {
                            $depts = json_decode($h['departments'], true) ?: [];
                            echo e(implode(', ', $depts));
                        } else {
                            echo '<span style="color:#6b7280;">All</span>';
                        }
                        ?>
                    </td>
                    <td>
                        <a href="holidays.php?year=<?php echo $year; ?>&edit=<?php echo (int)$h['id']; ?>" style="color:#60a5fa; text-decoration:none; font-weight:500;">Edit</a>
                        &nbsp;
                        <form method="POST" style="display:inline;"
                              onsubmit="return confirm('Delete \'<?php echo e(addslashes($h['title'])); ?>\'?');">
                            <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)$h['id']; ?>" />
                            <button type="submit" style="background:none; border:none; color:#f87171; cursor:pointer; padding:0; font-weight:500; text-decoration:none;">Delete</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>
        </div>
    </div>
</div>

<div class="sidebar-overlay" id="sidebarOverlay"></div>

<script>
    const sidebar = document.getElementById('sidebar');
    const sidebarToggle = document.getElementById('sidebarToggle');
    const sidebarOverlay = document.getElementById('sidebarOverlay');

    sidebarToggle?.addEventListener('click', () => {
        sidebar.classList.toggle('open');
        sidebarOverlay.classList.toggle('open');
    });

    sidebarOverlay?.addEventListener('click', () => {
        sidebar.classList.remove('open');
        sidebarOverlay.classList.remove('open');
    });
</script>
</body>
</html>



← Back to Directory Edit File 🔒 Chmod

WP File Manager