Added duty hours display on homepage

This commit is contained in:
noah.lancien 2023-04-18 12:39:40 +00:00
parent 8678ce520b
commit fa810ad17f
5 changed files with 237 additions and 3 deletions

View file

@ -10,6 +10,8 @@ use App\Enums\TicketStatus;
use App\Models\Ticket;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;
class Controller extends BaseController
@ -130,4 +132,43 @@ class Controller extends BaseController
}
return $query;
}
/**
* Sort columns with a query
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $ignore
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function sortQuery(Builder $query, array $ignore = []): Builder
{
$rName = request()->route()->getName();
$table = $query->getModel()->getTable();
if (request()->sort_col) {
Session::put("$rName.sort_col", request()->sort_col);
Session::put("$rName.sorted", true);
}
if (request()->sort_way) {
Session::put("$rName.sort_way", request()->sort_way);
Session::put("$rName.sorted", true);
}
if (request()->rst) {
Session::remove("$rName.sorted");
Session::remove("$rName.sort_col");
Session::remove("$rName.sort_way");
}
if (request()->sort_col and request()->sort_way and Schema::hasColumn($table, request()->sort_col)) {
if (
!isset($ignore) or
(isset($ignore) and !in_array(request()->sort_col, $ignore))
) {
$query = $query->orderBy(\sprintf("$table.%s", request()->sort_col), request()->sort_way);
}
} elseif (Schema::hasColumn($table, 'order')) {
$query = $query->orderBy('order', 'ASC');
Session::put("$rName.sort_col", 'order');
Session::put("$rName.sort_way", 'asc');
}
return $query;
}
}

View file

@ -2,15 +2,39 @@
namespace App\Http\Controllers;
use App\Models\Dutyhour;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Show Home page
*
* @param Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
*/
public function home()
public function home(Request $request)
{
return view('home');
/**
* Get only duty hours for today or later.
* @var \Illuminate\Database\Eloquent\Builder $dutyhour
*/
$dutyhour = Dutyhour::where('date', '>', now()->modify('-1 day'))->orderBy('date', 'asc');
if ($request->search) {
$this->searchQuery(
$dutyhour,
$request->search,
null,
'date',
);
}
$this->sortQuery($dutyhour);
$searchFields = trans('Date');
$dutyhourModels = $dutyhour->paginate(10);
return view('home', compact('dutyhourModels', 'searchFields'));
}
}

View file

