Files
firefly-iii/app/Http/Controllers/Controller.php

152 lines
5.1 KiB
PHP
Raw Normal View History

<?php
/**
* Controller.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
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/>.
*/
declare(strict_types=1);
2015-02-06 04:39:52 +01:00
namespace FireflyIII\Http\Controllers;
2016-12-28 11:34:00 +01:00
use FireflyConfig;
use FireflyIII\Support\Http\Controllers\UserNavigation;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2015-06-13 10:02:36 +02:00
use Illuminate\Foundation\Bus\DispatchesJobs;
2015-02-06 04:39:52 +01:00
use Illuminate\Foundation\Validation\ValidatesRequests;
2015-02-11 07:35:10 +01:00
use Illuminate\Routing\Controller as BaseController;
2017-07-22 10:50:30 +02:00
use Log;
use Route;
2015-02-06 04:39:52 +01:00
2015-02-11 07:35:10 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class Controller.
2018-07-17 22:21:03 +02:00
*
* @SuppressWarnings(PHPMD.NumberOfChildren)
2015-02-11 07:35:10 +01:00
*/
class Controller extends BaseController
2015-02-11 07:35:10 +01:00
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests, UserNavigation;
2015-05-14 13:41:21 +02:00
2018-07-22 08:10:16 +02:00
/** @var string Format for date and time. */
2016-03-01 21:31:25 +01:00
protected $dateTimeFormat;
2018-07-22 08:10:16 +02:00
/** @var string Format for "23 Feb, 2016". */
2016-01-09 07:48:45 +01:00
protected $monthAndDayFormat;
2018-07-22 08:10:16 +02:00
/** @var string Format for "March 2018" */
2016-01-24 15:58:16 +01:00
protected $monthFormat;
2018-07-22 08:10:16 +02:00
/** @var string Redirect user */
protected $redirectUri = '/';
2016-01-09 07:48:45 +01:00
2015-04-28 15:26:30 +02:00
/**
* Controller constructor.
2015-04-28 15:26:30 +02:00
*/
public function __construct()
{
2017-07-22 10:50:30 +02:00
// for transaction lists:
app('view')->share('hideBudgets', false);
app('view')->share('hideCategories', false);
app('view')->share('hideBills', false);
app('view')->share('hideTags', false);
2017-07-22 10:50:30 +02:00
// is site a demo site?
2016-12-26 09:18:45 +01:00
$isDemoSite = FireflyConfig::get('is_demo_site', config('firefly.configuration.is_demo_site'))->data;
app('view')->share('IS_DEMO_SITE', $isDemoSite);
app('view')->share('DEMO_USERNAME', env('DEMO_USERNAME', ''));
app('view')->share('DEMO_PASSWORD', env('DEMO_PASSWORD', ''));
app('view')->share('FF_VERSION', config('firefly.version'));
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
2017-07-22 10:50:30 +02:00
// translations for specific strings:
2016-10-29 07:44:46 +02:00
$this->monthFormat = (string)trans('config.month');
$this->monthAndDayFormat = (string)trans('config.month_and_day');
$this->dateTimeFormat = (string)trans('config.date_time');
// get shown-intro-preference:
if (auth()->check()) {
2018-07-17 22:21:03 +02:00
$language = $this->getLanguage();
$page = $this->getPageName();
$shownDemo = $this->hasSeenDemo();
app('view')->share('language', $language);
app('view')->share('shownDemo', $shownDemo);
app('view')->share('current_route_name', $page);
app('view')->share('original_route_name', Route::currentRouteName());
}
2016-10-29 07:44:46 +02:00
return $next($request);
}
);
2015-04-28 15:26:30 +02:00
}
2015-12-31 17:20:54 +01:00
2017-02-05 08:26:54 +01:00
2018-07-17 22:21:03 +02:00
/**
2018-07-22 08:10:16 +02:00
* Get user's language.
2018-08-06 19:14:30 +02:00
*
2018-07-17 22:21:03 +02:00
* @return string
*/
protected function getLanguage(): string // get preference
2018-07-17 22:21:03 +02:00
{
/** @var string $language */
$language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data;
return $language;
}
/**
* @return string
*/
protected function getPageName(): string // get request info
2018-07-17 22:21:03 +02:00
{
return str_replace('.', '_', Route::currentRouteName());
}
/**
2018-07-22 08:10:16 +02:00
* Get the specific name of a page for intro.
*
2018-07-17 22:21:03 +02:00
* @return string
*/
protected function getSpecificPageName(): string // get request info
2018-07-17 22:21:03 +02:00
{
return null === Route::current()->parameter('what') ? '' : '_' . Route::current()->parameter('what');
}
/**
2018-07-22 08:10:16 +02:00
* Returns if user has seen demo.
*
2018-07-17 22:21:03 +02:00
* @return bool
*/
protected function hasSeenDemo(): bool // get request info + get preference
2018-07-17 22:21:03 +02:00
{
$page = $this->getPageName();
$specificPage = $this->getSpecificPageName();
// indicator if user has seen the help for this page ( + special page):
$key = 'shown_demo_' . $page . $specificPage;
// is there an intro for this route?
$intro = config('intro.' . $page) ?? [];
$specialIntro = config('intro.' . $page . $specificPage) ?? [];
// some routes have a "what" parameter, which indicates a special page:
$shownDemo = true;
// both must be array and either must be > 0
if (\count($intro) > 0 || \count($specialIntro) > 0) {
$shownDemo = app('preferences')->get($key, false)->data;
Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %d', $key, $shownDemo));
}
return $shownDemo;
}
2015-02-06 04:39:52 +01:00
}