You've already forked Epicnabbo-Catalogus-Updated-Daily
119 lines
3.6 KiB
PHP
119 lines
3.6 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
|
|
{
|
|
$value = app(PermissionsService::class)->getOrDefault($permission);
|
|
return $value === true || $value === 1 || $value === '1';
|
|
}
|
|
}
|
|
|
|
if (! function_exists('hasHousekeepingPermission')) {
|
|
function hasHousekeepingPermission(string $permission): bool
|
|
{
|
|
$value = app(HousekeepingPermissionsService::class)->getOrDefault($permission);
|
|
return $value === true || $value === 1 || $value === '1';
|
|
}
|
|
}
|
|
|
|
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 = [];
|
|
|
|
if (isset($cache[$tableName])) {
|
|
return $cache[$tableName];
|
|
}
|
|
|
|
$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'")) {
|
|
$cache[$tableName] = $file->getFilename();
|
|
return $file->getFilename();
|
|
}
|
|
}
|
|
}
|
|
} 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 (! empty($foreignKey)) {
|
|
$connection->statement("ALTER TABLE {$table} DROP FOREIGN KEY {$foreignKey}");
|
|
}
|
|
}
|
|
}
|
|
}
|