@ -8,11 +8,70 @@
</div>
</div>
<div class="row mt-4">
<div class="col">
<div class="col mb-5">
<a href="{{ route('ticket.create') }}" class="btn btn-primary">
{{ __('Créer un ticket') }}
</a>
</div>
<div class="card">
<div class="card-header">
<div class="row">
<div class="col">
<h2 class="col h2">
<span class="fw-bold">{{ __('models.classes.dutyhours') }}</span>
</h2>
</div>
</div>
</div>
<div class="card-body">
@include('modules.searchBar')
<div class="row justify-content-center">
<div class="col-12 col-md-6 p-0">
{{ $dutyhourModels->withQueryString()->onEachSide(1)->links() }}
</div>
</div>
<div class="row">
<div class="col">
<div class="table-responsive">
<table class="table fix-action m-0">
<thead>
@php
$rst = !is_null(request()->rst);
$routeName = request()->route()->getName();
$noOrder = Session::get("$routeName.sort_col") !== 'order' and (Session::has("$routeName.sort_col") or Session::has("$routeName.sort_way"));
$noOrder = Session::get("$routeName.sort_col") !== '' and (Session::has("$routeName.sort_col") or Session::has("$routeName.sort_way"));
$cols = ['date' => __('Date'), 'person' => __('Personne présente'), 'place' => __('Lieu de la permanence')];
@endphp
@include('modules.tableColSorter', ['cols' => $cols, 'mobileHide' => ['status']])
</thead>
<tbody>
@if($dutyhourModels->count())
@foreach ($dutyhourModels as $dutyhourModel)
<tr>
<td class="align-middle">{{ $dutyhourModel->date->formatLocalized('%d/%m/%Y') }}</td>
<td class="align-middle">{{ $dutyhourModel->person }}</td>
<td class="align-middle">{{ $dutyhourModel->place }}</td>
</tr>
@endforeach
@else
<tr>
<td colspan="5" class="text-center">
<span class="h3">{{ __('No results for :models', ['models' => __('models.classes.dutyhours') ])}}</span>
</td>
</tr>
@endif
</tbody>
</table>
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-12 col-md-6 p-0">
{{ $dutyhourModels->withQueryString()->onEachSide(1)->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
@endsection

View file

@ -0,0 +1,46 @@
@php
$iArgs = [];
if(request()->sort_col) {
$iArgs['sort_col'] = request()->sort_col;
}
if(request()->search) {
$iArgs['search'] = request()->sort_way;
}
@endphp
<div class="row mb-3">
<div class="col-12">
<form id="searchForm" action="/{{ request()->path() }}">
<div class="input-group">
<label class="input-group-text" for="searchField">{{ $searchFields }}</label>
@if(request()->sort_col)
<input class="d-none" type="text" name="sort_col" value="{{ request()->sort_col }}">
@endif
@if(request()->sort_way)
<input class="d-none" type="text" name="sort_way" value="{{ request()->sort_way }}">
@endif
@if(request()->rangefilter)
<input class="d-none" type="text" name="rangefilter" value="{{ request()->rangefilter }}">
@endif
@if(request()->group)
<input class="d-none" type="text" name="group" value="{{ request()->group }}">
@endif
<input type="search" id="searchField" name="search" class="form-control" value="{{ request()->search }}" placeholder="{{ __('Vos mots clés ici...') }}"/>
<button type="submit" class="btn btn-primary" data-bs-toggle="tooltip" title="{{ __('Lancer la recherche') }}">
<i class="fas fa-search"></i>
</button>
<button id="searchDelete" type="button" class="btn btn-danger" data-bs-toggle="tooltip" title="{{ __('Effacer la recherche') }}">
<i class="fas fa-backspace"></i>
</button>
@push('scripts')
<script nonce="{{ $nonce }}">
document.getElementById('searchDelete').addEventListener('click', function (e) {
document.getElementById('searchField').value = '';
document.getElementById('searchForm').submit();
});
</script>
@endpush
</div>
</form>
</div>
</div>

View file

@ -0,0 +1,64 @@
@php
$route = request()->route();
$routeName = $route->getName();
$rst = !is_null(request()->rst) || !Session::has("$routeName.sorted");
$noOrder = Session::get("$routeName.sort_col") !== 'order' and (Session::has("$routeName.sort_col") or Session::has("$routeName.sort_way"));
$noOrder = Session::get("$routeName.sort_col") !== '' and (Session::has("$routeName.sort_col") or Session::has("$routeName.sort_way"));
View::share('noOrder', $noOrder);
$ignore = (isset($ignore) and is_array($ignore)) ? $ignore : [];
@endphp
<tr>
@foreach($cols as $col => $colname)
@php
$iArgs = ['sort_col' => $col];
if(request()->search) {
$iArgs['search'] = request()->search;
}
@endphp
{{-- NO Sort for order column --}}
@if($col === 'order' or in_array($col, $ignore))
@if($noOrder and !$rst and !in_array($col, $ignore)) @continue @endif
<th scope="col" @if(isset($mobileHide) and is_array($mobileHide) and in_array($col, $mobileHide)) class="d-none d-md-table-cell text-center" @endif>
<span class="text-nowrap">{{ $colname }}</span>
</th>
@continue
@endif
<th scope="col" @if(isset($mobileHide) and is_array($mobileHide) and in_array($col, $mobileHide)) class="d-none d-md-table-cell text-center" @endif>
<span class="text-nowrap">
{{ $colname }}
@if(!session("$routeName.sort_way") or session("$routeName.sort_col") !== $col or (session("$routeName.sort_way") === 'desc' and session("$routeName.sort_col") === $col))
<a class="col_sort text-info lead asc"
href="{{ request()->fullUrlWithQuery(array_merge($iArgs, ['sort_way' => 'asc', 'rst' => null])) }}"
title="{{ __('Trier :name en ordre ascendant', ['name' => Str::lower($colname)]) }}"
data-bs-toggle="tooltip">
&nbsp;&darr;&nbsp;
</a>
@endif
@if(!session("$routeName.sort_way") or session("$routeName.sort_col") !== $col or (session("$routeName.sort_way") === 'asc' and session("$routeName.sort_col") === $col))
<a class="col_sort text-info lead desc"
href="{{ request()->fullUrlWithQuery(array_merge($iArgs, ['sort_way' => 'desc', 'rst' => null])) }}"
title="{{ __('Trier :name en ordre descendant', ['name' => Str::lower($colname)]) }}"
data-bs-toggle="tooltip">
&nbsp;&uarr;&nbsp;
</a>
@endif
</span>
</th>
@endforeach
{{-- Buttons Col --}}
<th scope="col">
@if($noOrder and !$rst)
<a class="btn btn-small btn-danger"
href="{{ request()->fullUrlWithQuery(['sort_col' => null, 'sort_way' => null, 'rst' => true, 'search' => null]) }}"
data-bs-toggle="tooltip"
title="{{ __('Effacer le tri') }}">
<i class="fa fa-eraser"></i>
</a>
@else
<span data-bs-toggle="tooltip" data-bs-placement="top" title="{{ __('Ici, les flèches servent à trier et non pas à filtrer') }}">
<i class="fas fa-question-circle"></i>
</span>
@endif
</th>
</tr>