| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- declare(strict_types=1);
- namespace App\Http\Requests;
- use App\Models\CommonCatalogItem;
- use App\Services\Access\AccessService;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Validation\Rule;
- class StoreCommonCatalogItemRequest extends FormRequest
- {
- public function authorize(): bool
- {
- if (! $this->user()) {
- return false;
- }
- if (! $this->routeIs('common-catalog.store')) {
- return true;
- }
- $access = app(AccessService::class);
- return $access->canUpdateField($this->user(), 'common-catalog', 'article')
- && $access->canUpdateField($this->user(), 'common-catalog', 'calculator_name');
- }
- public function rules(): array
- {
- $item = $this->route('commonCatalogItem');
- $rules = [
- 'article' => [
- 'required',
- 'string',
- 'max:100',
- Rule::unique('common_catalog_items', 'article')
- ->where(fn ($query) => $query->where(
- 'calculator_name',
- (string) $this->input('calculator_name'),
- ))
- ->ignore($item instanceof CommonCatalogItem ? $item->getKey() : null),
- ],
- 'calculator_name' => ['required', 'string', 'max:255'],
- 'kind' => ['nullable', 'string', 'max:255'],
- 'dimension_length' => ['nullable', 'string', 'max:255'],
- 'dimension_width' => ['nullable', 'string', 'max:255'],
- 'dimension_height' => ['nullable', 'string', 'max:255'],
- 'site_length' => ['nullable', 'string', 'max:255'],
- 'site_width' => ['nullable', 'string', 'max:255'],
- 'fall_height' => ['nullable', 'numeric', 'min:0'],
- 'additional_info' => ['nullable', 'string'],
- 'dimension_unit' => ['nullable', 'string', 'max:50'],
- 'weight' => ['nullable', 'numeric', 'min:0'],
- 'volume' => ['nullable', 'numeric', 'min:0'],
- 'places' => ['nullable', 'integer', 'min:0'],
- 'composition' => ['nullable', 'string'],
- 'age_group' => ['nullable', 'string', 'max:100'],
- 'max_users' => ['nullable', 'integer', 'min:0'],
- 'unit' => ['nullable', 'string', 'max:50'],
- 'series' => ['nullable', 'string', 'max:255'],
- 'trademark' => ['nullable', 'string', 'max:255'],
- 'calculator_enabled' => ['nullable', 'boolean'],
- 'builders_price' => ['nullable', 'numeric', 'min:0'],
- 'wholesale_price' => ['nullable', 'numeric', 'min:0'],
- 'recommended_price' => ['nullable', 'numeric', 'min:0'],
- 'retail_price' => ['nullable', 'numeric', 'min:0'],
- 'project_price' => ['nullable', 'numeric', 'min:0'],
- 'project_with_installation_price' => ['nullable', 'numeric', 'min:0'],
- 'pik_price' => ['nullable', 'numeric', 'min:0'],
- 'recommended_plus_10_price' => ['nullable', 'numeric', 'min:0'],
- ];
- $access = app(AccessService::class);
- foreach (array_keys($rules) as $field) {
- if (! $access->canUpdateField($this->user(), 'common-catalog', $field)) {
- unset($rules[$field]);
- }
- }
- return $rules;
- }
- }
|