« Back to History
machine_add.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ============================================================================ File: /erp/machine_add.php Purpose: Bulk-create machines for the current company (company-scoped). - Uses modules/auth/page_acl.php bootstrap (page_require_access) - Uses $ctx['pdo'] if present, falls back to core/db.php only if needed - CSRF protection - Transactioned bulk insert with prepared statements - No inline CSS (use main.css) Author: assistant (adjust fields as needed) ============================================================================ */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('machine_ad'); // adjust permission key if needed $user = $ctx['user'] ?? null; $company_id = (int) ($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; // fallback if $ctx['pdo'] missing (project rule allows fallback) if (!$pdo) { require_once __DIR__ . '/core/db.php'; // core/db.php should require config.php internally // try to get $pdo from global if core/db.php exposes it if (isset($GLOBALS['pdo'])) $pdo = $GLOBALS['pdo']; } if (!$pdo) { die("Database connection not available."); } // helper escape if not present if (!function_exists('h')) { function h($s) { return htmlspecialchars($s ?? '', ENT_QUOTES, 'UTF-8'); } } // ensure session started (page_acl usually starts session, but safe-guard) if (session_status() === PHP_SESSION_NONE) { session_start(); } // CSRF token if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(16)); } $errors = []; $success_msg = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // validate CSRF $token = $_POST['csrf_token'] ?? ''; if (!hash_equals($_SESSION['csrf_token'], (string)$token)) { $errors[] = "Invalid CSRF token."; } // collect inputs $machine_type = trim($_POST['machine_type'] ?? ''); $group_slug = trim($_POST['group_slug'] ?? ''); $prefix = trim($_POST['prefix'] ?? ''); $start_no = (int) ($_POST['start_no'] ?? 1); $count = (int) ($_POST['count'] ?? 0); $pad_width = (int) ($_POST['pad_width'] ?? 3); $status = trim($_POST['status'] ?? 'Active'); $is_active = isset($_POST['is_active']) ? 1 : 0; // basic validation if ($count <= 0) $errors[] = "Count must be greater than zero."; if ($start_no < 0) $errors[] = "Start number must be 0 or greater."; if ($pad_width < 0) $pad_width = 0; if ($company_id <= 0) $errors[] = "Invalid company context."; if (empty($machine_type)) $errors[] = "Machine type is required."; if (empty($errors)) { try { $now = (new DateTime('now'))->format('Y-m-d H:i:s'); // columns included here must exist in your `machines` table. // If you have extra required columns, add them to INSERT and execute array. $sql = "INSERT INTO machines (code, name, machine_type, group_slug, company_id, is_active, status, created_by, updated_by, created_at, updated_at) VALUES (:code, :name, :machine_type, :group_slug, :company_id, :is_active, :status, :created_by, :updated_by, :created_at, :updated_at)"; $stmt = $pdo->prepare($sql); $pdo->beginTransaction(); $inserted = 0; $by = (int)($user['id'] ?? 0); for ($i = 0; $i < $count; $i++) { $num = $start_no + $i; $num_str = $pad_width > 0 ? str_pad((string)$num, $pad_width, '0', STR_PAD_LEFT) : (string)$num; // build code and name (adjust format as you prefer) $code = ($prefix !== '' ? $prefix . ' ' : '') . $num_str; $name = $machine_type . ' ' . $num_str; $stmt->execute([ ':code' => $code, ':name' => $name, ':machine_type' => $machine_type, ':group_slug' => $group_slug !== '' ? $group_slug : null, ':company_id' => $company_id, ':is_active' => $is_active, ':status' => $status, ':created_by' => $by, ':updated_by' => $by, ':created_at' => $now, ':updated_at' => $now, ]); $inserted++; } $pdo->commit(); $success_msg = "Success: {$inserted} machines created for company_id={$company_id}."; // regenerate csrf to prevent double submit $_SESSION['csrf_token'] = bin2hex(random_bytes(16)); } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $errors[] = "Database error: " . $e->getMessage(); } } } // ---------- Render Page ---------- // NOTE: Correct require paths to partials (no duplicate /erp/) require_once __DIR__ . '/partials/header.php'; ?> <div class="container card"> <div class="card-body"> <h2 class="card-title">Bulk Machine Add</h2> <?php if (!empty($errors)): ?> <div class="notice notice-danger"> <ul> <?php foreach ($errors as $err): ?> <li><?php echo h($err); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <?php if ($success_msg): ?> <div class="notice notice-success"> <?php echo h($success_msg); ?> </div> <?php endif; ?> <form method="post" action="" class="form-grid" autocomplete="off"> <input type="hidden" name="csrf_token" value="<?php echo h($_SESSION['csrf_token']); ?>"> <div class="form-row"> <label>Machine Type</label> <select name="machine_type" required> <option value="">-- select --</option> <option value="Loom">Loom Machine</option> <option value="Zari">Zari Machine</option> <option value="TFO">TFO Machine</option> <option value="Palti">Palti Machine</option> <option value="Lace">Lace Machine</option> <option value="Other">Other</option> </select> </div> <div class="form-row"> <label>Group Slug (optional)</label> <input type="text" name="group_slug" placeholder="e.g., loom" /> </div> <div class="form-row"> <label>Code Prefix (optional)</label> <input type="text" name="prefix" placeholder="e.g., loom" /> </div> <div class="form-row"> <label>Start Number</label> <input type="number" name="start_no" value="1" min="0" /> </div> <div class="form-row"> <label>Count (how many machines to create)</label> <input type="number" name="count" value="10" min="1" required /> </div> <div class="form-row"> <label>Pad Width (digits, e.g., 3 → 001)</label> <input type="number" name="pad_width" value="3" min="0" /> </div> <div class="form-row"> <label>Status</label> <select name="status"> <option value="Active">Active</option> <option value="Inactive">Inactive</option> </select> </div> <div class="form-row"> <label>Is Active?</label> <input type="checkbox" name="is_active" checked /> </div> <div class="form-row"> <button type="submit" class="btn">Create Machines</button> <a href="/erp/machines_manage.php" class="btn btn-secondary">Back to Machines</a> </div> </form> <div class="mt-2 small text-muted"> Note: Page is company-scoped and will create machines using current company context (company_id = <?php echo h($company_id); ?>). If your `machines` table requires more non-null columns, add them into the INSERT statement above. </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>