clrghouz/app/Http/Requests/AddressMerge.php

55 lines
1.5 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Models\{Address,System};
class AddressMerge extends FormRequest
{
protected $stopOnFirstFailure = TRUE;
public function authorize()
{
return Gate::allows('admin');
}
public function rules(Request $request)
{
if (! $request->isMethod('post'))
return [];
return [
'src' => [
'required',
'exists:addresses,id',
],
'dst' => [
'required',
'exists:addresses,id',
'different:src',
function ($attribute,$value,$fail) use ($request) {
$dst = Address::withTrashed()->findOrFail($value);
$src = Address::withTrashed()->findOrFail($request->src);
if ((! $dst->active) && ($dst->system_id !== $src->system_id) && ($src->system->name !== System::default))
$fail('Destination must be active, or be from the system system');
},
function ($attribute,$value,$fail) use ($request) {
$dst = Address::withTrashed()->findOrFail($value);
$src = Address::withTrashed()->findOrFail($request->src);
if ($src->zone->domain->flatten && $dst->zone->domain->flatten) {
if ((! $src->zone->domain->zones->pluck('id')->contains($dst->zone_id)) || (! $dst->zone->domain->zones->pluck('id')->contains($src->zone_id)))
$fail('Source must be Destination\'s domain zone');
} elseif ($src->ftn !== $dst->ftn)
$fail('Source and Destination must be the same FTN');
},
],
];
}
}