Files
breadcrumb-the-shire/lib/Support/helpers/ui.php
2026-02-04 23:31:53 +01:00

179 lines
5.9 KiB
PHP

<?php
function gridLang(): array
{
return [
'search' => [
'placeholder' => t('Search...'),
],
'sort' => [
'sortAsc' => t('Sort column ascending'),
'sortDesc' => t('Sort column descending'),
],
'pagination' => [
'previous' => t('Previous'),
'next' => t('Next'),
'navigate' => t('Page %d of %d'),
'page' => t('Page %d'),
'showing' => t('Showing'),
'of' => t('of'),
'to' => t('to'),
'results' => t('results'),
],
'loading' => t('Loading...'),
'noRecordsFound' => t('No records found'),
'error' => t('An error happened while fetching the data'),
];
}
/**
* Render a multi-select filter field with hidden input sync.
*
* @param string $id Base ID for the hidden input (select gets "-ui" suffix)
* @param string $label Field label (will be translated)
* @param string $placeholder Placeholder text (will be translated)
* @param array $items Array of items with 'id' and 'description' keys
* @param array $selected Array of selected item IDs
*/
function multiSelectFilter(
string $id,
string $label,
string $placeholder,
array $items,
array $selected
): void {
?>
<label class="app-field">
<span><?php e(t($label)); ?></span>
<input type="hidden" id="<?php e($id); ?>" value="<?php e(implode(',', $selected)); ?>">
<select
id="<?php e($id); ?>-ui"
multiple
data-multi-select
data-sync-target="#<?php e($id); ?>"
data-select-all="true"
data-list-all="false"
data-search="true"
data-placeholder="<?php e(t($placeholder)); ?>"
data-search-placeholder="<?php e(t('Search...')); ?>"
data-select-all-label="<?php e(t('Select all')); ?>"
data-selected-label="<?php e(t('%d selected')); ?>"
>
<?php foreach ($items as $item): ?>
<?php $itemId = (string) ($item['id'] ?? ''); ?>
<?php if ($itemId !== ''): ?>
<option value="<?php e($itemId); ?>"<?php if (in_array($itemId, $selected, true)) { ?> selected<?php } ?>>
<?php e($item['description'] ?? ''); ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
</label>
<?php
}
/**
* Render a multi-select form field for data entry.
*
* @param string $id Element ID
* @param string $name Form field name
* @param string $label Field label (will be translated)
* @param string $placeholder Placeholder text (will be translated)
* @param array $items Array of items with 'id' and 'description' keys
* @param array $selected Array of selected item IDs
* @param bool $disabled Whether the field is disabled/readonly
* @param string|array $labelKeys Key(s) to use for option label (tries in order)
* @param string|null $formId Form ID for selects outside form element
*/
function multiSelectForm(
string $id,
string $name,
string $label,
string $placeholder,
array $items,
array $selected,
bool $disabled = false,
string|array $labelKeys = 'description',
?string $formId = null
): void {
$labelKeyList = is_array($labelKeys) ? $labelKeys : [$labelKeys];
?>
<label>
<span><?php e(t($label)); ?></span>
</label>
<select
id="<?php e($id); ?>"
name="<?php e($name); ?>"
<?php if ($formId !== null): ?>form="<?php e($formId); ?>"<?php endif; ?>
multiple
data-multi-select="true"
data-select-all="true"
data-search="true"
data-placeholder="<?php e(t($placeholder)); ?>"
data-search-placeholder="<?php e(t('Search...')); ?>"
data-select-all-label="<?php e(t('Select all')); ?>"
<?php if ($disabled): ?>data-disabled="true" disabled<?php endif; ?>
>
<?php foreach ($items as $item): ?>
<?php $itemId = (int) ($item['id'] ?? 0); ?>
<?php if ($itemId > 0): ?>
<?php
$isSelected = in_array($itemId, $selected, true);
$itemLabel = '';
foreach ($labelKeyList as $key) {
if (isset($item[$key]) && (string) $item[$key] !== '') {
$itemLabel = (string) $item[$key];
break;
}
}
?>
<option value="<?php e((string) $itemId); ?>"<?php if ($isSelected) { ?> selected<?php } ?>>
<?php e($itemLabel); ?>
</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<?php
}
function navActive($path, bool $prefix = false): array
{
$current = \MintyPHP\Http\Request::path();
$paths = is_array($path) ? $path : [$path];
$isActive = false;
foreach ($paths as $p) {
$p = (string) $p;
$match = $prefix ? strpos($current, $p) === 0 : $current === $p;
if (!$match && ($current === '' || $current === '/')) {
$match = $p === '' || $p === '/';
}
if ($match) {
$isActive = true;
break;
}
}
return [
'class' => $isActive ? 'active' : '',
'aria' => $isActive ? 'aria-current="page"' : '',
'isActive' => $isActive,
];
}
function navActivePublicPages(array $extraSlugs = []): array
{
$current = \MintyPHP\Http\Request::path();
if ($current === '') {
return ['class' => '', 'aria' => '', 'isActive' => false];
}
$publicSlugs = array_merge(['imprint', 'privacy'], $extraSlugs);
$isActive = strpos($current, 'page/') === 0 || in_array($current, $publicSlugs, true);
return [
'class' => $isActive ? 'active' : '',
'aria' => $isActive ? 'aria-current="page"' : '',
'isActive' => $isActive,
];
}