Files
Epicnabbo-Catalogus-Updated…/Updated_Cms/app/Helpers/helper.php
T
Remco 35202aa825 🆙 More fixes 🆙
2026-01-19 23:22:33 +01:00

120 lines
3.7 KiB
PHP

<?php
use App\Services\HousekeepingPermissionsService;
use App\Services\PermissionsService;
use App\Services\SettingsService;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Schema;
if (! function_exists('setting')) {
function setting(string $setting): string
{
return app(SettingsService::class)->getOrDefault($setting);
}
}
if (! function_exists('isDarkColor')) {
function isDarkColor(?string $hex): bool
{
if (! is_string($hex)) {
return false;
}
$hex = trim($hex);
if (! preg_match('/^#?[0-9a-fA-F]{6}$/', $hex)) {
return false;
}
$hex = ltrim($hex, '#');
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
$luminance = (0.2126 * $r + 0.7152 * $g + 0.0722 * $b) / 255;
return $luminance < 0.5;
}
}
if (! function_exists('hasPermission')) {
function hasPermission(string $permission): bool
{
return app(PermissionsService::class)->getOrDefault($permission);
}
}
if (! function_exists('hasHousekeepingPermission')) {
function hasHousekeepingPermission(string $permission): bool
{
return app(HousekeepingPermissionsService::class)->getOrDefault($permission);
}
}
if (! function_exists('strLimit')) {
function strLimit(string $string, int $limit, string $replacement = '...'): string
{
return Str::limit($string, $limit, $replacement);
}
}
if (! function_exists('findMigration')) {
function findMigration(string $tableName): string
{
static $cache = [];
$cache = is_array($cache) ? $cache : [];
if (isset($cache[$tableName])) {
$cached = $cache[$tableName];
return is_string($cached) ? $cached : '';
}
$migrationsPath = database_path('migrations');
if (! is_dir($migrationsPath)) {
return '';
}
try {
$files = new \DirectoryIterator($migrationsPath);
foreach ($files as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$content = @file_get_contents($file->getPathname());
if ($content !== false && str_contains($content, "Schema::create('$tableName'")) {
$filename = (string) $file->getFilename();
$cache[$tableName] = $filename;
return $filename;
}
}
}
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::error("Error finding migration for table {$tableName}: {$e->getMessage()}");
}
$cache[$tableName] = '';
return '';
}
}
if (! function_exists('columnExists')) {
function columnExists(string $table, string $column): bool
{
return Schema::hasColumn($table, $column);
}
}
if (! function_exists('dropForeignKeyIfExists')) {
function dropForeignKeyIfExists(string $table, string $column): void
{
$connection = Schema::getConnection();
$tableWithPrefix = $connection->getTablePrefix() . $table;
$foreignKeys = collect($connection->select('SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = ?
AND COLUMN_NAME = ?', [$tableWithPrefix, $column]))
->pluck('CONSTRAINT_NAME');
foreach ($foreignKeys as $foreignKey) {
if (is_string($foreignKey) && $foreignKey !== '') {
$connection->statement('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $foreignKey);
}
}
}
}