« Back to History
designation_master.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('designation_master'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $u = $ctx['user']; require_once __DIR__ . '/helpers/activity_helper.php'; if (session_status() === PHP_SESSION_NONE) session_start(); if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } function h($s){ return htmlspecialchars($s ?? '', ENT_QUOTES, 'UTF-8'); } $msg = ''; $error = ''; // ================= INSERT ================= if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'add') { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { $error = "Invalid request"; } else { $name = trim($_POST['designation_name'] ?? ''); $code = trim($_POST['designation_id'] ?? ''); if ($name === '' || $code === '') { $error = "All fields required"; } else { try { $stmt = $pdo->prepare(" INSERT INTO designation_master (company_id, designation_id, designation_name) VALUES (:cid, :code, :name) "); $stmt->execute([ ':cid' => $company_id, ':code' => $code, ':name' => $name ]); log_activity([ 'activity_type' => 'action', 'module_name' => 'employee', 'action_name' => 'CREATE', 'reference_id' => (int)$pdo->lastInsertId(), 'activity_note' => "Added designation: $name ($code)" ]); header("Location: designation_master.php?added=1"); exit; } catch (PDOException $e) { if ($e->getCode() == 23000) { $error = "Duplicate designation or code"; } else { $error = $e->getMessage(); } } } } } // ================= UPDATE ================= if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'edit') { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { $error = "Invalid request"; } else { $id = (int)$_POST['id']; $name = trim($_POST['designation_name'] ?? ''); $code = trim($_POST['designation_id'] ?? ''); $status = (int)($_POST['status'] ?? 1); try { $stmt = $pdo->prepare(" UPDATE designation_master SET designation_name = :name, designation_id = :code, status = :status WHERE id = :id AND company_id = :cid "); $stmt->execute([ ':name' => $name, ':code' => $code, ':status' => $status, ':id' => $id, ':cid' => $company_id ]); log_activity([ 'activity_type' => 'action', 'module_name' => 'employee', 'action_name' => 'UPDATE', 'reference_id' => $id, 'activity_note' => "Updated designation ID: $id to $name" ]); header("Location: designation_master.php?updated=1"); exit; } catch (PDOException $e) { if ($e->getCode() == 23000) { $error = "Duplicate designation or code"; } else { $error = $e->getMessage(); } } } } // ================= FETCH ================= $stmt = $pdo->prepare(" SELECT * FROM designation_master WHERE company_id = :cid ORDER BY id DESC "); $stmt->execute([':cid' => $company_id]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // 🔴 अब header include (IMPORTANT) require_once __DIR__ . '/partials/header.php'; ?> <div class="container mt-4"> <div class="row"> <!-- ADD --> <div class="col-md-5"> <div class="card shadow-sm"> <div class="card-body"> <h5 class="mb-3">Add Designation</h5> <?php if (!empty($_GET['added'])): ?> <div class="alert alert-success">Added successfully</div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"><?= h($error) ?></div> <?php endif; ?> <form method="POST"> <input type="hidden" name="action" value="add"> <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>"> <div class="mb-3"> <label class="form-label">Designation Code</label> <input type="text" name="designation_id" class="form-control" required> </div> <div class="mb-3"> <label class="form-label">Designation Name</label> <input type="text" name="designation_name" class="form-control" required> </div> <button class="btn btn-primary w-100">Save</button> </form> </div> </div> </div> <!-- LIST --> <div class="col-md-7"> <div class="card shadow-sm"> <div class="card-body"> <h5 class="mb-3">Designation List</h5> <?php if (!empty($_GET['updated'])): ?> <div class="alert alert-success">Updated successfully</div> <?php endif; ?> <div class="table-responsive"> <table class="table table-bordered table-sm align-middle"> <thead class="table-light"> <tr> <th>#</th> <th>Code</th> <th>Name</th> <th>Status</th> <th style="width:120px;">Action</th> </tr> </thead> <tbody> <?php foreach ($rows as $i => $r): ?> <tr> <form method="POST"> <td><?= $i+1 ?></td> <td> <input type="text" name="designation_id" value="<?= h($r['designation_id']) ?>" class="form-control form-control-sm"> </td> <td> <input type="text" name="designation_name" value="<?= h($r['designation_name']) ?>" class="form-control form-control-sm"> </td> <td> <select name="status" class="form-control form-control-sm"> <option value="1" <?= $r['status']==1?'selected':'' ?>>Active</option> <option value="0" <?= $r['status']==0?'selected':'' ?>>Inactive</option> </select> </td> <td> <input type="hidden" name="id" value="<?= $r['id'] ?>"> <input type="hidden" name="action" value="edit"> <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>"> <button class="btn btn-sm btn-success">Save</button> </td> </form> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>