mirror of
https://github.com/itflow-org/itflow
synced 2026-11-17 01:47:25 +00:00
Categories stage 1: Admin > Categories rebuilt with the ten kinds in a side nav with counts and what each is for, each category shown as it looks on records (icon or dot in its color, asset statuses as their badge) with its description and what uses it linked to the filtered lists, search, sort by name, most used or order, Current and Archived tabs, Edit, Merge into, Archive, Restore and Delete only while unused, bulk merge, archive, restore and delete, drag or Move up and Move down ordering for the ordered kinds, names in use that are not categories listed with Add, one shared category window with live preview, color swatches or none and searchable icons or none, names unique per kind and held to their column length, renaming a kind kept by name carries the new name onto its records, merge moves every reference including budget months folded per month, payment provider fees and the mileage setting, all without touching record updated_at, database 3.1.5 normalizing category colors to #rrggbb and icons to fa-name, seed data in hex, new functions/categories.php, agent + window handler routed through the same validation, note-type icons fixed in pickers, Settings > Defaults payment methods read from Payment Methods, changelog updated
This commit is contained in:
@@ -1,225 +1,319 @@
|
||||
<?php
|
||||
|
||||
// Default Column Sortby Filter
|
||||
$sort = "category_name";
|
||||
$order = "ASC";
|
||||
/*
|
||||
* Admin > Categories - the categories of each kind: expense, income,
|
||||
* referral and ticket, and the interface, asset status, software, rack and
|
||||
* note types.
|
||||
*
|
||||
* Each shows as it looks on records, with its description and what uses it
|
||||
* (opening the list of them, where there is one). A category can be
|
||||
* changed, merged into another of its kind, archived - it stays on its
|
||||
* records but isn't offered for new ones - or, while nothing uses it,
|
||||
* deleted. The kinds pickers list in an order set here are dragged into it.
|
||||
* The work is in functions/categories.php; the handlers are
|
||||
* admin/post/category.php.
|
||||
*/
|
||||
|
||||
require_once "includes/inc_all_admin.php";
|
||||
|
||||
$kinds = categoryKinds();
|
||||
$kind = isset($kinds[$_GET['category'] ?? '']) ? (string) $_GET['category'] : 'Expense';
|
||||
$config = categoryKind($kind);
|
||||
$show_archived = isset($_GET['archived']);
|
||||
$search = trim((string) ($_GET['q'] ?? ''));
|
||||
|
||||
if (isset($_GET['category'])) {
|
||||
$category = escapeSql($_GET['category']);
|
||||
} else {
|
||||
$category = "Expense";
|
||||
// How the list is sorted - the order pickers use first, for the kinds that keep one
|
||||
$sorts = ($config['ordered'] ? ['order' => ['Order', 'fa-sort-numeric-down', 'The order they are offered in']] : [])
|
||||
+ ['name' => ['Name', 'fa-sort-alpha-down', 'A to Z'], 'use' => ['Most used', 'fa-sort-amount-down', 'The ones on the most records first']];
|
||||
$sort_by = isset($sorts[$_GET['sort'] ?? '']) ? (string) $_GET['sort'] : array_key_first($sorts);
|
||||
$default_sort = array_key_first($sorts);
|
||||
|
||||
$usage = categoryUsage($kind);
|
||||
$empty_use = ['all' => 0, 'archived' => 0, 'uses' => []];
|
||||
$categories = [];
|
||||
foreach (categoryRows($kind) as $category_id => $row) {
|
||||
if ((bool) $row['category_archived_at'] !== $show_archived) {
|
||||
continue;
|
||||
}
|
||||
if ($search !== '' && mb_stripos($row['category_name'], $search) === false && mb_stripos((string) $row['category_description'], $search) === false) {
|
||||
continue;
|
||||
}
|
||||
$row['used'] = $usage[$category_id] ?? $empty_use;
|
||||
$categories[$category_id] = $row;
|
||||
}
|
||||
if ($sort_by === 'name') {
|
||||
uasort($categories, function ($a, $b) {
|
||||
return strnatcasecmp($a['category_name'], $b['category_name']);
|
||||
});
|
||||
} elseif ($sort_by === 'use') {
|
||||
uasort($categories, function ($a, $b) {
|
||||
return $b['used']['all'] <=> $a['used']['all'] ?: strnatcasecmp($a['category_name'], $b['category_name']);
|
||||
});
|
||||
}
|
||||
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT SQL_CALC_FOUND_ROWS category_color, category_description, category_id, category_name FROM categories
|
||||
WHERE category_name LIKE '%$q%'
|
||||
AND category_type = '$category'
|
||||
AND category_$archive_query
|
||||
ORDER BY $sort $order LIMIT $record_from, $record_to"
|
||||
);
|
||||
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
// Names records have that aren't categories - a kind held by name, in the plain current view
|
||||
$loose = !$show_archived && $search === '' ? categoryLooseNames($kind) : [];
|
||||
|
||||
// Category types shown in the left nav
|
||||
$category_types = [
|
||||
'Expense' => ['label' => 'Expense', 'icon' => 'fa-shopping-cart'],
|
||||
'Income' => ['label' => 'Income', 'icon' => 'fa-hand-holding-usd'],
|
||||
'Referral' => ['label' => 'Referral', 'icon' => 'fa-share-alt'],
|
||||
'Ticket' => ['label' => 'Ticket', 'icon' => 'fa-life-ring'],
|
||||
'network_interface' => ['label' => 'Network Interface', 'icon' => 'fa-ethernet'],
|
||||
'asset_status' => ['label' => 'Asset Status', 'icon' => 'fa-heartbeat'],
|
||||
'software_type' => ['label' => 'Software Type', 'icon' => 'fa-cube'],
|
||||
'rack_type' => ['label' => 'Rack Type', 'icon' => 'fa-server'],
|
||||
'contact_note_type' => ['label' => 'Contact Note Type', 'icon' => 'fa-address-book'],
|
||||
'asset_note_type' => ['label' => 'Asset Note Type', 'icon' => 'fa-desktop'],
|
||||
];
|
||||
// Dragged into order - in the order view, with the whole current list showing
|
||||
$can_reorder = $config['ordered'] && $sort_by === 'order' && !$show_archived && $search === '' && count($categories) > 1;
|
||||
|
||||
// Label for the selected type, falling back for anything not in the map
|
||||
$category_label = $category_types[$category]['label'] ?? ucwords(str_replace('_', ' ', $category));
|
||||
|
||||
// Row count per type for the nav badges, respecting the archived view
|
||||
$category_type_counts = [];
|
||||
$sql_category_type_counts = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT category_type, COUNT(category_id) AS category_type_count FROM categories
|
||||
WHERE category_$archive_query
|
||||
GROUP BY category_type"
|
||||
);
|
||||
while ($row = mysqli_fetch_assoc($sql_category_type_counts)) {
|
||||
$category_type_counts[$row['category_type']] = intval($row['category_type_count']);
|
||||
// Current categories of each kind for the nav, and this kind's archived ones for the tab
|
||||
$kind_counts = [];
|
||||
$archived_count = 0;
|
||||
$sql = mysqli_query($mysqli, "SELECT category_type, SUM(category_archived_at IS NULL), SUM(category_archived_at IS NOT NULL) FROM categories GROUP BY category_type");
|
||||
while ($row = mysqli_fetch_row($sql)) {
|
||||
$kind_counts[$row[0]] = intval($row[1]);
|
||||
if ($row[0] === $kind) {
|
||||
$archived_count = intval($row[2]);
|
||||
}
|
||||
}
|
||||
|
||||
// Archived nav item toggles the view while holding the selected type/search
|
||||
$archive_toggle_query = $_GET;
|
||||
unset($archive_toggle_query['page']);
|
||||
$archive_toggle_query['category'] = $category;
|
||||
if ($archived) {
|
||||
unset($archive_toggle_query['archived']);
|
||||
} else {
|
||||
$archive_toggle_query['archived'] = 1;
|
||||
}
|
||||
$archive_toggle_url = '?' . http_build_query($archive_toggle_query);
|
||||
// This page with some of its query changed - null drops a key
|
||||
$page_url = function (array $change = []) use ($kind, $show_archived, $sort_by, $default_sort, $search) {
|
||||
$query = array_merge(['category' => $kind, 'q' => $search !== '' ? $search : null, 'sort' => $sort_by !== $default_sort ? $sort_by : null, 'archived' => $show_archived ? 1 : null], $change);
|
||||
return 'categories.php?' . http_build_query(array_filter($query, function ($v) {
|
||||
return $v !== null;
|
||||
}));
|
||||
};
|
||||
|
||||
$new_label = 'New ' . ucwords($config['one']);
|
||||
$add_url = 'modals/category/category_add.php?category=' . urlencode($kind);
|
||||
$csrf = $_SESSION['csrf_token'];
|
||||
|
||||
?>
|
||||
|
||||
<div class="card card-dark">
|
||||
<div class="card-header py-2">
|
||||
<h3 class="card-title mt-2"><i class="fa fa-fw fa-list-ul me-2"></i>
|
||||
<?= escapeHtml($category_label) ?> Categories
|
||||
</h3>
|
||||
<?php
|
||||
if (!isset($_GET['archived'])) {
|
||||
?>
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-list-ul me-2"></i>Categories</h3>
|
||||
<div class="card-tools">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="modals/category/category_add.php?category=<?= escapeHtml($category) ?>"><i
|
||||
class="fas fa-plus me-2"></i>New <?= escapeHtml($category_label) ?> Category</button>
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="<?= escapeHtml($add_url) ?>"><i class="fas fa-plus me-2"></i><?= escapeHtml($new_label) ?></button>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
|
||||
<!-- Category types -->
|
||||
<div class="col-md-3 border-end mb-3">
|
||||
<ul class="nav nav-pills flex-column bg-light">
|
||||
<?php foreach ($category_types as $category_type => $category_type_details) { ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link<?php if ($category == $category_type) {
|
||||
echo ' active';
|
||||
} ?>" href="?category=<?= urlencode($category_type) ?>">
|
||||
<i class="fa fa-fw <?= $category_type_details['icon'] ?> me-2"></i><?= escapeHtml($category_type_details['label']) ?>
|
||||
<span class="badge rounded-pill bg-dark float-end mt-1"><?= $category_type_counts[$category_type] ?? 0 ?></span>
|
||||
</a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="row g-0">
|
||||
<!-- Kinds -->
|
||||
<div class="col-md-4 col-lg-3 border-end">
|
||||
<nav class="nav nav-pills flex-nowrap flex-md-column overflow-auto p-2 gap-1 itflow-category-kinds" aria-label="Kinds of category">
|
||||
<?php foreach ($kinds as $kind_key => $kind_config) { ?>
|
||||
<a class="nav-link d-flex align-items-center gap-2 text-nowrap<?= $kind_key === $kind ? ' active' : '' ?>" href="categories.php?category=<?= urlencode($kind_key) ?>"<?= $kind_key === $kind ? ' aria-current="page"' : '' ?>>
|
||||
<i class="fas fa-fw <?= $kind_config['icon'] ?>"></i><span class="text-truncate"><?= escapeHtml($kind_config['label']) ?></span>
|
||||
<span class="badge rounded-pill ms-auto <?= $kind_key === $kind ? 'text-bg-light' : 'text-bg-secondary' ?>"><?= $kind_counts[$kind_key] ?? 0 ?></span>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
<!-- Categories -->
|
||||
<div class="col-md-9">
|
||||
<form autocomplete="off">
|
||||
<input type="hidden" name="category" value="<?= escapeHtml($category) ?>">
|
||||
<?php if ($archived) { ?>
|
||||
<input type="hidden" name="archived" value="1">
|
||||
<?php } ?>
|
||||
<div class="row">
|
||||
<div class="col-sm-6 mb-2">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q"
|
||||
value="<?php if (isset($q)) {
|
||||
echo stripslashes(escapeHtml($q));
|
||||
} ?>"
|
||||
placeholder="Search <?= escapeHtml($category_label) ?> Categories ">
|
||||
<button class="btn btn-primary" title="Search"><i class="fa fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6 mb-2">
|
||||
<a href="<?= $archive_toggle_url ?>"
|
||||
class="btn float-end <?php if ($archived) {
|
||||
echo 'btn-primary';
|
||||
} else {
|
||||
echo 'btn-default';
|
||||
} ?>"><i
|
||||
class="fas fa-fw fa-archive me-2"></i>Archived</a>
|
||||
<!-- This kind's categories -->
|
||||
<div class="col-md-8 col-lg-9">
|
||||
<div class="p-2 border-bottom">
|
||||
<p class="small text-secondary mb-2"><?= escapeHtml($config['about']) ?></p>
|
||||
<form autocomplete="off" class="d-flex flex-wrap align-items-center gap-2">
|
||||
<input type="hidden" name="category" value="<?= escapeHtml($kind) ?>">
|
||||
<?php if ($show_archived) { ?><input type="hidden" name="archived" value="1"><?php } ?>
|
||||
<?php if ($sort_by !== $default_sort) { ?><input type="hidden" name="sort" value="<?= escapeHtml($sort_by) ?>"><?php } ?>
|
||||
<div class="input-group input-group-sm w-auto">
|
||||
<input type="search" class="form-control" name="q" value="<?= escapeHtml($search) ?>" placeholder="Search <?= escapeHtml($config['many']) ?>">
|
||||
<button class="btn btn-primary" title="Search"><i class="fa fa-search"></i></button>
|
||||
</div>
|
||||
<div class="btn-group btn-group-sm" role="group" aria-label="Sort">
|
||||
<?php foreach ($sorts as $sort_key => [$sort_label, $sort_icon, $sort_title]) { ?>
|
||||
<a class="btn btn-outline-secondary<?= $sort_by === $sort_key ? ' active' : '' ?>" href="<?= escapeHtml($page_url(['sort' => $sort_key !== $default_sort ? $sort_key : null])) ?>" title="<?= $sort_title ?>"><i class="fas <?= $sort_icon ?> me-1"></i><?= $sort_label ?></a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="btn-group btn-group-sm me-auto" role="group" aria-label="Current or archived">
|
||||
<a class="btn btn-outline-secondary<?= $show_archived ? '' : ' active' ?>" href="<?= escapeHtml($page_url(['archived' => null])) ?>">Current</a>
|
||||
<a class="btn btn-outline-secondary<?= $show_archived ? ' active' : '' ?>" href="<?= escapeHtml($page_url(['archived' => 1])) ?>">Archived <span class="badge text-bg-secondary ms-1"><?= $archived_count ?></span></a>
|
||||
</div>
|
||||
<div class="dropdown" id="bulkActionButton" hidden>
|
||||
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
|
||||
<i class="fas fa-fw fa-layer-group me-1"></i>Bulk Action (<span id="selectedCount">0</span>)
|
||||
</button>
|
||||
<div class="dropdown-menu dropdown-menu-end">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/category/category_merge.php" data-bulk="true">
|
||||
<i class="fas fa-fw fa-compress-alt text-secondary me-2"></i>Merge into one...
|
||||
</a>
|
||||
<?php if ($show_archived) { ?>
|
||||
<button class="dropdown-item" type="submit" form="bulkActions" name="bulk_restore_categories">
|
||||
<i class="fas fa-fw fa-redo text-secondary me-2"></i>Restore
|
||||
</button>
|
||||
<?php } else { ?>
|
||||
<button class="dropdown-item" type="submit" form="bulkActions" name="bulk_archive_categories">
|
||||
<i class="fas fa-fw fa-archive text-secondary me-2"></i>Archive
|
||||
</button>
|
||||
<?php } ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<button class="dropdown-item text-danger confirm-link" type="submit" form="bulkActions" name="bulk_delete_categories"
|
||||
data-confirm-title="Delete these <?= escapeHtml($config['many']) ?>?"
|
||||
data-confirm-text="Only the ones nothing uses are deleted - the rest are kept, to merge into another or archive."
|
||||
data-confirm-button="Delete">
|
||||
<i class="fas fa-fw fa-trash me-2"></i>Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<hr>
|
||||
</div>
|
||||
|
||||
<form id="bulkActions" action="post.php" method="post">
|
||||
<input type="hidden" name="csrf_token" value="<?= $csrf ?>">
|
||||
<div class="table-responsive-sm">
|
||||
<table class="table table-striped table-borderless table-hover mb-0">
|
||||
<thead class="text-dark <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
|
||||
<table class="table table-borderless table-hover align-middle mb-0">
|
||||
<?php if ($categories) { ?>
|
||||
<thead class="text-dark">
|
||||
<tr>
|
||||
<th>
|
||||
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=category_name&order=<?= $disp ?>">
|
||||
Name <?php if ($sort == 'category_name') { echo $order_icon; } ?>
|
||||
</a>
|
||||
<th class="ps-3" style="width: 1%">
|
||||
<div class="form-check mb-0">
|
||||
<input class="form-check-input" id="selectAllCheckbox" type="checkbox" title="Select all">
|
||||
</div>
|
||||
</th>
|
||||
<th>Color</th>
|
||||
<th class="text-center">Action</th>
|
||||
<?php if ($can_reorder) { ?><th style="width: 1%"><span class="visually-hidden">Order</span></th><?php } ?>
|
||||
<th><?= escapeHtml(ucfirst($config['one'])) ?></th>
|
||||
<th class="d-none d-sm-table-cell">Used on</th>
|
||||
<th class="text-center" style="width: 1%">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$category_id = intval($row['category_id']);
|
||||
$category_name = escapeHtml($row['category_name']);
|
||||
$category_description = escapeHtml($row['category_description']);
|
||||
$category_color = escapeHtml($row['category_color']);
|
||||
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<a class="text-dark ajax-modal" href="#"
|
||||
data-modal-url="modals/category/category_edit.php?id=<?= $category_id ?>">
|
||||
<?= $category_name ?>
|
||||
<div><small class="text-secondary"><?= $category_description ?></small></div>
|
||||
</a>
|
||||
</td>
|
||||
<td><i class="fa fa-3x fa-circle" style="color:<?= $category_color ?>;"></i></td>
|
||||
<td>
|
||||
<div class="dropdown dropstart text-center">
|
||||
<button class="btn btn-secondary btn-sm" title="More actions" type="button" data-bs-toggle="dropdown">
|
||||
<i class="fas fa-ellipsis-h"></i>
|
||||
</button>
|
||||
<div class="dropdown-menu">
|
||||
<?php
|
||||
if ($archived) {
|
||||
?>
|
||||
<a class="dropdown-item text-info confirm-link"
|
||||
href="post.php?restore_category=<?= $category_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>">
|
||||
<i class="fas fa-fw fa-redo me-2"></i>Restore
|
||||
</a>
|
||||
<a class="dropdown-item text-danger confirm-link"
|
||||
href="post.php?delete_category=<?= $category_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>">
|
||||
<i class="fas fa-fw fa-trash me-2"></i>Delete
|
||||
</a>
|
||||
<?php
|
||||
} else {
|
||||
?>
|
||||
<a class="dropdown-item ajax-modal" href="#"
|
||||
data-modal-url="modals/category/category_edit.php?id=<?= $category_id ?>">
|
||||
<i class="fas fa-fw fa-edit me-2"></i>Edit
|
||||
</a>
|
||||
<a class="dropdown-item text-danger confirm-link"
|
||||
href="post.php?archive_category=<?= $category_id ?>&csrf_token=<?= $_SESSION['csrf_token'] ?>">
|
||||
<i class="fas fa-fw fa-archive me-2"></i>Archive
|
||||
</a>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
</thead>
|
||||
<?php } ?>
|
||||
<tbody<?= $can_reorder ? ' data-category-sortable data-category-kind="' . escapeHtml($kind) . '"' : '' ?>>
|
||||
<?php
|
||||
$positions = array_keys($categories);
|
||||
foreach ($categories as $category_id => $row) {
|
||||
$name = escapeHtml($row['category_name']);
|
||||
$used = $row['used'];
|
||||
$edit_url = "modals/category/category_edit.php?id=$category_id";
|
||||
$used_html = $used['all'] ? categoryUsageHtml($row, $used) : '<span class="text-secondary">' . ($row['category_archived_at'] ? 'Nothing uses it' : 'Nothing yet') . '</span>';
|
||||
$blocker = categoryDeleteBlocker($row, $used);
|
||||
$position = array_search($category_id, $positions, true);
|
||||
?>
|
||||
|
||||
<tr data-category-id="<?= $category_id ?>">
|
||||
<td class="ps-3">
|
||||
<div class="form-check mb-0">
|
||||
<input class="form-check-input bulk-select" type="checkbox" name="category_ids[]" value="<?= $category_id ?>" aria-label="Select <?= $name ?>">
|
||||
</div>
|
||||
</td>
|
||||
<?php if ($can_reorder) { ?>
|
||||
<td class="pe-0"><span class="itflow-category-grip text-secondary" title="Drag to move" data-category-grip><i class="fas fa-grip-vertical" aria-hidden="true"></i></span></td>
|
||||
<?php } ?>
|
||||
<td>
|
||||
<a class="ajax-modal text-decoration-none text-reset" href="#" data-modal-url="<?= $edit_url ?>" title="Edit <?= $name ?>"><?= categoryChip($row) ?></a>
|
||||
<?php if (trim((string) $row['category_description']) !== '') { ?>
|
||||
<div class="small text-secondary mt-1"><?= escapeHtml($row['category_description']) ?></div>
|
||||
<?php } ?>
|
||||
<div class="small d-sm-none mt-1"><?= $used_html ?></div>
|
||||
<?php if ($row['category_archived_at']) { ?>
|
||||
<div class="small text-secondary"><i class="fas fa-fw fa-archive me-1"></i>Archived <?= date('M j, Y', strtotime($row['category_archived_at'])) ?></div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="d-none d-sm-table-cell small"><?= $used_html ?></td>
|
||||
<td>
|
||||
<div class="dropdown dropstart text-center">
|
||||
<button class="btn btn-secondary btn-sm" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-ellipsis-h"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="<?= $edit_url ?>"><i class="fas fa-fw fa-edit me-2"></i>Edit</a>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/category/category_merge.php?category_ids[]=<?= $category_id ?>"><i class="fas fa-fw fa-compress-alt me-2"></i>Merge into...</a>
|
||||
<?php if ($can_reorder) { ?>
|
||||
<?php if ($position > 0) { ?>
|
||||
<a class="dropdown-item" href="post.php?move_category=<?= $category_id ?>&direction=up&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-arrow-up me-2"></i>Move up</a>
|
||||
<?php } ?>
|
||||
<?php if ($position < count($positions) - 1) { ?>
|
||||
<a class="dropdown-item" href="post.php?move_category=<?= $category_id ?>&direction=down&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-arrow-down me-2"></i>Move down</a>
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
<div class="dropdown-divider"></div>
|
||||
<?php if ($row['category_archived_at']) { ?>
|
||||
<a class="dropdown-item" href="post.php?restore_category=<?= $category_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-redo me-2"></i>Restore</a>
|
||||
<?php } else { ?>
|
||||
<a class="dropdown-item" href="post.php?archive_category=<?= $category_id ?>&csrf_token=<?= $csrf ?>" title="It stays on its records, but isn't offered for new ones"><i class="fas fa-fw fa-archive me-2"></i>Archive</a>
|
||||
<?php } ?>
|
||||
<?php if ($blocker !== '') { ?>
|
||||
<span class="dropdown-item disabled" title="<?= escapeHtml($blocker) ?>"><i class="fas fa-fw fa-trash me-2"></i>Delete</span>
|
||||
<?php } else { ?>
|
||||
<a class="dropdown-item text-danger text-bold confirm-link" href="post.php?delete_category=<?= $category_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Delete <?= $name ?>?" data-confirm-text="Nothing uses it, so no record changes." data-confirm-button="Delete"><i class="fas fa-fw fa-trash me-2"></i>Delete</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php if ($can_reorder) { ?>
|
||||
<div class="small text-secondary px-3 py-2 border-top"><i class="fas fa-fw fa-grip-vertical me-1"></i>Drag them into the order they're offered in.</div>
|
||||
<?php } elseif ($config['ordered'] && $sort_by === 'order' && !$show_archived && $search !== '') { ?>
|
||||
<div class="small text-secondary px-3 py-2 border-top">Clear the search to drag them into order.</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if (!$categories) { ?>
|
||||
<?= emptyStateHtml([
|
||||
'thing' => $config['many'],
|
||||
'title' => $show_archived ? 'No archived ' . $config['many'] : 'No ' . $config['many'] . ' yet',
|
||||
'text' => $show_archived ? 'An archived category stays on its records, but isn\'t offered for new ones.' : $config['about'],
|
||||
'icon' => $config['icon'],
|
||||
'filtered' => $search !== '',
|
||||
'clear_url' => $page_url(['q' => null]),
|
||||
'actions' => $show_archived ? [] : [['label' => $new_label, 'modal' => $add_url]],
|
||||
]) ?>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($loose) { ?>
|
||||
<div class="border-top px-3 py-2 itflow-category-loose">
|
||||
<div class="fw-semibold"><i class="fas fa-fw fa-question-circle text-secondary me-1"></i>In use, but not set up here</div>
|
||||
<p class="small text-secondary mb-1">Records have these, but they aren't <?= escapeHtml($config['many']) ?> - so they can't be picked again, and have no color or icon. Add one to make it <?= escapeHtml(categoryKindPhrase($kind)) ?>; to fold it into another, add it, then merge it.</p>
|
||||
<ul class="list-unstyled mb-0">
|
||||
<?php foreach ($loose as $loose_name => $loose_used) { ?>
|
||||
<li class="d-flex flex-wrap align-items-center gap-2 py-1">
|
||||
<span><?= escapeHtml($loose_name) ?></span>
|
||||
<span class="small"><?= categoryUsageHtml(['category_id' => 0, 'category_name' => $loose_name, 'category_type' => $kind], $loose_used) ?></span>
|
||||
<form action="post.php" method="post" class="ms-auto">
|
||||
<input type="hidden" name="csrf_token" value="<?= $csrf ?>">
|
||||
<input type="hidden" name="type" value="<?= escapeHtml($kind) ?>">
|
||||
<input type="hidden" name="name" value="<?= escapeHtml($loose_name) ?>">
|
||||
<button type="submit" name="add_category" class="btn btn-sm btn-outline-secondary" title="Make it <?= escapeHtml(categoryKindPhrase($kind)) ?>"><i class="fas fa-plus me-1"></i>Add</button>
|
||||
</form>
|
||||
</li>
|
||||
<?php } ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
'thing' => strtolower($category_label) . ' categories',
|
||||
'icon' => 'fa-list-ul',
|
||||
'views' => ['category'],
|
||||
'actions' => [['label' => "New $category_label Category", 'modal' => 'modals/category/category_add.php?category=' . urlencode($category)]],
|
||||
];
|
||||
?>
|
||||
<?php require_once "../includes/filter_footer.php"; ?>
|
||||
</div>
|
||||
|
||||
<script src="../js/bulk_actions.js"></script>
|
||||
<?php if ($can_reorder) { ?>
|
||||
<script src="/libs/SortableJS/Sortable.min.js"></script>
|
||||
<script>
|
||||
(function () {
|
||||
// Dragged into order - saved as soon as one is dropped; a failure reloads the page to show the order kept
|
||||
const body = document.querySelector('[data-category-sortable]');
|
||||
new Sortable(body, {
|
||||
handle: '[data-category-grip]',
|
||||
animation: 150,
|
||||
onEnd: function (event) {
|
||||
if (event.oldIndex === event.newIndex) {
|
||||
return;
|
||||
}
|
||||
const ids = Array.from(body.querySelectorAll('tr[data-category-id]')).map(function (row) {
|
||||
return row.dataset.categoryId;
|
||||
});
|
||||
itflowPost('post.php', { reorder_categories: 1, csrf_token: '<?= $csrf ?>', category: body.dataset.categoryKind, category_ids: ids })
|
||||
.then(function (text) {
|
||||
const result = JSON.parse(text);
|
||||
if (!result.ok) {
|
||||
throw new Error(result.error || 'Not saved');
|
||||
}
|
||||
itflowToast('Order saved', 'success');
|
||||
})
|
||||
.catch(function () {
|
||||
window.location.reload();
|
||||
});
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
<?php } ?>
|
||||
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
|
||||
62
admin/database_updates/3.1.5.php
Normal file
62
admin/database_updates/3.1.5.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 3.1.5 (from 3.1.4)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
// Every category colour as #rrggbb, so the colour picker shows it and the
|
||||
// lists and reports that read only hex draw it - short hex grows to six
|
||||
// digits, a CSS colour name ("lightblue", as the default categories were
|
||||
// made with) becomes the same colour in hex, and anything else becomes
|
||||
// no colour. Icons become "fa-name", the form the pages read.
|
||||
$category_css_colors = [
|
||||
'aliceblue' => '#f0f8ff', 'antiquewhite' => '#faebd7', 'aqua' => '#00ffff', 'aquamarine' => '#7fffd4', 'azure' => '#f0ffff',
|
||||
'beige' => '#f5f5dc', 'bisque' => '#ffe4c4', 'black' => '#000000', 'blanchedalmond' => '#ffebcd', 'blue' => '#0000ff',
|
||||
'blueviolet' => '#8a2be2', 'brown' => '#a52a2a', 'burlywood' => '#deb887', 'cadetblue' => '#5f9ea0', 'chartreuse' => '#7fff00',
|
||||
'chocolate' => '#d2691e', 'coral' => '#ff7f50', 'cornflowerblue' => '#6495ed', 'cornsilk' => '#fff8dc', 'crimson' => '#dc143c',
|
||||
'cyan' => '#00ffff', 'darkblue' => '#00008b', 'darkcyan' => '#008b8b', 'darkgoldenrod' => '#b8860b', 'darkgray' => '#a9a9a9',
|
||||
'darkgreen' => '#006400', 'darkgrey' => '#a9a9a9', 'darkkhaki' => '#bdb76b', 'darkmagenta' => '#8b008b', 'darkolivegreen' => '#556b2f',
|
||||
'darkorange' => '#ff8c00', 'darkorchid' => '#9932cc', 'darkred' => '#8b0000', 'darksalmon' => '#e9967a', 'darkseagreen' => '#8fbc8f',
|
||||
'darkslateblue' => '#483d8b', 'darkslategray' => '#2f4f4f', 'darkslategrey' => '#2f4f4f', 'darkturquoise' => '#00ced1', 'darkviolet' => '#9400d3',
|
||||
'deeppink' => '#ff1493', 'deepskyblue' => '#00bfff', 'dimgray' => '#696969', 'dimgrey' => '#696969', 'dodgerblue' => '#1e90ff',
|
||||
'firebrick' => '#b22222', 'floralwhite' => '#fffaf0', 'forestgreen' => '#228b22', 'fuchsia' => '#ff00ff', 'gainsboro' => '#dcdcdc',
|
||||
'ghostwhite' => '#f8f8ff', 'gold' => '#ffd700', 'goldenrod' => '#daa520', 'gray' => '#808080', 'green' => '#008000',
|
||||
'greenyellow' => '#adff2f', 'grey' => '#808080', 'honeydew' => '#f0fff0', 'hotpink' => '#ff69b4', 'indianred' => '#cd5c5c',
|
||||
'indigo' => '#4b0082', 'ivory' => '#fffff0', 'khaki' => '#f0e68c', 'lavender' => '#e6e6fa', 'lavenderblush' => '#fff0f5',
|
||||
'lawngreen' => '#7cfc00', 'lemonchiffon' => '#fffacd', 'lightblue' => '#add8e6', 'lightcoral' => '#f08080', 'lightcyan' => '#e0ffff',
|
||||
'lightgoldenrodyellow' => '#fafad2', 'lightgray' => '#d3d3d3', 'lightgreen' => '#90ee90', 'lightgrey' => '#d3d3d3', 'lightpink' => '#ffb6c1',
|
||||
'lightsalmon' => '#ffa07a', 'lightseagreen' => '#20b2aa', 'lightskyblue' => '#87cefa', 'lightslategray' => '#778899', 'lightslategrey' => '#778899',
|
||||
'lightsteelblue' => '#b0c4de', 'lightyellow' => '#ffffe0', 'lime' => '#00ff00', 'limegreen' => '#32cd32', 'linen' => '#faf0e6',
|
||||
'magenta' => '#ff00ff', 'maroon' => '#800000', 'mediumaquamarine' => '#66cdaa', 'mediumblue' => '#0000cd', 'mediumorchid' => '#ba55d3',
|
||||
'mediumpurple' => '#9370db', 'mediumseagreen' => '#3cb371', 'mediumslateblue' => '#7b68ee', 'mediumspringgreen' => '#00fa9a', 'mediumturquoise' => '#48d1cc',
|
||||
'mediumvioletred' => '#c71585', 'midnightblue' => '#191970', 'mintcream' => '#f5fffa', 'mistyrose' => '#ffe4e1', 'moccasin' => '#ffe4b5',
|
||||
'navajowhite' => '#ffdead', 'navy' => '#000080', 'oldlace' => '#fdf5e6', 'olive' => '#808000', 'olivedrab' => '#6b8e23',
|
||||
'orange' => '#ffa500', 'orangered' => '#ff4500', 'orchid' => '#da70d6', 'palegoldenrod' => '#eee8aa', 'palegreen' => '#98fb98',
|
||||
'paleturquoise' => '#afeeee', 'palevioletred' => '#db7093', 'papayawhip' => '#ffefd5', 'peachpuff' => '#ffdab9', 'peru' => '#cd853f',
|
||||
'pink' => '#ffc0cb', 'plum' => '#dda0dd', 'powderblue' => '#b0e0e6', 'purple' => '#800080', 'rebeccapurple' => '#663399',
|
||||
'red' => '#ff0000', 'rosybrown' => '#bc8f8f', 'royalblue' => '#4169e1', 'saddlebrown' => '#8b4513', 'salmon' => '#fa8072',
|
||||
'sandybrown' => '#f4a460', 'seagreen' => '#2e8b57', 'seashell' => '#fff5ee', 'sienna' => '#a0522d', 'silver' => '#c0c0c0',
|
||||
'skyblue' => '#87ceeb', 'slateblue' => '#6a5acd', 'slategray' => '#708090', 'slategrey' => '#708090', 'snow' => '#fffafa',
|
||||
'springgreen' => '#00ff7f', 'steelblue' => '#4682b4', 'tan' => '#d2b48c', 'teal' => '#008080', 'thistle' => '#d8bfd8',
|
||||
'tomato' => '#ff6347', 'turquoise' => '#40e0d0', 'violet' => '#ee82ee', 'wheat' => '#f5deb3', 'white' => '#ffffff',
|
||||
'whitesmoke' => '#f5f5f5', 'yellow' => '#ffff00', 'yellowgreen' => '#9acd32',
|
||||
];
|
||||
$sql = mysqli_query($mysqli, "SELECT category_id, category_color, category_icon FROM categories");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$color = strtolower(trim((string) $row['category_color']));
|
||||
if (preg_match('/^#([0-9a-f])([0-9a-f])([0-9a-f])$/', $color, $m)) {
|
||||
$color = "#$m[1]$m[1]$m[2]$m[2]$m[3]$m[3]";
|
||||
} elseif (preg_match('/^#[0-9a-f]{8}$/', $color)) {
|
||||
$color = substr($color, 0, 7);
|
||||
} elseif (!preg_match('/^#[0-9a-f]{6}$/', $color)) {
|
||||
$color = $category_css_colors[$color] ?? '';
|
||||
}
|
||||
$icon = preg_replace('/^(fa[srb]? +)?fa-/', '', strtolower(trim(strip_tags((string) $row['category_icon']))));
|
||||
$icon = trim(preg_replace('/[^a-z0-9-]/', '', $icon), '-');
|
||||
$category_id = intval($row['category_id']);
|
||||
mysqli_query($mysqli, "UPDATE categories SET category_color = " . ($color === '' ? 'NULL' : "'$color'") . ", category_icon = " . ($icon === '' ? 'NULL' : "'fa-$icon'") . ",
|
||||
category_updated_at = category_updated_at WHERE category_id = $category_id");
|
||||
}
|
||||
@@ -1,69 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* New category - from Admin > Categories (?category= the kind), or the +
|
||||
* beside a category picker in a form (which posts to the agent's post.php).
|
||||
* The fields are category_form.php.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$category = escapeHtml($_GET['category'] ?? '');
|
||||
$category = null;
|
||||
$kind = categoryKind($_GET['category'] ?? '') ? (string) $_GET['category'] : '';
|
||||
$kind_config = $kind !== '' ? categoryKind($kind) : null;
|
||||
|
||||
$category_types_array = ['Expense', 'Income', 'Referral', 'Ticket'];
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fa fa-fw fa-list-ul me-2"></i>New <strong><?= escapeHtml(ucwords(str_replace('_', ' ', $category))); ?></strong> Category</h5>
|
||||
<h5 class="modal-title"><i class="fas fa-fw <?= $kind_config['icon'] ?? 'fa-list-ul' ?> me-2"></i>New <?= escapeHtml($kind_config ? ucwords($kind_config['one']) : 'Category') ?></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<?php if ($category) { ?>
|
||||
<input type="hidden" name="type" value="<?= $category ?>">
|
||||
<?php } else { ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Type <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-tag"></i></span>
|
||||
<select class="form-select select2" name="type" required>
|
||||
<option value="">- Select Type -</option>
|
||||
<?php foreach ($category_types_array as $type_select) { ?>
|
||||
<option><?= $type_select ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php } ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-list-ul"></i></span>
|
||||
<input type="text" class="form-control" name="name" placeholder="Category name" maxlength="200" required autofocus>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Color <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-paint-brush"></i></span>
|
||||
<input type="color" class="form-control form-control-color" name="color" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Description</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-align-left"></i></span>
|
||||
<input type="text" class="form-control" name="description" placeholder="Enter a description" maxlength="200">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php require __DIR__ . '/category_form.php'; ?>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="add_category" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Create Category</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
|
||||
<button type="submit" name="add_category" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Create</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
|
||||
@@ -1,60 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Edit a category - its name (carried onto the records of a kind held by
|
||||
* name), description, colour and icon. The fields are category_form.php.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$category_id = intval($_GET['id']);
|
||||
$category = categoryRow(intval($_GET['id'] ?? 0));
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT category_color, category_description, category_name, category_type FROM categories WHERE category_id = $category_id LIMIT 1");
|
||||
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$category_name = escapeHtml($row['category_name']);
|
||||
$category_description = escapeHtml($row['category_description']);
|
||||
$category_color = escapeHtml($row['category_color']);
|
||||
$category_type = escapeHtml($row['category_type']);
|
||||
|
||||
// Generate the HTML form content using output buffering.
|
||||
ob_start();
|
||||
?>
|
||||
|
||||
if (!$category) { ?>
|
||||
<div class="modal-body text-center text-secondary py-5">That category is gone - it may have been merged into another, or deleted.</div>
|
||||
<?php } else {
|
||||
$category_id = intval($category['category_id']);
|
||||
$kind = (string) $category['category_type'];
|
||||
$kind_config = categoryKind($kind);
|
||||
$category_used = categoryUsageFor($category_id); ?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fa fa-fw fa-list-ul me-2"></i>Editing category: <strong><?= $category_name ?></strong></h5>
|
||||
<h5 class="modal-title"><i class="fas fa-fw <?= $kind_config['icon'] ?? 'fa-list-ul' ?> me-2"></i><?= escapeHtml(ucwords($kind_config['one'] ?? 'Category')) ?>: <strong><?= escapeHtml($category['category_name']) ?></strong></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="category_id" value="<?= $category_id ?>">
|
||||
<input type="hidden" name="type" value="<?= $category_type ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-list-ul"></i></span>
|
||||
<input type="text" class="form-control" name="name" maxlength="200" value="<?= $category_name ?>" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Color <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-paint-brush"></i></span>
|
||||
<input type="color" class="form-control form-control-color" name="color" value="<?= $category_color ?>" required>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Description</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-align-left"></i></span>
|
||||
<input type="text" class="form-control" name="description" placeholder="Enter a description" maxlength="200" value="<?= $category_description ?>">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if ($category['category_archived_at']) { ?>
|
||||
<div class="alert alert-secondary small py-2"><i class="fas fa-fw fa-archive me-1"></i>Archived <?= date('M j, Y', strtotime($category['category_archived_at'])) ?> - it stays on its records, but isn't offered for new ones.</div>
|
||||
<?php } ?>
|
||||
<?php if (!$kind_config) { ?>
|
||||
<div class="alert alert-warning small py-2">This is a "<?= escapeHtml($kind) ?>" category - nothing in ITFlow uses that kind any more.</div>
|
||||
<?php } ?>
|
||||
<?php require __DIR__ . '/category_form.php'; ?>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="edit_category" class="btn btn-primary text-bold"><i class="fa fa-check me-2"></i>Save</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Cancel</button>
|
||||
<button type="submit" name="edit_category" class="btn btn-primary text-bold"><i class="fas fa-check me-2"></i>Save changes</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
<?php }
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
|
||||
120
admin/modals/category/category_form.php
Normal file
120
admin/modals/category/category_form.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* A category's fields - for category_add.php and category_edit.php, which set
|
||||
* $category (a categoryRow(), or null when new), $kind (its category_type,
|
||||
* or '' to pick one) and for one being edited $category_used (what uses it,
|
||||
* categoryUsageFor()). The preview, colour swatches and icon search are
|
||||
* js/category_form.js. Posts to post.php - admin/post/category.php, or
|
||||
* agent/post/category.php when opened from a form on an agent page.
|
||||
*/
|
||||
|
||||
isset($mysqli) || die("Direct file access is not allowed");
|
||||
|
||||
$c = $category ?? null;
|
||||
$c_config = $kind !== '' ? categoryKind($kind) : null;
|
||||
$c_color = $c ? categoryColor($c['category_color']) : ($c_config ? categoryNextColor($kind) : CATEGORY_COLORS[0]);
|
||||
$c_icon = $c ? categoryIcon($c['category_icon']) : '';
|
||||
$c_icons = CATEGORY_ICONS;
|
||||
if ($c_icon !== '' && !in_array($c_icon, $c_icons, true)) {
|
||||
// An icon picked by name before - offered first, so it stays chosen
|
||||
array_unshift($c_icons, $c_icon);
|
||||
}
|
||||
$c_used = $category_used ?? ['all' => 0, 'archived' => 0, 'uses' => []];
|
||||
$c_preview = categoryChip([
|
||||
'category_name' => $c['category_name'] ?? 'New category',
|
||||
'category_type' => $kind,
|
||||
'category_color' => $c_color,
|
||||
'category_icon' => $c_icon,
|
||||
]);
|
||||
$c_max = $c_config['max'] ?? 200;
|
||||
|
||||
?>
|
||||
|
||||
<div data-category-form data-category-name-was="<?= escapeHtml($c['category_name'] ?? '') ?>">
|
||||
<div class="d-flex flex-wrap align-items-center gap-2 mb-3 p-2 rounded border bg-body-tertiary">
|
||||
<span class="small text-secondary">Looks like</span>
|
||||
<span data-category-preview data-category-badge="<?= $kind === 'asset_status' ? escapeHtml(assetStatusBadge((string) ($c['category_name'] ?? ''), '')['class'] ?: 'secondary') : '' ?>"><?= $c_preview ?></span>
|
||||
<?php if ($c) { ?>
|
||||
<span class="small text-secondary ms-auto text-end">
|
||||
<?= $c_used['all'] ? 'On ' . escapeHtml(categoryUsageText($c, $c_used)) : 'Nothing uses it yet' ?>
|
||||
</span>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<?php if (!$c && !$c_config) { ?>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">For <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-layer-group"></i></span>
|
||||
<select class="form-select" name="type" required>
|
||||
<option value="">- What it sorts -</option>
|
||||
<?php foreach (categoryKinds() as $kind_key => $kind_config) { ?>
|
||||
<option value="<?= escapeHtml($kind_key) ?>"><?= escapeHtml(ucfirst($kind_config['many'])) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<?php } elseif (!$c) { ?>
|
||||
<input type="hidden" name="type" value="<?= escapeHtml($kind) ?>">
|
||||
<?php } ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-list-ul"></i></span>
|
||||
<input type="text" class="form-control" name="name" maxlength="<?= $c_max ?>" placeholder="<?= escapeHtml(ucfirst($c_config['one'] ?? 'category')) ?> name" value="<?= escapeHtml($c['category_name'] ?? '') ?>" required<?= $c ? '' : ' autofocus' ?> data-category-name>
|
||||
</div>
|
||||
<?php if ($c && $c_config && $c_config['by'] === 'name' && $c_used['all']) { ?>
|
||||
<small class="text-secondary" data-category-rename-note hidden><i class="fas fa-fw fa-info-circle me-1"></i>Renaming it renames it on the <?= escapeHtml(categoryUsageText($c, $c_used)) ?> that <?= $c_used['all'] === 1 ? 'has' : 'have' ?> it too.</small>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Description</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-align-left"></i></span>
|
||||
<input type="text" class="form-control" name="description" maxlength="255" placeholder="What it's for, and when to use it" value="<?= escapeHtml($c['category_description'] ?? '') ?>">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Color</label>
|
||||
<input type="hidden" name="color" value="<?= $c_color ?>" data-category-color>
|
||||
<div class="d-flex flex-wrap align-items-center gap-2">
|
||||
<div class="d-flex flex-wrap gap-1" role="group" aria-label="Colors">
|
||||
<button type="button" class="itflow-tag-swatch itflow-category-swatch-none<?= $c_color === '' ? ' active' : '' ?>" data-category-swatch="" title="No color" aria-label="No color" aria-pressed="<?= $c_color === '' ? 'true' : 'false' ?>"><i class="fas fa-ban" aria-hidden="true"></i></button>
|
||||
<?php foreach (CATEGORY_COLORS as $swatch) { ?>
|
||||
<button type="button" class="itflow-tag-swatch<?= $swatch === $c_color ? ' active' : '' ?>" style="--itflow-tag-color: <?= $swatch ?>; --itflow-tag-text: <?= tagTextColor($swatch) ?>" data-category-swatch="<?= $swatch ?>" title="<?= $swatch ?>" aria-label="Color <?= $swatch ?>" aria-pressed="<?= $swatch === $c_color ? 'true' : 'false' ?>"><i class="fas fa-check" aria-hidden="true"></i></button>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="input-group input-group-sm w-auto" title="Any other color">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-paint-brush"></i></span>
|
||||
<input type="color" class="form-control form-control-color" value="<?= $c_color ?: '#6c757d' ?>" aria-label="Custom color" data-category-color-pick>
|
||||
<input type="text" class="form-control font-monospace" style="width: 6rem" maxlength="7" value="<?= $c_color ?>" placeholder="None" pattern="#?[0-9a-fA-F]{6}" aria-label="Color as hex" data-category-color-hex>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-1">
|
||||
<label class="form-label">Icon</label>
|
||||
<input type="hidden" name="icon" value="<?= $c_icon ?>" data-category-icon>
|
||||
<div class="input-group mb-2">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-search"></i></span>
|
||||
<input type="search" class="form-control" placeholder="Search icons - or type any Font Awesome icon's name" aria-label="Search icons" data-category-icon-search>
|
||||
</div>
|
||||
<div class="itflow-tag-icons border rounded p-1" role="group" aria-label="Icons" data-category-icons>
|
||||
<button type="button" class="btn btn-sm itflow-tag-icon<?= $c_icon === '' ? ' active' : '' ?>" data-category-icon-choice="" title="No icon - a dot in its color" aria-label="No icon" aria-pressed="<?= $c_icon === '' ? 'true' : 'false' ?>"><i class="fas fa-fw fa-ban" aria-hidden="true"></i></button>
|
||||
<?php foreach ($c_icons as $icon) { ?>
|
||||
<button type="button" class="btn btn-sm itflow-tag-icon<?= $icon === $c_icon ? ' active' : '' ?>" data-category-icon-choice="<?= $icon ?>" title="<?= $icon ?>" aria-label="<?= $icon ?>" aria-pressed="<?= $icon === $c_icon ? 'true' : 'false' ?>"><i class="fas fa-fw fa-<?= $icon ?>" aria-hidden="true"></i></button>
|
||||
<?php } ?>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary itflow-tag-icon-custom" data-category-icon-custom hidden><i class="fas fa-fw" aria-hidden="true"></i><span></span></button>
|
||||
<div class="small text-secondary p-2" data-category-icon-none hidden>No icon by that name here - type its whole name to use it</div>
|
||||
</div>
|
||||
<?php if ($kind === 'asset_status') { ?>
|
||||
<small class="text-secondary">Asset statuses show as a badge in their color - the icon shows in the status picker.</small>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/js/category_form.js"></script>
|
||||
110
admin/modals/category/category_merge.php
Normal file
110
admin/modals/category/category_merge.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Merge categories into one - from a category's Merge into..., or Bulk
|
||||
* Action on the ticked ones. Every record with one of them gets the one kept
|
||||
* instead, and the rest are deleted (categoryMerge()). Categories of one
|
||||
* kind only. Posts merge_categories.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
|
||||
$merge_ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_GET['category_ids'] ?? [])))));
|
||||
$merging = [];
|
||||
foreach ($merge_ids as $merge_id) {
|
||||
if ($row = categoryRow($merge_id)) {
|
||||
$merging[intval($row['category_id'])] = $row;
|
||||
}
|
||||
}
|
||||
$kinds = array_unique(array_map(function ($row) {
|
||||
return (string) $row['category_type'];
|
||||
}, $merging));
|
||||
$config = count($kinds) === 1 ? categoryKind(reset($kinds)) : null;
|
||||
$usage = $config ? categoryUsage($config['kind']) : [];
|
||||
$used = function ($category_id) use ($usage) {
|
||||
return $usage[intval($category_id)]['all'] ?? 0;
|
||||
};
|
||||
|
||||
// What they can go into: one of them (when there are several, the most used first), or another of the kind not archived
|
||||
$into = [];
|
||||
if ($config) {
|
||||
if (count($merging) > 1) {
|
||||
$into = $merging;
|
||||
uasort($into, function ($a, $b) use ($used) {
|
||||
return $used($b['category_id']) <=> $used($a['category_id']) ?: strnatcasecmp($a['category_name'], $b['category_name']);
|
||||
});
|
||||
}
|
||||
foreach (categoryRows($config['kind']) as $category_id => $row) {
|
||||
if (!isset($merging[$category_id]) && !$row['category_archived_at']) {
|
||||
$into[$category_id] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
$keep_id = count($merging) > 1 ? intval(array_key_first($into)) : 0;
|
||||
|
||||
// What merging does, in words
|
||||
$one = count($merging) === 1;
|
||||
$records = 0;
|
||||
foreach ($merging as $merge_id => $row) {
|
||||
$records += $used($merge_id);
|
||||
}
|
||||
$merge_text = '';
|
||||
if ($config) {
|
||||
$merge_text = ($records ? 'The ' . number_format($records) . ' ' . ($records === 1 ? 'record' : 'records') . ' with ' . ($one ? 'it get' : 'these get') . ' the ' . $config['one'] . ' you keep instead'
|
||||
. ($config['by'] === 'name' ? ' - renamed to it' : '') . ($config['kind'] === 'Expense' ? ', and a month budgeted under more than one ends with one line of their total' : '') . '.'
|
||||
: 'Nothing uses ' . ($one ? 'it' : 'these') . ' yet.')
|
||||
. ($one ? ' It is then deleted.' : ' The others are then deleted.') . " This can't be undone.";
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-compress-alt me-2"></i>Merge <?= $one ? 'Category' : count($merging) . ' Categories' ?></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
|
||||
<?php if (!$merging || !$config) { ?>
|
||||
<div class="modal-body text-center text-secondary py-5"><?= $merging ? 'Categories of one kind can be merged - pick expense categories, or asset statuses, and so on.' : 'Those categories are gone - they may have been merged or deleted already.' ?></div>
|
||||
<?php } elseif (!$into) { ?>
|
||||
<div class="modal-body text-center text-secondary py-5">There's no other <?= escapeHtml($config['one']) ?> to merge it into yet.</div>
|
||||
<?php } else { ?>
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<?php foreach ($merging as $merge_id => $row) { ?>
|
||||
<input type="hidden" name="category_ids[]" value="<?= $merge_id ?>">
|
||||
<?php } ?>
|
||||
|
||||
<div class="modal-body">
|
||||
<label class="form-label">Merging</label>
|
||||
<div class="d-flex flex-wrap gap-3 mb-3">
|
||||
<?php foreach ($merging as $merge_id => $row) { ?>
|
||||
<span class="d-inline-flex align-items-center gap-2"><?= categoryChip($row) ?><span class="small text-secondary"><?= number_format($used($merge_id)) ?> <?= $used($merge_id) === 1 ? 'record' : 'records' ?></span></span>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Into <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-list-ul"></i></span>
|
||||
<select class="form-select select2" name="into_category_id" data-placeholder="- Keep which <?= escapeHtml($config['one']) ?> -" required>
|
||||
<option value=""></option>
|
||||
<?php foreach ($into as $into_id => $row) { ?>
|
||||
<option value="<?= intval($into_id) ?>"<?= intval($into_id) === $keep_id ? ' selected' : '' ?><?= selectOptionColor(categoryColor($row['category_color']), categoryIcon($row['category_icon'])) ?>><?= escapeHtml($row['category_name']) ?><?= isset($merging[$into_id]) ? ' (one of these)' : '' ?><?= $row['category_archived_at'] ? ' (archived)' : '' ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="small text-secondary mb-0"><?= escapeHtml($merge_text) ?></p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="merge_categories" class="btn btn-primary text-bold"><i class="fas fa-compress-alt me-2"></i>Merge</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
<?php } ?>
|
||||
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
@@ -1,26 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - GET/POST request handler for categories ('category')
|
||||
* Categories (Admin > Categories) - making, changing, ordering, merging,
|
||||
* archiving and deleting them. The work is in functions/categories.php.
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
// "asset status" - what a category is, for the audit log and messages
|
||||
$category_kind_word = function ($row) {
|
||||
return categoryKind($row['category_type'] ?? '')['one'] ?? 'category';
|
||||
};
|
||||
|
||||
// Admin > Categories on a kind, to go back to after making one
|
||||
$category_page = function ($kind, $archived = false) {
|
||||
return 'categories.php?category=' . urlencode($kind) . ($archived ? '&archived=1' : '');
|
||||
};
|
||||
|
||||
if (isset($_POST['add_category'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
require_once 'category_model.php';
|
||||
[$fields, $error] = categoryFromInput($_POST);
|
||||
if ($error !== '') {
|
||||
flashAlert(escapeHtml($error), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO categories SET category_name = '$name', category_description = '$description', category_type = '$type', category_color = '$color'");
|
||||
$category_id = categoryCreate($fields);
|
||||
$kind = $category_kind_word(['category_type' => $fields['type']]);
|
||||
|
||||
$category_id = mysqli_insert_id($mysqli);
|
||||
logAudit("Category", "Create", "$session_name created $kind " . escapeSql($fields['name']), 0, $category_id);
|
||||
|
||||
logAudit("Category", "Create", "$session_name created category $type $name", 0, $category_id);
|
||||
flashAlert(ucfirst($kind) . " <strong>" . escapeHtml($fields['name']) . "</strong> created");
|
||||
|
||||
flashAlert("Category $type <strong>$name</strong> created");
|
||||
|
||||
redirect();
|
||||
redirect($category_page($fields['type']));
|
||||
|
||||
}
|
||||
|
||||
@@ -28,15 +42,25 @@ if (isset($_POST['edit_category'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
require_once 'category_model.php';
|
||||
$row = categoryRow(intval($_POST['category_id'] ?? 0));
|
||||
if (!$row) {
|
||||
flashAlert("That category is gone - it may have been merged into another, or deleted", 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
$category_id = intval($_POST['category_id']);
|
||||
[$fields, $error] = categoryFromInput($_POST, $row);
|
||||
if ($error !== '') {
|
||||
flashAlert(escapeHtml($error), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli,"UPDATE categories SET category_name = '$name', category_description = '$description', category_type = '$type', category_color = '$color' WHERE category_id = $category_id");
|
||||
$renamed = categoryUpdate($row, $fields);
|
||||
$kind = $category_kind_word($row);
|
||||
$was = $row['category_name'] !== $fields['name'] ? " (was " . escapeSql($row['category_name']) . ")" : '';
|
||||
|
||||
logAudit("Category", "Edit", "$session_name edited category $type $name", 0, $category_id);
|
||||
logAudit("Category", "Edit", "$session_name edited $kind " . escapeSql($fields['name']) . $was . ($renamed ? " - renamed on $renamed " . ($renamed === 1 ? 'record' : 'records') : ''), 0, intval($row['category_id']));
|
||||
|
||||
flashAlert("Category $type <strong>$name</strong> edited");
|
||||
flashAlert(ucfirst($kind) . " <strong>" . escapeHtml($fields['name']) . "</strong> saved" . ($renamed ? " - and renamed on the " . number_format($renamed) . ' ' . ($renamed === 1 ? 'record' : 'records') . " that had it" : ''));
|
||||
|
||||
redirect();
|
||||
|
||||
@@ -46,19 +70,14 @@ if (isset($_GET['archive_category'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$category_id = intval($_GET['archive_category']);
|
||||
|
||||
// Get Category Name and Type for logging
|
||||
$sql = mysqli_query($mysqli,"SELECT category_name, category_type FROM categories WHERE category_id = $category_id");
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$category_name = escapeSql($row['category_name']);
|
||||
$category_type = escapeSql($row['category_type']);
|
||||
|
||||
mysqli_query($mysqli,"UPDATE categories SET category_archived_at = NOW() WHERE category_id = $category_id");
|
||||
|
||||
logAudit("Category", "Archive", "$session_name archived category $category_type $category_name", 0, $category_id);
|
||||
|
||||
flashAlert("Category $category_type <strong>$category_name</strong> archived", 'error');
|
||||
$row = categoryRow(intval($_GET['archive_category']));
|
||||
if ($row && !$row['category_archived_at']) {
|
||||
categoryArchive($row['category_id']);
|
||||
$used = categoryUsageFor($row['category_id']);
|
||||
logAudit("Category", "Archive", "$session_name archived " . $category_kind_word($row) . " " . escapeSql($row['category_name']), 0, intval($row['category_id']));
|
||||
flashAlert(ucfirst($category_kind_word($row)) . " <strong>" . escapeHtml($row['category_name']) . "</strong> archived - "
|
||||
. ($used['all'] ? "it stays on the " . escapeHtml(categoryUsageText($row, $used)) . " that have it, but isn't offered for new ones" : "it isn't offered for new ones"));
|
||||
}
|
||||
|
||||
redirect();
|
||||
|
||||
@@ -68,19 +87,12 @@ if (isset($_GET['restore_category'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$category_id = intval($_GET['restore_category']);
|
||||
|
||||
// Get Category Name and Type for logging
|
||||
$sql = mysqli_query($mysqli,"SELECT category_name, category_type FROM categories WHERE category_id = $category_id");
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$category_name = escapeSql($row['category_name']);
|
||||
$category_type = escapeSql($row['category_type']);
|
||||
|
||||
mysqli_query($mysqli,"UPDATE categories SET category_archived_at = NULL WHERE category_id = $category_id");
|
||||
|
||||
logAudit("Category", "Restore", "$session_name retored category $category_type $category_name", 0, $category_id);
|
||||
|
||||
flashAlert("Category $category_type <strong>$category_name</strong> restored");
|
||||
$row = categoryRow(intval($_GET['restore_category']));
|
||||
if ($row && $row['category_archived_at']) {
|
||||
categoryRestore($row['category_id']);
|
||||
logAudit("Category", "Restore", "$session_name restored " . $category_kind_word($row) . " " . escapeSql($row['category_name']), 0, intval($row['category_id']));
|
||||
flashAlert(ucfirst($category_kind_word($row)) . " <strong>" . escapeHtml($row['category_name']) . "</strong> restored");
|
||||
}
|
||||
|
||||
redirect();
|
||||
|
||||
@@ -90,19 +102,168 @@ if (isset($_GET['delete_category'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$category_id = intval($_GET['delete_category']);
|
||||
$row = categoryRow(intval($_GET['delete_category']));
|
||||
if (!$row) {
|
||||
redirect();
|
||||
}
|
||||
|
||||
// Get Category Name and Type for logging
|
||||
$sql = mysqli_query($mysqli,"SELECT category_name, category_type FROM categories WHERE category_id = $category_id");
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$category_name = escapeSql($row['category_name']);
|
||||
$category_type = escapeSql($row['category_type']);
|
||||
$error = '';
|
||||
if (!categoryDelete($row['category_id'], $error)) {
|
||||
flashAlert(escapeHtml(ucfirst($category_kind_word($row)) . " " . $row['category_name'] . " is kept - " . lcfirst($error)), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
mysqli_query($mysqli,"DELETE FROM categories WHERE category_id = $category_id");
|
||||
logAudit("Category", "Delete", "$session_name deleted " . $category_kind_word($row) . " " . escapeSql($row['category_name']));
|
||||
|
||||
logAudit("Category", "Delete", "$session_name deleted category $category_type $category_name");
|
||||
|
||||
flashAlert("Category $category_type <strong>$category_name</strong> deleted", 'error');
|
||||
flashAlert(ucfirst($category_kind_word($row)) . " <strong>" . escapeHtml($row['category_name']) . "</strong> deleted", 'error');
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['merge_categories'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$into = categoryRow(intval($_POST['into_category_id'] ?? 0));
|
||||
if (!$into) {
|
||||
flashAlert("Pick the category to keep", 'error');
|
||||
redirect();
|
||||
}
|
||||
$into_id = intval($into['category_id']);
|
||||
|
||||
// Their names before they go - other categories of the same kind only
|
||||
$from = [];
|
||||
foreach (array_unique(array_map('intval', (array) ($_POST['category_ids'] ?? []))) as $from_id) {
|
||||
$from_row = $from_id !== $into_id ? categoryRow($from_id) : null;
|
||||
if ($from_row && $from_row['category_type'] === $into['category_type']) {
|
||||
$from[$from_id] = $from_row['category_name'];
|
||||
}
|
||||
}
|
||||
if (!$from) {
|
||||
flashAlert("Pick another " . escapeHtml($category_kind_word($into)) . " to merge into " . escapeHtml($into['category_name']), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
$moved = categoryMerge(array_keys($from), $into_id);
|
||||
$kind = $category_kind_word($into);
|
||||
$records = $moved ? number_format($moved) . ($moved === 1 ? ' record' : ' records') : '';
|
||||
|
||||
logAudit("Category", "Merge", "$session_name merged " . (count($from) === 1 ? $kind : categoryKind($into['category_type'])['many']) . " " . escapeSql(implode(', ', $from)) . " into " . escapeSql($into['category_name'])
|
||||
. ($moved ? " - $records" : ''), 0, $into_id);
|
||||
|
||||
flashAlert("Merged " . implode(', ', array_map(function ($name) {
|
||||
return '<strong>' . escapeHtml($name) . '</strong>';
|
||||
}, $from)) . " into <strong>" . escapeHtml($into['category_name']) . "</strong>"
|
||||
. ($moved ? " - the $records that had " . (count($from) === 1 ? 'it' : 'them') . " have it now" : ''));
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['move_category'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
// One place up or down among its kind's current categories, in the order pickers use
|
||||
$row = categoryRow(intval($_GET['move_category']));
|
||||
$config = $row ? categoryKind($row['category_type']) : null;
|
||||
if ($config && $config['ordered']) {
|
||||
$ids = array_keys(array_filter(categoryRows($row['category_type']), function ($r) {
|
||||
return !$r['category_archived_at'];
|
||||
}));
|
||||
$at = array_search(intval($row['category_id']), $ids, true);
|
||||
$to = ($_GET['direction'] ?? '') === 'up' ? $at - 1 : $at + 1;
|
||||
if ($at !== false && isset($ids[$to])) {
|
||||
[$ids[$at], $ids[$to]] = [$ids[$to], $ids[$at]];
|
||||
categoryReorder($row['category_type'], $ids);
|
||||
logAudit("Category", "Edit", "$session_name moved " . $category_kind_word($row) . " " . escapeSql($row['category_name']) . (($_GET['direction'] ?? '') === 'up' ? ' up' : ' down'), 0, intval($row['category_id']));
|
||||
}
|
||||
}
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['reorder_categories'])) {
|
||||
|
||||
// Dragged into order on Admin > Categories - answers with JSON, as the page saves it in the background
|
||||
header('Content-Type: application/json');
|
||||
if (!hash_equals((string) ($_SESSION['csrf_token'] ?? ''), (string) ($_POST['csrf_token'] ?? ''))) {
|
||||
echo json_encode(['ok' => false, 'error' => 'The page is out of date - reload it']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$config = categoryKind($_POST['category'] ?? '');
|
||||
if (!$config || !$config['ordered']) {
|
||||
echo json_encode(['ok' => false, 'error' => 'Those categories keep no order']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// The current ones in the order dragged, the archived ones after them
|
||||
$rows = categoryRows($config['kind']);
|
||||
$ids = array_values(array_filter(array_map('intval', (array) ($_POST['category_ids'] ?? [])), function ($id) use ($rows) {
|
||||
return isset($rows[$id]);
|
||||
}));
|
||||
if (categoryReorder($config['kind'], $ids)) {
|
||||
logAudit("Category", "Edit", "$session_name reordered the " . $config['many']);
|
||||
}
|
||||
|
||||
echo json_encode(['ok' => true]);
|
||||
exit;
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['bulk_archive_categories']) || isset($_POST['bulk_restore_categories'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$archive = isset($_POST['bulk_archive_categories']);
|
||||
$count = 0;
|
||||
foreach (array_unique(array_map('intval', (array) ($_POST['category_ids'] ?? []))) as $category_id) {
|
||||
$row = categoryRow($category_id);
|
||||
if (!$row || (bool) $row['category_archived_at'] === $archive) {
|
||||
continue;
|
||||
}
|
||||
if ($archive) {
|
||||
categoryArchive($category_id);
|
||||
} else {
|
||||
categoryRestore($category_id);
|
||||
}
|
||||
logAudit("Category", $archive ? "Archive" : "Restore", "$session_name " . ($archive ? 'archived ' : 'restored ') . $category_kind_word($row) . " " . escapeSql($row['category_name']), 0, $category_id);
|
||||
$count++;
|
||||
}
|
||||
|
||||
flashAlert(($archive ? "Archived " : "Restored ") . $count . ($count === 1 ? " category" : " categories") . ($archive && $count ? " - they stay on their records, but aren't offered for new ones" : ''));
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['bulk_delete_categories'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
$deleted = 0;
|
||||
$kept = [];
|
||||
foreach (array_unique(array_map('intval', (array) ($_POST['category_ids'] ?? []))) as $category_id) {
|
||||
$row = categoryRow($category_id);
|
||||
if (!$row) {
|
||||
continue;
|
||||
}
|
||||
$error = '';
|
||||
if (!categoryDelete($category_id, $error)) {
|
||||
if ($error !== '') {
|
||||
$kept[] = '<strong>' . escapeHtml($row['category_name']) . '</strong>';
|
||||
}
|
||||
continue;
|
||||
}
|
||||
logAudit("Category", "Delete", "$session_name deleted " . $category_kind_word($row) . " " . escapeSql($row['category_name']));
|
||||
$deleted++;
|
||||
}
|
||||
|
||||
flashAlert("Deleted $deleted " . ($deleted === 1 ? 'category' : 'categories')
|
||||
. ($kept ? ". Kept " . implode(', ', $kept) . " - " . (count($kept) === 1 ? 'it is' : 'they are') . " in use, so merge " . (count($kept) === 1 ? 'it' : 'them') . " into another, or archive " . (count($kept) === 1 ? 'it' : 'them') : ''), $kept ? 'warning' : 'error');
|
||||
|
||||
redirect();
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
<?php
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
$name = escapeSql($_POST['name']);
|
||||
$description = escapeSql($_POST['description']);
|
||||
$type = escapeSql($_POST['type']);
|
||||
$color = escapeSql($_POST['color']);
|
||||
@@ -161,13 +161,13 @@ $net_terms_array = array (
|
||||
<select class="form-select select2" name="payment_method">
|
||||
<option value="">- None -</option>
|
||||
<?php
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT category_name, category_color, category_icon FROM categories WHERE category_type = 'Payment Method' ORDER BY category_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$payment_method = escapeHtml($row['category_name']); ?>
|
||||
<option <?php if ($config_default_payment_method == $payment_method) {
|
||||
echo "selected";
|
||||
} ?><?= selectOptionColor($row['category_color'] ?? '', $row['category_icon'] ?? '') ?>><?= $payment_method ?></option>
|
||||
// Admin > Payment Methods - and the saved one, should it no longer be among them
|
||||
$default_methods = paymentMethods();
|
||||
if ((string) $config_default_payment_method !== '' && !in_array((string) $config_default_payment_method, $default_methods, true)) {
|
||||
$default_methods[] = (string) $config_default_payment_method;
|
||||
}
|
||||
foreach ($default_methods as $payment_method) { ?>
|
||||
<option <?= (string) $config_default_payment_method === $payment_method ? 'selected' : '' ?> value="<?= escapeHtml($payment_method) ?>"><?= escapeHtml($payment_method) ?></option>
|
||||
<?php } ?>
|
||||
|
||||
</select>
|
||||
@@ -181,13 +181,13 @@ $net_terms_array = array (
|
||||
<select class="form-select select2" name="expense_payment_method">
|
||||
<option value="">- None -</option>
|
||||
<?php
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT category_name, category_color, category_icon FROM categories WHERE category_type = 'Payment Method' ORDER BY category_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$payment_method = escapeHtml($row['category_name']); ?>
|
||||
<option <?php if ($config_default_expense_payment_method == $payment_method) {
|
||||
echo "selected";
|
||||
} ?><?= selectOptionColor($row['category_color'] ?? '', $row['category_icon'] ?? '') ?>><?= $payment_method ?></option>
|
||||
// Admin > Payment Methods - and the saved one, should it no longer be among them
|
||||
$default_methods = paymentMethods();
|
||||
if ((string) $config_default_expense_payment_method !== '' && !in_array((string) $config_default_expense_payment_method, $default_methods, true)) {
|
||||
$default_methods[] = (string) $config_default_expense_payment_method;
|
||||
}
|
||||
foreach ($default_methods as $payment_method) { ?>
|
||||
<option <?= (string) $config_default_expense_payment_method === $payment_method ? 'selected' : '' ?> value="<?= escapeHtml($payment_method) ?>"><?= escapeHtml($payment_method) ?></option>
|
||||
<?php } ?>
|
||||
|
||||
</select>
|
||||
|
||||
Reference in New Issue
Block a user