firefly-iii/app/Http/Controllers/Import/StatusController.php

126 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2017-12-17 07:41:58 -06:00
/**
* StatusController.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;
2018-01-25 12:02:14 -06:00
use FireflyIII\Http\Middleware\IsDemoUser;
2017-12-16 12:47:14 -06:00
use FireflyIII\Models\ImportJob;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2018-03-24 12:55:02 -05:00
use Log;
/**
* Class StatusController
*/
class StatusController extends Controller
{
2017-12-16 12:47:14 -06:00
/**
*
*/
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'));
return $next($request);
}
);
2018-01-25 12:02:14 -06:00
$this->middleware(IsDemoUser::class);
2017-12-16 12:47:14 -06:00
}
2017-12-16 12:48:31 -06:00
2017-12-16 12:47:14 -06:00
/**
* @param ImportJob $job
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function index(ImportJob $job)
{
2017-12-23 10:42:07 -06:00
$statuses = ['configured', 'running', 'finished', 'error'];
2018-04-27 23:23:13 -05:00
if (!\in_array($job->status, $statuses)) {
2017-12-23 10:42:07 -06:00
return redirect(route('import.configure', [$job->key]));
2017-12-16 12:47:14 -06:00
}
$subTitle = trans('import.status_sub_title');
2017-12-16 12:47:14 -06:00
$subTitleIcon = 'fa-star';
return view('import.status', compact('job', 'subTitle', 'subTitleIcon'));
}
/**
* Show status of import job in JSON.
*
* @param ImportJob $job
*
* @return \Illuminate\Http\JsonResponse
*/
public function json(ImportJob $job)
{
$result = [
'started' => false,
'finished' => false,
'running' => false,
'errors' => array_values($job->extended_status['errors']),
'percentage' => 0,
'show_percentage' => false,
'steps' => $job->extended_status['steps'],
'done' => $job->extended_status['done'],
'statusText' => trans('import.status_job_' . $job->status),
2017-12-16 12:47:14 -06:00
'status' => $job->status,
'finishedText' => '',
];
if (0 !== $job->extended_status['steps']) {
$result['percentage'] = round(($job->extended_status['done'] / $job->extended_status['steps']) * 100, 0);
$result['show_percentage'] = true;
}
if ('finished' === $job->status) {
$result['finished'] = true;
2018-04-02 08:10:40 -05:00
$tagId = (int)$job->extended_status['tag'];
if ($tagId !== 0) {
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$tag = $repository->find($tagId);
2018-01-25 12:02:14 -06:00
$count = $tag->transactionJournals()->count();
$result['finishedText'] = trans(
'import.status_finished_job', ['count' => $count, 'link' => route('tags.show', [$tag->id, 'all']), 'tag' => $tag->tag]
);
}
if ($tagId === 0) {
2018-01-25 11:41:27 -06:00
$result['finishedText'] = trans('import.status_finished_no_tag'); // @codeCoverageIgnore
}
2017-12-16 12:47:14 -06:00
}
if ('running' === $job->status) {
$result['started'] = true;
$result['running'] = true;
}
$result['percentage'] = $result['percentage'] > 100 ? 100 : $result['percentage'];
2018-03-24 12:55:02 -05:00
Log::debug(sprintf('JOB STATUS: %d/%d', $result['done'], $result['steps']));
2017-12-16 12:48:31 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($result);
2017-12-16 12:47:14 -06:00
}
2017-12-22 11:32:43 -06:00
}