File manager - Edit - /var/www/html/portal/app/Http/Controllers/Accounts/LinhasDoTempo/LinhasDoTempoController.php
Back
<?php namespace App\Http\Controllers\Accounts\LinhasDoTempo; use App\Enums\Accounts\LinhasDoTempoAttachmentEntity; use App\Enums\Accounts\LinhasDoTempoDraftFormType; use App\Exports\LinhasDoTempoPontosExport; use App\Http\Controllers\Controller; use App\Http\Requests\Accounts\LinhasDoTempo\LinhasDoTempoRequest; use App\Http\Resources\Accounts\LinhasDoTempo\LinhasDoTempoResource; use App\Http\Services\Accounts\LinhasDoTempo\LinhasDoTempoService; use App\Models\Accounts\LinhasDoTempo\Attachment; use App\Models\Accounts\LinhasDoTempo\HistoricalPoint; use App\Models\Accounts\LinhasDoTempo\Note; use App\Models\Accounts\LinhasDoTempo\Timeline; use Barryvdh\DomPDF\Facade\Pdf; use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use Illuminate\View\View; use Maatwebsite\Excel\Facades\Excel; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\StreamedResponse; /** * Orquestração do módulo Linhas do Tempo (Conta-Angra): telas web e endpoints JSON. */ class LinhasDoTempoController extends Controller { private string $permissao = 'GERENCIADOR_DE_LINHAS_DO_TEMPO'; public function __construct( private readonly LinhasDoTempoService $service, private readonly LinhasDoTempoResource $resource, ) {} public function index(Request $request): View|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'visualizar'); if ($gate instanceof RedirectResponse) { return $gate; } $user = Auth::user(); $timelines = $this->service->paginateIndex($request, $user); $viewMode = $request->get('view', 'timeline'); return view('admin.contaAngra.linhasDoTempo.index', compact('timelines', 'viewMode')); } public function create(Request $request): View|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return $gate; } return view('admin.contaAngra.linhasDoTempo.create'); } public function store(LinhasDoTempoRequest $request): RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return $gate; } $data = $request->validated(); $files = $request->file('attachments', []) ?: []; $timeline = $this->service->createTimeline(Auth::user(), $data, $files); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::CreateTimeline->value, null, null); return redirect() ->route('admin.contaAngra.linhasDoTempo.show', $timeline) ->with('success', __('Linha do tempo criada com sucesso.')); } public function show(Request $request, Timeline $timeline): View|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'visualizar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineVisible($timeline, Auth::user()); $timeline->load([ 'historicalPoints' => function ($q) { $q->whereNull('deleted_at') ->orderBy('event_date') ->orderBy('created_at') ->with(['activeNotes.author']); }, ]); $pointIds = $timeline->historicalPoints->pluck('id'); $attachmentsByEntity = Attachment::query() ->whereNull('deleted_at') ->where(function ($q) use ($timeline, $pointIds) { $q->where(function ($q2) use ($timeline) { $q2->where('entity_type', LinhasDoTempoAttachmentEntity::Timeline->value) ->where('entity_id', $timeline->id); }); if ($pointIds->isNotEmpty()) { $q->orWhere(function ($q2) use ($pointIds) { $q2->where('entity_type', LinhasDoTempoAttachmentEntity::HistoricalPoint->value) ->whereIn('entity_id', $pointIds); }); } }) ->orderBy('created_at') ->get() ->groupBy(fn ($a) => $a->entity_type . ':' . $a->entity_id); $pointsData = $timeline->historicalPoints->map(static function (HistoricalPoint $p) { return [ 'id' => $p->id, 'name' => $p->name, 'event_date' => $p->event_date?->toIso8601String(), 'process_text' => $p->process_text, ]; })->values(); return view('admin.contaAngra.linhasDoTempo.show', compact('timeline', 'attachmentsByEntity', 'pointsData')); } public function edit(Timeline $timeline): View|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $timeline->load(['historicalPoints' => fn ($q) => $q->whereNull('deleted_at')->orderBy('event_date')]); return view('admin.contaAngra.linhasDoTempo.edit', compact('timeline')); } public function update(LinhasDoTempoRequest $request, Timeline $timeline): RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $data = $request->validated(); $files = $request->file('attachments', []) ?: []; $remove = $request->input('remove_attachment_ids', []) ?: []; $this->service->updateTimeline($timeline, Auth::user(), $data, $files, $remove); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::EditTimeline->value, $timeline->id, $timeline->id); return redirect() ->route('admin.contaAngra.linhasDoTempo.show', $timeline) ->with('success', __('Linha do tempo atualizada com sucesso.')); } public function destroy(Request $request, Timeline $timeline): RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'excluir'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); if (trim((string) $request->input('confirm_name')) !== trim((string) $timeline->name)) { return redirect() ->back() ->with('error', __('Confirmação inválida: digite o nome exato da linha do tempo.')) ->withInput(); } $this->service->softDeleteTimeline($timeline, Auth::user()); $query = array_filter($request->only(['q', 'status', 'date_from', 'date_to', 'tag', 'sort', 'dir', 'view']), fn ($v) => $v !== null && $v !== ''); return redirect() ->route('admin.contaAngra.linhasDoTempo.index', $query) ->with('success', __('Linha do tempo excluída.')); } public function arquivar(Timeline $timeline): RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->archiveTimeline($timeline, Auth::user()); return redirect()->back()->with('success', __('Linha do tempo arquivada.')); } public function exportPdf(Timeline $timeline): mixed { $gate = $this->checkPermissao($this->permissao, 'visualizar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineVisible($timeline, Auth::user()); $timeline->load([ 'historicalPoints' => fn ($q) => $q->whereNull('deleted_at')->orderBy('event_date'), 'historicalPoints.activeNotes', ]); $pdf = Pdf::loadView('admin.contaAngra.linhasDoTempo.exports.pdf', ['timeline' => $timeline]); return $pdf->download('linha-do-tempo-' . $timeline->id . '.pdf'); } public function exportExcel(Timeline $timeline): BinaryFileResponse|RedirectResponse|StreamedResponse { $gate = $this->checkPermissao($this->permissao, 'visualizar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineVisible($timeline, Auth::user()); $points = $timeline->historicalPoints()->whereNull('deleted_at')->orderBy('event_date')->get(); return Excel::download(new LinhasDoTempoPontosExport($timeline, $points), 'linha-do-tempo-' . $timeline->id . '.xlsx'); } public function draftUpsertNewTimeline(LinhasDoTempoRequest $request): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $data = $request->validated(); $draft = $this->service->upsertDraft(Auth::user(), $data['form_type'], null, null, $data['form_data']); return response()->json([ 'saved_at' => $draft->updated_at?->toIso8601String(), ]); } public function draftShowNewTimeline(): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $draft = $this->service->findDraft(Auth::user(), LinhasDoTempoDraftFormType::CreateTimeline->value, null, null); return response()->json([ 'draft' => $draft ? ['form_data' => $draft->form_data, 'updated_at' => $draft->updated_at?->toIso8601String()] : null, ]); } public function draftDestroyNewTimeline(): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::CreateTimeline->value, null, null); return response()->json(['ok' => true]); } public function draftUpsertNewPoint(LinhasDoTempoRequest $request, Timeline $timeline): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->assertTimelineWritable($timeline, Auth::user()); $data = $request->validated(); $draft = $this->service->upsertDraft(Auth::user(), $data['form_type'], null, $timeline->id, $data['form_data']); return response()->json(['saved_at' => $draft->updated_at?->toIso8601String()]); } public function draftShowNewPoint(Timeline $timeline): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->assertTimelineWritable($timeline, Auth::user()); $draft = $this->service->findDraft(Auth::user(), LinhasDoTempoDraftFormType::CreatePoint->value, null, $timeline->id); return response()->json([ 'draft' => $draft ? ['form_data' => $draft->form_data, 'updated_at' => $draft->updated_at?->toIso8601String()] : null, ]); } public function draftDestroyNewPoint(Timeline $timeline): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::CreatePoint->value, null, $timeline->id); return response()->json(['ok' => true]); } public function draftUpsert(LinhasDoTempoRequest $request, Timeline $timeline): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->assertTimelineWritable($timeline, Auth::user()); $data = $request->validated(); $draft = $this->service->upsertDraft(Auth::user(), $data['form_type'], $timeline->id, $timeline->id, $data['form_data']); return response()->json(['saved_at' => $draft->updated_at?->toIso8601String()]); } public function draftShow(Timeline $timeline): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->assertTimelineWritable($timeline, Auth::user()); $draft = $this->service->findDraft(Auth::user(), LinhasDoTempoDraftFormType::EditTimeline->value, $timeline->id, $timeline->id); return response()->json([ 'draft' => $draft ? ['form_data' => $draft->form_data, 'updated_at' => $draft->updated_at?->toIso8601String()] : null, ]); } public function draftDestroy(Timeline $timeline): JsonResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return response()->json(['message' => __('Permissão negada.')], 403); } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::EditTimeline->value, $timeline->id, $timeline->id); return response()->json(['ok' => true]); } public function attachmentStoreTimeline(LinhasDoTempoRequest $request, Timeline $timeline): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $files = $request->file('attachments', []) ?: []; $this->service->appendTimelineAttachments($timeline, Auth::user(), $files); return response()->json(['ok' => true]); } public function attachmentDownload(Timeline $timeline, Attachment $attachment): RedirectResponse|StreamedResponse { $gate = $this->checkPermissao($this->permissao, 'visualizar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertAttachmentDownloadable($timeline, $attachment, Auth::user()); $disk = config('filesystems.default', 'local'); if (! Storage::disk($disk)->exists($attachment->storage_key)) { abort(404); } return Storage::disk($disk)->download($attachment->storage_key, $attachment->original_name); } public function pointStore(LinhasDoTempoRequest $request, Timeline $timeline): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $data = $request->validated(); $files = $request->file('attachments', []) ?: []; $point = $this->service->createHistoricalPoint($timeline, Auth::user(), $data, $files); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::CreatePoint->value, null, $timeline->id); return response()->json([ 'point' => [ 'id' => $point->id, 'name' => $point->name, 'event_date' => $point->event_date->format('c'), 'process_html' => $point->process_text, 'anchor' => 'ponto-' . $point->id, ], 'message' => __('Ponto histórico adicionado.'), ]); } public function pointUpdate(LinhasDoTempoRequest $request, Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $data = $request->validated(); $files = $request->file('attachments', []) ?: []; $remove = $request->input('remove_attachment_ids', []) ?: []; $point = $this->service->updateHistoricalPoint($historicalPoint, Auth::user(), $data, $files, $remove); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::EditPoint->value, $historicalPoint->id, $timeline->id); return response()->json([ 'point' => [ 'id' => $point->id, 'name' => $point->name, 'event_date' => $point->event_date->format('c'), 'process_html' => $point->process_text, 'anchor' => 'ponto-' . $point->id, ], 'message' => __('Ponto histórico atualizado.'), ]); } public function pointDestroy(Request $request, Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'excluir'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $this->service->softDeleteHistoricalPoint($historicalPoint, Auth::user()); return response()->json(['ok' => true, 'message' => __('Ponto histórico excluído.')]); } public function draftUpsertPoint(LinhasDoTempoRequest $request, Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $data = $request->validated(); $draft = $this->service->upsertDraft(Auth::user(), $data['form_type'], $historicalPoint->id, $timeline->id, $data['form_data']); return response()->json(['saved_at' => $draft->updated_at?->toIso8601String()]); } public function draftShowPoint(Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $draft = $this->service->findDraft(Auth::user(), LinhasDoTempoDraftFormType::EditPoint->value, $historicalPoint->id, $timeline->id); return response()->json([ 'draft' => $draft ? ['form_data' => $draft->form_data, 'updated_at' => $draft->updated_at?->toIso8601String()] : null, ]); } public function draftDestroyPoint(Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $this->service->deleteDraft(Auth::user(), LinhasDoTempoDraftFormType::EditPoint->value, $historicalPoint->id, $timeline->id); return response()->json(['ok' => true]); } public function attachmentStorePoint(LinhasDoTempoRequest $request, Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $files = $request->file('attachments', []) ?: []; $this->service->appendPointAttachments($historicalPoint, Auth::user(), $files); return response()->json(['ok' => true]); } public function noteStore(LinhasDoTempoRequest $request, Timeline $timeline, HistoricalPoint $historicalPoint): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'adicionar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $data = $request->validated(); $note = $this->service->createNote($historicalPoint, Auth::user(), $data['content']); $note->load('author'); $canDelete = (Auth::user()?->getPermissaoDetalhes($this->permissao)['excluir'] ?? false) === true; return response()->json([ 'note' => [ 'id' => $note->id, 'content' => $note->content, 'created_at' => $note->created_at?->toIso8601String(), 'author' => $note->author?->firstName . ' ' . $note->author?->lastName, 'can_delete' => $canDelete, 'delete_url' => $canDelete ? route('admin.contaAngra.linhasDoTempo.notas.destroy', [$timeline, $historicalPoint, $note]) : null, ], 'message' => __('Nota salva.'), ]); } public function noteUpdate(LinhasDoTempoRequest $request, Timeline $timeline, HistoricalPoint $historicalPoint, Note $note): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'editar'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $this->service->assertNoteBelongsToPoint($note, $historicalPoint); $data = $request->validated(); $note = $this->service->updateNote($note, Auth::user(), $data['content']); $note->load('author'); return response()->json([ 'note' => [ 'id' => $note->id, 'content' => $note->content, 'created_at' => $note->created_at?->toIso8601String(), 'author' => $note->author?->firstName . ' ' . $note->author?->lastName, ], 'message' => __('Nota atualizada.'), ]); } public function noteDestroy(Request $request, Timeline $timeline, HistoricalPoint $historicalPoint, Note $note): JsonResponse|RedirectResponse { $gate = $this->checkPermissao($this->permissao, 'excluir'); if ($gate instanceof RedirectResponse) { return $gate; } $this->service->assertTimelineWritable($timeline, Auth::user()); $this->service->assertPointBelongsToTimeline($historicalPoint, $timeline); $this->service->assertNoteBelongsToPoint($note, $historicalPoint); $this->service->softDeleteNote($note, Auth::user()); return response()->json(['ok' => true, 'message' => __('Nota excluída.')]); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings