new func for assign to
This commit is contained in:
parent
17da95852f
commit
baf8947a57
8
assets/js/choices.js
Normal file
8
assets/js/choices.js
Normal file
@ -0,0 +1,8 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const assignedTo = document.getElementById('assigned_to');
|
||||
if (assignedTo) {
|
||||
const choices = new Choices(assignedTo, {
|
||||
removeItemButton: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -10,11 +10,12 @@ if (!can($_SESSION['user_role'], 'asset', 'update')) {
|
||||
}
|
||||
|
||||
$allowed_fields_str = can($_SESSION['user_role'], 'asset', 'update');
|
||||
$allowed_fields = ($allowed_fields_str === '*') ? ['name', 'asset_tag', 'status', 'location', 'manufacturer', 'model', 'purchase_date'] : explode(',', $allowed_fields_str);
|
||||
$allowed_fields = ($allowed_fields_str === '*') ? ['name', 'asset_tag', 'status', 'location', 'manufacturer', 'model', 'purchase_date', 'assigned_to'] : explode(',', $allowed_fields_str);
|
||||
|
||||
$success_message = '';
|
||||
$error_message = '';
|
||||
$asset = null;
|
||||
$users = [];
|
||||
|
||||
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
||||
header("Location: index.php");
|
||||
@ -33,6 +34,11 @@ try {
|
||||
header("Location: index.php?error=not_found");
|
||||
exit;
|
||||
}
|
||||
|
||||
// Fetch users for dropdown
|
||||
$stmt = $pdo->query("SELECT id, name FROM users ORDER BY name");
|
||||
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$error_message = 'Database error: ' . $e->getMessage();
|
||||
}
|
||||
@ -43,7 +49,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
foreach ($allowed_fields as $field) {
|
||||
if (isset($_POST[$field])) {
|
||||
$data[] = $_POST[$field];
|
||||
$value = $_POST[$field];
|
||||
if ($field === 'assigned_to' && $value === '') {
|
||||
$value = null;
|
||||
}
|
||||
$data[] = $value;
|
||||
$set_parts[] = "$field = ?";
|
||||
}
|
||||
}
|
||||
@ -76,6 +86,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
<meta name="description" content="Edit an existing asset in the inventory.">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css"/>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
@ -147,16 +158,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php if (in_array('purchase_date', $allowed_fields)): ?>
|
||||
<div class="mb-3">
|
||||
<label for="purchase_date" class="form-label">Purchase Date*</label>
|
||||
<input type="date" class="form-control" id="purchase_date" name="purchase_date" value="<?php echo htmlspecialchars($asset['purchase_date']); ?>" required>
|
||||
<div class="row">
|
||||
<?php if (in_array('purchase_date', $allowed_fields)): ?>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label for="purchase_date" class="form-label">Purchase Date*</label>
|
||||
<input type="date" class="form-control" id="purchase_date" name="purchase_date" value="<?php echo htmlspecialchars($asset['purchase_date']); ?>" required>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if (in_array('assigned_to', $allowed_fields)): ?>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label for="assigned_to" class="form-label">Assigned To</label>
|
||||
<select class="form-select" id="assigned_to" name="assigned_to">
|
||||
<option value="">Unassigned</option>
|
||||
<?php foreach ($users as $user): ?>
|
||||
<option value="<?php echo $user['id']; ?>" <?php if ($asset['assigned_to'] == $user['id']) echo 'selected'; ?>>
|
||||
<?php echo htmlspecialchars($user['name']); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Update Asset</button>
|
||||
<a href="index.php" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
</form>p
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</main>
|
||||
@ -164,6 +190,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
|
||||
<script src="assets/js/choices.js?v=<?php echo time(); ?>"></script>
|
||||
<script>
|
||||
feather.replace();
|
||||
</script>
|
||||
|
||||
54
index.php
54
index.php
@ -6,7 +6,20 @@ require_once 'auth-helpers.php';
|
||||
|
||||
// Get allowed fields for the current user
|
||||
$allowed_fields_str = can($_SESSION['user_role'], 'asset', 'read');
|
||||
$allowed_fields = $allowed_fields_str ? explode(',', $allowed_fields_str) : [];
|
||||
$allowed_fields = [];
|
||||
if ($allowed_fields_str === '*') {
|
||||
// Wildcard means all fields
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->query("SHOW COLUMNS FROM assets");
|
||||
$allowed_fields = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
||||
} catch (PDOException $e) {
|
||||
// Handle error, maybe log it
|
||||
$allowed_fields = [];
|
||||
}
|
||||
} elseif ($allowed_fields_str) {
|
||||
$allowed_fields = explode(',', $allowed_fields_str);
|
||||
}
|
||||
|
||||
// Function to count total assets
|
||||
function count_assets($search = '', $status = '') {
|
||||
@ -48,23 +61,43 @@ function get_assets($fields, $search = '', $status = '', $limit = 10, $offset =
|
||||
$fields[] = 'id';
|
||||
}
|
||||
|
||||
$select_fields = implode(', ', $fields);
|
||||
$select_fields = [];
|
||||
$join_users = in_array('assigned_to', $fields);
|
||||
|
||||
foreach ($fields as $field) {
|
||||
if ($field === 'assigned_to') {
|
||||
// Use a different alias for the user name to avoid conflict with the original column name
|
||||
$select_fields[] = 'users.name AS assigned_to_name';
|
||||
}
|
||||
// Always select the original assigned_to field for reference if needed
|
||||
$select_fields[] = 'assets.' . $field;
|
||||
}
|
||||
|
||||
// Remove duplicates that might be caused by adding assets.id and assets.assigned_to
|
||||
$select_fields = array_unique($select_fields);
|
||||
|
||||
$select_fields_sql = implode(', ', $select_fields);
|
||||
|
||||
$sql = "SELECT $select_fields_sql FROM assets";
|
||||
|
||||
if ($join_users) {
|
||||
$sql .= " LEFT JOIN users ON assets.assigned_to = users.id";
|
||||
}
|
||||
|
||||
$sql = "SELECT $select_fields FROM assets";
|
||||
$where = [];
|
||||
$params = [];
|
||||
|
||||
if (!empty($search)) {
|
||||
// Assuming 'name' is a field that can be searched.
|
||||
if (in_array('name', $fields)) {
|
||||
$where[] = "name LIKE :search";
|
||||
$where[] = "assets.name LIKE :search";
|
||||
$params[':search'] = "%$search%";
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($status)) {
|
||||
if (in_array('status', $fields)) {
|
||||
$where[] = "status = :status";
|
||||
$where[] = "assets.status = :status";
|
||||
$params[':status'] = $status;
|
||||
}
|
||||
}
|
||||
@ -75,9 +108,14 @@ function get_assets($fields, $search = '', $status = '', $limit = 10, $offset =
|
||||
|
||||
// Whitelist sortable columns
|
||||
$sortable_columns = array_merge($fields, ['created_at']);
|
||||
if (!in_array($sort_by, $sortable_columns)) {
|
||||
$sort_by = 'created_at';
|
||||
if ($sort_by === 'assigned_to') {
|
||||
$sort_by = 'assigned_to_name'; // Sort by the alias
|
||||
} elseif (in_array($sort_by, $fields)) {
|
||||
$sort_by = 'assets.' . $sort_by;
|
||||
} elseif (!in_array($sort_by, $sortable_columns)) {
|
||||
$sort_by = 'assets.created_at';
|
||||
}
|
||||
|
||||
$sort_order = strtoupper($sort_order) === 'ASC' ? 'ASC' : 'DESC';
|
||||
|
||||
$sql .= " ORDER BY $sort_by $sort_order LIMIT :limit OFFSET :offset";
|
||||
@ -228,6 +266,8 @@ function getStatusClass($status) {
|
||||
<td>
|
||||
<?php if ($field === 'status'): ?>
|
||||
<span class="status <?php echo getStatusClass($asset[$field]); ?>"><?php echo htmlspecialchars($asset[$field]); ?></span>
|
||||
<?php elseif ($field === 'assigned_to'): ?>
|
||||
<?php echo htmlspecialchars($asset['assigned_to_name'] ?? ($asset['assigned_to'] ?? '')); ?>
|
||||
<?php else: ?>
|
||||
<?php echo htmlspecialchars($asset[$field] ?? ''); ?>
|
||||
<?php endif; ?>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user