Loheide-eu-mitarbeiterplatt.../plugins-core/qm/src/Library.php
Friederich Loheide 4d855c8a74
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m29s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m38s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m40s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m7s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m24s
QM: Wiedervorlage und Lesebestätigung, Platzhalter für Profilbilder (0.21.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 07:36:47 +00:00

133 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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->fill([...$this->attributes([...$data, 'type' => $entry->type]), 'updated_by' => $user->id]);
// Neuer Prüftermin oder andere Verantwortliche → erneut erinnern
if ($entry->isDirty(['review_due_at', 'responsible_id'])) {
$entry->review_notified_at = null;
}
$entry->save();
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,
'review_due_at' => filled($data['review_due_at'] ?? null) ? Carbon::parse($data['review_due_at'])->toDateString() : null,
'review_interval_months' => filled($data['review_interval_months'] ?? null) ? (int) $data['review_interval_months'] : null,
'responsible_id' => filled($data['responsible_id'] ?? null) ? (int) $data['responsible_id'] : null,
'requires_acknowledgement' => (bool) ($data['requires_acknowledgement'] ?? false),
];
}
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);
}
}
}