File manager - Edit - /var/www/html/spdc/api/app/Http/Services/OccurrencyFormService.php
Back
<?php namespace App\Http\Services; use App\Models\Form; use App\Models\Field; use App\Models\Occurrency; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\DB; use App\Classes\BaseServiceResponse; use Illuminate\Support\Facades\Validator; class OccurrencyFormService extends BaseService { public function store(array $forms, Occurrency $occurrency): BaseServiceResponse { DB::beginTransaction(); /** * saving forms and fields and validations from registro */ $savedForms = []; $savedFields = []; foreach($forms as $fo) { $savedForms[] = $fo['id']; $form = Form::findOrFail($fo['id']); $occurrencyForm = $occurrency->forms()->create([ ...$form->toArray(), 'section' => $fo['section'] ]); foreach($fo['fields'] as $fi) { $savedFields[] = $fi['id']; $field = Field::findOrFail($fi['id']); $fieldValue = ""; /** * Treat system field */ if ($field->system_item) { $fieldValue = (new FieldService())->getSystemValue($field); } else { /** * validations */ $fieldValue = isset($fi['value']) ? $fi['value'] : ""; $key = str($field->label)->slug()->value(); $fieldRequest = [ $key => $fieldValue ]; $rules = $field->validations->pluck('rule')->toArray(); $validator = Validator::make($fieldRequest, [ $key => $rules ], [], [ $key => $field->label ]); if ($validator->fails()) { DB::rollBack(); return $this->response( [ 'success' => false, 'errors' => $validator->errors()->toArray() ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY, __('general.validation_error') ); } } /** * saving answers */ $occurrencyField = $occurrencyForm->fields()->create([ ...$field->toArray(), 'section' => $fo['section'] ]); $occurrencyValidations = $occurrencyField->validations()->createMany( $field->validations->toArray() ); $occurrencyField->answer()->create([ 'value' => $fieldValue ]); } } /** * treating remaing forms and fields */ $remainingForms = Form::whereRelation('categories', 'id', $occurrency->originalCategory?->id)->whereNotIn('id', $savedForms)->get(); foreach($remainingForms as $form) { $category = array_filter($form->categories->toArray(), function ($category) use ($occurrency) { return $category['id'] === $occurrency->originalCategory?->id; }); $category = array_values($category); $occurrencyForm = $occurrency->forms()->create([ ...$form->toArray(), 'section' => $category[0]['pivot']['section'] ]); foreach($form->fields as $field) { if (!in_array($field->id, $savedFields)) { $fieldValue = ""; /** * Treat system field */ if ($field->system_item) { $fieldValue = (new FieldService())->getSystemValue($field); } else { /** * validations */ $fieldValue = ""; } /** * saving answers */ $occurrencyField = $occurrencyForm->fields()->create([ ...$field->toArray(), 'section' => $category[0]['pivot']['section'] ]); $occurrencyValidations = $occurrencyField->validations()->createMany( $field->validations->toArray() ); $occurrencyField->answer()->create([ 'value' => $fieldValue ]); } } } DB::commit(); return $this->response( [ 'success' => true, 'errors' => [] ], ); } public function update(array $forms, Occurrency $occurrency, string $currentSection): BaseServiceResponse { DB::beginTransaction(); /** * saving forms and fields and validations */ foreach($forms as $fo) { foreach($fo['fields'] as $fi) { $occurrencyField = $occurrency->fields()->where('occurrency_fields.id', $fi['id'])->first(); if (isset($occurrencyField)) { $fieldValue = !$occurrencyField->answer ? "" : $occurrencyField->answer->value; /** * Treat system field */ if ($occurrencyField->system_item && $occurrencyField->must_be_updated) { $fieldValue = (new FieldService())->getSystemValue($occurrencyField); } else { /** * validations */ $fieldValue = isset($fi['value']) ? $fi['value'] : ""; $key = str($occurrencyField->label)->slug()->value(); $occurrencyFieldRequest = [ $key => $fieldValue ]; $rules = $occurrencyField->validations->pluck('rule')->toArray(); $validator = Validator::make($occurrencyFieldRequest, [ $key => $rules ], [], [ $key => $occurrencyField->label ]); if ($validator->fails()) { DB::rollBack(); return $this->response( [ 'success' => false, 'errors' => $validator->errors()->toArray() ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY, __('general.validation_error') ); } } /** * saving answers */ if (!$occurrencyField->answer) { $occurrencyField->answer()->create(['value' => $fieldValue]); } else { $occurrencyField->answer->fill(['value' => $fieldValue])->save(); } } } } DB::commit(); return $this->response( [ 'success' => true, 'errors' => [] ], ); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings