forked from fa/breadcrumb-the-shire
176 lines
5.9 KiB
PHTML
176 lines
5.9 KiB
PHTML
<?php
|
|
use MintyPHP\Router;
|
|
|
|
$canCreateDepartments = can('departments.create');
|
|
$canDeleteDepartments = can('departments.delete');
|
|
$activeTenant = trim((string) ($_GET['tenant'] ?? ''));
|
|
$showTenantTabs = isset($tenants) && is_array($tenants) && count($tenants) > 1;
|
|
?>
|
|
<div class="app-breadcrumb">
|
|
<a href="/admin">Startseite</a><i class="bi bi-chevron-right"></i><?php e(t('Departments')); ?>
|
|
</div>
|
|
<div class="app-list-titlebar">
|
|
<h1><?php e(t('Departments')); ?></h1>
|
|
<div class="app-list-titlebar-actions">
|
|
<button
|
|
class="outline secondary"
|
|
type="button"
|
|
data-toolbar-toggle
|
|
data-toolbar-target="#departments-toolbar"
|
|
data-toolbar-text-show="<?php e(t('Show filters')); ?>"
|
|
data-toolbar-text-hide="<?php e(t('Hide filters')); ?>"
|
|
>
|
|
<i class="bi bi-funnel-fill"></i> <span data-toolbar-label><?php e(t('Hide filters')); ?></span>
|
|
</button>
|
|
<?php if ($canCreateDepartments): ?>
|
|
<a role="button" class="primary" href="admin/departments/create">
|
|
<i class="bi bi-plus"></i> <?php e(t('Create department')); ?>
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php if ($showTenantTabs): ?>
|
|
<div class="app-list-tabs" id="departments-tabs">
|
|
<a href="admin/departments" class="<?php e($activeTenant === '' ? 'tab-link is-active' : 'tab-link'); ?>">
|
|
<?php e(t('All')); ?>
|
|
</a>
|
|
<?php foreach ($tenants ?? [] as $tenant): ?>
|
|
<?php if (!empty($tenant['uuid'])): ?>
|
|
<?php $isActive = $activeTenant === $tenant['uuid']; ?>
|
|
<a href="admin/departments?tenant=<?php e($tenant['uuid']); ?>" class="<?php e($isActive ? 'tab-link is-active' : 'tab-link'); ?>">
|
|
<?php e($tenant['description'] ?? ''); ?>
|
|
</a>
|
|
<?php endif; ?>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<div class="app-list-toolbar" id="departments-toolbar">
|
|
<input type="hidden" id="department-tenant-filter" value="<?php e($activeTenant); ?>">
|
|
<input type="search" id="department-search" placeholder="<?php e(t('Search...')); ?>">
|
|
</div>
|
|
<div class="app-list-table">
|
|
<div id="departments-grid"></div>
|
|
</div>
|
|
|
|
<?php $gridFactoryVersion = @filemtime('web/js/core/app-grid-factory.js') ?: time(); ?>
|
|
<script src="<?php e(asset('vendor/gridjs/gridjs.umd.js')); ?>"></script>
|
|
<script type="module">
|
|
import { initToolbarToggles } from "<?php e(asset('js/components/app-toggle-toolbar.js')); ?>";
|
|
import { createServerGrid } from "<?php e(asset('js/core/app-grid-factory.js?v=' . $gridFactoryVersion)); ?>";
|
|
import { buildUrl, postAction, badgeHtml } from "<?php e(asset('js/pages/app-list-utils.js')); ?>";
|
|
|
|
initToolbarToggles();
|
|
|
|
const appBase = "<?php e(localeBase()); ?>";
|
|
const csrf = <?php MintyPHP\Buffer::get('grid_csrf'); ?>;
|
|
const deleteConfirm = "<?php e(t('Delete this department?')); ?>";
|
|
const buildActionUrl = (action, id) => buildUrl(appBase, `admin/departments/${action}/${id}`);
|
|
|
|
const initDepartmentsGrid = () => {
|
|
const formatBadge = (value) => badgeHtml(gridjs, value);
|
|
const buildBadgeList = (items) => {
|
|
if (!Array.isArray(items) || items.length === 0) return '';
|
|
const badges = items.map((label) => `<span class="badge" data-variant="neutral">${label}</span>`).join(' ');
|
|
return `<div class="badge-list">${badges}</div>`;
|
|
};
|
|
const gridConfig = createServerGrid({
|
|
gridjs: window.gridjs,
|
|
container: '#departments-grid',
|
|
dataUrl: 'admin/departments/data',
|
|
appBase,
|
|
columns: [
|
|
{ name: "<?php e(t('Description')); ?>", sort: true },
|
|
{
|
|
name: "<?php e(t('Created')); ?>",
|
|
sort: true,
|
|
formatter: (cell) => formatBadge(cell)
|
|
},
|
|
{
|
|
name: "<?php e(t('Modified')); ?>",
|
|
sort: true,
|
|
formatter: (cell) => formatBadge(cell)
|
|
},
|
|
{
|
|
name: "UUID",
|
|
hidden: true
|
|
},
|
|
{
|
|
name: "ID",
|
|
hidden: true
|
|
}
|
|
],
|
|
sortColumns: ['description', 'created', 'modified', null, null],
|
|
paginationLimit: 10,
|
|
language: <?php MintyPHP\Buffer::get('grid_lang'); ?>,
|
|
mapData: (data) => data.data.map((row) => [
|
|
row.description,
|
|
row.created,
|
|
row.modified,
|
|
row.uuid,
|
|
row.id
|
|
]),
|
|
actions: {
|
|
enabled: true,
|
|
label: "<?php e(t('Actions')); ?>",
|
|
getRowId: (row) => row?.cells?.[3]?.data,
|
|
items: [
|
|
{
|
|
key: 'edit',
|
|
type: 'link',
|
|
label: "<i class=\"bi bi-pencil-fill\"></i>",
|
|
href: ({ id }) => buildActionUrl('edit', id)
|
|
},
|
|
<?php if ($canDeleteDepartments): ?>
|
|
{
|
|
key: 'delete',
|
|
label: "<i class=\"bi bi-trash3-fill\"></i>",
|
|
className: 'danger outline',
|
|
confirm: deleteConfirm
|
|
}
|
|
<?php endif; ?>
|
|
],
|
|
onAction: async ({ action, id, grid }) => {
|
|
if (action !== 'delete') return;
|
|
const url = buildActionUrl('delete', id);
|
|
const response = await postAction(url, csrf);
|
|
if (response.ok) {
|
|
grid.forceRender();
|
|
} else {
|
|
window.location.href = new URL('admin/departments', appBase).toString();
|
|
}
|
|
}
|
|
},
|
|
search: {
|
|
input: '#department-search',
|
|
param: 'search',
|
|
debounce: 250
|
|
},
|
|
filters: [
|
|
{
|
|
input: '#department-tenant-filter',
|
|
param: 'tenant'
|
|
}
|
|
],
|
|
urlSync: true,
|
|
rowDataset: (row) => ({
|
|
uuid: row?.cells?.[4]?.data
|
|
}),
|
|
rowDblClick: {
|
|
getUrl: (rowData) => {
|
|
const uuid = rowData?.cells?.[3]?.data;
|
|
return uuid ? new URL(`admin/departments/edit/${uuid}`, appBase).toString() : '';
|
|
}
|
|
}
|
|
});
|
|
|
|
if (!gridConfig || !gridConfig.grid) {
|
|
console.warn('Departments grid init failed');
|
|
return null;
|
|
}
|
|
|
|
return gridConfig;
|
|
};
|
|
|
|
initDepartmentsGrid();
|
|
</script>
|