netgescon-day0/app/Models/UserSetting.php

137 lines
4.5 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Support\StabileContext;
class UserSetting extends Model
{
protected $fillable = ['user_id', 'key', 'value'];
private const GESCON_KEYS = [
'gescon.source_path',
'gescon.mdb_stabili',
'gescon.mdb_index',
];
public function user()
{
return $this->belongsTo(User::class);
}
public static function get($key, $default = null)
{
if (!auth()->check()) {
return $default;
}
$adminFallback = static::resolveAdminGesconPath((string) $key);
$setting = static::where('user_id', auth()->id())
->where('key', $key)
->first();
if (!$setting) {
return $adminFallback ?? $default;
}
$val = $setting->value;
if ($adminFallback && (empty($val) || $val === $default || $val === '/mnt/gescon-archives/gescon')) {
return $adminFallback;
}
// If caller expects an array/object (default provided as array/object), try to decode JSON safely
if (is_array($default) || is_object($default)) {
if (is_string($val) && $val !== '') {
$decoded = json_decode($val, true);
if (json_last_error() === JSON_ERROR_NONE) {
return $decoded;
}
}
return is_array($val) ? $val : $default;
}
// If no explicit default type is provided, try a best-effort JSON decode for structured payloads
if ($default === null && is_string($val) && $val !== '') {
$first = $val[0];
if ($first === '{' || $first === '[') {
$decoded = json_decode($val, true);
if (json_last_error() === JSON_ERROR_NONE) {
return $decoded;
}
}
}
return $val;
}
private static function resolveAdminGesconPath(string $key): ?string
{
if (!in_array($key, self::GESCON_KEYS, true)) {
return null;
}
$user = auth()->user();
if (!$user || !method_exists($user, 'amministratore')) {
return null;
}
$amministratore = $user->amministratore;
$base = $amministratore?->cartella_dati ? rtrim((string) $amministratore->cartella_dati, DIRECTORY_SEPARATOR) : '';
if ($base === '') {
return null;
}
$legacyBase = $base . DIRECTORY_SEPARATOR . 'legacy';
$stabileLegacyBase = null;
try {
$stabile = StabileContext::getActiveStabile($user);
$stabileCode = $stabile?->codice_univoco;
if ($stabileCode) {
$candidate = $base . DIRECTORY_SEPARATOR . 'stabili' . DIRECTORY_SEPARATOR . $stabileCode . DIRECTORY_SEPARATOR . 'legacy';
if (is_dir($candidate)) {
$stabileLegacyBase = $candidate;
}
}
} catch (\Throwable $e) {
}
$legacyRoot = $stabileLegacyBase ?: (is_dir($legacyBase) ? $legacyBase : null);
if (!$legacyRoot) {
return null;
}
return match ($key) {
'gescon.source_path' => $legacyRoot,
'gescon.mdb_stabili' => is_file($legacyRoot . DIRECTORY_SEPARATOR . 'dbc' . DIRECTORY_SEPARATOR . 'Stabili.mdb')
? $legacyRoot . DIRECTORY_SEPARATOR . 'dbc' . DIRECTORY_SEPARATOR . 'Stabili.mdb'
: null,
'gescon.mdb_index' => is_file($legacyRoot . DIRECTORY_SEPARATOR . 'cartellestabili_e_anni.mdb')
? $legacyRoot . DIRECTORY_SEPARATOR . 'cartellestabili_e_anni.mdb'
: null,
default => null,
};
}
public static function set($key, $value)
{
if (!auth()->check()) {
return false;
}
// Normalize value: encode arrays/objects as JSON, keep primitives as strings
if (is_array($value) || is_object($value)) {
$stored = json_encode($value, JSON_UNESCAPED_UNICODE);
} elseif (is_bool($value)) {
// Store booleans as 'true'/'false' strings for backward compatibility
$stored = $value ? 'true' : 'false';
} elseif (is_null($value)) {
$stored = null;
} else {
$stored = (string) $value;
}
return static::updateOrCreate(
['user_id' => auth()->id(), 'key' => $key],
['value' => $stored]
);
}
}