StoreCommonCatalogItemRequest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 'print_name' => ['nullable', 'string', 'max:255'],
  39. 'kind' => ['nullable', 'string', 'max:255'],
  40. 'product_group' => ['nullable', 'string', 'max:255'],
  41. 'dimension_length' => ['nullable', 'string', 'max:255'],
  42. 'dimension_width' => ['nullable', 'string', 'max:255'],
  43. 'dimension_height' => ['nullable', 'string', 'max:255'],
  44. 'site_length' => ['nullable', 'string', 'max:255'],
  45. 'site_width' => ['nullable', 'string', 'max:255'],
  46. 'fall_height' => ['nullable', 'numeric', 'min:0'],
  47. 'additional_info' => ['nullable', 'string'],
  48. 'dimension_unit' => ['nullable', 'string', 'max:50'],
  49. 'weight' => ['nullable', 'numeric', 'min:0'],
  50. 'volume' => ['nullable', 'numeric', 'min:0'],
  51. 'places' => ['nullable', 'integer', 'min:0'],
  52. 'composition' => ['nullable', 'string'],
  53. 'characteristics' => ['nullable', 'string'],
  54. 'technical_description' => ['nullable', 'string'],
  55. 'technical_description_short' => ['nullable', 'string'],
  56. 'age_group' => ['nullable', 'string', 'max:100'],
  57. 'max_users' => ['nullable', 'integer', 'min:0'],
  58. 'unit' => ['nullable', 'string', 'max:50'],
  59. 'series' => ['nullable', 'string', 'max:255'],
  60. 'trademark' => ['nullable', 'string', 'max:255'],
  61. 'calculator_enabled' => ['nullable', 'boolean'],
  62. 'builders_price' => ['nullable', 'numeric', 'min:0'],
  63. 'wholesale_price' => ['nullable', 'numeric', 'min:0'],
  64. 'recommended_price' => ['nullable', 'numeric', 'min:0'],
  65. 'retail_price' => ['nullable', 'numeric', 'min:0'],
  66. 'project_price' => ['nullable', 'numeric', 'min:0'],
  67. 'project_with_installation_price' => ['nullable', 'numeric', 'min:0'],
  68. 'pik_price' => ['nullable', 'numeric', 'min:0'],
  69. 'recommended_plus_10_price' => ['nullable', 'numeric', 'min:0'],
  70. ];
  71. $access = app(AccessService::class);
  72. foreach (array_keys($rules) as $field) {
  73. if (! $access->canUpdateField($this->user(), 'common-catalog', $field)) {
  74. unset($rules[$field]);
  75. }
  76. }
  77. return $rules;
  78. }
  79. }