102 lines
3.0 KiB
PHP
102 lines
3.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\DocumentoCartella;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class DocumentoCartellaController extends Controller
|
||
|
|
{
|
||
|
|
public function store(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$this->authorizeWrite('documenti');
|
||
|
|
|
||
|
|
$data = $request->validate([
|
||
|
|
'nome' => 'required|string|max:255',
|
||
|
|
'parent_id' => 'nullable|integer|exists:documenti_cartelle,id',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$cartella = DocumentoCartella::create([
|
||
|
|
'nome' => $data['nome'],
|
||
|
|
'parent_id' => $data['parent_id'] ?? null,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'cartella' => $cartella,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(Request $request, int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$this->authorizeWrite('documenti');
|
||
|
|
|
||
|
|
$cartella = DocumentoCartella::findOrFail($id);
|
||
|
|
|
||
|
|
$data = $request->validate([
|
||
|
|
'nome' => 'required|string|max:255',
|
||
|
|
'parent_id' => 'nullable|integer|exists:documenti_cartelle,id',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$updateData = ['nome' => $data['nome']];
|
||
|
|
|
||
|
|
if (array_key_exists('parent_id', $data)) {
|
||
|
|
if ((int) $data['parent_id'] === $id) {
|
||
|
|
return response()->json(['success' => false, 'message' => 'Una cartella non può essere spostata dentro se stessa.'], 422);
|
||
|
|
}
|
||
|
|
$updateData['parent_id'] = $data['parent_id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
$cartella->update($updateData);
|
||
|
|
|
||
|
|
return response()->json(['success' => true]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$this->authorizeDelete('documenti');
|
||
|
|
|
||
|
|
$cartella = DocumentoCartella::findOrFail($id);
|
||
|
|
|
||
|
|
$documentCount = $cartella->documenti()->count();
|
||
|
|
if ($documentCount > 0) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => "Impossibile eliminare: {$documentCount} documento(i) presenti nella cartella. Sposta o elimina prima i documenti.",
|
||
|
|
], 409);
|
||
|
|
}
|
||
|
|
|
||
|
|
$childrenCount = $cartella->children()->count();
|
||
|
|
if ($childrenCount > 0) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => "Impossibile eliminare: {$childrenCount} sottocartella(e) presenti. Elimina prima le sottocartelle.",
|
||
|
|
], 409);
|
||
|
|
}
|
||
|
|
|
||
|
|
$cartella->delete();
|
||
|
|
|
||
|
|
return response()->json(['success' => true]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function breadcrumb(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$cartella = DocumentoCartella::with('parent')->findOrFail($id);
|
||
|
|
$ancestors = $cartella->ancestors();
|
||
|
|
|
||
|
|
$items = [];
|
||
|
|
$items[] = ['id' => null, 'nome' => 'Tutti i documenti'];
|
||
|
|
|
||
|
|
foreach ($ancestors as $anc) {
|
||
|
|
$items[] = ['id' => $anc->id, 'nome' => $anc->nome];
|
||
|
|
}
|
||
|
|
|
||
|
|
$items[] = ['id' => $cartella->id, 'nome' => $cartella->nome];
|
||
|
|
|
||
|
|
return response()->json(['items' => $items]);
|
||
|
|
}
|
||
|
|
}
|