« Back to History
company_roles.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_role = strtolower($u['role'] ?? ''); if (!in_array($user_role, ['owner','admin'])) { http_response_code(403); exit('Only Owner/Admin allowed'); } if (!isset($pdo) || !($pdo instanceof PDO)) { require __DIR__ . '/core/db.php'; } require_once __DIR__ . '/helpers/activity_helper.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } $msg = ''; $err = ''; /* ================= ADD ================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['act'] ?? '') === 'add') { $name = trim($_POST['name'] ?? ''); $code = strtoupper(trim($_POST['code'] ?? '')); $code = preg_replace('/[^A-Z0-9_]/', '', $code); $is_active = isset($_POST['is_active']) ? 1 : 0; if ($name === '' || $code === '') { $err = 'Name और Code दोनों जरूरी हैं.'; } else { $dup = $pdo->prepare("SELECT id FROM company_roles WHERE company_id=? AND (name=? OR code=?) LIMIT 1"); $dup->execute([$company_id, $name, $code]); if ($dup->fetch()) { $err = 'Same Name या Code पहले से मौजूद है.'; } else { $pdo->prepare(" INSERT INTO company_roles (company_id, name, code, is_active, created_at) VALUES (?, ?, ?, ?, NOW()) ")->execute([$company_id, $name, $code, $is_active]); activity_create('company', 'company_role_create', (int)$pdo->lastInsertId(), 'Created company role'); header("Location: ".$_SERVER['PHP_SELF']."?ok=1"); exit; } } } /* ================= FETCH ================= */ $list = $pdo->prepare(" SELECT id, name, code, is_active, created_at FROM company_roles WHERE company_id=? ORDER BY id DESC "); $list->execute([$company_id]); $rows = $list->fetchAll(PDO::FETCH_ASSOC); require __DIR__ . '/partials/header.php'; ?> <div class="container py-4"> <div class="d-flex justify-content-between align-items-center mb-4"> <h4 class="fw-bold">Company Roles</h4> <span class="badge bg-secondary">Company ID: <?= $company_id ?></span> </div> <?php if(isset($_GET['ok'])): ?> <div class="alert alert-success">Role added successfully.</div> <?php endif; ?> <?php if($err): ?> <div class="alert alert-danger"><?= h($err) ?></div> <?php endif; ?> <!-- Add Role --> <div class="card shadow-sm mb-4"> <div class="card-body"> <form method="post" class="row g-3"> <input type="hidden" name="act" value="add"> <div class="col-md-5"> <label class="form-label">Role Name</label> <input type="text" name="name" class="form-control" required> </div> <div class="col-md-4"> <label class="form-label">Role Code</label> <input type="text" name="code" class="form-control text-uppercase" required> </div> <div class="col-md-2 d-flex align-items-end"> <div class="form-check"> <input type="checkbox" name="is_active" class="form-check-input" checked> <label class="form-check-label">Active</label> </div> </div> <div class="col-md-1 d-flex align-items-end"> <button class="btn btn-success w-100">Save</button> </div> </form> </div> </div> <!-- Role List --> <div class="card shadow-sm"> <div class="card-body table-responsive"> <table class="table table-bordered table-hover align-middle"> <thead class="table-light"> <tr> <th>ID</th> <th>Name</th> <th>Code</th> <th>Status</th> <th>Created</th> <th style="width:120px;">Action</th> </tr> </thead> <tbody> <?php if(!$rows): ?> <tr> <td colspan="6" class="text-center text-muted">No roles found.</td> </tr> <?php else: ?> <?php foreach($rows as $r): ?> <tr> <td><?= $r['id'] ?></td> <td><?= h($r['name']) ?></td> <td><code><?= h($r['code']) ?></code></td> <td> <?php if($r['is_active']): ?> <span class="badge bg-success">Active</span> <?php else: ?> <span class="badge bg-danger">Inactive</span> <?php endif; ?> </td> <td><?= h($r['created_at']) ?></td> <td> <a href="company_roles_edit.php?id=<?= $r['id'] ?>" class="btn btn-sm btn-primary"> Edit </a> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> </div> </div> <?php require __DIR__ . '/partials/footer.php'; ?>