|
|
|
@ -72,66 +72,101 @@ class MapController extends Controller |
|
|
|
*/ |
|
|
|
public function index() |
|
|
|
{ |
|
|
|
$mainTitle = 'Map data'; |
|
|
|
$subTitle = 'Map values in file to actual data in Firefly III'; |
|
|
|
$data = []; |
|
|
|
$roles = []; |
|
|
|
Log::debug('Now in mapController index'); |
|
|
|
|
|
|
|
// get configuration object.
|
|
|
|
$mainTitle = 'Map data'; |
|
|
|
$subTitle = 'Map values in file to actual data in Firefly III'; |
|
|
|
$configuration = $this->restoreConfiguration(); |
|
|
|
$data = []; |
|
|
|
$roles = []; |
|
|
|
|
|
|
|
// depends on flow how to handle mapping
|
|
|
|
if ('csv' === $configuration->getFlow()) { |
|
|
|
$roles = $configuration->getRoles(); |
|
|
|
$data = $this->getCSVMapInformation(); |
|
|
|
} |
|
|
|
|
|
|
|
// then we can use them:
|
|
|
|
$roles = $configuration->getRoles(); |
|
|
|
$existingMapping = $configuration->getMapping(); |
|
|
|
$doMapping = $configuration->getDoMapping(); |
|
|
|
$data = []; |
|
|
|
|
|
|
|
foreach ($roles as $index => $role) { |
|
|
|
$info = config('csv.import_roles')[$role] ?? null; |
|
|
|
$mappable = $info['mappable'] ?? false; |
|
|
|
if (null === $info) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (false === $mappable) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
$mapColumn = $doMapping[$index] ?? false; |
|
|
|
if (false === $mapColumn) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
Log::debug(sprintf('Mappable role is "%s"', $role)); |
|
|
|
// nordigen, spectre and others:
|
|
|
|
if ('csv' !== $configuration->getFlow()) { |
|
|
|
$roles = []; |
|
|
|
$data = $this->getImporterMapInformation(); |
|
|
|
} |
|
|
|
|
|
|
|
$info['role'] = $role; |
|
|
|
$info['values'] = []; |
|
|
|
// if nothing to map, just set mappable to true and go to the next step:
|
|
|
|
if (0 === count($data)) { |
|
|
|
// set map config as complete.
|
|
|
|
session()->put(Constants::MAPPING_COMPLETE_INDICATOR, true); |
|
|
|
return redirect()->route('007-convert.index'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// create the "mapper" class which will get data from Firefly III.
|
|
|
|
$class = sprintf('App\\Services\\CSV\\Mapper\\%s', $info['mapper']); |
|
|
|
if (!class_exists($class)) { |
|
|
|
throw new InvalidArgumentException(sprintf('Class %s does not exist.', $class)); |
|
|
|
} |
|
|
|
Log::debug(sprintf('Associated class is %s', $class)); |
|
|
|
return view('import.006-mapping.index', compact('mainTitle', 'subTitle', 'roles', 'data')); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Return the map data necessary for the CSV mapping based on some weird helpers. |
|
|
|
* TODO needs refactoring and proper splitting into helpers. |
|
|
|
* |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
private function getCSVMapInformation(): array |
|
|
|
{ |
|
|
|
$configuration = $this->restoreConfiguration(); |
|
|
|
$roles = $configuration->getRoles(); |
|
|
|
$existingMapping = $configuration->getMapping(); |
|
|
|
$doMapping = $configuration->getDoMapping(); |
|
|
|
$data = []; |
|
|
|
|
|
|
|
foreach ($roles as $index => $role) { |
|
|
|
$info = config('csv.import_roles')[$role] ?? null; |
|
|
|
$mappable = $info['mappable'] ?? false; |
|
|
|
if (null === $info) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (false === $mappable) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
$mapColumn = $doMapping[$index] ?? false; |
|
|
|
if (false === $mapColumn) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
Log::debug(sprintf('Mappable role is "%s"', $role)); |
|
|
|
|
|
|
|
/** @var MapperInterface $object */ |
|
|
|
$object = app($class); |
|
|
|
$info['mapping_data'] = $object->getMap(); |
|
|
|
$info['mapped'] = $existingMapping[$index] ?? []; |
|
|
|
$info['role'] = $role; |
|
|
|
$info['values'] = []; |
|
|
|
|
|
|
|
Log::debug(sprintf('Mapping data length is %d', count($info['mapping_data']))); |
|
|
|
|
|
|
|
$data[$index] = $info; |
|
|
|
// create the "mapper" class which will get data from Firefly III.
|
|
|
|
$class = sprintf('App\\Services\\CSV\\Mapper\\%s', $info['mapper']); |
|
|
|
if (!class_exists($class)) { |
|
|
|
throw new InvalidArgumentException(sprintf('Class %s does not exist.', $class)); |
|
|
|
} |
|
|
|
Log::debug(sprintf('Associated class is %s', $class)); |
|
|
|
|
|
|
|
|
|
|
|
// get columns from file
|
|
|
|
$content = StorageService::getContent(session()->get(Constants::UPLOAD_CSV_FILE), $configuration->isConversion()); |
|
|
|
$delimiter = (string) config(sprintf('csv.delimiters.%s', $configuration->getDelimiter())); |
|
|
|
$data = MapperService::getMapData($content, $delimiter, $configuration->isHeaders(), $configuration->getSpecifics(), $data); |
|
|
|
/** @var MapperInterface $object */ |
|
|
|
$object = app($class); |
|
|
|
$info['mapping_data'] = $object->getMap(); |
|
|
|
$info['mapped'] = $existingMapping[$index] ?? []; |
|
|
|
|
|
|
|
Log::debug(sprintf('Mapping data length is %d', count($info['mapping_data']))); |
|
|
|
|
|
|
|
$data[$index] = $info; |
|
|
|
} |
|
|
|
|
|
|
|
// get columns from file
|
|
|
|
$content = StorageService::getContent(session()->get(Constants::UPLOAD_CSV_FILE), $configuration->isConversion()); |
|
|
|
$delimiter = (string) config(sprintf('csv.delimiters.%s', $configuration->getDelimiter())); |
|
|
|
return MapperService::getMapData($content, $delimiter, $configuration->isHeaders(), $configuration->getSpecifics(), $data); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Weird bunch of code to return info on Spectre and Nordigen. |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
private function getImporterMapInformation(): array |
|
|
|
{ |
|
|
|
$data = []; |
|
|
|
$configuration = $this->restoreConfiguration(); |
|
|
|
/* |
|
|
|
* To map Nordigen, pretend the file has one "column" (this is based on the CSV importer after all) |
|
|
|
* that contains: |
|
|
|
@ -181,16 +216,7 @@ class MapController extends Controller |
|
|
|
$opposingName['mapped'] = $existingMapping[$index] ?? []; |
|
|
|
$data[] = $category; |
|
|
|
} |
|
|
|
|
|
|
|
// if nothing to map, just set mappable to true and go to the next step:
|
|
|
|
if (0 === count($data)) { |
|
|
|
// set map config as complete.
|
|
|
|
session()->put(Constants::MAPPING_COMPLETE_INDICATOR, true); |
|
|
|
return redirect()->route('007-convert.index'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return view('import.006-mapping.index', compact('mainTitle', 'subTitle', 'roles', 'data')); |
|
|
|
return $data; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -235,6 +261,13 @@ class MapController extends Controller |
|
|
|
return array_unique($filtered); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @return array |
|
|
|
* @throws ContainerExceptionInterface |
|
|
|
* @throws FileNotFoundException |
|
|
|
* @throws ImporterErrorException |
|
|
|
* @throws NotFoundExceptionInterface |
|
|
|
*/ |
|
|
|
private function getCategories(): array |
|
|
|
{ |
|
|
|
Log::debug(sprintf('Now in %s', __METHOD__)); |
|
|
|
|