File manager - Edit - /var/www/html/portal/app/Http/Controllers/ArquivosConselhosController.php
Back
<?php namespace App\Http\Controllers; use App\Models\Arquivo; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; use App\Models\Conselho; use Illuminate\Http\Request; use App\Http\Requests\ArquivosConselhosRequest; use Spatie\PdfToText\Pdf; use App\Helpers\FileUploadHelper; class ArquivosConselhosController extends Controller { public function __construct() { $this->middleware('conselhos'); } public function index(Request $request, Conselho $conselho) { $paginate = 20; $arquivosQuery = $conselho->arquivos() ->orderBy('tipo', 'asc') ->orderBy('evento_at', 'desc'); if ($request->filled('q')) { $request->validate([ 'q' => 'nullable|string|max:255', ]); $q = trim($request->q); $dataConvertida = $this->converterDataParaAmericano($q); $arquivosQuery->where(function ($query) use ($q, $dataConvertida) { $query->where('nome', 'like', "%{$q}%") ->orWhere('tipo', 'like', "%{$q}%"); if ($dataConvertida) { $query->orWhere('evento_at', 'like', "%{$dataConvertida}%"); } }); } $arquivos = $arquivosQuery->paginate($paginate); return view("admin.conselhos.arquivos.index", [ "conselho" => $conselho, "arquivos" => $arquivos ]); } public function create(Conselho $conselho) { return view("admin.conselhos.arquivos.create", [ "conselho" => $conselho ]); } public function store(ArquivosConselhosRequest $request, Conselho $conselho) { $path = "pmar/assets/img/icons/neutro.png"; if ($request->hasFile('link') && $request->file('link')->isValid()) { $file = $request->file('link'); $uploadedPath = FileUploadHelper::processSafeImageUpload($file, 'arquivos'); if ($uploadedPath) { $path = $uploadedPath; } } try { DB::beginTransaction(); Arquivo::create([ 'user_id' => Auth::user()->id, 'owner_id' => $conselho->id, 'tipo' => $request->tipo, 'nome' => $request->nome, 'link' => $path, 'evento_at' => $request->evento_at, 'status' => $request->status, ]); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/conselhos/{$conselho->id}/arquivos?page={$page}")->with("success", __("* Arquivo criado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar criar um arquivo. ' . $e->getMessage()); } } public function show(Arquivo $arquivo) { } public function edit(Conselho $conselho, Arquivo $arquivo) { return view("admin.conselhos.arquivos.edit", [ "conselho" => $conselho, "arquivo" => $arquivo ]); } public function update(ArquivosConselhosRequest $request, Conselho $conselho, Arquivo $arquivo) { $array_arquivo = [ 'tipo' => $request->tipo, 'nome' => $request->nome, 'evento_at' => $request->evento_at, 'status' => $request->status, ]; $ds = DIRECTORY_SEPARATOR; $path = $arquivo->link; if ($request->hasFile('link') && $request->file('link')->isValid()) { $file = $request->file('link'); $uploadedPath = FileUploadHelper::processSafeImageUpload($file, 'arquivos'); if ($uploadedPath) { $path = $uploadedPath; } $search = "pmar{$ds}assets{$ds}img"; $file_exist = storage_path() . str_replace($search, "{$ds}app{$ds}public", $arquivo->link); if (@file_exists($file_exist)) { unlink($file_exist); } } $array_arquivo["link"] = $path; try { DB::beginTransaction(); $arquivo->update($array_arquivo); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/conselhos/{$conselho->id}/arquivos?page={$page}")->with("success", __("* O arquivo foi atualizado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro tentar atualizar este arquivo. ' . $e->getMessage()); } } public function destroy(Request $request, Conselho $conselho, Arquivo $arquivo) { if (!in_array("DELETE", explode(",", Auth::user()->papers))) { return redirect() ->back() ->withInput() ->with('error', 'Permissão negada! Entre em contato com o administrador do sistema.'); } $ds = DIRECTORY_SEPARATOR; if ($arquivo->link != "pmar/assets/img/icons/neutro.png") { $search = "pmar{$ds}assets{$ds}img"; $file_exist = storage_path() . str_replace($search, "{$ds}app{$ds}public", $arquivo->link); if (@file_exists($file_exist)) { unlink($file_exist); } } try { DB::beginTransaction(); Arquivo::destroy($arquivo->id); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/conselhos/{$conselho->id}/arquivos?page={$page}")->with("success", __("* O arquivo {$arquivo->nome} foi excluído com sucesso! Esta ação não pode ser desfeita!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro tentar excluir este arquivo. ' . $e->getMessage()); } } public function status(Request $request, Conselho $conselho, Arquivo $arquivo) { try { DB::beginTransaction(); $arquivo->fill([ 'status' => $request->checkStatus ]); $arquivo->save(); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/conselhos/{$conselho->id}/arquivos?page={$page}")->with("success", __("* O status deste arquivo foi alterado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar alterar o status deste arquivo. ' . $e->getMessage()); } } public function indexacao(Request $request, Conselho $conselho, Arquivo $arquivo) { $ds = DIRECTORY_SEPARATOR; $search = "pmar{$ds}assets{$ds}img"; $file_exist = storage_path() . str_replace($search, "{$ds}app{$ds}public", $arquivo->link); $text = ""; if (file_exists($file_exist)) { try { $text = Pdf::getText($file_exist); } catch (\Exception $e) { abort(500, $e->getMessage()); return dd($e->getMessage()); } } try { DB::beginTransaction(); $arquivo->fill(['descricao' => $text, 'indexado' => 1,]); $arquivo->save(); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/conselhos/{$conselho->id}/arquivos?page={$page}")->with("success", __("* Boletim indexado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro tentar indexar as informações deste boletim. ' . $e->getMessage()); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.09 |
proxy
|
phpinfo
|
Settings