43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Admin\Controllers;
|
|
|
|
use App\Models\DocumentAsset;
|
|
use App\Models\DocumentTemplate;
|
|
use App\Scopes\CommonController;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class DocumentAssetDeleteController extends CommonController
|
|
{
|
|
public function __invoke(string $name): JsonResponse
|
|
{
|
|
$asset = DocumentAsset::where('name', $name)->first();
|
|
|
|
// Durchweg 200 mit Status im Body -- wie die übrigen Admin-Endpunkte, damit der Grund im Frontend
|
|
// ankommt (der Ajax-Helfer verwirft den Body bei Fehler-Statuscodes).
|
|
if ($asset === null) {
|
|
return response()->json(['status' => 'error', 'message' => 'Das Bild existiert nicht.']);
|
|
}
|
|
|
|
// Ein noch referenziertes Bild zu löschen würde stillschweigend eine Lücke in jede künftige
|
|
// Rechnung reißen -- der Platzhalter löst dann zu einem Leerstring auf.
|
|
if ($this->isReferenced($asset->name)) {
|
|
return response()->json([
|
|
'status' => 'error',
|
|
'message' => sprintf('Das Bild wird noch als {asset:%s} in der Vorlage verwendet.', $asset->name),
|
|
]);
|
|
}
|
|
|
|
$asset->delete();
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Das Bild wurde gelöscht.']);
|
|
}
|
|
|
|
private function isReferenced(string $name): bool
|
|
{
|
|
return DocumentTemplate::query()
|
|
->where('content', 'like', '%{asset:' . $name . '}%')
|
|
->exists();
|
|
}
|
|
}
|