adrema/app/Member/MemberRequest.php

87 lines
2.7 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-01-02 12:32:57 +01:00
use App\Group;
use Illuminate\Foundation\Http\FormRequest;
2021-07-15 20:56:53 +02:00
use Illuminate\Support\Facades\Auth;
2022-01-02 12:32:57 +01:00
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-03-11 20:19:17 +01:00
'first_activity_id' => Rule::requiredIf(fn () => 'POST' == $this->method()),
'first_subactivity_id' => Rule::requiredIf(fn () => 'POST' == $this->method()),
'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',
'region_id' => 'nullable|exists:regions,id',
'country_id' => 'required|exists:countries,id',
'nationality_id' => 'required|exists:nationalities,id',
'email' => 'nullable|email',
'email_parents' => 'nullable|email',
'bill_kind_id' => 'nullable|exists:bill_kinds,id',
'joined_at' => 'date|required',
'confession_id' => 'nullable|exists:confessions,id',
2022-03-15 17:28:27 +01:00
'efz' => 'nullable|date',
2021-04-11 20:01:37 +02:00
];
}
2022-03-11 20:19:17 +01:00
public function persistCreate(): void
{
2021-07-15 20:56:53 +02:00
$this->merge(['group_id' => Group::where('nami_id', Auth::user()->getNamiGroupId())->firstOrFail()->id]);
2021-06-24 00:12:47 +02:00
$member = Member::create($this->input());
2022-03-11 20:19:17 +01:00
if ($this->input('has_nami')) {
2021-06-24 00:12:47 +02:00
CreateJob::dispatch($member, auth()->user());
}
2021-04-11 20:01:37 +02:00
}
2022-02-12 16:16:56 +01:00
public function persistUpdate(Member $member): void
{
2021-07-05 13:31:20 +02:00
$member->update($this->input());
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) {
2021-06-23 01:45:48 +02:00
CreateJob::dispatch($member, auth()->user());
}
2022-03-11 20:19:17 +01:00
if ($this->input('has_nami') && null !== $member->nami_id) {
2021-06-23 01:45:48 +02:00
UpdateJob::dispatch($member, auth()->user());
}
2022-03-11 20:19:17 +01:00
if (!$this->input('has_nami') && null !== $member->nami_id) {
2021-06-24 00:12:47 +02:00
DeleteJob::dispatch($member, auth()->user());
}
2021-04-11 20:01:37 +02:00
}
}