StoreCommonCatalogItemRequest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Requests;
  4. use App\Models\CommonCatalogItem;
  5. use App\Services\Access\AccessService;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. use Illuminate\Validation\Rule;
  8. class StoreCommonCatalogItemRequest extends FormRequest
  9. {
  10. public function authorize(): bool
  11. {
  12. if (! $this->user()) {
  13. return false;
  14. }
  15. if (! $this->routeIs('common-catalog.store')) {
  16. return true;
  17. }
  18. $access = app(AccessService::class);
  19. return $access->canUpdateField($this->user(), 'common-catalog', 'article')
  20. && $access->canUpdateField($this->user(), 'common-catalog', 'calculator_name');
  21. }
  22. public function rules(): array
  23. {
  24. $item = $this->route('commonCatalogItem');
  25. $rules = [
  26. 'article' => [
  27. 'required',
  28. 'string',
  29. 'max:100',
  30. Rule::unique('common_catalog_items', 'article')
  31. ->where(fn ($query) => $query->where(
  32. 'calculator_name',
  33. (string) $this->input('calculator_name'),
  34. ))
  35. ->ignore($item instanceof CommonCatalogItem ? $item->getKey() : null),
  36. ],
  37. 'calculator_name' => ['required', 'string', 'max:255'],
  38. 'kind' => ['nullable', 'string', 'max:255'],
  39. 'dimension_length' => ['nullable', 'string', 'max:255'],
  40. 'dimension_width' => ['nullable', 'string', 'max:255'],
  41. 'dimension_height' => ['nullable', 'string', 'max:255'],
  42. 'site_length' => ['nullable', 'string', 'max:255'],
  43. 'site_width' => ['nullable', 'string', 'max:255'],
  44. 'fall_height' => ['nullable', 'numeric', 'min:0'],
  45. 'additional_info' => ['nullable', 'string'],
  46. 'dimension_unit' => ['nullable', 'string', 'max:50'],
  47. 'weight' => ['nullable', 'numeric', 'min:0'],
  48. 'volume' => ['nullable', 'numeric', 'min:0'],
  49. 'places' => ['nullable', 'integer', 'min:0'],
  50. 'composition' => ['nullable', 'string'],
  51. 'age_group' => ['nullable', 'string', 'max:100'],
  52. 'max_users' => ['nullable', 'integer', 'min:0'],
  53. 'unit' => ['nullable', 'string', 'max:50'],
  54. 'series' => ['nullable', 'string', 'max:255'],
  55. 'trademark' => ['nullable', 'string', 'max:255'],
  56. 'calculator_enabled' => ['nullable', 'boolean'],
  57. 'builders_price' => ['nullable', 'numeric', 'min:0'],
  58. 'wholesale_price' => ['nullable', 'numeric', 'min:0'],
  59. 'recommended_price' => ['nullable', 'numeric', 'min:0'],
  60. 'retail_price' => ['nullable', 'numeric', 'min:0'],
  61. 'project_price' => ['nullable', 'numeric', 'min:0'],
  62. 'project_with_installation_price' => ['nullable', 'numeric', 'min:0'],
  63. 'pik_price' => ['nullable', 'numeric', 'min:0'],
  64. 'recommended_plus_10_price' => ['nullable', 'numeric', 'min:0'],
  65. ];
  66. $access = app(AccessService::class);
  67. foreach (array_keys($rules) as $field) {
  68. if (! $access->canUpdateField($this->user(), 'common-catalog', $field)) {
  69. unset($rules[$field]);
  70. }
  71. }
  72. return $rules;
  73. }
  74. }