adrema/app/Member/MemberRequest.php

162 lines
5.8 KiB
PHP
Raw Normal View History

2021-04-11 20:01:37 +02:00
<?php
namespace App\Member;
2021-06-23 01:45:48 +02:00
use App\Activity;
2022-05-01 21:08:16 +02:00
use App\Group;
2023-04-18 22:08:45 +02:00
use App\Invoice\BillKind;
2023-06-12 13:12:46 +02:00
use App\Maildispatcher\Actions\ResyncAction;
2023-08-15 23:00:01 +02:00
use App\Member\Actions\NamiDeleteMemberAction;
2022-11-16 23:39:44 +01:00
use App\Member\Actions\NamiPutMemberAction;
2022-05-01 21:00:15 +02:00
use App\Setting\NamiSettings;
2022-11-16 23:39:44 +01:00
use App\Subactivity;
2022-01-02 12:32:57 +01:00
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
2023-07-24 16:55:07 +02:00
use Illuminate\Validation\Validator;
2023-03-03 00:30:33 +01:00
use Zoomyboy\Phone\ValidPhoneRule;
2021-04-11 20:01:37 +02:00
class MemberRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
2022-11-16 23:39:44 +01:00
...'POST' === $this->method() ? [
'first_activity' => 'exclude|required',
'first_subactivity' => 'exclude|required',
] : [],
2022-03-11 20:19:17 +01:00
'subscription_id' => Rule::requiredIf(function () {
2023-09-07 16:48:51 +02:00
if (!$this->input('has_nami')) {
return false;
}
2022-03-11 20:19:17 +01:00
if ('POST' != $this->method()) {
2021-06-23 01:45:48 +02:00
return false;
}
2022-01-02 12:32:57 +01:00
2022-03-11 20:19:17 +01:00
if (!$this->input('first_activity_id')) {
return true;
}
2022-01-02 12:32:57 +01:00
2021-06-23 01:45:48 +02:00
return Str::contains(Activity::findOrFail($this->input('first_activity_id'))->name, '€');
}),
2021-04-11 20:01:37 +02:00
'firstname' => 'required',
'lastname' => 'required',
'country_id' => 'required|exists:countries,id',
'email' => 'nullable|email',
'email_parents' => 'nullable|email',
2022-12-06 23:11:57 +01:00
'bill_kind' => ['nullable', Rule::in(BillKind::values())],
2021-04-11 20:01:37 +02:00
'confession_id' => 'nullable|exists:confessions,id',
2022-05-31 21:50:35 +02:00
'ps_at' => 'nullable|date_format:Y-m-d',
'more_ps_at' => 'nullable|date_format:Y-m-d',
'has_svk' => 'boolean',
'has_vk' => 'boolean',
'efz' => 'nullable|date_format:Y-m-d',
'without_education_at' => 'nullable|date_format:Y-m-d',
'without_efz_at' => 'nullable|date_format:Y-m-d',
2023-11-24 13:28:54 +01:00
'recertified_at' => 'nullable|date_format:Y-m-d',
2022-05-31 21:50:35 +02:00
'multiply_pv' => 'boolean',
'multiply_more_pv' => 'boolean',
2022-08-12 22:07:59 +02:00
'send_newspaper' => 'boolean',
2023-03-03 00:30:33 +01:00
'main_phone' => ['nullable', new ValidPhoneRule('Telefon (Eltern)')],
'mobile_phone' => ['nullable', new ValidPhoneRule('Handy (Eltern)')],
2023-04-18 22:08:45 +02:00
'invoice_address' => '',
2022-08-12 22:07:59 +02:00
'gender_id' => 'nullable|exists:genders,id',
'region_id' => 'nullable|exists:regions,id',
2023-03-03 00:30:33 +01:00
'children_phone' => ['nullable', new ValidPhoneRule('Telefon (Kind)')],
'fax' => ['nullable', new ValidPhoneRule('Fax')],
'other_country' => '',
2023-02-27 22:40:47 +01:00
'salutation' => '',
2023-02-27 23:15:57 +01:00
'comment' => '',
2021-04-11 20:01:37 +02:00
];
}
2022-05-01 21:00:15 +02:00
public function persistCreate(NamiSettings $settings): void
2022-03-11 20:19:17 +01:00
{
2023-03-03 00:30:33 +01:00
$member = new Member([
2022-08-12 22:07:59 +02:00
...$this->validated(),
'group_id' => Group::where('nami_id', $settings->default_group_id)->firstOrFail()->id,
]);
2023-03-03 00:30:33 +01:00
$member->updatePhoneNumbers()->save();
2022-03-11 20:19:17 +01:00
if ($this->input('has_nami')) {
$this->storeFreshMemberInNami($member);
2021-06-24 00:12:47 +02:00
}
2023-06-12 13:12:46 +02:00
ResyncAction::dispatch();
2021-04-11 20:01:37 +02:00
}
protected function storeFreshMemberInNami(Member $member): void
{
NamiPutMemberAction::run(
$member->fresh(),
Activity::findOrFail($this->input('first_activity_id')),
Subactivity::find($this->input('first_subactivity_id')),
);
}
2022-02-12 16:16:56 +01:00
public function persistUpdate(Member $member): void
{
2023-03-03 00:30:33 +01:00
$member->fill($this->validated())->updatePhoneNumbers();
$namiSync = $member->isDirty(Member::$namiFields);
$member->save();
2021-06-23 01:45:48 +02:00
2022-03-11 20:19:17 +01:00
if ($this->input('has_nami') && null === $member->nami_id) {
$this->storeFreshMemberInNami($member);
2021-06-23 01:45:48 +02:00
}
if ($this->input('has_nami') && null !== $member->nami_id && $namiSync) {
2022-11-16 23:39:44 +01:00
NamiPutMemberAction::run($member->fresh(), null, null);
2021-06-23 01:45:48 +02:00
}
2022-03-11 20:19:17 +01:00
if (!$this->input('has_nami') && null !== $member->nami_id) {
2023-08-15 23:00:01 +02:00
NamiDeleteMemberAction::dispatch($member->nami_id);
2021-06-24 00:12:47 +02:00
}
2023-06-12 13:12:46 +02:00
ResyncAction::dispatch();
2021-04-11 20:01:37 +02:00
}
2023-07-24 16:55:07 +02:00
public function withValidator(Validator $validator): void
{
$this->namiIfElse($validator, 'birthday', 'date|required');
$this->namiIfElse($validator, 'nationality_id', 'required|exists:nationalities,id');
$this->namiIfElse($validator, 'address', 'required|max:255');
$this->namiIfElse($validator, 'zip', 'required|numeric');
$this->namiIfElse($validator, 'location', 'required|max:255');
$this->namiIfElse($validator, 'joined_at', 'date|required');
$this->namiIfStoring($validator, 'first_activity_id', 'required|exclude|exists:activities,id');
$this->namiIfStoring($validator, 'first_subactivity_id', 'required|exclude|exists:subactivities,id');
2023-07-24 16:55:07 +02:00
}
private function namiIfElse(Validator $validator, string $attribute, string $rules): void
2023-07-24 16:55:07 +02:00
{
$request = request();
$when = fn () => true === $request->input('has_nami');
$notWhen = fn () => true !== $request->input('has_nami');
$validator->sometimes($attribute, $rules, $when);
$validator->sometimes($attribute, 'present', $notWhen);
}
private function namiIfStoring(Validator $validator, string $attribute, string $rules): void
{
$request = request();
/** @var Member */
$member = request()->route('member');
2024-03-09 00:30:44 +01:00
$when = fn () => true === $request->input('has_nami') && ($member !== null || !$member->has_nami);
$validator->sometimes($attribute, $rules, $when);
}
2021-04-11 20:01:37 +02:00
}