Files
Epicnabbo-Catalogus-Updated…/cms update/app/Helpers/helper.php
T
Remco b67e0ec2b9 🆙 Add fixed cms 🆙
2026-02-02 19:30:21 +01:00

101 lines
3.2 KiB
PHP

<?php
use App\Services\HousekeepingPermissionsService;
use App\Services\PermissionsService;
use App\Services\SettingsService;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
if (! function_exists('setting')) {
function setting(string $key, mixed $default = null): mixed
{
return app(SettingsService::class)->getOrDefault($key, $default);
}
}
if (! function_exists('hasPermission')) {
function hasPermission(string $permission): bool
{
return (bool) app(PermissionsService::class)->getOrDefault($permission);
}
}
if (! function_exists('hasHousekeepingPermission')) {
function hasHousekeepingPermission(string $permission): bool
{
return (bool) 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
{
// Iterate through all migration files in the migrations directory
foreach (glob(database_path('migrations/*.php')) as $filename) {
// Check if the migration file has the Schema::create() line with the given table name
$content = file_get_contents($filename);
if (preg_match("/Schema::create\\(['\"]".preg_quote($tableName, '/')."['\"]/", $content)) {
return basename($filename, '.php');
}
}
throw new RuntimeException("Migration for table '{$tableName}' not found.");
}
}
if (! function_exists('getPredominantImageColor')) {
function getPredominantImageColor(string $imagePath): string
{
// Stub implementation - returns a default color
// In production, this would extract the predominant color from the image
return '#000000';
}
}
if (! function_exists('columnExists')) {
function columnExists(string $table, string $column): bool
{
try {
return Schema::hasColumn($table, $column);
} catch (Throwable) {
return false;
}
}
}
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}");
}
}
}
}