adrema/app/Member/MemberRequest.php

82 lines
2.6 KiB
PHP
Raw Normal View History

2021-04-11 20:01:37 +02:00
<?php
namespace App\Member;
use Illuminate\Foundation\Http\FormRequest;
2021-06-23 01:05:17 +02:00
use Illuminate\Validation\Rule;
use App\Group;
2021-06-23 01:45:48 +02:00
use Illuminate\Support\Str;
use App\Activity;
use Illuminate\Support\Arr;
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 [
2021-06-23 01:05:17 +02:00
'first_activity_id' => Rule::requiredIf(fn() => $this->method() == 'POST'),
'first_subactivity_id' => Rule::requiredIf(fn() => $this->method() == 'POST'),
2021-07-11 22:34:21 +02:00
'subscription_id' => Rule::requiredIf(function() {
2021-06-23 01:45:48 +02:00
if ($this->method() != 'POST') {
return false;
}
if (!$this->input('first_activity_id')) { return true; }
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',
];
}
public function persistCreate() {
2021-06-23 01:05:17 +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());
if($this->input('has_nami')) {
CreateJob::dispatch($member, auth()->user());
}
2021-04-11 20:01:37 +02:00
}
public function persistUpdate(Member $member) {
2021-07-05 13:31:20 +02:00
$member->update($this->input());
2021-06-23 01:45:48 +02:00
if($this->input('has_nami') && $member->nami_id === null) {
CreateJob::dispatch($member, auth()->user());
}
if($this->input('has_nami') && $member->nami_id !== null) {
UpdateJob::dispatch($member, auth()->user());
}
2021-06-24 00:12:47 +02:00
if(!$this->input('has_nami') && $member->nami_id !== null) {
DeleteJob::dispatch($member, auth()->user());
}
2021-04-11 20:01:37 +02:00
}
}