clrghouz/app/Http/Requests/AddressMerge.php

55 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2023-09-08 14:07:46 +00:00
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
2023-09-15 12:57:32 +00:00
use App\Models\{Address,System};
2023-09-08 14:07:46 +00:00
class AddressMerge extends FormRequest
{
protected $stopOnFirstFailure = TRUE;
public function authorize()
{
2024-04-25 05:27:45 +00:00
return Gate::allows('admin');
2023-09-08 14:07:46 +00:00
}
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);
2023-09-15 12:57:32 +00:00
if ((! $dst->active) && ($dst->system_id !== $src->system_id) && ($src->system->name !== System::default))
$fail('Destination must be active, or be from the same system');
2023-09-08 14:07:46 +00:00
},
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)
2023-09-08 14:07:46 +00:00
$fail('Source and Destination must be the same FTN');
},
],
];
}
}