Files
firefly-iii/app/Http/Controllers/Import/IndexController.php

194 lines
7.2 KiB
PHP
Raw Normal View History

<?php
2017-12-17 14:41:58 +01:00
/**
* IndexController.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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Import;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Import\Prerequisites\PrerequisitesInterface;
2018-05-30 18:36:21 +02:00
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
2018-05-13 09:01:10 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-07-20 14:34:56 +02:00
use FireflyIII\Support\Binder\ImportProvider;
2018-05-30 18:36:21 +02:00
use Illuminate\Http\Response as LaravelResponse;
2018-05-13 09:01:10 +02:00
use Log;
/**
2018-07-21 08:06:24 +02:00
*
* Class IndexController
*/
class IndexController extends Controller
{
2018-07-20 14:34:56 +02:00
/** @var array */
public $providers;
2018-07-21 08:06:24 +02:00
/** @var ImportJobRepositoryInterface The import job repository */
public $repository;
2018-07-22 08:10:16 +02:00
/** @var UserRepositoryInterface The user repository */
2018-05-13 09:01:10 +02:00
public $userRepository;
/**
2018-07-21 08:06:24 +02:00
* IndexController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-archive');
2018-07-15 09:38:49 +02:00
app('view')->share('title', (string)trans('firefly.import_index_title'));
2018-05-13 09:01:10 +02:00
$this->repository = app(ImportJobRepositoryInterface::class);
$this->userRepository = app(UserRepositoryInterface::class);
2018-07-20 14:34:56 +02:00
$this->providers = ImportProvider::getProviders();
return $next($request);
}
);
}
/**
2018-05-01 20:47:38 +02:00
* Creates a new import job for $importProvider.
*
* @param string $importProvider
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-12-22 18:32:43 +01:00
*
2018-07-20 14:34:56 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function create(string $importProvider)
{
2018-05-30 18:04:43 +02:00
Log::debug(sprintf('Will create job for provider "%s"', $importProvider));
2018-07-29 07:30:06 +02:00
$importJob = $this->repository->create($importProvider);
$hasPreReq = (bool)config(sprintf('import.has_prereq.%s', $importProvider));
$hasConfig = (bool)config(sprintf('import.has_job_config.%s', $importProvider));
$allowedForDemo = (bool)config(sprintf('import.allowed_for_demo.%s', $importProvider));
$isDemoUser = $this->userRepository->hasRole(auth()->user(), 'demo');
if ($isDemoUser && !$allowedForDemo) {
return redirect(route('import.index'));
}
2018-07-20 14:34:56 +02:00
Log::debug(sprintf('Created job #%d for provider %s', $importJob->id, $importProvider));
2018-07-20 14:34:56 +02:00
// no prerequisites and no config:
if (false === $hasPreReq && false === $hasConfig) {
Log::debug('Provider needs no configuration for job. Job is ready to start.');
$this->repository->updateStatus($importJob, 'ready_to_run');
Log::debug('Redirect to status-page.');
return redirect(route('import.job.status.index', [$importJob->key]));
}
// no prerequisites but job has config:
if (false === $hasPreReq && false !== $hasConfig) {
Log::debug('Provider has no prerequisites. Continue.');
$this->repository->setStatus($importJob, 'has_prereq');
2018-05-13 09:47:51 +02:00
Log::debug('Redirect to configuration.');
return redirect(route('import.job.configuration.index', [$importJob->key]));
}
2018-07-20 14:34:56 +02:00
// job has prerequisites:
2018-05-13 09:47:51 +02:00
Log::debug('Job provider has prerequisites.');
2018-05-01 20:47:38 +02:00
/** @var PrerequisitesInterface $providerPre */
2018-07-20 14:34:56 +02:00
$providerPre = app((string)config(sprintf('import.prerequisites.%s', $importProvider)));
2018-07-09 19:24:08 +02:00
$providerPre->setUser($importJob->user);
2018-07-20 14:34:56 +02:00
// and are not filled in:
2018-05-01 20:47:38 +02:00
if (!$providerPre->isComplete()) {
2018-05-13 09:47:51 +02:00
Log::debug('Job provider prerequisites are not yet filled in. Redirect to prerequisites-page.');
// redirect to global prerequisites
return redirect(route('import.prerequisites.index', [$importProvider, $importJob->key]));
}
2018-05-13 09:47:51 +02:00
Log::debug('Prerequisites are complete.');
2018-07-20 14:34:56 +02:00
// but are filled in:
$this->repository->setStatus($importJob, 'has_prereq');
2018-07-20 14:34:56 +02:00
// and has no config:
2018-07-08 07:59:58 +02:00
if (false === $hasConfig) {
2018-05-30 18:04:43 +02:00
// @codeCoverageIgnoreStart
2018-05-13 09:47:51 +02:00
Log::debug('Provider has no configuration. Job is ready to start.');
$this->repository->updateStatus($importJob, 'ready_to_run');
Log::debug('Redirect to status-page.');
2018-05-13 09:47:51 +02:00
return redirect(route('import.job.status.index', [$importJob->key]));
2018-05-30 18:04:43 +02:00
// @codeCoverageIgnoreEnd
2018-05-13 09:47:51 +02:00
}
2018-07-20 14:34:56 +02:00
// but also needs config:
2018-05-13 09:51:05 +02:00
Log::debug('Job has configuration. Redirect to job-config.');
2018-05-30 18:36:21 +02:00
// Otherwise just redirect to job configuration.
return redirect(route('import.job.configuration.index', [$importJob->key]));
}
2018-05-30 18:36:21 +02:00
/**
* Generate a JSON file of the job's configuration and send it to the user.
*
* @param ImportJob $job
*
* @return LaravelResponse
*/
public function download(ImportJob $job): LaravelResponse
{
Log::debug('Now in download()', ['job' => $job->key]);
2018-06-01 22:04:52 +02:00
$config = $this->repository->getConfiguration($job);
2018-05-30 18:36:21 +02:00
// This is CSV import specific:
2018-06-01 22:04:52 +02:00
$config['delimiter'] = $config['delimiter'] ?? ',';
2018-05-30 18:36:21 +02:00
$config['delimiter'] = "\t" === $config['delimiter'] ? 'tab' : $config['delimiter'];
2018-07-03 17:48:26 +02:00
$result = json_encode($config, JSON_PRETTY_PRINT);
$name = sprintf('"%s"', addcslashes('import-configuration-' . date('Y-m-d') . '.json', '"\\'));
2018-05-30 18:36:21 +02:00
/** @var LaravelResponse $response */
$response = response($result, 200);
$response->header('Content-disposition', 'attachment; filename=' . $name)
->header('Content-Type', 'application/json')
->header('Content-Description', 'File Transfer')
->header('Connection', 'Keep-Alive')
->header('Expires', '0')
->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->header('Pragma', 'public')
->header('Content-Length', \strlen($result));
return $response;
}
2017-12-16 19:47:14 +01:00
/**
* General import index.
*
2018-07-08 12:08:53 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
2018-07-20 14:34:56 +02:00
$providers = $this->providers;
2018-07-15 09:38:49 +02:00
$subTitle = (string)trans('import.index_breadcrumb');
2018-05-13 09:01:10 +02:00
$subTitleIcon = 'fa-home';
2018-07-29 07:30:06 +02:00
$isDemoUser = $this->userRepository->hasRole(auth()->user(), 'demo');
2018-05-13 09:01:10 +02:00
2018-07-29 07:30:06 +02:00
return view('import.index', compact('subTitle', 'subTitleIcon', 'providers', 'isDemoUser'));
2018-05-13 09:01:10 +02:00
}
2017-12-22 18:32:43 +01:00
}