File manager - Edit - /var/www/html/portalHomolog/app/Http/Controllers/LicitacaoProcedimentoController.php
Back
<?php namespace App\Http\Controllers; use App\Enums\StatusPermission; use App\Http\Requests\LicitacaoProcedimentoRequest; use App\Http\Resources\LicitacaoProcedimentoResource; use App\Http\Services\LicitacaoProcedimentoService; use App\Models\Comunicacao\Boletim; use App\Models\LicitacoesProcedimentos\Processo; use App\Models\LicitacoesProcedimentos\ProcessoUpload; use App\Models\Structure; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Illuminate\View\View; class LicitacaoProcedimentoController extends Controller { public function __construct( private readonly LicitacaoProcedimentoService $service, private readonly LicitacaoProcedimentoResource $resource ) { $this->middleware('licitacoesProcedimentos')->except(['boletinsApiData']); } public function index(Request $request): View|RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'visualizar'); if ($response instanceof RedirectResponse) { return $response; } $registros = $this->service->listWithFilters($request); return view('admin.licitacoesProcedimentos.index', [ 'registros' => $registros, ]); } public function create(): View|RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'adicionar'); if ($response instanceof RedirectResponse) { return $response; } return view('admin.licitacoesProcedimentos.create', [ 'secretarias' => $this->getSecretariasResponsaveis(), ]); } public function store(LicitacaoProcedimentoRequest $request): RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'adicionar'); if ($response instanceof RedirectResponse) { return $response; } $request->validated(); $request->merge(['id_usuario' => (string) auth()->id()]); return $this->resource->store($request); } public function edit(Processo $licitacaoProcedimento): View|RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } $licitacaoProcedimento->load('uploads'); return view('admin.licitacoesProcedimentos.edit', [ 'registro' => $licitacaoProcedimento, 'secretarias' => $this->getSecretariasResponsaveis(), ]); } public function update(LicitacaoProcedimentoRequest $request, Processo $licitacaoProcedimento): RedirectResponse { $response = $this->checkPermissao( StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar' ); if ($response instanceof RedirectResponse) { return $response; } $data = $request->validated(); $data['id_usuario'] = auth()->id(); $this->service->update($licitacaoProcedimento, $data); return redirect() ->route('admin.licitacoesProcedimentos.index', $request->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) ->with('success', __('* Registro atualizado com sucesso!')); } public function destroy(Request $request, Processo $licitacaoProcedimento): RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'excluir'); if ($response instanceof RedirectResponse) { return $response; } return $this->resource->destroy($request, $licitacaoProcedimento); } public function destroyUpload( Request $request, Processo $licitacaoProcedimento, ProcessoUpload $upload ): RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } return $this->resource->destroyUpload($request, $licitacaoProcedimento, $upload); } public function uploads(Processo $licitacaoProcedimento): View|RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } $licitacaoProcedimento->load('uploads'); return view('admin.licitacoesProcedimentos.uploads', [ 'registro' => $licitacaoProcedimento, ]); } public function updateUpload( Request $request, Processo $licitacaoProcedimento, ProcessoUpload $upload ): RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } if ((int) $upload->licitacao_procedimento_id !== (int) $licitacaoProcedimento->id) { return redirect()->back()->with('error', __('Upload inválido para este registro.')); } $request->validate([ 'ds_legenda' => ['nullable', 'string', 'max:100'], 'arquivo' => ['nullable', 'file', 'mimes:pdf,mp4', 'max:71680'], ]); if ($request->hasFile('arquivo') && $request->file('arquivo')->isValid()) { $this->service->removeUpload($upload); $request->merge([ 'uploads' => [$request->file('arquivo')], 'uploads_legendas' => [$request->input('ds_legenda')], ]); $this->service->storeUploads($licitacaoProcedimento, $request); return redirect() ->route('admin.licitacoesProcedimentos.uploads', [$licitacaoProcedimento->id] + $request->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) ->with('success', __('* Upload atualizado com sucesso!')); } $upload->update([ 'ds_legenda' => $request->input('ds_legenda'), ]); return redirect() ->route('admin.licitacoesProcedimentos.uploads', [$licitacaoProcedimento->id] + $request->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) ->with('success', __('* Upload atualizado com sucesso!')); } public function storeUpload(Request $request, Processo $licitacaoProcedimento): RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } $request->validate([ 'uploads' => ['required', 'array'], 'uploads.*' => ['required', 'file', 'mimes:pdf,mp4', 'max:71680'], 'uploads_legendas' => ['nullable', 'array'], 'uploads_legendas.*' => ['nullable', 'string', 'max:100'], ]); $this->service->storeUploads($licitacaoProcedimento, $request); return redirect() ->route('admin.licitacoesProcedimentos.uploads', [$licitacaoProcedimento->id] + $request->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) ->with('success', __('* Upload adicionado com sucesso!')); } public function homologacao(Processo $licitacaoProcedimento): View|RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } return view('admin.licitacoesProcedimentos.homologacao', [ 'registro' => $licitacaoProcedimento, ]); } public function updateHomologacao(Request $request, Processo $licitacaoProcedimento): RedirectResponse { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'editar'); if ($response instanceof RedirectResponse) { return $response; } $validated = $request->validate([ 'favorecidos' => ['nullable', 'array'], 'favorecidos.*.ds_favorecido' => ['nullable', 'string'], 'favorecidos.*.dt_homologacao' => ['nullable', 'date'], 'favorecidos.*.boletins' => ['nullable', 'array', 'max:3'], 'favorecidos.*.boletins.*.ds_bo' => ['nullable', 'string', 'max:50'], 'favorecidos.*.boletins.*.ds_urlbo' => ['nullable', 'string', 'max:250'], ]); $favorecidos = collect($validated['favorecidos'] ?? []) ->map(function ($favorecido) { $boletins = collect($favorecido['boletins'] ?? []) ->map(function ($boletim) { return [ 'ds_bo' => trim((string) ($boletim['ds_bo'] ?? '')), 'ds_urlbo' => trim((string) ($boletim['ds_urlbo'] ?? '')), ]; }) ->filter(function ($boletim) { return ! empty($boletim['ds_bo']); }) ->take(3) ->values() ->all(); return [ 'ds_favorecido' => trim((string) ($favorecido['ds_favorecido'] ?? '')), 'dt_homologacao' => ! empty($favorecido['dt_homologacao']) ? (string) $favorecido['dt_homologacao'] : null, 'boletins' => $boletins, ]; }) ->filter(function ($favorecido) { return ! empty($favorecido['ds_favorecido']) || ! empty($favorecido['dt_homologacao']) || ! empty($favorecido['boletins']); }) ->values() ->all(); $primeiroFavorecido = $favorecidos[0] ?? null; $primeiroBoletim = $primeiroFavorecido['boletins'][0] ?? null; $payload = [ 'ds_favorecido' => collect($favorecidos) ->pluck('ds_favorecido') ->filter() ->implode(' | '), 'dt_homologacao' => $primeiroFavorecido['dt_homologacao'] ?? null, 'ds_bo' => $primeiroBoletim['ds_bo'] ?? null, 'ds_urlbo' => $primeiroBoletim['ds_urlbo'] ?? null, 'boletins' => $favorecidos, 'id_usuario' => (string) auth()->id(), ]; DB::transaction(function () use ($licitacaoProcedimento, $payload) { $this->service->update($licitacaoProcedimento, $payload); }); return redirect() ->route('admin.licitacoesProcedimentos.index', $request->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) ->with('success', __('* Homologação atualizada com sucesso!')); } public function downloadUpload( Processo $licitacaoProcedimento, ProcessoUpload $upload ) { $response = $this->checkPermissao(StatusPermission::GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS, 'visualizar'); if ($response instanceof RedirectResponse) { return $response; } if ((int) $upload->licitacao_procedimento_id !== (int) $licitacaoProcedimento->id) { return redirect()->back()->with('error', __('Upload inválido para este registro.')); } $path = trim((string) $upload->ds_arquivo); $path = str_replace('\\', '/', $path); $urlPath = parse_url($path, PHP_URL_PATH); if (is_string($urlPath) && $urlPath !== '') { $path = $urlPath; } $path = ltrim(rawurldecode($path), '/'); $candidates = []; $candidates[] = $path; if (str_starts_with($path, 'pmar/assets/img/')) { $candidates[] = substr($path, strlen('pmar/assets/img/')); } if (str_starts_with($path, LicitacaoProcedimentoService::UPLOAD_PUBLIC_PREFIX.'/')) { $fileName = basename($path); $candidates[] = 'licitacoes_procedimentos/'.$fileName; $candidates[] = $fileName; } $candidates[] = basename($path); $candidates = array_values(array_unique(array_filter($candidates))); foreach ($candidates as $relative) { if (! Storage::disk('public')->exists($relative)) { continue; } $this->service->incrementClick($upload); return Storage::disk('public')->download($relative, basename($relative)); } $fallbackDir = LicitacaoProcedimentoService::physicalUploadStorageDir(); foreach ($candidates as $relative) { $fallback = $fallbackDir.DIRECTORY_SEPARATOR.str_replace('/', DIRECTORY_SEPARATOR, $relative); if (! is_file($fallback)) { continue; } $this->service->incrementClick($upload); return response()->download($fallback, basename($fallback)); } $publicUrl = LicitacaoProcedimentoService::resolveUploadPublicUrl($upload->ds_arquivo); if (! empty($publicUrl)) { $this->service->incrementClick($upload); return redirect()->to($publicUrl); } return redirect()->back()->with('error', __('Arquivo não encontrado.')); } public function boletinsApiData() { $boletins = Boletim::query() ->select(['id', 'number', 'url', 'publicationDate', 'title', 'fileName']) ->whereNotNull('number') ->orderByDesc('publicationDate') ->get() ->map(function (Boletim $item) { $date = null; if (! empty($item->url) && preg_match('/(\d{2})-(\d{2})-(\d{4})/', (string) $item->url, $matches)) { $date = $matches[0]; } $label = $date ? 'BO-'.$item->number.'_de_'.$date.'.pdf' : 'BO-'.$item->number.'.pdf'; $pub = $item->publicationDate; $publicationDateDisplay = $pub ? $pub->format('d/m/Y') : ''; return [ 'id' => $item->id, 'number' => (string) $item->number, 'url' => (string) ($item->url ?? ''), 'label' => $label, 'title' => (string) ($item->title ?? ''), 'publicationDateDisplay' => $publicationDateDisplay, 'pdfUrl' => $item->public_bo_pdf_url, ]; }); return response()->json($boletins); } private function getSecretariasResponsaveis() { return Structure::query() ->select(['id', 'name', 'abbreviation', 'type', 'status']) ->whereIn('type', Processo::getStructureSetorTypes()) ->orderBy('name') ->get(); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings