Can handle some mandatory fields (not all).

This commit is contained in:
James Cole
2017-12-09 20:02:26 +01:00
parent e488d7d84c
commit 2365fb69b4
9 changed files with 207 additions and 29 deletions

View File

@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Support\Import\Configuration\Spectre;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
/**
* Class InputMandatory
*/
class InputMandatory implements ConfigurationInterface
{
/** @var ImportJob */
private $job;
/**
* Get the data necessary to show the configuration screen.
*
* @return array
*/
public function getData(): array
{
$config = $this->job->configuration;
$providerId = $config['provider'];
$provider = SpectreProvider::where('spectre_id', $providerId)->first();
if (is_null($provider)) {
throw new FireflyException(sprintf('Cannot find Spectre provider with ID #%d', $providerId));
}
$fields = $provider->data['required_fields'] ?? [];
$positions = [];
// Obtain a list of columns
foreach ($fields as $key => $row) {
$positions[$key] = $row['position'];
}
array_multisort($positions, SORT_ASC, $fields);
$country = SelectCountry::$allCountries[$config['country']] ?? $config['country'];
return compact('provider', 'country', 'fields');
}
/**
* Return possible warning to user.
*
* @return string
*/
public function getWarningMessage(): string
{
return '';
}
/**
* @param ImportJob $job
*
* @return ConfigurationInterface
*/
public function setJob(ImportJob $job)
{
$this->job = $job;
}
/**
* Store the result.
*
* @param array $data
*
* @return bool
*/
public function storeConfiguration(array $data): bool
{
$config = $this->job->configuration;
$providerId = $config['provider'];
$provider = SpectreProvider::where('spectre_id', $providerId)->first();
if (is_null($provider)) {
throw new FireflyException(sprintf('Cannot find Spectre provider with ID #%d', $providerId));
}
$mandatory = [];
$fields = $provider->data['required_fields'] ?? [];
foreach ($fields as $field) {
$name = $field['name'];
$mandatory[$name] = app('crypt')->encrypt($data[$name]) ?? null;
}
// store in config of job:
$config['mandatory-fields'] = $mandatory;
$config['has-input-mandatory'] = true;
$this->job->configuration = $config;
$this->job->save();
// try to grab login for this job. See what happens?
// fire job that creates login object. user is redirected to "wait here" page (status page). Page should
// refresh and go back to interactive when user is supposed to enter SMS code or something.
// otherwise start downloading stuff
return true;
}
}

View File

@@ -13,7 +13,7 @@ use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
*/
class SelectCountry implements ConfigurationInterface
{
private $allCountries
public static $allCountries
= [
'AF' => 'Afghanistan',
'AX' => 'Aland Islands',
@@ -282,7 +282,7 @@ class SelectCountry implements ConfigurationInterface
$countries = [];
/** @var SpectreProvider $provider */
foreach ($providers as $provider) {
$countries[$provider->country_code] = $this->allCountries[$provider->country_code] ?? $provider->country_code;
$countries[$provider->country_code] = self::$allCountries[$provider->country_code] ?? $provider->country_code;
}
asort($countries);

View File

@@ -9,9 +9,9 @@ use FireflyIII\Models\SpectreProvider;
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
/**
* Class SelectBank
* Class SelectProvider
*/
class SelectBank implements ConfigurationInterface
class SelectProvider implements ConfigurationInterface
{
/** @var ImportJob */
private $job;
@@ -32,8 +32,9 @@ class SelectBank implements ConfigurationInterface
$name = $provider->data['name'];
$providers[$providerId] = $name;
}
$country = SelectCountry::$allCountries[$config['country']] ?? $config['country'];
return compact('providers');
return compact('providers', 'country');
}
/**
@@ -66,8 +67,8 @@ class SelectBank implements ConfigurationInterface
public function storeConfiguration(array $data): bool
{
$config = $this->job->configuration;
$config['bank'] = intval($data['bank_code']) ?? 0; // default to fake country.
$config['selected-bank'] = true;
$config['provider'] = intval($data['provider_code']) ?? 0; // default to fake country.
$config['selected-provider'] = true;
$this->job->configuration = $config;
$this->job->save();