File manager - Edit - /var/www/html/portal/app/Http/Controllers/PontoFocalController.php
Back
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; use App\Models\PontoFocal; use App\Models\Structure; use Illuminate\Http\RedirectResponse; class PontoFocalController extends Controller { private $permissao = "GERENCIADOR_DE_PONTOS_FOCAIS"; public function __construct() { $this->middleware('auth'); } public function index(Request $request) { $response = $this->checkPermissao($this->permissao, "visualizar"); if ($response instanceof RedirectResponse) { return $response; } $paginate = 20; if (isset($request->q)) { $request->validate([ 'q' => 'nullable|string|max:255', ]); $pontosFocais = PontoFocal::select('pontos_focais.*') ->join('structures', 'structures.id', '=', 'pontos_focais.structure_id') ->where(function ($query) use ($request) { $q = trim($request->q); $query->where('pontos_focais.nome', 'like', "%{$q}%") ->orWhere('pontos_focais.matricula', 'like', "%{$q}%") ->orWhere('pontos_focais.telefone_ramal', 'like', "%{$q}%") ->orWhere('pontos_focais.email', 'like', "%{$q}%") ->orWhere('structures.name', 'like', "%{$q}%"); }) ->orderByDesc('pontos_focais.created_at') ->paginate($paginate); } else { $pontosFocais = PontoFocal::with('instituicao')->orderBy("created_at", "desc")->paginate($paginate); } return view("admin.pontosFocais.index", [ "pontosFocais" => $pontosFocais, ]); } public function create() { $response = $this->checkPermissao($this->permissao, "adicionar"); if ($response instanceof RedirectResponse) { return $response; } // Buscar todas as structures ativas, agrupadas por tipo $gabinetes = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->whereIn("type", ['GABINETE_PREFIETURA', 'GABINETE_VICE_PREFIETURA']) ->orderBy('name', 'asc') ->get(); $secretarias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "SECRETARIA") ->orderBy('name', 'asc') ->get(); $executivas = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "SECRETARIA_EXECUTIVA") ->orderBy('name', 'asc') ->get(); $autarquias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->whereIn("type", ['AUTARQUIA', 'FUNDACAO']) ->orderBy('name', 'asc') ->get(); $superintendencias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "SUPERINTENDENCIA") ->orderBy('name', 'asc') ->get(); $diretorias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "DIRETORIA") ->orderBy('name', 'asc') ->get(); return view("admin.pontosFocais.create", [ "gabinetes" => $gabinetes, "secretarias" => $secretarias, "executivas" => $executivas, "autarquias" => $autarquias, "superintendencias" => $superintendencias, "diretorias" => $diretorias, ]); } public function store(Request $request): RedirectResponse { $response = $this->checkPermissao($this->permissao, "adicionar"); if ($response instanceof RedirectResponse) { return $response; } $request->validate([ 'structure_id' => 'required|uuid|exists:structures,id', 'nome' => 'required|string|max:255', 'matricula' => 'required|string|max:255', 'telefone_ramal' => 'nullable|string|max:255', 'email' => 'nullable|email|max:255', 'observacao' => 'nullable|string', ]); $pontoFocal_array = [ "structure_id" => $request->structure_id, "nome" => $request->nome, "matricula" => $request->matricula, "telefone_ramal" => $request->telefone_ramal, "email" => $request->email, "observacao" => $request->observacao, ]; try { DB::beginTransaction(); PontoFocal::create($pontoFocal_array); DB::commit(); return redirect("/admin/pontos-focais")->with("success", __("* O ponto focal {$request->nome} foi cadastrado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar cadastrar este ponto focal. ' . $e->getMessage()); } } public function edit(PontoFocal $pontoFocal) { $response = $this->checkPermissao($this->permissao, "editar"); if ($response instanceof RedirectResponse) { return $response; } // Buscar todas as structures ativas, agrupadas por tipo $gabinetes = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->whereIn("type", ['GABINETE_PREFIETURA', 'GABINETE_VICE_PREFIETURA']) ->orderBy('name', 'asc') ->get(); $secretarias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "SECRETARIA") ->orderBy('name', 'asc') ->get(); $executivas = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "SECRETARIA_EXECUTIVA") ->orderBy('name', 'asc') ->get(); $autarquias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->whereIn("type", ['AUTARQUIA', 'FUNDACAO']) ->orderBy('name', 'asc') ->get(); $superintendencias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "SUPERINTENDENCIA") ->orderBy('name', 'asc') ->get(); $diretorias = Structure::select("id", "name", "abbreviation", "type") ->where("status", "=", "ATIVADO") ->where("type", "=", "DIRETORIA") ->orderBy('name', 'asc') ->get(); return view("admin.pontosFocais.edit", [ "pontoFocal" => $pontoFocal, "gabinetes" => $gabinetes, "secretarias" => $secretarias, "executivas" => $executivas, "autarquias" => $autarquias, "superintendencias" => $superintendencias, "diretorias" => $diretorias, ]); } public function update(Request $request, PontoFocal $pontoFocal): RedirectResponse { $response = $this->checkPermissao($this->permissao, "editar"); if ($response instanceof RedirectResponse) { return $response; } $request->validate([ 'structure_id' => 'required|uuid|exists:structures,id', 'nome' => 'required|string|max:255', 'matricula' => 'required|string|max:255', 'telefone_ramal' => 'nullable|string|max:255', 'email' => 'nullable|email|max:255', 'observacao' => 'nullable|string', ]); $pontoFocal_array = [ "structure_id" => $request->structure_id, "nome" => $request->nome, "matricula" => $request->matricula, "telefone_ramal" => $request->telefone_ramal, "email" => $request->email, "observacao" => $request->observacao, ]; try { DB::beginTransaction(); $pontoFocal->update($pontoFocal_array); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/pontos-focais?page={$page}")->with("success", __("* O ponto focal {$request->nome} foi atualizado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar atualizar este ponto focal. ' . $e->getMessage()); } } public function destroy(Request $request, PontoFocal $pontoFocal): RedirectResponse { $response = $this->checkPermissao($this->permissao, "excluir"); if ($response instanceof RedirectResponse) { return $response; } try { DB::beginTransaction(); PontoFocal::destroy($pontoFocal->id); DB::commit(); $page = $request->input('page', 1); return redirect("/admin/pontos-focais?page={$page}")->with("success", __("* O ponto focal {$pontoFocal->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 ao tentar excluir este ponto focal. ' . $e->getMessage()); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.11 |
proxy
|
phpinfo
|
Settings