mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Lots of new code for the import routine part 2
This commit is contained in:
@@ -29,6 +29,7 @@ use FireflyIII\Import\Specifics\SpecificInterface;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
||||
use League\Csv\Reader;
|
||||
use League\Csv\Statement;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -49,6 +50,7 @@ class Map implements ConfigurationInterface
|
||||
* @return array
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
@@ -57,13 +59,15 @@ class Map implements ConfigurationInterface
|
||||
|
||||
// in order to actually map we also need all possible values from the CSV file.
|
||||
$content = $this->job->uploadFileContents();
|
||||
$offset = 0;
|
||||
/** @var Reader $reader */
|
||||
$reader = Reader::createFromString($content);
|
||||
$reader->setDelimiter($this->configuration['delimiter']);
|
||||
if($this->configuration['has-headers']) {
|
||||
$reader->setHeaderOffset(0);
|
||||
if ($this->configuration['has-headers']) {
|
||||
$offset = 1;
|
||||
}
|
||||
$results = $reader->getRecords();
|
||||
$stmt = (new Statement)->offset($offset);
|
||||
$results = $stmt->process($reader);
|
||||
$this->validSpecifics = array_keys(config('csv.import_specifics'));
|
||||
$indexes = array_keys($this->data);
|
||||
$rowIndex = 0;
|
||||
@@ -71,13 +75,12 @@ class Map implements ConfigurationInterface
|
||||
$row = $this->runSpecifics($row);
|
||||
|
||||
//do something here
|
||||
|
||||
foreach ($indexes as $index) { // this is simply 1, 2, 3, etc.
|
||||
if (!isset($row[$index])) {
|
||||
// don't really know how to handle this. Just skip, for now.
|
||||
continue;
|
||||
}
|
||||
$value = $row[$index];
|
||||
$value = trim($row[$index]);
|
||||
if (strlen($value) > 0) {
|
||||
// we can do some preprocessing here,
|
||||
// which is exclusively to fix the tags:
|
||||
|
||||
@@ -45,19 +45,28 @@ class Roles implements ConfigurationInterface
|
||||
* Get the data necessary to show the configuration screen.
|
||||
*
|
||||
* @return array
|
||||
* @throws \League\Csv\Exception
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
$config = $this->job->configuration;
|
||||
$content = $this->job->uploadFileContents();
|
||||
|
||||
$config = $this->job->configuration;
|
||||
$content = $this->job->uploadFileContents();
|
||||
$config['has-headers'] = true;
|
||||
$headers = [];
|
||||
$offset = 0;
|
||||
// create CSV reader.
|
||||
$reader = Reader::createFromString($content);
|
||||
$reader->setDelimiter($config['delimiter']);
|
||||
|
||||
if ($config['has-headers']) {
|
||||
$reader->setHeaderOffset(0);
|
||||
$offset = 1;
|
||||
// get headers:
|
||||
$stmt = (new Statement)->limit(1)->offset(0);
|
||||
$records = $stmt->process($reader);
|
||||
$headers = $records->fetchOne();
|
||||
}
|
||||
$stmt = (new Statement)->limit(intval(config('csv.example_rows', 5)));
|
||||
// example rows:
|
||||
$stmt = (new Statement)->limit(intval(config('csv.example_rows', 5)))->offset($offset);
|
||||
// set data:
|
||||
$roles = $this->getRoles();
|
||||
asort($roles);
|
||||
@@ -65,10 +74,9 @@ class Roles implements ConfigurationInterface
|
||||
'examples' => [],
|
||||
'roles' => $roles,
|
||||
'total' => 0,
|
||||
'headers' => $config['has-headers'] ? $reader->fetchOne(0) : [],
|
||||
'headers' => $headers,
|
||||
];
|
||||
|
||||
|
||||
$records = $stmt->process($reader);
|
||||
foreach ($records as $row) {
|
||||
$row = $this->processSpecifics($row);
|
||||
@@ -142,7 +150,7 @@ class Roles implements ConfigurationInterface
|
||||
{
|
||||
$roles = [];
|
||||
foreach (array_keys(config('csv.import_roles')) as $role) {
|
||||
$roles[$role] = trans('csv.column_' . $role);
|
||||
$roles[$role] = trans('import.column_' . $role);
|
||||
}
|
||||
|
||||
return $roles;
|
||||
@@ -236,7 +244,7 @@ class Roles implements ConfigurationInterface
|
||||
*/
|
||||
private function processSpecifics(array $row): array
|
||||
{
|
||||
$names = array_keys($this->job->configuration['specifics']);
|
||||
$names = array_keys($this->job->configuration['specifics'] ?? []);
|
||||
foreach ($names as $name) {
|
||||
/** @var SpecificInterface $specific */
|
||||
$specific = app('FireflyIII\Import\Specifics\\' . $name);
|
||||
@@ -271,7 +279,7 @@ class Roles implements ConfigurationInterface
|
||||
$this->warning = '';
|
||||
}
|
||||
if (0 === $assigned || !$hasAmount) {
|
||||
$this->warning = strval(trans('csv.roles_warning'));
|
||||
$this->warning = strval(trans('import.roles_warning'));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -22,11 +22,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Import\Configuration\File;
|
||||
|
||||
use FireflyIII\Import\Specifics\SpecificInterface;
|
||||
use FireflyIII\Models\ImportJob;
|
||||
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
||||
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
|
||||
use League\Csv\Reader;
|
||||
use League\Csv\Statement;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -47,7 +45,17 @@ class Upload implements ConfigurationInterface
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return [];
|
||||
$importFileTypes = [];
|
||||
$defaultImportType = config('import.options.file.default_import_format');
|
||||
|
||||
foreach (config('import.options.file.import_formats') as $type) {
|
||||
$importFileTypes[$type] = trans('import.import_file_type_' . $type);
|
||||
}
|
||||
|
||||
return [
|
||||
'default_type' => $defaultImportType,
|
||||
'file_types' => $importFileTypes,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,8 +89,28 @@ class Upload implements ConfigurationInterface
|
||||
*/
|
||||
public function storeConfiguration(array $data): bool
|
||||
{
|
||||
echo 'do something with data.';
|
||||
exit;
|
||||
Log::debug('Now in storeConfiguration for file Upload.');
|
||||
/** @var ImportJobRepositoryInterface $repository */
|
||||
$repository = app(ImportJobRepositoryInterface::class);
|
||||
$type = $data['import_file_type'] ?? 'unknown';
|
||||
$config = $this->job->configuration;
|
||||
$config['file-type'] = in_array($type, config('import.options.file.import_formats')) ? $type : 'unknown';
|
||||
$repository->setConfiguration($this->job, $config);
|
||||
$uploaded = $repository->processFile($this->job, $data['import_file'] ?? null);
|
||||
$this->job->save();
|
||||
Log::debug(sprintf('Result of upload is %s', var_export($uploaded, true)));
|
||||
// process config, if present:
|
||||
if (isset($data['configuration_file'])) {
|
||||
$repository->processConfiguration($this->job, $data['configuration_file']);
|
||||
}
|
||||
$config = $this->job->configuration;
|
||||
$config['has-file-upload'] = $uploaded;
|
||||
$repository->setConfiguration($this->job, $config);
|
||||
|
||||
if ($uploaded === false) {
|
||||
$this->warning = 'No valid upload.';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user