2017-12-16 01:03:35 -06:00
|
|
|
<?php
|
2017-12-17 07:41:58 -06:00
|
|
|
/**
|
|
|
|
* ConfigurationController.php
|
|
|
|
* Copyright (c) 2017 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/>.
|
|
|
|
*/
|
2017-12-16 01:03:35 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Import;
|
|
|
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2017-12-19 12:25:50 -06:00
|
|
|
use FireflyIII\Http\Middleware\IsDemoUser;
|
2017-12-16 01:03:35 -06:00
|
|
|
use FireflyIII\Import\Configuration\ConfiguratorInterface;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
2017-12-16 10:19:10 -06:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Log;
|
2017-12-16 01:03:35 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ConfigurationController
|
|
|
|
*/
|
|
|
|
class ConfigurationController extends Controller
|
|
|
|
{
|
|
|
|
/** @var ImportJobRepositoryInterface */
|
|
|
|
public $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
app('view')->share('mainTitleIcon', 'fa-archive');
|
|
|
|
app('view')->share('title', trans('firefly.import_index_title'));
|
|
|
|
$this->repository = app(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2018-01-25 12:02:14 -06:00
|
|
|
$this->middleware(IsDemoUser::class);
|
2017-12-16 01:03:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure the job. This method is returned to until job is deemed "configured".
|
|
|
|
*
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-16 01:03:35 -06:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function index(ImportJob $job)
|
|
|
|
{
|
|
|
|
// create configuration class:
|
|
|
|
$configurator = $this->makeConfigurator($job);
|
|
|
|
|
|
|
|
// is the job already configured?
|
|
|
|
if ($configurator->isJobConfigured()) {
|
|
|
|
$this->repository->updateStatus($job, 'configured');
|
|
|
|
|
2017-12-16 10:19:10 -06:00
|
|
|
return redirect(route('import.status', [$job->key]));
|
2017-12-16 01:03:35 -06:00
|
|
|
}
|
2018-01-04 11:34:51 -06:00
|
|
|
|
2017-12-16 10:19:10 -06:00
|
|
|
$this->repository->updateStatus($job, 'configuring');
|
2018-01-04 11:34:51 -06:00
|
|
|
|
2017-12-16 01:03:35 -06:00
|
|
|
$view = $configurator->getNextView();
|
|
|
|
$data = $configurator->getNextData();
|
|
|
|
$subTitle = trans('firefly.import_config_bread_crumb');
|
|
|
|
$subTitleIcon = 'fa-wrench';
|
|
|
|
|
|
|
|
return view($view, compact('data', 'job', 'subTitle', 'subTitleIcon'));
|
|
|
|
}
|
|
|
|
|
2017-12-16 10:19:10 -06:00
|
|
|
/**
|
2017-12-17 12:06:11 -06:00
|
|
|
* Store the configuration. Returns to "configure" method until job is configured.
|
|
|
|
*
|
2017-12-16 10:19:10 -06:00
|
|
|
* @param Request $request
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-16 10:19:10 -06:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function post(Request $request, ImportJob $job)
|
|
|
|
{
|
|
|
|
Log::debug('Now in postConfigure()', ['job' => $job->key]);
|
|
|
|
$configurator = $this->makeConfigurator($job);
|
|
|
|
|
|
|
|
// is the job already configured?
|
|
|
|
if ($configurator->isJobConfigured()) {
|
|
|
|
return redirect(route('import.status', [$job->key]));
|
|
|
|
}
|
2017-12-16 12:48:31 -06:00
|
|
|
$data = $request->all();
|
2017-12-16 10:19:10 -06:00
|
|
|
$configurator->configureJob($data);
|
|
|
|
|
|
|
|
// get possible warning from configurator:
|
|
|
|
$warning = $configurator->getWarningMessage();
|
|
|
|
|
2018-04-27 23:23:13 -05:00
|
|
|
if (\strlen($warning) > 0) {
|
2017-12-16 10:19:10 -06:00
|
|
|
$request->session()->flash('warning', $warning);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return to configure
|
|
|
|
return redirect(route('import.configure', [$job->key]));
|
|
|
|
}
|
|
|
|
|
2017-12-16 01:03:35 -06:00
|
|
|
/**
|
|
|
|
* @param ImportJob $job
|
|
|
|
*
|
|
|
|
* @return ConfiguratorInterface
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function makeConfigurator(ImportJob $job): ConfiguratorInterface
|
|
|
|
{
|
|
|
|
$type = $job->file_type;
|
|
|
|
$key = sprintf('import.configuration.%s', $type);
|
|
|
|
$className = config($key);
|
|
|
|
if (null === $className || !class_exists($className)) {
|
2017-12-16 10:19:10 -06:00
|
|
|
throw new FireflyException(sprintf('Cannot find configurator class for job of type "%s".', $type)); // @codeCoverageIgnore
|
2017-12-16 01:03:35 -06:00
|
|
|
}
|
2018-01-03 00:35:29 -06:00
|
|
|
Log::debug(sprintf('Going to create class "%s"', $className));
|
2017-12-16 01:03:35 -06:00
|
|
|
/** @var ConfiguratorInterface $configurator */
|
|
|
|
$configurator = app($className);
|
|
|
|
$configurator->setJob($job);
|
|
|
|
|
|
|
|
return $configurator;
|
|
|
|
}
|
2017-12-22 11:32:43 -06:00
|
|
|
}
|