2023-10-06 11:02:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Bo;
|
|
|
|
|
|
2026-04-13 12:36:48 +02:00
|
|
|
use App\Enums\PeriodEnum;
|
2023-10-06 11:02:07 +00:00
|
|
|
use App\Models\Brand;
|
|
|
|
|
use App\Models\Url;
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
2026-04-13 12:36:48 +02:00
|
|
|
use Illuminate\Validation\Rule;
|
2023-10-06 11:02:07 +00:00
|
|
|
|
|
|
|
|
class StatsRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2023-12-19 15:24:31 +01:00
|
|
|
public function authorize(): bool
|
2023-10-06 11:02:07 +00:00
|
|
|
{
|
|
|
|
|
return auth('backend')->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the data for validation.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-12-19 15:24:31 +01:00
|
|
|
protected function prepareForValidation(): void
|
2023-10-06 11:02:07 +00:00
|
|
|
{
|
2023-12-19 15:24:31 +01:00
|
|
|
$now = Carbon::now();
|
|
|
|
|
$urlId = $this->input('url');
|
|
|
|
|
$brandId = $this->input('brand');
|
|
|
|
|
$startDate = Carbon::parse($this->input('start'));
|
|
|
|
|
$endDate = Carbon::parse($this->input('end'));
|
|
|
|
|
$adjustedEndDate = $endDate->greaterThan($now) ? $now : $endDate;
|
|
|
|
|
|
2026-04-13 12:36:48 +02:00
|
|
|
$period = PeriodEnum::tryFrom($this->input('period'));
|
|
|
|
|
|
2023-12-19 15:24:31 +01:00
|
|
|
// Get start & end dates by period.
|
2026-04-13 12:36:48 +02:00
|
|
|
[$adjustedStart, $adjustedEnd] = match ($period) {
|
|
|
|
|
PeriodEnum::week => [$startDate->copy()->startOfWeek(), $endDate->copy()->endOfWeek()],
|
|
|
|
|
PeriodEnum::month => [$startDate->copy()->startOfMonth(), $endDate->copy()->endOfMonth()],
|
2023-12-19 15:24:31 +01:00
|
|
|
default => [$startDate, $adjustedEndDate],
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-06 11:02:07 +00:00
|
|
|
$this->merge([
|
2023-12-19 15:24:31 +01:00
|
|
|
'url' => Url::find($urlId) ? $urlId : null,
|
|
|
|
|
'brand' => Brand::find($brandId) ? $brandId : null,
|
|
|
|
|
'start' => $adjustedStart->toDateString(),
|
|
|
|
|
'end' => $adjustedEnd->toDateString(),
|
2023-10-06 11:02:07 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-12-19 15:24:31 +01:00
|
|
|
public function rules(): array
|
2023-10-06 11:02:07 +00:00
|
|
|
{
|
2023-12-19 15:24:31 +01:00
|
|
|
return (request()->post()) ? [
|
2026-04-13 12:36:48 +02:00
|
|
|
'period' => [
|
|
|
|
|
'required',
|
|
|
|
|
'string',
|
|
|
|
|
Rule::enum(PeriodEnum::class)
|
|
|
|
|
],
|
2023-12-19 15:24:31 +01:00
|
|
|
'start' => 'required|date|date_format:Y-m-d|before_or_equal:today|after_or_equal:'
|
|
|
|
|
. Carbon::parse($this->input('end'))->modify('-1 year'),
|
|
|
|
|
'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
|
|
|
|
|
'brand' => 'nullable|integer|exists:App\Models\Brand,id',
|
|
|
|
|
'url' => 'nullable|integer|exists:App\Models\Url,id'
|
|
|
|
|
] : [];
|
2023-10-06 11:02:07 +00:00
|
|
|
}
|
|
|
|
|
}
|