mirror of
https://github.com/itflow-org/itflow
synced 2026-08-05 07:07:14 +00:00
Feature: On all export modals added Filter Tab and Selectable Columns tab with default selected, also you can now export to a PDF
This commit is contained in:
@@ -1,25 +1,70 @@
|
||||
<?php
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
require_once '../../../includes/modal_header.php';
|
||||
|
||||
enforceAdminPermission();
|
||||
|
||||
// Pre-filled from the users page's current filters. Everything here is editable -
|
||||
// the export runs whatever this form posts, not whatever the page happened to show.
|
||||
$client_id = intval($_GET['client_id'] ?? 0);
|
||||
if ($client_id) {
|
||||
enforceClientAccess();
|
||||
}
|
||||
|
||||
// Search Filter
|
||||
$q_filter = $_GET['q'] ?? '';
|
||||
|
||||
// Archived Filter
|
||||
$archived_filter = (isset($_GET['archived']) && $_GET['archived'] == 1) ? 1 : 0;
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-download mr-2"></i>Export Users to CSV</h5>
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-download mr-2"></i>Export Users</h5>
|
||||
<button type="button" class="close text-white" data-dismiss="modal">
|
||||
<span>×</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<?php exportTabsNav(); ?>
|
||||
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<?php exportTabsFiltersOpen(); ?>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Search</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-search"></i></span>
|
||||
</div>
|
||||
<input type="text" class="form-control" name="q" value="<?= stripslashes(escapeHtml($q_filter)) ?>" placeholder="Name or email">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Archived</label>
|
||||
<div class="input-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-archive"></i></span>
|
||||
</div>
|
||||
<select class="form-control select2" name="archived">
|
||||
<option <?php if (!$archived_filter) { echo "selected"; } ?> value="0">Active only</option>
|
||||
<option <?php if ($archived_filter) { echo "selected"; } ?> value="1">Archived only</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php exportTabsColumns('users'); ?>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="export_users_csv" class="btn btn-primary text-bold"><i class="fas fa-fw fa-download mr-2"></i>Download CSV</button>
|
||||
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fas fa-times mr-2"></i>Cancel</button>
|
||||
<?php renderExportButtons('export_users'); ?>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
|
||||
@@ -301,57 +301,68 @@ if (isset($_POST['restore_user'])) {
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['export_users_csv'])) {
|
||||
if (isset($_POST['export_users'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
//get records from database
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM users LEFT JOIN user_roles ON user_role_id = role_id ORDER BY user_name ASC");
|
||||
enforceAdminPermission();
|
||||
|
||||
$count = mysqli_num_rows($sql);
|
||||
$format = resolveExportFormat($_POST['export_users']);
|
||||
|
||||
if ($count > 0) {
|
||||
$delimiter = ",";
|
||||
$enclosure = '"';
|
||||
$escape = '\\'; // backslash
|
||||
$filename = "Users-" . date('Y-m-d') . ".csv";
|
||||
// Filters inherited from the users page - mirrors admin/users.php
|
||||
$filter_summary = [];
|
||||
|
||||
//create a file pointer
|
||||
$f = fopen('php://memory', 'w');
|
||||
// Archived Filter
|
||||
if (isset($_POST['archived']) && $_POST['archived'] == 1) {
|
||||
$archive_query = "user_archived_at IS NOT NULL";
|
||||
$filter_summary['Archived'] = 'Archived only';
|
||||
} else {
|
||||
$archive_query = "user_archived_at IS NULL";
|
||||
}
|
||||
|
||||
//set column headers
|
||||
$fields = array('Name', 'Email', 'Role', 'Status', 'Creation Date');
|
||||
fputcsv($f, $fields, $delimiter, $enclosure, $escape);
|
||||
// Search Filter
|
||||
$q = escapeSql($_POST['q'] ?? '');
|
||||
if (!empty($q)) {
|
||||
$filter_summary['Search'] = $_POST['q'];
|
||||
}
|
||||
|
||||
//output each row of the data, format line as csv and write to file pointer
|
||||
while($row = $sql->fetch_assoc()) {
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT * FROM users
|
||||
LEFT JOIN user_roles ON user_role_id = role_id
|
||||
WHERE (user_name LIKE '%$q%' OR user_email LIKE '%$q%')
|
||||
AND user_type = 1
|
||||
AND $archive_query
|
||||
ORDER BY user_name ASC"
|
||||
);
|
||||
|
||||
$num_rows = mysqli_num_rows($sql);
|
||||
|
||||
if ($num_rows > 0) {
|
||||
|
||||
guardExportPdfRowCount($format, $num_rows);
|
||||
|
||||
$export = beginExport('users', $format, "$session_company_name-Users", 'Users', summarizeExportFilters($filter_summary));
|
||||
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
|
||||
$user_status = intval($row['user_status']);
|
||||
if ($user_status == 2) {
|
||||
$user_status_display = "Invited";
|
||||
$row['user_status_display'] = "Invited";
|
||||
} elseif ($user_status == 1) {
|
||||
$user_status_display = "Active";
|
||||
} else{
|
||||
$user_status_display = "Disabled";
|
||||
$row['user_status_display'] = "Active";
|
||||
} else {
|
||||
$row['user_status_display'] = "Disabled";
|
||||
}
|
||||
|
||||
$lineData = array($row['user_name'], $row['user_email'], $row['role_name'], $user_status_display, $row['user_created_at']);
|
||||
fputcsv($f, array_map('escapeCsvFormula', $lineData), $delimiter, $enclosure, $escape);
|
||||
addExportRow($export, $row);
|
||||
}
|
||||
|
||||
//move back to beginning of file
|
||||
fseek($f, 0);
|
||||
|
||||
//set headers to download file rather than displayed
|
||||
header('Content-Type: text/csv');
|
||||
header('Content-Disposition: attachment; filename="' . $filename . '";');
|
||||
|
||||
//output all remaining data on a file pointer
|
||||
fpassthru($f);
|
||||
|
||||
// Logging
|
||||
logAudit("User", "Export", "$session_name exported $count user(s) to a CSV file");
|
||||
finishExport($export);
|
||||
}
|
||||
|
||||
logAudit("User", "Export", "$session_name exported $num_rows user(s) to a " . strtoupper($format) . " file");
|
||||
|
||||
exit;
|
||||
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
<!--<a class="dropdown-item text-dark ajax-modal" href="#" data-modal-url="modals/user/user_invite.php"><i class="fas fa-paper-plane mr-2"></i>Invite User</a>-->
|
||||
<?php if ($num_rows[0] > 1) { ?>
|
||||
<a class="dropdown-item text-dark ajax-modal" href="#"
|
||||
data-modal-url="modals/user/user_export.php">
|
||||
data-modal-url="<?= buildExportModalUrl('modals/user/user_export.php', ['archived', 'q']) ?>">
|
||||
<i class="fa fa-fw fa-download mr-2"></i>Export
|
||||
</a>
|
||||
<div class="dropdown-divider"></div>
|
||||
|
||||
Reference in New Issue
Block a user