baseline
This commit is contained in:
208
lib/Support/helpers/app.php
Normal file
208
lib/Support/helpers/app.php
Normal file
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
function e($string): void
|
||||
{
|
||||
echo htmlspecialchars((string) $string, ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
|
||||
function asset(string $path): string
|
||||
{
|
||||
return \MintyPHP\Router::getBaseUrl() . ltrim($path, '/');
|
||||
}
|
||||
|
||||
function localeBase(): string
|
||||
{
|
||||
$locale = \MintyPHP\I18n::$locale ?? \MintyPHP\I18n::$defaultLocale;
|
||||
$prefix = $locale !== '' ? $locale . '/' : '';
|
||||
return \MintyPHP\Router::getBaseUrl() . $prefix;
|
||||
}
|
||||
|
||||
function lurl(string $path = ''): string
|
||||
{
|
||||
return localeBase() . ltrim($path, '/');
|
||||
}
|
||||
|
||||
function appTitle(): string
|
||||
{
|
||||
$default = defined('APP_NAME') ? APP_NAME : 'IMVS';
|
||||
$title = appSetting('app_title');
|
||||
if ($title !== null) {
|
||||
return $title;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
function appUrl(string $path = ''): string
|
||||
{
|
||||
if ($path !== '' && preg_match('#^https?://#i', $path)) {
|
||||
return $path;
|
||||
}
|
||||
$base = getenv('APP_URL') ?: '';
|
||||
$base = rtrim((string) $base, '/');
|
||||
if ($base === '') {
|
||||
$scheme = 'http';
|
||||
$https = $_SERVER['HTTPS'] ?? '';
|
||||
$forwarded = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '';
|
||||
if ($https === 'on' || $https === '1' || $forwarded === 'https') {
|
||||
$scheme = 'https';
|
||||
}
|
||||
$host = $_SERVER['HTTP_HOST'] ?? '';
|
||||
if ($host !== '') {
|
||||
$base = $scheme . '://' . $host;
|
||||
}
|
||||
}
|
||||
if ($base === '') {
|
||||
return \MintyPHP\Router::getBaseUrl() . ltrim((string) $path, '/');
|
||||
}
|
||||
$path = ltrim((string) $path, '/');
|
||||
return $base . '/' . $path;
|
||||
}
|
||||
|
||||
function appLogoUrlAbsolute(int $size = 128): string
|
||||
{
|
||||
$url = appLogoUrl($size);
|
||||
return appUrl($url);
|
||||
}
|
||||
|
||||
function appSetting(string $key): ?string
|
||||
{
|
||||
$cacheFile = dirname(__DIR__, 3) . '/config/settings.php';
|
||||
if (!is_file($cacheFile)) {
|
||||
return null;
|
||||
}
|
||||
$settings = include $cacheFile;
|
||||
if (!is_array($settings)) {
|
||||
return null;
|
||||
}
|
||||
$value = $settings[$key] ?? null;
|
||||
$value = $value !== null ? trim((string) $value) : '';
|
||||
return $value !== '' ? $value : null;
|
||||
}
|
||||
|
||||
function appDefaultLocale(): ?string
|
||||
{
|
||||
$locale = appSetting('app_locale');
|
||||
if ($locale === null) {
|
||||
return null;
|
||||
}
|
||||
$available = defined('APP_LOCALES') ? APP_LOCALES : [];
|
||||
if ($available && !in_array($locale, $available, true)) {
|
||||
return null;
|
||||
}
|
||||
return $locale;
|
||||
}
|
||||
|
||||
function appDefaultTheme(): string
|
||||
{
|
||||
$setting = appSetting('app_theme');
|
||||
if (in_array($setting, ['light', 'dark'], true)) {
|
||||
return $setting;
|
||||
}
|
||||
$envTheme = getenv('APP_THEME') ?: 'light';
|
||||
$envTheme = strtolower(trim((string) $envTheme));
|
||||
return in_array($envTheme, ['light', 'dark'], true) ? $envTheme : 'light';
|
||||
}
|
||||
|
||||
function allowUserTheme(): bool
|
||||
{
|
||||
$value = appSetting('app_theme_user');
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
|
||||
}
|
||||
|
||||
function appPrimaryColor(): ?string
|
||||
{
|
||||
$tenantColor = $_SESSION['current_tenant']['primary_color'] ?? null;
|
||||
if ($tenantColor !== null) {
|
||||
$tenantColor = strtolower(trim((string) $tenantColor));
|
||||
if (preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $tenantColor)) {
|
||||
return $tenantColor;
|
||||
}
|
||||
}
|
||||
|
||||
$value = appSetting('app_primary_color');
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
$value = strtolower(trim($value));
|
||||
if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
|
||||
return null;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
function appPrimaryCssVars(): string
|
||||
{
|
||||
$hex = appPrimaryColor();
|
||||
if ($hex === null) {
|
||||
return '';
|
||||
}
|
||||
$hex = ltrim($hex, '#');
|
||||
if (strlen($hex) === 3) {
|
||||
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
|
||||
}
|
||||
$r = hexdec(substr($hex, 0, 2)) / 255;
|
||||
$g = hexdec(substr($hex, 2, 2)) / 255;
|
||||
$b = hexdec(substr($hex, 4, 2)) / 255;
|
||||
|
||||
$max = max($r, $g, $b);
|
||||
$min = min($r, $g, $b);
|
||||
$delta = $max - $min;
|
||||
|
||||
$h = 0.0;
|
||||
if ($delta !== 0.0) {
|
||||
if ($max === $r) {
|
||||
$h = 60 * fmod((($g - $b) / $delta), 6);
|
||||
} elseif ($max === $g) {
|
||||
$h = 60 * ((($b - $r) / $delta) + 2);
|
||||
} else {
|
||||
$h = 60 * ((($r - $g) / $delta) + 4);
|
||||
}
|
||||
}
|
||||
if ($h < 0) {
|
||||
$h += 360;
|
||||
}
|
||||
|
||||
$l = ($max + $min) / 2;
|
||||
$s = $delta === 0.0 ? 0.0 : $delta / (1 - abs(2 * $l - 1));
|
||||
|
||||
$h = round($h, 2);
|
||||
$s = round($s * 100, 2) . '%';
|
||||
$l = round($l * 100, 2) . '%';
|
||||
|
||||
return "--app-primary-h-base: {$h}; --app-primary-s-base: {$s}; --app-primary-l-base: {$l}; --app-primary-h-light: {$h}; --app-primary-s-light: {$s}; --app-primary-l-light: {$l};";
|
||||
}
|
||||
|
||||
function appLogoUrl(?int $size = null): string
|
||||
{
|
||||
if (class_exists('MintyPHP\\Service\\BrandingLogoService') && \MintyPHP\Service\BrandingLogoService::hasLogo()) {
|
||||
$query = $size ? '?size=' . (int) $size : '';
|
||||
return lurl('branding/logo' . $query);
|
||||
}
|
||||
return asset('brand/logo.svg');
|
||||
}
|
||||
|
||||
function appFaviconUrl(string $file): string
|
||||
{
|
||||
$tenantUuid = $_SESSION['current_tenant']['uuid'] ?? '';
|
||||
if ($tenantUuid !== '' && class_exists('MintyPHP\\Service\\TenantFaviconService')) {
|
||||
if (\MintyPHP\Service\TenantFaviconService::hasFavicon($tenantUuid)) {
|
||||
return asset('favicon/tenants/' . $tenantUuid . '/' . ltrim($file, '/'));
|
||||
}
|
||||
}
|
||||
return asset('favicon/' . ltrim($file, '/'));
|
||||
}
|
||||
|
||||
function d()
|
||||
{
|
||||
return call_user_func_array('MintyPHP\\Debugger::debug', func_get_args());
|
||||
}
|
||||
|
||||
function sortByDescription(array &$items): void
|
||||
{
|
||||
usort($items, static fn($a, $b) =>
|
||||
strcasecmp((string) ($a['description'] ?? ''), (string) ($b['description'] ?? ''))
|
||||
);
|
||||
}
|
||||
27
lib/Support/helpers/auth.php
Normal file
27
lib/Support/helpers/auth.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
function accountUrl(): string
|
||||
{
|
||||
$user = $_SESSION['user'] ?? [];
|
||||
$uuid = (string) ($user['uuid'] ?? '');
|
||||
return $uuid !== '' ? lurl('profile') : lurl('login');
|
||||
}
|
||||
|
||||
function can(string $permissionKey): bool
|
||||
{
|
||||
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return false;
|
||||
}
|
||||
if (!class_exists('MintyPHP\\Service\\PermissionService')) {
|
||||
return false;
|
||||
}
|
||||
$keys = \MintyPHP\Service\PermissionService::getCachedPermissions($userId);
|
||||
if (!$keys) {
|
||||
$keys = \MintyPHP\Service\PermissionService::getUserPermissions($userId);
|
||||
if (!$keys) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return in_array($permissionKey, $keys, true);
|
||||
}
|
||||
42
lib/Support/helpers/i18n.php
Normal file
42
lib/Support/helpers/i18n.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
function t()
|
||||
{
|
||||
$arguments = func_get_args();
|
||||
$arguments[0] = \MintyPHP\I18n::translate($arguments[0]);
|
||||
if (count($arguments) === 1) {
|
||||
return $arguments[0];
|
||||
}
|
||||
return call_user_func_array('sprintf', $arguments);
|
||||
}
|
||||
|
||||
function dt($value, ?string $format = null)
|
||||
{
|
||||
$displayTz = null;
|
||||
try {
|
||||
$displayTz = new \DateTimeZone(defined('APP_TIMEZONE') ? APP_TIMEZONE : date_default_timezone_get());
|
||||
} catch (\Exception $e) {
|
||||
$displayTz = new \DateTimeZone(date_default_timezone_get());
|
||||
}
|
||||
|
||||
if ($value instanceof \DateTimeInterface) {
|
||||
$date = (new \DateTimeImmutable('@' . $value->getTimestamp()))->setTimezone($displayTz);
|
||||
} elseif (is_string($value) && $value !== '') {
|
||||
try {
|
||||
// Treat DB timestamps as UTC and convert for display
|
||||
$date = new \DateTimeImmutable($value, new \DateTimeZone('UTC'));
|
||||
$date = $date->setTimezone($displayTz);
|
||||
} catch (\Exception $e) {
|
||||
return $value;
|
||||
}
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($format === null) {
|
||||
$locale = \MintyPHP\I18n::$locale ?? '';
|
||||
$format = (strpos((string) $locale, 'de') === 0) ? 'd.m.Y H:i' : 'Y-m-d H:i';
|
||||
}
|
||||
|
||||
return $date->format($format);
|
||||
}
|
||||
178
lib/Support/helpers/ui.php
Normal file
178
lib/Support/helpers/ui.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?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,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user