Option to re-enable intro boxes.

This commit is contained in:
James Cole
2017-07-23 07:30:05 +02:00
parent 456dd39ec4
commit f16a186faf
7 changed files with 107 additions and 36 deletions

View File

@@ -30,6 +30,8 @@ class Help implements HelpInterface
/** @var string */ /** @var string */
protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'; protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36';
const CACHEKEY = 'help_%s_%s';
/** /**
* @param string $route * @param string $route
* @param string $language * @param string $language
@@ -38,7 +40,7 @@ class Help implements HelpInterface
*/ */
public function getFromCache(string $route, string $language): string public function getFromCache(string $route, string $language): string
{ {
$line = sprintf('help.%s.%s', $route, $language); $line = sprintf(self::CACHEKEY, $route, $language);
return Cache::get($line); return Cache::get($line);
} }
@@ -64,12 +66,12 @@ class Help implements HelpInterface
return ''; return '';
} }
Log::debug(sprintf('Status code is %d', $result->status_code)); Log::debug(sprintf('Status code is %d', $result->status_code));
if ($result->status_code === 200) { if ($result->status_code === 200) {
$content = trim($result->body); $content = trim($result->body);
} }
if (strlen($content) > 0) { if (strlen($content) > 0) {
Log::debug('Content is longer than zero. Expect something.'); Log::debug('Content is longer than zero. Expect something.');
$converter = new CommonMarkConverter(); $converter = new CommonMarkConverter();
@@ -98,7 +100,7 @@ class Help implements HelpInterface
*/ */
public function inCache(string $route, string $language): bool public function inCache(string $route, string $language): bool
{ {
$line = sprintf('help.%s.%s', $route, $language); $line = sprintf(self::CACHEKEY, $route, $language);
$result = Cache::has($line); $result = Cache::has($line);
if ($result) { if ($result) {
Log::debug(sprintf('Cache has this entry: %s', 'help.' . $route . '.' . $language)); Log::debug(sprintf('Cache has this entry: %s', 'help.' . $route . '.' . $language));
@@ -120,7 +122,7 @@ class Help implements HelpInterface
*/ */
public function putInCache(string $route, string $language, string $content) public function putInCache(string $route, string $language, string $content)
{ {
$key = sprintf('help.%s.%s', $route, $language); $key = sprintf(self::CACHEKEY, $route, $language);
if (strlen($content) > 0) { if (strlen($content) > 0) {
Log::debug(sprintf('Will store entry in cache: %s', $key)); Log::debug(sprintf('Will store entry in cache: %s', $key));
Cache::put($key, $content, 10080); // a week. Cache::put($key, $content, 10080); // a week.

View File

@@ -25,62 +25,95 @@ use Response;
*/ */
class HelpController extends Controller class HelpController extends Controller
{ {
/** @var HelpInterface */
private $help;
/** /**
* HelpController constructor. * HelpController constructor.
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->middleware(
function ($request, $next) {
$this->help = app(HelpInterface::class);
return $next($request);
}
);
} }
/** /**
* @param HelpInterface $help
* @param $route * @param $route
* *
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
*/ */
public function show(HelpInterface $help, string $route) public function show(string $route)
{ {
$language = Preferences::get('language', config('firefly.default_language', 'en_US'))->data; $language = Preferences::get('language', config('firefly.default_language', 'en_US'))->data;
$content = '<p>' . strval(trans('firefly.route_has_no_help')) . '</p>'; $html = $this->getHelpText($route, $language);
if (!$help->hasRoute($route)) { 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.
$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)) {
Log::error('No such route: ' . $route); Log::error('No such route: ' . $route);
return Response::json($content); return $content;
} }
if ($help->inCache($route, $language)) { // help content may be cached:
$content = $help->getFromCache($route, $language); if ($this->help->inCache($route, $language)) {
$content = $this->help->getFromCache($route, $language);
Log::debug(sprintf('Help text %s was in cache.', $language)); Log::debug(sprintf('Help text %s was in cache.', $language));
return Response::json($content); return $content;
} }
$content = $help->getFromGithub($route, $language); // get help content from Github:
$notYourLanguage = '<p><em>' . strval(trans('firefly.help_may_not_be_your_language')) . '</em></p>'; $content = $this->help->getFromGithub($route, $language);
// get backup language content (try English): // content will have 0 length when Github failed. Try en_US when it does:
if (strlen($content) === 0) { if (strlen($content) === 0) {
$language = 'en_US'; $language = 'en_US';
if ($help->inCache($route, $language)) {
// also check cache first:
if ($this->help->inCache($route, $language)) {
Log::debug(sprintf('Help text %s was in cache.', $language)); Log::debug(sprintf('Help text %s was in cache.', $language));
$content = $notYourLanguage . $help->getFromCache($route, $language); $content = $this->help->getFromCache($route, $language);
}
if (!$help->inCache($route, $language)) { return $content;
$content = trim($notYourLanguage . $help->getFromGithub($route, $language));
} }
$content = $this->help->getFromGithub($route, $language);
} }
if ($content === $notYourLanguage) { // help still empty?
$content = '<p>' . strval(trans('firefly.route_has_no_help')) . '</p>'; if (strlen($content) !== 0) {
$this->help->putInCache($route, $language, $content);
return $content;
} }
$help->putInCache($route, $language, $content); return '<p>' . strval(trans('firefly.route_has_no_help')) . '</p>';
return Response::json($content);
} }

View File

@@ -69,6 +69,23 @@ class IntroController
return in_array('outro', $keys); return in_array('outro', $keys);
} }
/**
* @param string $route
* @param string $specialPage
*
* @return \Illuminate\Http\JsonResponse
*/
public function postEnable(string $route, string $specialPage = '')
{
$key = 'shown_demo_' . $route;
if ($specialPage !== '') {
$key .= '_' . $specialPage;
}
Log::debug(sprintf('Going to mark the following route as NOT done: %s with special "%s" (%s)', $route, $specialPage, $key));
Preferences::set($key, false);
return Response::json(['message' => trans('firefly.intro_boxes_after_refresh')]);
}
/** /**
* @param string $route * @param string $route
@@ -82,8 +99,8 @@ class IntroController
if ($specialPage !== '') { if ($specialPage !== '') {
$key .= '_' . $specialPage; $key .= '_' . $specialPage;
} }
Log::debug(sprintf('Going to mark the following route as doen: %s with special "%s" (%s)', $route, $specialPage, $key)); Log::debug(sprintf('Going to mark the following route as done: %s with special "%s" (%s)', $route, $specialPage, $key));
//Preferences::set($key, true); Preferences::set($key, true);
return Response::json(['result' => sprintf('Reported demo watched for route "%s".', $route)]); return Response::json(['result' => sprintf('Reported demo watched for route "%s".', $route)]);
} }

View File

@@ -11,27 +11,40 @@
$(function () { $(function () {
"use strict"; "use strict";
$('#help').click(showHelp); $('#help').click(showHelp);
$(function () {
//$('[data-toggle="tooltip"]').tooltip();
});
}); });
function showHelp(e) { function showHelp(e) {
"use strict"; "use strict";
var target = $(e.target); var target = $(e.target);
var route = target.data('route'); var route = target.data('route');
// var specialPage = target.data('extra');
if (typeof specialPage === 'undefined') {
specialPage = '';
}
$('#helpBody').html('<i class="fa fa-refresh fa-spin"></i>'); $('#helpBody').html('<i class="fa fa-refresh fa-spin"></i>');
$('#helpTitle').html('Please hold...'); $('#helpTitle').html('Please hold...');
$('#helpModal').modal('show'); $('#helpModal').modal('show');
$('#helpTitle').html('Help for this page'); $('#helpTitle').html('Help for this page');
$.getJSON('help/' + encodeURI(route)).done(function (data) { $.getJSON('help/' + encodeURI(route)).done(function (data) {
$('#helpBody').html(data); $('#helpBody').html(data.html);
}).fail(function () { }).fail(function () {
$('#helpBody').html('<p class="text-danger">No help text could be found.</p>'); $('#helpBody').html('<p class="text-danger">No help text could be found.</p>');
$('#helpTitle').html('Apologies'); $('#helpTitle').html('Apologies');
}); });
$('#reenableGuidance').unbind('click').click(function () {
enableGuidance(route, specialPage);
});
return false; return false;
} }
function enableGuidance(route, specialPage) {
$.post('json/intro/enable/' + route + '/' + specialPage).done(function (data) {
alert(data.message);
}).fail(function () {
alert('Could not re-enable introduction.');
})
}

View File

@@ -80,6 +80,8 @@ return [
'user_id_is' => 'Your user id is <strong>:user</strong>', 'user_id_is' => 'Your user id is <strong>:user</strong>',
'field_supports_markdown' => 'This field supports <a href="https://en.support.wordpress.com/markdown-quick-reference/">Markdown</a>.', 'field_supports_markdown' => 'This field supports <a href="https://en.support.wordpress.com/markdown-quick-reference/">Markdown</a>.',
'need_more_help' => 'If you need more help using Firefly III, please <a href="https://github.com/firefly-iii/firefly-iii/issues">open a ticket on Github</a>.', 'need_more_help' => 'If you need more help using Firefly III, please <a href="https://github.com/firefly-iii/firefly-iii/issues">open a ticket on Github</a>.',
'reenable_intro_text' => 'You can also reenable <a href="#" id="reenableGuidance">the introduction guidance</a>.',
'intro_boxes_after_refresh' => 'The introduction boxes will reappear when you refresh the page.',
'nothing_to_display' => 'There are no transactions to show you', 'nothing_to_display' => 'There are no transactions to show you',
'show_all_no_filter' => 'Show all transactions without grouping them by date.', 'show_all_no_filter' => 'Show all transactions without grouping them by date.',
'expenses_by_category' => 'Expenses by category', 'expenses_by_category' => 'Expenses by category',

View File

@@ -46,7 +46,7 @@
<header class="main-header"> <header class="main-header">
<!-- Logo --> {# Logo #}
<a href="{{ route('index') }}" class="logo"> <a href="{{ route('index') }}" class="logo">
<!-- mini logo for sidebar mini 50x50 pixels --> <!-- mini logo for sidebar mini 50x50 pixels -->
<span class="logo-mini">FF</span> <span class="logo-mini">FF</span>
@@ -65,7 +65,7 @@
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li class="hidden-sm hidden-xs"> <li class="hidden-sm hidden-xs">
<a href="#" id="help" data-route="{{ original_route_name }}"> <a href="#" id="help" data-route="{{ original_route_name }}" data-extra="{{ what|default("") }}">
<i class="fa fa-question-circle" data-route="{{ original_route_name }}"></i> <i class="fa fa-question-circle" data-route="{{ original_route_name }}"></i>
</a> </a>
</li> </li>
@@ -168,6 +168,9 @@
<div class="modal-footer"> <div class="modal-footer">
<small class="pull-left"> <small class="pull-left">
{{ 'need_more_help'|_ }} {{ 'need_more_help'|_ }}
</small><br />
<small class="pull-left">
{{ trans('firefly.reenable_intro_text')|raw }}
</small> </small>
<button type="button" class="btn btn-default" data-dismiss="modal">{{ 'close'|_ }}</button> <button type="button" class="btn btn-default" data-dismiss="modal">{{ 'close'|_ }}</button>
</div> </div>

View File

@@ -452,6 +452,7 @@ Route::group(
// intro things: // intro things:
Route::get('intro/{route}/{specificPage?}', ['uses' => 'Json\IntroController@getIntroSteps', 'as' => 'intro']); Route::get('intro/{route}/{specificPage?}', ['uses' => 'Json\IntroController@getIntroSteps', 'as' => 'intro']);
Route::post('intro/finished/{route}/{specificPage?}', ['uses' => 'Json\IntroController@postFinished', 'as' => 'intro.finished']); Route::post('intro/finished/{route}/{specificPage?}', ['uses' => 'Json\IntroController@postFinished', 'as' => 'intro.finished']);
Route::post('intro/enable/{route}/{specificPage?}', ['uses' => 'Json\IntroController@postEnable', 'as' => 'intro.enable']);
} }
); );