Files
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

99 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Cadastro\Estabelecimento\CentroCusto;
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function auth($functionName)
{
$this->authorize("{$functionName}-{$this->modulo}-{$this->funcionalidade}");
}
protected function get($model, $id = null)
{
$model = $model::query();
$query = request()->query();
if (isset($query['with'])) {
$with = is_array($query['with']) ? $query['with'] : [$query['with']];
foreach ($with as $value) {
$model->with($value);
}
}
return is_null($id) ? $model : $model->findOrFail($id);
}
protected function getWhereFromQueryString(array $fields): array
{
$where = [];
foreach ($fields as $field) {
$operator = "";
$fld = $field;
if (is_array($field)) {
$fld = $field[0];
$operator = $field[1];
}
$query = request()->query();
if (!array_key_exists($fld, $query)) {
continue;
}
$vlr = $query[$fld];
if ($vlr === 'true') $vlr = 1;
if ($vlr === 'false') $vlr = 0;
if ($vlr === 'null') $vlr = null;
$fld = $fld === 'not_id' ? 'id' : $fld;
if ($operator === "")
$where[] = [$field, $vlr];
else
$where[] = [$fld, $operator, $vlr];
}
return $where;
}
protected function addPaginationToQuery($builder, $perPage, $currentPage)
{
$numberOfRows = (int) $perPage;
$rowsToSkip = ((int) $currentPage - 1) * $numberOfRows;
$builder->skip($rowsToSkip)->take($numberOfRows);
}
protected function getDataTableResponseBody($totalRows, $collection)
{
return ['total_rows' => $totalRows, 'items' => $collection];
}
protected function getCurrentLotacao()
{
return Lotacao::findOrFail(auth()->payload()->get('lotacao_id'));
}
protected function getCurrentCentroCusto()
{
return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id'));
}
}