firefly-iii/app/Http/Controllers/HelpController.php

131 lines
3.4 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* HelpController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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;
2015-04-07 11:26:14 -05:00
use Log;
use Preferences;
2015-02-25 09:10:02 -06:00
use Response;
2015-04-07 11:26:14 -05:00
2015-02-25 09:10:02 -06:00
/**
* Class HelpController
*
* @package FireflyIII\Http\Controllers
*/
2015-03-10 11:26:31 -05:00
class HelpController extends Controller
{
2017-07-23 00:30:05 -05:00
/** @var HelpInterface */
private $help;
/**
* 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
/**
2015-05-03 05:58:55 -05:00
* @param $route
2015-02-25 09:10:02 -06:00
*
* @return \Illuminate\Http\JsonResponse
*/
2017-07-23 00:30:05 -05:00
public function show(string $route)
2015-02-25 09:10:02 -06:00
{
2016-11-05 05:47:21 -05:00
$language = Preferences::get('language', config('firefly.default_language', 'en_US'))->data;
2017-07-23 00:30:05 -05:00
$html = $this->getHelpText($route, $language);
return Response::json(['html' => $html]);
}
/**
* @param string $route
* @param string $language
*
* @return string
*/
private function getHelpText(string $route, string $language): string
{
// get language and default variables.
2015-02-25 09:10:02 -06:00
2017-07-23 00:30:05 -05:00
$content = '<p>' . strval(trans('firefly.route_has_no_help')) . '</p>';
// 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:
$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:
if (strlen($content) === 0) {
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
}
2017-07-23 00:30:05 -05:00
$content = $this->help->getFromGithub($route, $language);
}
2017-07-23 00:30:05 -05:00
// help still empty?
if (strlen($content) !== 0) {
$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
2017-07-23 00:30:05 -05:00
return '<p>' . strval(trans('firefly.route_has_no_help')) . '</p>';
2015-02-25 09:10:02 -06:00
}
}