2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* HelpController.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:27:31 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* 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
|
2017-12-17 14:41:58 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-05-20 08:57:45 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-25 16:10:02 +01:00
|
|
|
|
2015-04-07 19:58:49 +02:00
|
|
|
use FireflyIII\Helpers\Help\HelpInterface;
|
2018-07-08 12:28:42 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2015-04-07 18:26:14 +02:00
|
|
|
use Log;
|
|
|
|
|
2015-02-25 16:10:02 +01:00
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class HelpController.
|
2015-02-25 16:10:02 +01:00
|
|
|
*/
|
2015-03-10 17:26:31 +01:00
|
|
|
class HelpController extends Controller
|
|
|
|
{
|
2018-07-22 08:10:16 +02:00
|
|
|
/** @var HelpInterface Help interface. */
|
2017-07-23 07:30:05 +02:00
|
|
|
private $help;
|
|
|
|
|
2016-01-09 08:20:55 +01:00
|
|
|
/**
|
|
|
|
* HelpController constructor.
|
|
|
|
*/
|
2016-01-08 18:29:47 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-01-08 20:40:48 +01:00
|
|
|
parent::__construct();
|
2017-07-23 07:30:05 +02:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
$this->help = app(HelpInterface::class);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2016-01-08 18:29:47 +01:00
|
|
|
}
|
2015-02-25 16:10:02 +01:00
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Show help for a route.
|
|
|
|
*
|
2017-11-15 12:25:49 +01:00
|
|
|
* @param $route
|
2015-02-25 16:10:02 +01:00
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2015-02-25 16:10:02 +01:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function show(string $route): JsonResponse
|
2015-02-25 16:10:02 +01:00
|
|
|
{
|
2018-07-27 04:46:21 +02:00
|
|
|
/** @var string $language */
|
2018-07-14 16:08:34 +02:00
|
|
|
$language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data;
|
2017-07-23 07:30:05 +02:00
|
|
|
$html = $this->getHelpText($route, $language);
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json(['html' => $html]);
|
2017-07-23 07:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Gets the help text.
|
|
|
|
*
|
2017-07-23 07:30:05 +02:00
|
|
|
* @param string $route
|
|
|
|
* @param string $language
|
|
|
|
*
|
|
|
|
* @return string
|
2018-07-20 14:34:56 +02:00
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2017-07-23 07:30:05 +02:00
|
|
|
*/
|
2018-08-09 16:07:33 +02:00
|
|
|
protected function getHelpText(string $route, string $language): string // get from internet.
|
2017-07-23 07:30:05 +02:00
|
|
|
{
|
|
|
|
// get language and default variables.
|
2018-07-26 06:10:17 +02:00
|
|
|
$content = '<p>' . trans('firefly.route_has_no_help') . '</p>';
|
2017-07-23 07:30:05 +02:00
|
|
|
|
|
|
|
// if no such route, log error and return default text.
|
|
|
|
if (!$this->help->hasRoute($route)) {
|
2015-04-07 18:25:21 +02:00
|
|
|
Log::error('No such route: ' . $route);
|
2015-02-25 16:10:02 +01:00
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
return $content;
|
2015-02-25 16:10:02 +01:00
|
|
|
}
|
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
// help content may be cached:
|
|
|
|
if ($this->help->inCache($route, $language)) {
|
|
|
|
$content = $this->help->getFromCache($route, $language);
|
2016-11-02 14:33:57 +01:00
|
|
|
Log::debug(sprintf('Help text %s was in cache.', $language));
|
2015-02-25 16:10:02 +01:00
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
return $content;
|
2015-02-25 16:10:02 +01:00
|
|
|
}
|
2016-04-09 09:27:04 +02:00
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
// get help content from Github:
|
2018-07-08 07:59:58 +02:00
|
|
|
$content = $this->help->getFromGitHub($route, $language);
|
2015-02-25 16:10:02 +01:00
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
// content will have 0 length when Github failed. Try en_US when it does:
|
2018-07-08 07:59:58 +02:00
|
|
|
if ('' === $content) {
|
2016-11-02 14:33:57 +01:00
|
|
|
$language = 'en_US';
|
2017-07-23 07:30:05 +02:00
|
|
|
|
|
|
|
// also check cache first:
|
|
|
|
if ($this->help->inCache($route, $language)) {
|
2016-11-02 14:33:57 +01:00
|
|
|
Log::debug(sprintf('Help text %s was in cache.', $language));
|
2017-07-23 07:30:05 +02:00
|
|
|
$content = $this->help->getFromCache($route, $language);
|
|
|
|
|
|
|
|
return $content;
|
2016-11-02 14:33:57 +01:00
|
|
|
}
|
2016-10-31 18:31:52 +01:00
|
|
|
|
2018-07-08 07:59:58 +02:00
|
|
|
$content = $this->help->getFromGitHub($route, $language);
|
2016-10-31 18:31:52 +01:00
|
|
|
}
|
2016-11-02 07:04:14 +01:00
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
// help still empty?
|
2018-07-08 07:59:58 +02:00
|
|
|
if ('' !== $content) {
|
2017-07-23 07:30:05 +02:00
|
|
|
$this->help->putInCache($route, $language, $content);
|
2015-02-25 16:10:02 +01:00
|
|
|
|
2017-07-23 07:30:05 +02:00
|
|
|
return $content;
|
|
|
|
}
|
2015-02-25 16:10:02 +01:00
|
|
|
|
2018-07-26 06:10:17 +02:00
|
|
|
return '<p>' . trans('firefly.route_has_no_help') . '</p>';
|
2015-02-25 16:10:02 +01:00
|
|
|
}
|
|
|
|
}
|