File manager - Edit - /var/www/html/portal/app/Http/Services/Comunicacao/NoticiaService.php
Back
<?php namespace App\Http\Services\Comunicacao; use App\Models\Structure; use App\Models\Comunicacao\Noticia; use App\Models\Comunicacao\NoticiaRelSecretarias; use App\Http\Controllers\Controller; use Illuminate\Support\Carbon; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Collection as SupportCollection; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Pagination\LengthAwarePaginator; use App\Http\Requests\Comunicacao\NoticiaRequest; class NoticiaService extends Controller { protected $Noticia = Noticia::class; protected $NoticiaRelSecretarias = NoticiaRelSecretarias::class; public function getSecretarias(Array $like): Collection { return Structure::select("id", "name", "abbreviation") ->whereIn('type', $like) ->where("status", "ATIVADO") ->where("status", "=", "ATIVADO") ->orderBy('name', 'asc')->get(); } public function getNoticia($dta, $slug): ?Noticia { $date = Carbon::parse($dta)->toDateString(); return Noticia::where('slug', $slug) ->where('status', 'ATIVADO') ->whereDate('created_at', $date) ->first(); } public function getNoticias(Request $request): LengthAwarePaginator { $paginate = 15; $query = Noticia::query() ->where('status', 'ATIVADO'); $termoBusca = $request->input('q'); if (is_string($termoBusca) && trim($termoBusca) !== '') { $termoBusca = trim($termoBusca); $query->where(function ($sub) use ($termoBusca) { $sub->where('title', 'LIKE', '%' . $termoBusca . '%') ->orWhere('summary', 'LIKE', '%' . $termoBusca . '%') ->orWhere('description', 'LIKE', '%' . $termoBusca . '%'); }); } if ($request->filled('secretaria_id')) { $secretariaId = $request->input('secretaria_id'); $query->whereHas('relacionadas', static function ($rel) use ($secretariaId) { $rel->where('structures.id', $secretariaId); }); } if ($request->filled('ano')) { $query->whereYear('created_at', (int) $request->input('ano')); } if ($request->filled('categoria')) { $tagCategoria = $request->input('categoria'); $query->whereNotNull('tags') ->where('tags', '!=', '') ->where(function ($sub) use ($tagCategoria) { $sub->where('tags', $tagCategoria) ->orWhere('tags', 'like', $tagCategoria . ',%') ->orWhere('tags', 'like', '%,' . $tagCategoria . ',%') ->orWhere('tags', 'like', '%,' . $tagCategoria); }); } return $query->orderByDesc('created_at')->paginate($paginate); } /** * Anos distintos com notícias ativas (filtro da listagem pública). * * @return SupportCollection<int, int> */ public function getAnosComNoticiasPublicas(): SupportCollection { return Noticia::query() ->where('status', 'ATIVADO') ->whereNotNull('created_at') ->selectRaw('YEAR(created_at) as ano') ->groupBy(DB::raw('YEAR(created_at)')) ->orderByDesc('ano') ->pluck('ano'); } /** * Anos distintos com qualquer notícia (listagem admin). * * @return SupportCollection<int, int> */ public function getAnosComNoticiasAdmin(): SupportCollection { return Noticia::query() ->whereNotNull('created_at') ->selectRaw('YEAR(created_at) as ano') ->groupBy(DB::raw('YEAR(created_at)')) ->orderByDesc('ano') ->pluck('ano'); } /** * Listagem paginada do admin com filtros combináveis (termo, data, secretaria, ano, categoria em tags). */ public function paginateAdminListagem(Request $request): LengthAwarePaginator { $paginate = 15; $query = Noticia::query(); $termoBusca = $request->input('q'); if (is_string($termoBusca) && trim($termoBusca) !== '') { $termoBusca = trim($termoBusca); $query->where(function ($sub) use ($termoBusca) { $sub->where('title', 'like', '%' . $termoBusca . '%') ->orWhere('summary', 'like', '%' . $termoBusca . '%') ->orWhere('description', 'like', '%' . $termoBusca . '%'); }); } if ($request->filled('dta')) { $query->whereDate('created_at', $request->input('dta')); } if ($request->filled('secretaria_id')) { $secretariaId = $request->input('secretaria_id'); $query->whereHas('relacionadas', static function ($rel) use ($secretariaId) { $rel->where('structures.id', $secretariaId); }); } if ($request->filled('ano')) { $query->whereYear('created_at', (int) $request->input('ano')); } if ($request->filled('categoria')) { $tagCategoria = $request->input('categoria'); $query->whereNotNull('tags') ->where('tags', '!=', '') ->where(function ($sub) use ($tagCategoria) { $sub->where('tags', $tagCategoria) ->orWhere('tags', 'like', $tagCategoria . ',%') ->orWhere('tags', 'like', '%,' . $tagCategoria . ',%') ->orWhere('tags', 'like', '%,' . $tagCategoria); }); } return $query->orderByDesc('created_at')->paginate($paginate)->withQueryString(); } /** * Query string para manter filtros ao redirecionar após ações na listagem admin. * * @return array<string, mixed> */ public function queryStringAdminNoticiasIndex(Request $request): array { $query = []; foreach (['q', 'dta', 'secretaria_id', 'ano', 'categoria'] as $key) { if ($request->filled($key)) { $query[$key] = $request->input($key); } } $query['page'] = $request->input('page', 1); return $query; } public function store(NoticiaRequest $request, Array $array_noticia): RedirectResponse { try { DB::beginTransaction(); if (isset($request->fix) && ($request->fix == "on" || $request->fix == 1)) { Noticia::query()->update(['fix' => 0]); } $noticia = Noticia::create($array_noticia); if (isset($request->belongsTo)) { foreach ($request->belongsTo as $key => $value) { NoticiaRelSecretarias::create([ "news_id" => $noticia->id, "structure_id" => $value, ]); } } DB::commit(); return redirect()->route('admin.noticias.index', $this->queryStringAdminNoticiasIndex($request)) ->with("success", __("* A notícia {$request->title} foi criada com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar criar uma notícia. ' . $e->getMessage()); } } public function update(NoticiaRequest $request, Noticia $noticia, Array $array_noticia): RedirectResponse { try { DB::beginTransaction(); if (isset($request->fix) && ($request->fix == "on" || $request->fix == 1)) { Noticia::query()->update(['fix' => 0]); } $noticia->update($array_noticia); NoticiaRelSecretarias::where("news_id", $noticia->id)->delete(); if (isset($request->belongsTo)) { foreach ($request->belongsTo as $key => $value) { NoticiaRelSecretarias::create([ "news_id" => $noticia->id, "structure_id" => $value, ]); } } DB::commit(); return redirect()->route('admin.noticias.index', $this->queryStringAdminNoticiasIndex($request)) ->with("success", __("* A notícia {$request->title} foi atualizada com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar atualizar esta notícia. ' . $e->getMessage()); } } public function destroy(Request $request, Noticia $noticia): RedirectResponse { try { DB::beginTransaction(); NoticiaRelSecretarias::where("news_id", $noticia->id)->delete(); $noticia::destroy($noticia->id); DB::commit(); return redirect()->route('admin.noticias.index', $this->queryStringAdminNoticiasIndex($request)) ->with("success", __("* A notícia foi excluída 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 esta notícia. ' . $e->getMessage()); } } public function status(Request $request, Noticia $noticia): RedirectResponse { try { DB::beginTransaction(); $noticia->fill(['status' => $request->checkStatus]); $noticia->save(); DB::commit(); return redirect()->route('admin.noticias.index', $this->queryStringAdminNoticiasIndex($request)) ->with("success", __("* O status da notícia foi atualizado com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar alterar o status desta notícia. ' . $e->getMessage()); } } public function fixar(Request $request, Noticia $noticia): RedirectResponse { try { DB::beginTransaction(); Noticia::query()->update(['fix' => 0]); $noticia->fill(['fix' => 1]); $noticia->save(); DB::commit(); return redirect()->route('admin.noticias.index', $this->queryStringAdminNoticiasIndex($request)) ->with("success", __("* A notícia foi fixada com sucesso!")); } catch (\Exception $e) { DB::rollBack(); return redirect() ->back() ->withInput() ->with('error', 'Erro ao tentar fixar esta notícia. ' . $e->getMessage()); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings