« Back to History
rapier_karigar_machine_assign.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('rapier_karigar_machine_assign'); $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $u = $ctx['user'] ?? null; if (!$pdo) { require_once __DIR__ . '/core/db.php'; } // CSRF if (session_status() !== PHP_SESSION_ACTIVE) session_start(); if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } // Save (POST handler BEFORE header.php to allow redirect) if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { die('Invalid CSRF token'); } $pdo->beginTransaction(); try { $stmt = $pdo->prepare("DELETE FROM rapier_karigar_machine_map WHERE company_id = :cid"); $stmt->execute([':cid' => $company_id]); if (!empty($_POST['assign'])) { $stmt = $pdo->prepare(" INSERT INTO rapier_karigar_machine_map (company_id, employee_id, machine_id) VALUES (:cid, :eid, :mid) "); foreach ($_POST['assign'] as $eid => $machine_ids) { foreach ($machine_ids as $mid) { $stmt->execute([ ':cid' => $company_id, ':eid' => (int)$eid, ':mid' => (int)$mid ]); } } } $pdo->commit(); header("Location: rapier_karigar_machine_assign.php?success=1"); exit; } catch (Exception $e) { $pdo->rollBack(); $save_error = htmlspecialchars($e->getMessage()); } } require_once __DIR__ . '/partials/header.php'; // Fetch karigar (Rapier department_id = 20) $stmt = $pdo->prepare(" SELECT id, name FROM company_employee_master WHERE company_id = :cid AND department_id = 20 AND COALESCE(is_active,1) = 1 ORDER BY name "); $stmt->execute([':cid' => $company_id]); $karigars = $stmt->fetchAll(PDO::FETCH_ASSOC); // Fetch machines (rapier only) $stmt = $pdo->prepare(" SELECT id, code FROM machines WHERE company_id = :cid AND machine_type = 'Rapier' AND is_active = 1 ORDER BY code "); $stmt->execute([':cid' => $company_id]); $machines = $stmt->fetchAll(PDO::FETCH_ASSOC); // Existing mapping $stmt = $pdo->prepare(" SELECT employee_id, machine_id FROM rapier_karigar_machine_map WHERE company_id = :cid "); $stmt->execute([':cid' => $company_id]); $map = []; foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { $map[$row['employee_id']][] = $row['machine_id']; } ?> <div class="container-fluid mt-3"> <div class="card"> <div class="card-header"> <h5>Rapier Karigar Machine Assign</h5> </div> <div class="card-body"> <?php if (!empty($save_error)): ?> <div class="alert alert-danger">Error: <?= $save_error ?></div> <?php endif; ?> <?php if (!empty($_GET['success'])): ?> <div class="alert alert-success">Saved successfully</div> <?php endif; ?> <form method="post"> <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>"> <div class="table-responsive"> <table class="table table-bordered table-sm"> <thead> <tr> <th>Karigar</th> <th>Machines</th> </tr> </thead> <tbody> <?php foreach ($karigars as $k): ?> <tr> <td><?= htmlspecialchars($k['name']) ?></td> <td> <?php foreach ($machines as $m): ?> <label class="me-2"> <input type="checkbox" name="assign[<?= $k['id'] ?>][]" value="<?= $m['id'] ?>" <?= in_array($m['id'], $map[$k['id']] ?? []) ? 'checked' : '' ?> > <?= htmlspecialchars($m['code']) ?> </label> <?php endforeach; ?> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <button type="submit" class="btn btn-primary">Save</button> </form> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>