« Back to History
department_master.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('department_manage'); $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $u = $ctx['user'] ?? null; require_once __DIR__ . '/helpers/activity_helper.php'; require_once __DIR__ . '/partials/header.php'; // CSRF if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } $msg = ''; $error = ''; // INSERT if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { $error = "Invalid request"; } else { $department_name = trim($_POST['department_name'] ?? ''); if ($department_name === '') { $error = "Department name required"; } else { $checkStmt = $pdo->prepare( "SELECT 1 FROM department_master WHERE company_id = :cid AND department_name = :name LIMIT 1" ); $checkStmt->execute([ ':cid' => $company_id, ':name' => $department_name ]); if ($checkStmt->fetch()) { $error = "Department already exists for this company"; } else { try { $stmt = $pdo->prepare(" INSERT INTO department_master (company_id, department_name) VALUES (:cid, :name) "); $stmt->execute([ ':cid' => $company_id, ':name' => $department_name ]); activity_create('company', 'department_master_create', (int)$pdo->lastInsertId(), 'Created department'); $msg = "Department added successfully"; } catch (PDOException $e) { if ($e->getCode() == 23000) { $error = "Department already exists for this company"; } else { $error = "Error: " . $e->getMessage(); } } } } } } // FETCH LIST $stmt = $pdo->prepare(" SELECT id, department_name, created_at FROM department_master WHERE company_id = :cid ORDER BY id DESC "); $stmt->execute([':cid' => $company_id]); $departments = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <div class="container mt-4"> <div class="row"> <div class="col-md-5"> <div class="card shadow-sm"> <div class="card-body"> <h5 class="mb-3">Add Department</h5> <?php if ($msg): ?> <div class="alert alert-success"><?= htmlspecialchars($msg) ?></div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div> <?php endif; ?> <form method="POST"> <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>"> <div class="mb-3"> <label class="form-label">Department Name</label> <input type="text" name="department_name" class="form-control" required> </div> <button class="btn btn-primary w-100">Save Department</button> </form> </div> </div> </div> <div class="col-md-7"> <div class="card shadow-sm"> <div class="card-body"> <h5 class="mb-3">Department List</h5> <div class="table-responsive"> <table class="table table-bordered table-sm align-middle"> <thead class="table-light"> <tr> <th style="width:60px;">#</th> <th>Department</th> <th style="width:180px;">Created</th> </tr> </thead> <tbody> <?php if ($departments): ?> <?php foreach ($departments as $i => $d): ?> <tr> <td><?= $i + 1 ?></td> <td><?= htmlspecialchars($d['department_name']) ?></td> <td><?= htmlspecialchars($d['created_at']) ?></td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="3" class="text-center text-muted">No departments found</td> </tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>