New code for import routine.

This commit is contained in:
James Cole
2016-07-02 17:33:57 +02:00
parent cbe3fb73a8
commit a56a5fc228
11 changed files with 264 additions and 129 deletions

View File

@@ -14,10 +14,11 @@ namespace FireflyIII\Import\Importer;
use ExpandedForm;
use FireflyIII\Crud\Account\AccountCrud;
use FireflyIII\Import\Role\Map;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\ImportJob;
use Illuminate\Http\Request;
use League\Csv\Reader;
use Log;
use Symfony\Component\HttpFoundation\FileBag;
/**
@@ -32,11 +33,38 @@ class CsvImporter implements ImporterInterface
public $job;
/**
* Create initial (empty) configuration array.
*
*
*
* @return bool
*/
public function configure(): bool
{
if (is_null($this->job->configuration) || (is_array($this->job->configuration) && count($this->job->configuration) === 0)) {
Log::debug('No config detected, will create empty one.');
$config = [
'has-headers' => false, // assume
'date-format' => 'Ymd', // assume
'delimiter' => ',', // assume
'import-account' => 0, // none,
'specifics' => [], // none
'column-count' => 0, // unknown
'column-roles' => [], // unknown
'column-do-mapping' => [], // not yet set which columns must be mapped
'column-roles-complete' => false, // not yet configured roles for columns
'column-mapping-config' => [], // no mapping made yet.
'column-mapping-complete' => false, // so mapping is not complete.
];
$this->job->configuration = $config;
$this->job->save();
return true;
}
// need to do nothing, for now.
Log::debug('Detected config in upload, will use that one. ', $this->job->configuration);
return true;
}
@@ -89,15 +117,17 @@ class CsvImporter implements ImporterInterface
'columnCount' => 0,
];
if (!isset($config['columns'])) {
if ($this->doColumnRoles()) {
// show user column configuration.
// show user column role configuration.
$content = $this->job->uploadFileContents();
// create CSV reader.
$reader = Reader::createFromString($content);
$start = $config['has_headers'] ? 1 : 0;
$start = $config['has-headers'] ? 1 : 0;
$end = $start + self::EXAMPLE_ROWS; // first X rows
// collect example data in $data['columns']
while ($start < $end) {
$row = $reader->fetchOne($start);
foreach ($row as $index => $value) {
@@ -110,20 +140,28 @@ class CsvImporter implements ImporterInterface
$data['columnCount'] = count($row);
}
// make unique
// make unique example data
foreach ($data['columns'] as $index => $values) {
$data['columns'][$index] = array_unique($values);
}
// TODO preset roles from config
$data['set_roles'] = [];
// collect possible column roles:
$data['available_roles'] = [];
foreach (array_keys(config('csv.import_roles')) as $role) {
$data['available_roles'][$role] = trans('csv.csv_column_'.$role);
$data['available_roles'][$role] = trans('csv.column_' . $role);
}
$config['column-count'] = $data['columnCount'];
$this->job->configuration = $config;
$this->job->save();
return $data;
}
echo 'no settings to do.';
exit;
}
@@ -135,18 +173,10 @@ class CsvImporter implements ImporterInterface
*/
public function getViewForSettings(): string
{
return 'import.csv.map';
}
/**
* Returns a Map thing used to allow the user to
* define roles for each entry.
*
* @return Map
*/
public function prepareRoles(): Map
{
return 'do not work';
if ($this->doColumnRoles()) {
return 'import.csv.roles';
}
echo 'no view for settings';
exit;
}
@@ -174,33 +204,25 @@ class CsvImporter implements ImporterInterface
*/
public function saveImportConfiguration(array $data, FileBag $files): bool
{
/*
* TODO file upload is ignored for now.
*/
/** @var AccountCrud $repository */
$repository = app(AccountCrud::class);
$account = $repository->find(intval($data['csv_import_account']));
$hasHeaders = isset($data['has_headers']) && intval($data['has_headers']) === 1 ? true : false;
$configuration = [
'has_headers' => $hasHeaders,
'date_format' => $data['date_format'],
'csv_delimiter' => $data['csv_delimiter'],
'csv_import_account' => 0,
'specifics' => [],
];
$repository = app(AccountCrud::class);
$account = $repository->find(intval($data['csv_import_account']));
$hasHeaders = isset($data['has_headers']) && intval($data['has_headers']) === 1 ? true : false;
$config = $this->job->configuration;
$config['has-headers'] = $hasHeaders;
$config['date-format'] = $data['date_format'];
$config['delimiter'] = $data['csv_delimiter'];
if (!is_null($account->id)) {
$configuration['csv_import_account'] = $account->id;
$config['import-account'] = $account->id;
}
// loop specifics.
if (is_array($data['specifics'])) {
if (isset($data['specifics']) && is_array($data['specifics'])) {
foreach ($data['specifics'] as $name => $enabled) {
$configuration['specifics'][] = $name;
$config['specifics'][$name] = 1;
}
}
$this->job->configuration = $configuration;
$this->job->configuration = $config;
$this->job->save();
return true;
@@ -215,4 +237,43 @@ class CsvImporter implements ImporterInterface
{
$this->job = $job;
}
/**
* Store the settings filled in by the user, if applicable.
*
* @param Request $request
*
*/
public function storeSettings(Request $request)
{
$config = $this->job->configuration;
$count = $config['column-count'];
$all = $request->all();
$roleSet = 0;
for ($i = 0; $i < $count; $i++) {
$selectedRole = $all['role'][$i] ?? '_ignore';
$doMapping = isset($all['map'][$i]) && $all['map'][$i] == '1' ? true : false;
if ($selectedRole == '_ignore' && $doMapping === true) {
$doMapping = false; // cannot map ignored columns.
}
if ($selectedRole != '_ignore') {
$roleSet++;
}
$config['column-roles'][$i] = $selectedRole;
$config['column-do-mapping'][$i] = $doMapping;
}
if ($roleSet > 0) {
$config['column-roles-complete'] = true;
$this->job->configuration = $config;
$this->job->save();
}
}
/**
* @return bool
*/
private function doColumnRoles(): bool
{
return $this->job->configuration['column-roles-complete'] === false;
}
}