172 lines
8.4 KiB
PHP
172 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Settings;
|
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Validation\Rule;
|
|
use LDC\LDCCRM\Enums\Country;
|
|
|
|
class UpdateSettingsRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('update', $this->route('settings'));
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
// phpcs:enable
|
|
$this->merge([
|
|
// Checkboxes.
|
|
'notify_prize_sent' => $this->boolean('notify_prize_sent'),
|
|
'use_ean_codes' => $this->boolean('use_ean_codes'),
|
|
'activated_teaser' => $this->boolean('activated_teaser'),
|
|
'activated_close_page' => $this->boolean('activated_close_page'),
|
|
'discounts_activated' => $this->boolean('discounts_activated'),
|
|
'winning_proof' => $this->boolean('winning_proof'),
|
|
// Arrays.
|
|
'notification_emails' => collect($this->input('notification_emails'))->filter()->all(),
|
|
'authorized_countries' => collect($this->input('authorized_countries'))->filter()->all(),
|
|
// Remove some dates if the checkbox is uncheck.
|
|
'close_page_active_date' => $this->input('activated_close_page') ?
|
|
$this->input('close_page_active_date') : null,
|
|
// Remove discounts datas if the discount checkbox is uncheck.
|
|
'discounts_site' => $this->input('discounts_activated') ? $this->input('discounts_site') : null,
|
|
'discounts_campaign' => $this->input('discounts_activated') ? $this->input('discounts_campaign') : null,
|
|
'web_coupons' => collect($this->input('web_coupons'))->filter()->all(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'name' => 'required|string|min:3|max:255',
|
|
'company' => 'required|string|min:3|max:255',
|
|
|
|
'start_date' => 'required|date',
|
|
'end_date' => 'required|date',
|
|
'nb_participation_player' => 'required|numeric|min:0',
|
|
'nb_participation_ip' => 'required|numeric|min:0',
|
|
'nb_unique_code_use' => 'required|numeric|min:0',
|
|
'use_ean_codes' => 'required|boolean',
|
|
'activated_teaser' => 'required|boolean',
|
|
'activated_close_page' => 'required|boolean',
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'close_page_active_date' => 'nullable|required_if:activated_close_page,true,1|date|after_or_equal:end_date',
|
|
'redirect_close' => 'required|url:http,https',
|
|
|
|
'notify_prize_sent' => 'required|boolean',
|
|
'discounts_activated' => 'required|boolean',
|
|
'discounts_site' => 'nullable|required_if:discounts_activated,true,1|string|min:3|max:255',
|
|
'discounts_campaign' => 'nullable|required_if:discounts_activated,true,1|string|min:3|max:255',
|
|
|
|
'web_coupons' => [
|
|
'present', 'array', function ($attribute, $value, $fail) {
|
|
if ($this->post('discounts_activated') and !collect($value)->filter()->count()) {
|
|
$fail(trans('validation.min.array', [
|
|
'attribute' => Arr::get($this->attributes(), $attribute),
|
|
'min' => 1
|
|
]));
|
|
}
|
|
if (collect($value)->sum('percentage') > 100) {
|
|
$fail(trans('validation.max.numeric', [
|
|
'attribute' => Arr::get($this->attributes(), $attribute),
|
|
'max' => 100
|
|
]));
|
|
}
|
|
}
|
|
],
|
|
'web_coupons.*' => 'array',
|
|
'web_coupons.*.web_coupon_identifiant' => 'required|digits:13',
|
|
'web_coupons.*.percentage' => 'required|numeric|min:1|max:100',
|
|
'web_coupons.*.face_value' => 'required|numeric|min:10|max:300',
|
|
'web_coupons.*.valid_on' => 'required|string|min:3|max:255',
|
|
'web_coupons.*.picture' => [
|
|
'required', function (string $attribute, $value, callable $fail) {
|
|
ToolboxHelper::validatePicture(
|
|
$attribute,
|
|
$value,
|
|
$fail,
|
|
(object)[
|
|
'maxHeight' => 409,
|
|
'maxWith' => 920,
|
|
'minHeight' => 409,
|
|
'minWith' => 920
|
|
]
|
|
);
|
|
}
|
|
],
|
|
|
|
'notification_emails' => 'required|array|min:1',
|
|
'notification_emails.*' => 'email:rfc,spoof,dns',
|
|
|
|
'authorized_countries' => 'sometimes|array|max:' . count(Country::getValidCountries()),
|
|
'authorized_countries.*' => [
|
|
'sometimes',
|
|
Rule::in(Country::getValidCountries())
|
|
],
|
|
|
|
'winning_proof' => 'required|boolean',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'name' => trans('validation.attributes.name'),
|
|
'company' => trans('validation.custom.company'),
|
|
'start_date' => trans('validation.custom.start_date'),
|
|
'end_date' => trans('validation.custom.end_date'),
|
|
'nb_participation_player' => trans('validation.custom.nb_participation_player'),
|
|
'nb_participation_ip' => trans('validation.custom.nb_participation_ip'),
|
|
'nb_unique_code_use' => trans('validation.custom.nb_unique_code_use'),
|
|
'use_ean_codes' => trans('validation.custom.use_ean_codes'),
|
|
'activated_teaser' => trans('validation.custom.activated_teaser'),
|
|
'activated_close_page' => trans('validation.custom.activated_close_page'),
|
|
'close_page_active_date' => trans('validation.custom.close_page_active_date'),
|
|
'redirect_close' => trans('validation.custom.redirect_close'),
|
|
'winning_proof' => trans('validation.custom.winning_proof'),
|
|
'discounts_activated' => trans('validation.custom.discounts_activated'),
|
|
'discounts_site' => trans('validation.custom.discounts_site'),
|
|
'discounts_campaign' => trans('validation.custom.discounts_campaign'),
|
|
'notify_prize_sent' => trans('validation.custom.notify_prize_sent'),
|
|
|
|
'web_coupons' => trans('Tableau des coupons'),
|
|
'web_coupons.*.web_coupon_identifiant' => trans('Identifiant coupon :index'),
|
|
'web_coupons.*.percentage' => trans('Pourcentage :index'),
|
|
'web_coupons.*.face_value' => trans('Valeur faciale :index'),
|
|
'web_coupons.*.valid_on' => trans('Valable sur :index'),
|
|
'web_coupons.*.img_name' => trans('Nom image :index'),
|
|
|
|
'notification_emails' => trans('Tableau des email à notifier'),
|
|
'notification_emails.*.email' => trans('validation.attributes.email'),
|
|
|
|
'authorized_countries' => trans('Tableau des pays éligibles au jeu'),
|
|
'authorized_countries.*' => trans('validation.custom.authorized_countries'),
|
|
];
|
|
}
|
|
}
|