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