All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m46s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m51s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m55s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 4m15s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m46s
CI / Build-Artefakte aktuell (pull_request) Successful in 52s
- Kategorien mit farbigen Ordnerkacheln; Farbe und Symbol je Ordner (Auswahl oder eigener Upload, SVG bereinigt), an Unterordner vererbt - Ordnerseite: Einführungstext, Ansprechpartner (Profilfelder wählbar), Unterordner, Liste mit Dokument, Version, Stand, Größe, Aktion - Einträge: Datei (Lesefassung + bearbeitbare Vorlage), Link, Online-Formular; neue Fassungen archivieren die bisherigen - PDF-Betrachter im Portal (pdf.js) - Eigene Volltextsuche (PDF, Word, Excel, PowerPoint, OpenDocument) mit Textausschnitt; optionale Texterkennung per Tesseract - Rollen-Ausschluss für das ganze Modul, Pflegerecht qm.manage, Zielgruppen je Kategorie - Vorbereitet: Wiedervorlage, Verantwortliche, Lesebestätigungen - Abhängigkeit smalot/pdfparser Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
122 lines
4.8 KiB
PHP
122 lines
4.8 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Qm;
|
||
|
||
use App\Models\User;
|
||
use Illuminate\Http\UploadedFile;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Str;
|
||
use Inhaus\Plugins\Qm\Models\QmEntry;
|
||
use Inhaus\Plugins\Qm\Models\QmFile;
|
||
use Inhaus\Plugins\Qm\Models\QmFolder;
|
||
|
||
/**
|
||
* Einträge anlegen, ändern und neue Fassungen hochladen. Eine neue Fassung
|
||
* schiebt die bisherigen Dateien ins Archiv – nichts geht verloren.
|
||
*/
|
||
class Library
|
||
{
|
||
/** Erlaubte Dateitypen (Lesefassung vor allem PDF, Vorlagen Office). */
|
||
public const EXTENSIONS = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp', 'rtf', 'txt', 'csv', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'zip'];
|
||
|
||
public const MAX_KB = 102400;
|
||
|
||
public function __construct(private readonly Indexer $indexer) {}
|
||
|
||
/**
|
||
* @param array{type: string, title: string, description?: ?string, version?: ?string, valid_from?: ?string, url?: ?string, form_id?: ?int} $data
|
||
*/
|
||
public function create(QmFolder $folder, array $data, ?UploadedFile $reading, ?UploadedFile $editable, User $user): QmEntry
|
||
{
|
||
return DB::transaction(function () use ($folder, $data, $reading, $editable, $user) {
|
||
$entry = $folder->entries()->create([
|
||
...$this->attributes($data),
|
||
'created_by' => $user->id,
|
||
'updated_by' => $user->id,
|
||
]);
|
||
|
||
if ($entry->type === QmEntry::FILE) {
|
||
$this->storeFiles($entry, $reading, $editable, $user);
|
||
}
|
||
|
||
return $entry;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Angaben ändern; mit Datei(en) = neue Fassung (bisherige ins Archiv).
|
||
*
|
||
* @param array<string, mixed> $data
|
||
*/
|
||
public function update(QmEntry $entry, array $data, ?UploadedFile $reading, ?UploadedFile $editable, bool $removeEditable, User $user): QmEntry
|
||
{
|
||
return DB::transaction(function () use ($entry, $data, $reading, $editable, $removeEditable, $user) {
|
||
$entry->update([...$this->attributes([...$data, 'type' => $entry->type]), 'updated_by' => $user->id]);
|
||
|
||
if ($entry->type !== QmEntry::FILE) {
|
||
return $entry;
|
||
}
|
||
|
||
if ($reading || $editable) {
|
||
$this->storeFiles($entry, $reading, $editable, $user);
|
||
} else {
|
||
// Nur Angaben korrigiert (z. B. Tippfehler in der Version): aktuelle Dateien anpassen.
|
||
$entry->files()->whereNull('archived_at')->update(['version' => $entry->version, 'valid_from' => $entry->valid_from]);
|
||
}
|
||
|
||
if ($removeEditable && ! $editable) {
|
||
$entry->files()->where('kind', QmFile::EDITABLE)->whereNull('archived_at')->update(['archived_at' => now()]);
|
||
}
|
||
|
||
return $entry->refresh();
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function attributes(array $data): array
|
||
{
|
||
$type = $data['type'];
|
||
|
||
return [
|
||
'type' => $type,
|
||
'title' => trim((string) $data['title']),
|
||
'description' => filled($data['description'] ?? null) ? trim((string) $data['description']) : null,
|
||
'version' => filled($data['version'] ?? null) ? trim((string) $data['version']) : null,
|
||
'valid_from' => filled($data['valid_from'] ?? null) ? Carbon::parse($data['valid_from'])->toDateString() : now()->toDateString(),
|
||
'url' => $type === QmEntry::LINK ? trim((string) $data['url']) : null,
|
||
'form_id' => $type === QmEntry::FORM ? (int) $data['form_id'] : null,
|
||
];
|
||
}
|
||
|
||
private function storeFiles(QmEntry $entry, ?UploadedFile $reading, ?UploadedFile $editable, User $user): void
|
||
{
|
||
foreach ([QmFile::READING => $reading, QmFile::EDITABLE => $editable] as $kind => $upload) {
|
||
if (! $upload) {
|
||
continue;
|
||
}
|
||
|
||
$entry->files()->where('kind', $kind)->whereNull('archived_at')->update(['archived_at' => now()]);
|
||
|
||
$name = Str::limit(preg_replace('/[^\pL\pN ._()\-]+/u', '_', $upload->getClientOriginalName()) ?: 'datei', 200, '');
|
||
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||
|
||
$file = $entry->files()->create([
|
||
'kind' => $kind,
|
||
'version' => $entry->version,
|
||
'valid_from' => $entry->valid_from,
|
||
'name' => $name,
|
||
'path' => $upload->storeAs('qm/'.$entry->id, Str::random(40).($extension ? '.'.$extension : ''), QmFile::DISK),
|
||
'mime' => $upload->getMimeType(),
|
||
'size' => $upload->getSize(),
|
||
'uploaded_by' => $user->id,
|
||
'created_at' => now(),
|
||
]);
|
||
|
||
$this->indexer->index($file);
|
||
}
|
||
}
|
||
}
|