adrema/app/Member/MemberRequest.php

118 lines
3.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;
2022-12-06 23:11:57 +01:00
use App\Letter\BillKind;
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;
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 () {
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',
'address' => 'required',
'zip' => 'required|numeric',
'location' => 'required',
'birthday' => 'date|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
'joined_at' => 'date|required',
'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',
'multiply_pv' => 'boolean',
'multiply_more_pv' => 'boolean',
2022-08-12 22:07:59 +02:00
'send_newspaper' => 'boolean',
'main_phone' => '',
'mobile_phone' => '',
'letter_address' => '',
'gender_id' => 'nullable|exists:genders,id',
'region_id' => 'nullable|exists:regions,id',
'nationality_id' => 'nullable|exists:nationalities,id',
'children_phone' => '',
'fax' => '',
'other_country' => '',
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
{
2022-08-12 22:07:59 +02:00
$member = Member::create([
...$this->validated(),
'group_id' => Group::where('nami_id', $settings->default_group_id)->firstOrFail()->id,
]);
2022-03-11 20:19:17 +01:00
if ($this->input('has_nami')) {
2022-11-16 23:39:44 +01:00
NamiPutMemberAction::run(
$member,
Activity::findOrFail($this->input('first_activity_id')),
Subactivity::find($this->input('first_subactivity_id')),
);
2021-06-24 00:12:47 +02:00
}
2021-04-11 20:01:37 +02:00
}
2022-02-12 16:16:56 +01:00
public function persistUpdate(Member $member): void
{
$member->fill($this->validated());
$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) {
2022-11-16 23:39:44 +01:00
NamiPutMemberAction::run($member->fresh(), null, null);
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) {
2022-11-16 22:08:08 +01:00
DeleteJob::dispatch($member->nami_id);
2021-06-24 00:12:47 +02:00
}
2021-04-11 20:01:37 +02:00
}
}