diff --git a/assets/js/choices.js b/assets/js/choices.js
new file mode 100644
index 0000000..a60b61c
--- /dev/null
+++ b/assets/js/choices.js
@@ -0,0 +1,8 @@
+document.addEventListener('DOMContentLoaded', function() {
+ const assignedTo = document.getElementById('assigned_to');
+ if (assignedTo) {
+ const choices = new Choices(assignedTo, {
+ removeItemButton: true,
+ });
+ }
+});
\ No newline at end of file
diff --git a/edit-asset.php b/edit-asset.php
index dfddf3d..080cd48 100644
--- a/edit-asset.php
+++ b/edit-asset.php
@@ -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') {
+
@@ -147,16 +158,31 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
Cancel
-
+ p
@@ -164,6 +190,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+
+
diff --git a/index.php b/index.php
index e456312..60b823c 100644
--- a/index.php
+++ b/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 = '') {
@@ -47,24 +60,44 @@ function get_assets($fields, $search = '', $status = '', $limit = 10, $offset =
if (!in_array('id', $fields)) {
$fields[] = 'id';
}
-
- $select_fields = implode(', ', $fields);
- $sql = "SELECT $select_fields FROM assets";
+ $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";
+ }
+
$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) {
+
+
|