80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
class CalendarioConnessione extends Model
|
|
{
|
|
protected $table = 'calendario_connessioni';
|
|
|
|
protected $fillable = [
|
|
'nome',
|
|
'tipo',
|
|
'config',
|
|
'stato',
|
|
'sync_direction',
|
|
'sync_interval_minutes',
|
|
'last_sync_at',
|
|
'last_error_at',
|
|
'last_error_message',
|
|
'is_active',
|
|
'ordine',
|
|
];
|
|
|
|
protected $casts = [
|
|
'config' => 'array',
|
|
'is_active' => 'boolean',
|
|
'sync_interval_minutes' => 'integer',
|
|
'last_sync_at' => 'datetime',
|
|
'last_error_at' => 'datetime',
|
|
'ordine' => 'integer',
|
|
];
|
|
|
|
public function scopeAttivi($query)
|
|
{
|
|
return $query->where('is_active', true)->orderBy('ordine');
|
|
}
|
|
|
|
public function getDecryptedConfig(): array
|
|
{
|
|
$config = $this->config;
|
|
foreach (['username', 'password', 'client_secret', 'refresh_token', 'api_key'] as $field) {
|
|
if (isset($config[$field])) {
|
|
try {
|
|
$config[$field] = Crypt::decryptString($config[$field]);
|
|
} catch (\Exception) {
|
|
}
|
|
}
|
|
}
|
|
return $config;
|
|
}
|
|
|
|
public function encryptAndSetConfig(array $config): void
|
|
{
|
|
foreach (['username', 'password', 'client_secret', 'refresh_token', 'api_key'] as $field) {
|
|
if (isset($config[$field]) && $config[$field] !== '' && !str_starts_with($config[$field], 'eyJpdiI')) {
|
|
$config[$field] = Crypt::encryptString($config[$field]);
|
|
}
|
|
}
|
|
$this->config = $config;
|
|
}
|
|
|
|
public static function opzioniTipo(): array
|
|
{
|
|
return ['google_calendar', 'caldav'];
|
|
}
|
|
|
|
public static function etichettaTipo(string $tipo): string
|
|
{
|
|
return match ($tipo) {
|
|
'google_calendar' => 'Google Calendar',
|
|
'caldav' => 'CalDAV (Infomaniak/NextCloud)',
|
|
default => ucfirst($tipo),
|
|
};
|
|
}
|
|
}
|