Can now create jobs, and set prerequisites for the fake provider, which will be skipped when they're not necessary.

This commit is contained in:
James Cole 2018-04-29 19:07:54 +02:00
parent b5be1b11d1
commit d2bb65bf04
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
8 changed files with 237 additions and 40 deletions

View File

@ -74,8 +74,23 @@ class IndexController extends Controller
{
$importJob = $this->repository->create($importProvider);
// redirect to global prerequisites
return redirect(route('import.prerequisites.index', [$importProvider, $importJob->key]));
// if need to set prerequisites, do that first.
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $importProvider)); // @codeCoverageIgnore
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if (!$object->isComplete()) {
// redirect to global prerequisites
return redirect(route('import.prerequisites.index', [$importProvider, $importJob->key]));
}
// Otherwise just redirect to job configuration.
return redirect(route('import.job.configuration.index', [$importJob->key]));
}
// /**

View File

@ -27,6 +27,7 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Import\Prerequisites\PrerequisitesInterface;
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
use Illuminate\Http\Request;
use Log;
@ -36,6 +37,9 @@ use Log;
class PrerequisitesController extends Controller
{
/** @var ImportJobRepositoryInterface */
private $repository;
/**
*
*/
@ -48,6 +52,8 @@ class PrerequisitesController extends Controller
app('view')->share('mainTitleIcon', 'fa-archive');
app('view')->share('title', trans('firefly.import_index_title'));
$this->repository = app(ImportJobRepositoryInterface::class);
return $next($request);
}
);
@ -62,29 +68,30 @@ class PrerequisitesController extends Controller
* @param string $importProvider
* @param ImportJob $importJob
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws FireflyException
*/
public function index(string $importProvider, ImportJob $importJob)
public function index(string $importProvider, ImportJob $importJob = null)
{
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) {
throw new FireflyException(sprintf('No class to handle configuration for "%s".', $importProvider)); // @codeCoverageIgnore
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if ($object->hasPrerequisites()) {
$view = $object->getView();
$parameters = ['title' => (string)trans('firefly.import_index_title'), 'mainTitleIcon' => 'fa-archive'];
$parameters = array_merge($object->getViewParameters(), $parameters);
return view($view, $parameters);
// TODO if prerequisites have been met and job is not null, just skip this step.
if (null !== $importJob && $object->isComplete()) {
// set job to
}
// if no (more) prerequisites, return to create a job:
return redirect(route('import.create-job', [$bank]));
$view = $object->getView();
$parameters = ['title' => (string)trans('firefly.import_index_title'), 'mainTitleIcon' => 'fa-archive', 'importJob' => $importJob];
$parameters = array_merge($object->getViewParameters(), $parameters);
return view($view, $parameters);
}
/**
@ -95,33 +102,25 @@ class PrerequisitesController extends Controller
*
* @see PrerequisitesInterface::storePrerequisites
*
* @param Request $request
* @param string $bank
* @param Request $request
* @param string $importProvider
* @param ImportJob $importJob
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*
* @throws FireflyException
*/
public function post(Request $request, string $bank)
public function post(Request $request, string $importProvider, ImportJob $importJob = null)
{
Log::debug(sprintf('Now in postPrerequisites for %s', $bank));
Log::debug(sprintf('Now in postPrerequisites for %s', $importProvider));
if (true === !config(sprintf('import.enabled.%s', $bank))) {
throw new FireflyException(sprintf('Cannot import from "%s" at this time.', $bank)); // @codeCoverageIgnore
}
$class = (string)config(sprintf('import.prerequisites.%s', $bank));
$class = (string)config(sprintf('import.prerequisites.%s', $importProvider));
if (!class_exists($class)) {
throw new FireflyException(sprintf('Cannot find class %s', $class)); // @codeCoverageIgnore
}
/** @var PrerequisitesInterface $object */
$object = app($class);
$object->setUser(auth()->user());
if (!$object->hasPrerequisites()) {
Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));
return redirect(route('import.create-job', [$bank]));
}
Log::debug('Going to store entered prerequisites.');
// store post data
$result = $object->storePrerequisites($request);
@ -129,8 +128,23 @@ class PrerequisitesController extends Controller
if ($result->count() > 0) {
$request->session()->flash('error', $result->first());
// redirect back:
return redirect(route('import.prerequisites.index', [$importProvider, $importJob->key]))->withInput();
}
return redirect(route('import.prerequisites', [$bank]));
// session flash!
$request->session()->flash('success', (string)trans('firefly.prerequisites_saved_for_' . $importProvider));
// if has job, redirect to global config for provider
// if no job, back to index!
if (null === $importJob) {
return redirect(route('import.index'));
}
// redirect to global config:
return redirect(route('import.index'));
}
}

View File

