« Back to History
mesr_dynamic_delete.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_dynamic_delete'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; /* ================= TABLE LIST ================= */ $allowed_tables = [ 'mesr_beam_to_yarn_stock', 'mesr_kasab_stock', 'mesr_yarn_in_beam_stock', 'mesr_yarn_in_open_box', 'mesr_yarn_in_closed_box', 'mesr_yarn_in_bobin', 'mesr_yarn_in_running_beam', 'mesr_yarn_in_machine', ]; /* ================= LOCATION LIST ================= */ $loc_st = $pdo->prepare(" SELECT id, location_name FROM mesr_data_location WHERE company_id = :cid ORDER BY location_name "); $loc_st->execute([':cid'=>$company_id]); $locations = $loc_st->fetchAll(PDO::FETCH_ASSOC); /* ================= INPUT ================= */ $table = $_GET['table'] ?? ''; $location = $_GET['location'] ?? 'all'; $success = ''; $error = ''; /* ================= DELETE ================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $table = $_POST['table'] ?? ''; $id = (int)$_POST['id']; if (!in_array($table, $allowed_tables)) { $error = "Invalid table."; } elseif ($id <= 0) { $error = "Invalid ID."; } else { $st = $pdo->prepare(" DELETE FROM {$table} WHERE id = :id AND company_id = :cid LIMIT 1 "); $st->execute([ ':id'=>$id, ':cid'=>$company_id ]); $success = $st->rowCount() ? "Deleted successfully" : "Not found"; if ($st->rowCount()) { require_once __DIR__ . '/../helpers/activity_helper.php'; log_activity([ 'activity_type' => 'entry', 'module_name' => 'mesr_dynamic_delete', 'action_name' => 'delete', 'activity_note' => "Table: $table, ID: $id deleted", 'reference_id' => $id, ]); } } } /* ================= FETCH DATA ================= */ $rows = []; if ($table && in_array($table, $allowed_tables)) { $sql = " SELECT t.*, l.location_name FROM {$table} t LEFT JOIN mesr_data_location l ON l.id = t.location_id AND l.company_id = t.company_id WHERE t.company_id = :cid "; $params = [':cid'=>$company_id]; if ($location !== 'all') { $sql .= " AND t.location_id = :loc"; $params[':loc'] = $location; } $sql .= " ORDER BY t.id DESC LIMIT 50"; $st = $pdo->prepare($sql); $st->execute($params); $rows = $st->fetchAll(PDO::FETCH_ASSOC); } require_once __DIR__ . '/../partials/header.php'; ?> <div class="container-fluid py-4"> <div class="card shadow-sm mb-4"> <div class="card-body"> <h4 class="fw-bold mb-3">MESR Dynamic Delete</h4> <?php if ($success): ?> <div class="alert alert-success"><?= htmlspecialchars($success) ?></div> <?php endif; ?> <?php if ($error): ?> <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div> <?php endif; ?> <!-- ================= FILTER ================= --> <form method="get" class="row g-3 align-items-end"> <div class="col-md-3"> <label class="form-label">Table</label> <select name="table" class="form-select" required> <option value="">Select</option> <?php foreach ($allowed_tables as $t): ?> <option value="<?= $t ?>" <?= $table==$t?'selected':'' ?>> <?= $t ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label">Location</label> <select name="location" class="form-select"> <option value="all">All</option> <?php foreach ($locations as $loc): ?> <option value="<?= $loc['id'] ?>" <?= $location==$loc['id']?'selected':'' ?>> <?= htmlspecialchars($loc['location_name']) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-2"> <button class="btn btn-primary w-100">Load</button> </div> </form> </div> </div> <!-- ================= TABLE ================= --> <?php if ($rows): ?> <div class="card shadow-sm"> <div class="card-body"> <div class="table-responsive"> <table class="table table-bordered table-sm align-middle"> <thead class="table-light"> <tr> <?php foreach (array_keys($rows[0]) as $col): ?> <?php if ($col === 'location_id') continue; ?> <th><?= htmlspecialchars($col) ?></th> <?php endforeach; ?> <th>Action</th> </tr> </thead> <tbody> <?php foreach ($rows as $r): ?> <tr> <?php foreach ($r as $col=>$val): ?> <?php if ($col === 'location_id') continue; ?> <?php if ($col === 'location_name'): ?> <td><strong><?= htmlspecialchars($val ?? '-') ?></strong></td> <?php continue; endif; ?> <td><?= htmlspecialchars($val) ?></td> <?php endforeach; ?> <td> <form method="post" onsubmit="return confirm('Delete this record?')"> <input type="hidden" name="id" value="<?= $r['id'] ?>"> <input type="hidden" name="table" value="<?= $table ?>"> <button class="btn btn-danger btn-sm">Delete</button> </form> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <?php elseif ($table): ?> <div class="alert alert-info">No data found.</div> <?php endif; ?> </div> <?php require_once __DIR__ . '/../partials/footer.php'; ?>