2017-08-04 15:56:14 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BankController.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* 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/>.
|
2017-08-04 15:56:14 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Import;
|
|
|
|
|
2017-12-09 12:08:24 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2017-08-04 15:56:14 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2017-12-09 19:13:00 +01:00
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
2017-08-11 05:21:00 +02:00
|
|
|
use FireflyIII\Support\Import\Prerequisites\PrerequisitesInterface;
|
2017-08-18 21:09:22 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Log;
|
|
|
|
use Session;
|
2017-08-04 15:56:14 +02:00
|
|
|
|
|
|
|
class BankController extends Controller
|
|
|
|
{
|
2017-08-27 08:54:58 +02:00
|
|
|
|
|
|
|
/**
|
2017-12-09 19:13:00 +01:00
|
|
|
* Once there are no prerequisites, this method will create an importjob object and
|
|
|
|
* redirect the user to a view where this object can be used by a bank specific
|
|
|
|
* class to process.
|
2017-08-27 08:54:58 +02:00
|
|
|
*
|
2017-12-09 19:13:00 +01:00
|
|
|
* @param ImportJobRepositoryInterface $repository
|
|
|
|
* @param string $bank
|
2017-08-27 08:54:58 +02:00
|
|
|
*
|
2017-09-09 06:28:21 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|null
|
2017-12-09 19:13:00 +01:00
|
|
|
* @throws FireflyException
|
2017-08-27 08:54:58 +02:00
|
|
|
*/
|
2017-12-09 19:13:00 +01:00
|
|
|
public function createJob(ImportJobRepositoryInterface $repository, string $bank)
|
2017-08-27 08:54:58 +02:00
|
|
|
{
|
|
|
|
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
2017-12-09 19:13:00 +01:00
|
|
|
if (!class_exists($class)) {
|
2017-12-09 12:08:24 +01:00
|
|
|
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
|
|
|
}
|
2017-12-09 19:13:00 +01:00
|
|
|
$importJob = $repository->create($bank);
|
2017-08-27 08:54:58 +02:00
|
|
|
|
2017-12-09 20:02:26 +01:00
|
|
|
return redirect(route('import.file.configure', [$importJob->key]));
|
2017-08-12 07:47:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-18 21:09:22 +02:00
|
|
|
/**
|
2017-08-19 09:22:44 +02:00
|
|
|
* This method processes the prerequisites the user has entered in the previous step.
|
|
|
|
*
|
|
|
|
* Whatever storePrerequisites does, it should make sure that the system is ready to continue immediately. So
|
|
|
|
* no extra calls or stuff, except maybe to open a session
|
|
|
|
*
|
|
|
|
* @see PrerequisitesInterface::storePrerequisites
|
|
|
|
*
|
2017-08-18 21:09:22 +02:00
|
|
|
* @param Request $request
|
|
|
|
* @param string $bank
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-12-09 19:13:00 +01:00
|
|
|
* @throws FireflyException
|
2017-08-18 21:09:22 +02:00
|
|
|
*/
|
|
|
|
public function postPrerequisites(Request $request, string $bank)
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('Now in postPrerequisites for %s', $bank));
|
|
|
|
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
2017-12-09 19:13:00 +01:00
|
|
|
if (!class_exists($class)) {
|
2017-12-09 12:08:24 +01:00
|
|
|
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
|
|
|
}
|
2017-08-18 21:09:22 +02:00
|
|
|
/** @var PrerequisitesInterface $object */
|
|
|
|
$object = app($class);
|
|
|
|
$object->setUser(auth()->user());
|
|
|
|
if (!$object->hasPrerequisites()) {
|
2017-08-18 23:02:29 +02:00
|
|
|
Log::debug(sprintf('No more prerequisites for %s, move to form.', $bank));
|
2017-08-27 08:54:58 +02:00
|
|
|
|
2017-12-09 19:13:00 +01:00
|
|
|
return redirect(route('import.bank.create-job', [$bank]));
|
2017-08-18 21:09:22 +02:00
|
|
|
}
|
|
|
|
Log::debug('Going to store entered preprerequisites.');
|
|
|
|
// store post data
|
|
|
|
$result = $object->storePrerequisites($request);
|
|
|
|
|
|
|
|
if ($result->count() > 0) {
|
|
|
|
Session::flash('error', $result->first());
|
|
|
|
|
|
|
|
return redirect(route('import.bank.prerequisites', [$bank]));
|
|
|
|
}
|
|
|
|
|
2017-12-09 19:13:00 +01:00
|
|
|
return redirect(route('import.bank.create-job', [$bank]));
|
2017-08-18 21:09:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 15:56:14 +02:00
|
|
|
/**
|
2017-08-19 09:22:44 +02:00
|
|
|
* This method shows you, if necessary, a form that allows you to enter any required values, such as API keys,
|
|
|
|
* login passwords or other values.
|
|
|
|
*
|
2017-08-04 15:56:14 +02:00
|
|
|
* @param string $bank
|
2017-08-18 21:09:22 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
2017-12-09 19:13:00 +01:00
|
|
|
* @throws FireflyException
|
2017-08-04 15:56:14 +02:00
|
|
|
*/
|
|
|
|
public function prerequisites(string $bank)
|
|
|
|
{
|
2017-08-11 05:21:00 +02:00
|
|
|
$class = config(sprintf('firefly.import_pre.%s', $bank));
|
2017-12-09 19:13:00 +01:00
|
|
|
if (!class_exists($class)) {
|
2017-12-09 12:08:24 +01:00
|
|
|
throw new FireflyException(sprintf('Cannot find class %s', $class));
|
|
|
|
}
|
2017-08-11 05:21:00 +02:00
|
|
|
/** @var PrerequisitesInterface $object */
|
|
|
|
$object = app($class);
|
|
|
|
$object->setUser(auth()->user());
|
|
|
|
|
2017-08-18 23:02:29 +02:00
|
|
|
if ($object->hasPrerequisites()) {
|
2017-08-11 05:21:00 +02:00
|
|
|
$view = $object->getView();
|
2017-12-09 19:13:00 +01:00
|
|
|
$parameters = ['title' => strval(trans('firefly.import_index_title')), 'mainTitleIcon' => 'fa-archive'];
|
2017-12-09 12:08:24 +01:00
|
|
|
$parameters = $object->getViewParameters() + $parameters;
|
2017-08-12 07:47:42 +02:00
|
|
|
|
2017-08-11 05:21:00 +02:00
|
|
|
return view($view, $parameters);
|
2017-08-18 23:02:29 +02:00
|
|
|
}
|
2017-08-27 08:54:58 +02:00
|
|
|
|
2017-12-09 19:13:00 +01:00
|
|
|
return redirect(route('import.bank.create-job', [$bank]));
|
2017-08-11 05:21:00 +02:00
|
|
|
}
|
2017-08-12 07:48:39 +02:00
|
|
|
}
|