@ -0,0 +1,127 @@
<?php
/**
* FakePrerequisites.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Import\Prerequisites;
use FireflyIII\User;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
/**
* This class contains all the routines necessary for the fake import provider.
*
* Class FakePrerequisites
*/
class FakePrerequisites implements PrerequisitesInterface
{
/** @var User */
private $user;
/**
* Returns view name that allows user to fill in prerequisites. Currently asks for the API key.
*
* @return string
*/
public function getView(): string
{
return 'import.fake.prerequisites';
}
/**
* Returns any values required for the prerequisites-view.
*
* @return array
*/
public function getViewParameters(): array
{
$apiKey = '';
if ($this->hasApiKey()) {
$apiKey = app('preferences')->getForUser($this->user, 'fake_api_key', null)->data;
}
$oldKey = (string)\request()->old('api_key');
if ($oldKey !== '') {
$apiKey = \request()->old('api_key');
}
return ['api_key' => $apiKey];
}
/**
* Indicate if all prerequisites have been met.
*
* @return bool
*/
public function isComplete(): bool
{
return $this->hasApiKey();
}
/**
* Set the user for this Prerequisites-routine. Class is expected to implement and save this.
*
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param Request $request
*
* @return MessageBag
*/
public function storePrerequisites(Request $request): MessageBag
{
$apiKey = (string)$request->get('api_key');
$messageBag = new MessageBag();
if (32 !== \strlen($apiKey)) {
$messageBag->add('api_key', 'API key must be 32 chars.');
return $messageBag;
}
app('preferences')->setForUser($this->user, 'fake_api_key', $apiKey);
return $messageBag;
}
/**
* @return bool
*/
private function hasApiKey(): bool
{
$apiKey = app('preferences')->getForUser($this->user, 'fake_api_key', false);
if (null === $apiKey) {
return false;
}
if (null === $apiKey->data) {
return false;
}
if (\strlen((string)$apiKey->data) === 32) {
return true;
}
return false;
}
}

View File

@ -45,14 +45,6 @@ interface PrerequisitesInterface
*/
public function getViewParameters(): array;
/**
* Returns if this import method has any special prerequisites such as config
* variables or other things.
*
* @return bool
*/
public function hasPrerequisites(): bool;
/**
* Indicate if all prerequisites have been met.
*
@ -68,6 +60,10 @@ interface PrerequisitesInterface
public function setUser(User $user): void;
/**
* This method responds to the user's submission of an API key. Should do nothing but store the value.
*
* Errors must be returned in the message bag under the field name they are requested by.
*
* @param Request $request
*
* @return MessageBag

View File

@ -6,6 +6,7 @@ use FireflyIII\Import\Configuration\FileConfigurator;
use FireflyIII\Import\Configuration\SpectreConfigurator;
use FireflyIII\Import\FileProcessor\CsvProcessor;
use FireflyIII\Import\Prerequisites\BunqPrerequisites;
use FireflyIII\Import\Prerequisites\FakePrerequisites;
use FireflyIII\Import\Prerequisites\FilePrerequisites;
use FireflyIII\Import\Prerequisites\SpectrePrerequisites;
use FireflyIII\Import\Routine\BunqRoutine;
@ -53,7 +54,7 @@ return [
'yodlee' => true,
],
'prerequisites' => [
'fake' => false,
'fake' => FakePrerequisites::class,
'file' => FilePrerequisites::class,
'bunq' => BunqPrerequisites::class,
'spectre' => SpectrePrerequisites::class,

View File

@ -0,0 +1,43 @@
{% extends "./layout/default" %}
{% block breadcrumbs %}
{{ Breadcrumbs.render }}
{% endblock %}
{% block content %}
<div class="row">
<form class="form-horizontal" action="{{ route('import.prerequisites.post',['fake', importJob.key]) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">Fake prerequisites</h3>
</div>
<div class="box-body">
<div class="row">
<div class="col-lg-8">
<p>
Bla bla bla bla
</p>
</div>
</div>
<div class="row">
<div class="col-lg-8">
{{ ExpandedForm.text('api_key', api_key) }}
</div>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">
{{ ('submit')|_ }}
</button>
</div>
</div>
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{% endblock %}
{% block styles %}
{% endblock %}

View File

@ -593,10 +593,10 @@ Breadcrumbs::register(
);
Breadcrumbs::register(
'import.prerequisites',
function (BreadCrumbsGenerator $breadcrumbs, string $bank) {
'import.prerequisites.index',
function (BreadCrumbsGenerator $breadcrumbs, string $importProvider) {
$breadcrumbs->parent('import.index');
$breadcrumbs->push(trans('import.prerequisites'), route('import.prerequisites', [$bank]));
$breadcrumbs->push(trans('import.prerequisites'), route('import.prerequisites.index', [$importProvider]));
}
);

View File

@ -449,7 +449,8 @@ Route::group(
Route::get('create/{import_provider}', ['uses' => 'Import\IndexController@create', 'as' => 'create']);
// set global prerequisites for an import source, possible with a job already attached.
Route::get('prerequisites/{import_provider}/{importJob}', ['uses' => 'Import\PrerequisitesController@index', 'as' => 'prerequisites.index']);
Route::get('prerequisites/{import_provider}/{importJob?}', ['uses' => 'Import\PrerequisitesController@index', 'as' => 'prerequisites.index']);
Route::post('prerequisites/{import_provider}/{importJob?}', ['uses' => 'Import\PrerequisitesController@post', 'as' => 'prerequisites.post']);
// import method prerequisites:
#
#