Certified/resources/views/pages/settings/⚡certificates.blade.php
2026-08-09 16:33:53 +02:00

114 lines
4.1 KiB
PHP

<?php
use App\Ocr\OcrManager;
use Flux\Flux;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Title;
use Livewire\Component;
new #[Title('Certificate settings')] class extends Component
{
public int $reminder_months_before = 1;
public string $ocr_provider = 'none';
public string $ocr_api_key = '';
public bool $hasApiKey = false;
public bool $ocrAvailable = false;
public function mount(): void
{
$user = Auth::user();
$this->reminder_months_before = $user->reminder_months_before;
$this->ocr_provider = $user->ocr_provider;
$this->hasApiKey = filled($user->ocr_api_key);
$this->ocrAvailable = $user->plan()->includesOcr();
}
/**
* @return array<int, string>
*/
private function allowedOcrProviders(): array
{
return $this->ocrAvailable ? OcrManager::availableProviders() : ['none'];
}
public function updateSettings(): void
{
$validated = $this->validate([
'reminder_months_before' => ['required', 'integer', 'min:0', 'max:24'],
'ocr_provider' => ['required', Rule::in($this->allowedOcrProviders())],
'ocr_api_key' => ['nullable', 'string', 'max:255'],
]);
$user = Auth::user();
$user->reminder_months_before = $validated['reminder_months_before'];
$user->ocr_provider = $validated['ocr_provider'];
// Only overwrite the stored key when a new one is entered, so the
// masked field can be left blank to keep the existing key.
if (filled($validated['ocr_api_key'])) {
$user->ocr_api_key = $validated['ocr_api_key'];
} elseif ($validated['ocr_provider'] === 'none') {
$user->ocr_api_key = null;
}
$user->save();
$this->reset('ocr_api_key');
$this->hasApiKey = filled($user->ocr_api_key);
Flux::toast(variant: 'success', text: __('Certificate settings updated.'));
}
}; ?>
<section class="w-full">
@include('partials.settings-heading')
<x-pages::settings.layout :heading="__('Certificates')" :subheading="__('Configure expiry reminders and document scanning')">
<form wire:submit="updateSettings" class="my-6 w-full space-y-6">
<flux:input
type="number"
min="0"
max="24"
wire:model="reminder_months_before"
:label="__('Remind me this many months before expiry')"
/>
<flux:select wire:model.live="ocr_provider" :label="__('Document scanning (OCR)')" :disabled="! $ocrAvailable">
<flux:select.option value="none">{{ __('None (manual entry)') }}</flux:select.option>
@if ($ocrAvailable)
<flux:select.option value="klippa">{{ __('Klippa (EU / NL)') }}</flux:select.option>
<flux:select.option value="mistral">{{ __('Mistral Pixtral (EU / FR)') }}</flux:select.option>
@endif
</flux:select>
@unless ($ocrAvailable)
<flux:callout icon="lock-closed" variant="secondary">
<flux:callout.text>
{{ __('Document scanning is available on Certified Starter and Certified Pro.') }}
<flux:callout.link :href="route('marketing.pricing')">{{ __('Upgrade your plan') }}</flux:callout.link>
</flux:callout.text>
</flux:callout>
@endunless
@if ($ocr_provider !== 'none')
<flux:input
type="password"
wire:model="ocr_api_key"
:label="__('API key')"
:placeholder="$hasApiKey ? __('Leave blank to keep the current key') : __('Enter your API key')"
viewable
/>
@endif
<div class="flex items-center gap-4">
<flux:button type="submit" variant="primary">{{ __('Save') }}</flux:button>
</div>
</form>
</x-pages::settings.layout>
</section>