relations = $relations; } /** * @param mixed $entity */ public function setEntity($entity): void { $this->entity = $entity; } /** * @param mixed $repo */ public function setRepo($repo): void { $this->repo = $repo; } /** * @param mixed $fields */ public function setFields($fields): void { $this->fields = $fields; } public function updateEntity($attributes,$existingRecord){ $finalAttributes = $this->prepareAttributes($attributes); $existingRecord->fill($finalAttributes); $existingRecord->save(); return $existingRecord; } /** * Import row from csv and create entity * * @param $attributes * @return mixed */ public function createEntity($attributes){ $finalAttributes = $this->prepareAttributes($attributes); $entity = $this->repo->create($finalAttributes,$this->entity); return $entity; } /** * Prepare raw import attributes without persisting the entity. * * @param array $attributes * @return array */ public function prepareAttributes(array $attributes): array { $finalAttributes = []; $assginedTo = ''; foreach ($attributes as $key => $attribute) { if ($key == '') { continue; } $result = CrudHelper::array_search_key($key, $this->fields); if (isset($result['type']) && $result['type'] == 'assigned_to') { $assginedTo = $attribute; } else if (isset($result['type']) && $result['type'] == 'manyToOne') { if ($attribute != '') { $attribute = $this->getManyToOneId($result['relation'], $result['column'], $attribute); $finalAttributes[$key] = $attribute; } } else { $finalAttributes[$key] = $attribute; } } if ($assginedTo != '') { $user = $this->getUserByName($assginedTo); if (!empty($user)) { $finalAttributes['owned_by_id'] = $user->id; $finalAttributes['owned_by_type'] = User::class; } else { $group = $this->getGroupByName($assginedTo); if (!empty($group)) { $finalAttributes['owned_by_id'] = $group->id; $finalAttributes['owned_by_type'] = Group::class; } } } // If entity has first_name/last_name but no full_name, populate it so batch inserts keep names if (!array_key_exists('full_name', $finalAttributes)) { $first = ''; $last = ''; if (array_key_exists('first_name', $finalAttributes)) { $first = trim($finalAttributes['first_name']); } if (array_key_exists('last_name', $finalAttributes)) { $last = trim($finalAttributes['last_name']); } $combined = trim($first . ' ' . $last); if ($combined !== '') { $finalAttributes['full_name'] = $combined; } } return $finalAttributes; } /** * Preload all many-to-one relation lookups in batch for all CSV rows. * * @param array $csvData * @param array $requestFields */ public function preloadRelations(array $csvData, array $requestFields): void { $relationColumns = []; foreach ($this->fields as $fieldName => $field) { if (!isset($field['type']) || $field['type'] !== 'manyToOne') { continue; } if (!isset($requestFields[$fieldName])) { continue; } $csvColumnIndex = $requestFields[$fieldName]; $relationKey = $field['relation']; $column = $field['column'] ?? 'name'; if (!isset($relationColumns[$relationKey])) { $relationColumns[$relationKey] = [ 'column' => $column, 'values' => [], ]; } foreach ($csvData as $row) { if (isset($row[$csvColumnIndex]) && !empty($row[$csvColumnIndex])) { $value = trim($row[$csvColumnIndex]); if ($value !== '') { $relationColumns[$relationKey]['values'][$value] = strtolower($value); } } } } foreach ($relationColumns as $relationKey => $config) { if (empty($config['values'])) { continue; } if (!empty($this->relations[$relationKey])) { $relation = $this->relations[$relationKey]; $modelClass = \App::make($relation['model']); $column = $config['column']; $values = array_values($config['values']); $results = $modelClass::whereIn($column, $values) ->pluck('id', $column) ->toArray(); foreach ($results as $name => $id) { $cacheKey = $relationKey . '|' . $column . '|' . strtolower($name); $this->relationCache[$cacheKey] = $id; } } } } /** * Get cached related entity id for a many-to-one lookup. * * @param string $relationKey * @param string $column * @param string $attribute * @return int|null */ private function getManyToOneId($relationKey, $column, $attribute) { $cacheKey = $relationKey . '|' . $column . '|' . strtolower($attribute); if (array_key_exists($cacheKey, $this->relationCache)) { return $this->relationCache[$cacheKey]; } $id = null; if (!empty($this->relations[$relationKey])) { $relation = $this->relations[$relationKey]; $dictionary = \App::make($relation['model'])->where($column, '=', strtolower($attribute))->first(); if (!empty($dictionary)) { $id = $dictionary->id; } } $this->relationCache[$cacheKey] = $id; return $id; } private function getUserByName($name) { if (array_key_exists($name, $this->userCache)) { return $this->userCache[$name]; } $this->userCache[$name] = User::whereName($name)->first(); return $this->userCache[$name]; } private function getGroupByName($name) { if (array_key_exists($name, $this->groupCache)) { return $this->groupCache[$name]; } $this->groupCache[$name] = Group::whereName($name)->first(); return $this->groupCache[$name]; } }