CreateOrUpdateRequest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Requests\API\Task;
  3. use App\Http\Requests\CustomFieldRuleHelper;
  4. use App\Http\Requests\NamingRuleHelper;
  5. use App\Http\Requests\RuleHelper;
  6. use App\Models\Enums\TaskACL;
  7. use App\Models\User;
  8. use Illuminate\Foundation\Http\FormRequest;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Validation\Rule;
  11. use Illuminate\Validation\Rules\Enum;
  12. class CreateOrUpdateRequest extends FormRequest
  13. {
  14. use RuleHelper, CustomFieldRuleHelper, NamingRuleHelper;
  15. /**
  16. * Determine if the user is authorized to make this request.
  17. */
  18. public function authorize(): bool
  19. {
  20. return true;
  21. }
  22. /**
  23. * Get the validation rules that apply to the request.
  24. *
  25. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  26. */
  27. public function rules(): array
  28. {
  29. $rules = $this->generalRules();
  30. $taskRules = $this->customFieldRuleByGroup("task", ['doc_type', 'task_type', 'doc_stage', "state", "suitability"]);
  31. if ($this->has("naming_rule_id") && $this->get("naming_rule_id") > 0) {
  32. $this->namingRuleCheck($this->get("naming_rule_id"));
  33. $namingRules = $this->customFieldRuleByGroup($this->get("naming_rule_id"));
  34. $rules = [...$rules, ... $namingRules];
  35. }
  36. return [
  37. ...$rules,
  38. ...$taskRules
  39. ];
  40. }
  41. public function importRules()
  42. {
  43. $taskRules = $this->customFieldRuleByGroup("task", ['doc_type', 'task_type', 'doc_stage', "state", "suitability"], 'label');
  44. return [
  45. ...$this->rules(),
  46. ...$taskRules,
  47. ];
  48. }
  49. public function generalRules(): array
  50. {
  51. return [
  52. 'project_id' => [
  53. 'required',
  54. Rule::exists('projects', 'id')->where($this->userCompanyWhere()),
  55. ],
  56. 'requirement_id' => [
  57. 'nullable',
  58. Rule::exists('requirements', 'id')->where($this->userCompanyWhere()),
  59. ],
  60. 'naming_rule_id' => [
  61. Rule::when($this->get('naming_rule_id') > 0, [
  62. Rule::exists('naming_rules', 'id')->whereIn('company_id', [
  63. 0, Auth::user()->company_id,
  64. ]),
  65. ])
  66. ],
  67. 'assign' => [
  68. 'nullable',
  69. Rule::exists('users', 'id')->where($this->userCompanyWhere()),
  70. ],
  71. 'name' => 'required|max:255',
  72. 'parent_id' => [
  73. Rule::when($this->get('parent_id') > 0, [
  74. Rule::exists('tasks', 'id')->where($this->userCompanyWhere())->where("parent_id", 0),
  75. ])
  76. ],
  77. 'begin' => ['nullable','date'],
  78. 'end' => ['nullable','date'],
  79. 'acl' => [
  80. new Enum(TaskACL::class),
  81. ],
  82. 'whitelist' => [
  83. 'array',
  84. 'nullable',
  85. function ($attribute, $value, $fail) {
  86. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  87. if ($userCount != count($value)) {
  88. $fail('The selected user is invalid.');
  89. }
  90. }
  91. ],
  92. 'mailto' => [
  93. 'array',
  94. 'nullable',
  95. function ($attribute, $value, $fail) {
  96. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  97. if ($userCount != count($value)) {
  98. $fail('The selected user is invalid.');
  99. }
  100. }
  101. ],
  102. ];
  103. }
  104. }