Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All asset report - custom columns #1012

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions report_all_assets_by_client_custom_columns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

require_once "inc_all_reports.php";

validateTechRole();

if (isset($_GET['year'])) {
$year = intval($_GET['year']);
} else {
$year = date('Y');
}


// Custom columns
if (isset($_POST['submit'])) {
$selected_columns = isset($_POST['columns']) ? $_POST['columns'] : [];
} else {
// Set default columns if no selection is made
$selected_columns = ['client_name', 'asset_name', 'asset_type', 'asset_status']; // Example default columns
}

$available_columns_sql = mysqli_query($mysqli, "SHOW COLUMNS FROM assets");
while ($row = mysqli_fetch_array($available_columns_sql)) {
$available_columns[] = $row['Field'];
}

if (!empty($selected_columns)) {
$selected_columns = array_intersect($selected_columns, $available_columns); // Filter acceptable columns
$selected_columns = ['client_name', ...$selected_columns];

$columns_to_display = implode(", ", $selected_columns);
$query = "SELECT $columns_to_display FROM assets
LEFT JOIN clients on asset_client_id = client_id
ORDER BY asset_client_id, asset_name";
} else {
$query = "SELECT client_name, asset_name, asset_type, asset_status FROM assets
LEFT JOIN clients on asset_client_id = client_id
ORDER BY asset_client_id, asset_name"; // Fallback to default columns
}

$assets_sql = mysqli_query($mysqli, $query);

?>

<div class="card card-dark">
<div class="card-header py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-life-ring mr-2"></i>All Assets by Client - with custom columns</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary d-print-none" onclick="window.print();"><i class="fas fa-fw fa-print mr-2"></i>Print</button>
</div>
</div>
<div class="card-body">
<div class="table-responsive-sm">
<table class="table table-striped">
<thead>
<tr>
<?php
foreach ($selected_columns as $col) {
echo "<th>" . htmlspecialchars($col) . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($assets_sql)) {
echo "<tr>";
foreach ($selected_columns as $col) {
echo "<td>" . nullable_htmlentities($row[$col]) . "</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
</div>
<form method="post" action="">
<label><input type="checkbox" name="columns[]" value="client_name" checked>Client Name</label>
<label><input type="checkbox" name="columns[]" value="asset_name" checked>Name</label>
<label><input type="checkbox" name="columns[]" value="asset_type" checked>Type</label>
<label><input type="checkbox" name="columns[]" value="asset_make">Make</label>
<label><input type="checkbox" name="columns[]" value="asset_model">Model</label>
<label><input type="checkbox" name="columns[]" value="asset_serial">Serial</label>
<label><input type="checkbox" name="columns[]" value="asset_os">OS</label>
<label><input type="checkbox" name="columns[]" value="asset_status" checked>Status</label>
<label><input type="checkbox" name="columns[]" value="asset_purchase_date">Purchase</label>
<label><input type="checkbox" name="columns[]" value="asset_install_date">Install</label>
<label><input type="checkbox" name="columns[]" value="asset_warranty_expire">Warranty</label>
<label><input type="checkbox" name="columns[]" value="asset_archived_at">Archived</label>

<input type="submit" name="submit" value="Update Columns">
</form>
</div>
</div>

<?php
require_once "footer.php";

Loading