[Done] Updated classes with a range instead of week - year

This commit is contained in:
guillaume91 2024-06-19 14:10:32 +02:00
parent 76d4babd98
commit ecc1a9a47b
5 changed files with 43 additions and 32 deletions

View file

@ -4,6 +4,7 @@
use App\Jobs\ProjectMosiacGeneratorJob; use App\Jobs\ProjectMosiacGeneratorJob;
use App\Models\Project; use App\Models\Project;
use Carbon\Carbon;
use Livewire\Component; use Livewire\Component;
use Livewire\WithPagination; use Livewire\WithPagination;
@ -14,8 +15,8 @@ class Mosaic extends Component
public Project $project; public Project $project;
public $formData = [ public $formData = [
'week' => '', 'end_date' => '',
'year' => '', 'offset' => '7',
]; ];
public $showCreateModal = false; public $showCreateModal = false;
@ -27,18 +28,19 @@ class Mosaic extends Component
public function mount(Project $project) public function mount(Project $project)
{ {
$this->path = $project->download_path; $this->path = $project->download_path;
$this->formData['end_date'] = Carbon::yesterday()->toDateString();
} }
public function saveMosaic() public function saveMosaic()
{ {
$this->validate([ $this->validate([
'formData.year' => 'required', 'formData.end_date' => ['required','date','before:today'],
'formData.week' => 'required', 'formData.offset' => 'required|integer|min:1|max:1000',
]); ]);
$mosaic = $this->project->mosaics()->updateOrCreate([ $mosaic = $this->project->mosaics()->updateOrCreate([
'name' => sprintf('Week %s, %s', $this->formData['week'], $this->formData['year']), 'name' => sprintf('', $this->formData['end_date'], $this->formData['offset']),
'year' => $this->formData['year'], 'offset' => $this->formData['offset'],
'week' => $this->formData['week'], 'end_date' => $this->formData['end_date'],
], [ ], [
'path' => $this->project->getMosaicPath(), 'path' => $this->project->getMosaicPath(),
]); ]);
@ -55,24 +57,20 @@ public function openCreateMosiacsModal()
public function getDateRangeProperty() public function getDateRangeProperty()
{ {
if (empty($this->formData['week']) || strlen($this->formData['year']) !== 4) { if (!$this->formData['end_date'] || !$this->formData['offset']) {
return '<span class="text-red-500">Invalid week or year</span>'; return '<span class="text-red-500">Please give a correct date or offset</span>';
} }
$begin = now() $start = (new Carbon($this->formData['end_date']))->subDays($this->formData['offset']-1);
->setISODate($this->formData['year'], $this->formData['week']) $end = new Carbon($this->formData['end_date']);
->startOfWeek(); return 'from '.$start->format('Y-m-d').' to '.$end->format('Y-m-d');
$end = now()
->setISODate($this->formData['year'], $this->formData['week'])
->endOfWeek();
return $begin->format('Y-m-d').' - '.$end->format('Y-m-d');
} }
private function applySearch($query) private function applySearch($query)
{ {
return $query->when($this->search !== '', function ($q){ return $query->when($this->search !== '', function ($q){
$q->where('name', 'like', '%'.$this->search.'%') $q->where('name', 'like', '%'.$this->search.'%')
->orWhere('year', 'like', '%' . $this->search . '%') ->orWhere('offset', 'like', '%' . $this->search . '%')
->orWhere('week', 'like', '%' . $this->search . '%'); ->orWhere('end_date', 'like', '%' . $this->search . '%');
}); });
} }
@ -86,8 +84,8 @@ public function update($property)
public function render() public function render()
{ {
$query = $this->project->mosaics() $query = $this->project->mosaics()
->orderBy('year', 'desc') ->orderBy('offset', 'desc')
->orderBy('week', 'desc'); ->orderBy('end_date', 'desc');
$query = $this->applySearch($query); $query = $this->applySearch($query);
$mosaics = $query->paginate(10, pageName: 'mosaicPage'); $mosaics = $query->paginate(10, pageName: 'mosaicPage');

View file

@ -22,13 +22,14 @@ class ProjectMosaic extends Model
'week', 'week',
'year', 'year',
'end_date', 'end_date',
'offset',
'status' 'status'
]; ];
public static function getFilenameByPeriod(Carbon $endDate,int $offset) public static function getFilenameByPeriod(Carbon $endDate,int $offset)
{ {
return sprintf('period_%s_%s.tif',(clone $endDate)->subdays($offset-1)->format('Y-m-d'),$endDate->format('Y-m-d')); return sprintf('period_%s_%s.tif',(clone $endDate)->subdays($offset)->format('Y-m-d'),$endDate->format('Y-m-d'));
} }
public function project() public function project()

View file

@ -28,7 +28,8 @@ public function passes($attribute, $value)
} }
return $project->allMosaicsPresent( return $project->allMosaicsPresent(
ProjectReport::getReportDateForYearAndWeek($project, $value['year'], $value['week']) // TODO change with end_date.
$value['end_date']
); );
} catch (\Exception $e) { } catch (\Exception $e) {
$this->errorMessage = $e->getMessage(); $this->errorMessage = $e->getMessage();

View file

@ -17,13 +17,13 @@
<x-slot name="form"> <x-slot name="form">
<div class="col-span-6 sm:col-span-4"> <div class="col-span-6 sm:col-span-4">
<x-label for="year" value="{{ __('Year') }}"/> <x-label for="end_date" value="{{ __('Date') }}"/>
<x-input id="year" type="number" min="1900" max="{{\Carbon\Carbon::now()->year}}" class="mt-1 block w-full" wire:model.live="formData.year" autofocus/> <x-input id="end_date" type="date" max="{{\Carbon\Carbon::yesterday()->toDateString()}}" class="mt-1 block w-full" wire:model.live="formData.end_date" autofocus/>
<x-input-error for="formData.year" class="mt-2"/> <x-input-error for="formData.end_date" class="mt-2"/>
</div><div class="col-span-6 sm:col-span-4"> </div><div class="col-span-6 sm:col-span-4">
<x-label for="week" value="{{ __('Week') }}"/> <x-label for="offset" value="{{ __('Offset') }}"/>
<x-input id="week" type="number" min="1" max="53" class="mt-1 block w-full" wire:model.live="formData.week" autofocus/> <x-input id="offset" type="number" min="1" max="1000" placeholder="{{__('Number of days used to generate the mosaic (7 by default)')}}" class="mt-1 block w-full" wire:model.live="formData.offset" autofocus/>
<x-input-error for="formData.week" class="mt-2"/> <x-input-error for="formData.offset" class="mt-2"/>
<x-input-error for="formData" class="mt-2"/> <x-input-error for="formData" class="mt-2"/>
</div> </div>
<div> <div>

View file

@ -22,10 +22,16 @@ class="justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold t
<tr> <tr>
<th scope="col" <th scope="col"
class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6 lg:pl-8"> class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6 lg:pl-8">
Name {{__('Name')}}
</th> </th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"> <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
Year-Week {{__('Start')}}
</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
{{__('End')}}
</th>
<th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">
{{__('Range (days)')}}
</th> </th>
<th scope="col" <th scope="col"
class="px-3 py-3.5 text-right text-sm font-semibold text-gray-900 sm:pr-8 lg:pr-8"> class="px-3 py-3.5 text-right text-sm font-semibold text-gray-900 sm:pr-8 lg:pr-8">
@ -38,7 +44,13 @@ class="px-3 py-3.5 text-right text-sm font-semibold text-gray-900 sm:pr-8 lg:pr-
<tr> <tr>
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 lg:pl-8">{{ $mosaic->name }}</td> <td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6 lg:pl-8">{{ $mosaic->name }}</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"> <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{{ $mosaic->year }}-{{ $mosaic->week}} {{ $mosaic->end_date->copy()->subDays($mosaic->offset)->format('Y-m-d') }}
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{{ $mosaic->end_date->format('Y-m-d') }}
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
{{ $mosaic->offset }}
</td> </td>
<td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8"> <td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8">
<livewire:components.badge :status="$mosaic->status" :id="$mosaic->id" type="mosaic" wire:key="{{$mosaic->id}}"></livewire:components.badge> <livewire:components.badge :status="$mosaic->status" :id="$mosaic->id" type="mosaic" wire:key="{{$mosaic->id}}"></livewire:components.badge>
@ -52,7 +64,6 @@ class="px-3 py-3.5 text-right text-sm font-semibold text-gray-900 sm:pr-8 lg:pr-
<div class="text-gray-700 text-sm"> <div class="text-gray-700 text-sm">
Results: {{ \Illuminate\Support\Number::format($mosaics->total()) }} Results: {{ \Illuminate\Support\Number::format($mosaics->total()) }}
</div> </div>
{{ $mosaics->links('livewire.pagination') }} {{ $mosaics->links('livewire.pagination') }}
</div> </div>
</div> </div>