2016-01-08 16:01:21 +01:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* Controller.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
|
|
|
|
* 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);
|
2015-02-06 04:39:52 +01:00
|
|
|
|
2016-01-08 16:01:21 +01:00
|
|
|
namespace FireflyIII\Http\Controllers;
|
|
|
|
|
2016-12-28 11:34:00 +01:00
|
|
|
use FireflyConfig;
|
2016-11-22 19:10:17 +01:00
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2017-07-15 21:40:42 +02:00
|
|
|
use FireflyIII\Support\Facades\Preferences;
|
2016-01-08 16:01:21 +01:00
|
|
|
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;
|
2017-07-15 22:17:24 +02:00
|
|
|
use Route;
|
2016-11-22 19:10:17 +01:00
|
|
|
use Session;
|
2017-02-05 08:26:54 +01:00
|
|
|
use URL;
|
2016-01-09 07:48:45 +01:00
|
|
|
use View;
|
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.
|
2015-02-11 07:35:10 +01:00
|
|
|
*/
|
2016-01-08 16:01:21 +01:00
|
|
|
class Controller extends BaseController
|
2015-02-11 07:35:10 +01:00
|
|
|
{
|
2016-01-08 16:01:21 +01:00
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
2015-05-14 13:41:21 +02:00
|
|
|
|
2017-11-15 12:25:49 +01:00
|
|
|
/** @var string */
|
2016-03-01 21:31:25 +01:00
|
|
|
protected $dateTimeFormat;
|
2016-01-27 19:35:00 +01:00
|
|
|
/** @var string */
|
2016-01-09 07:48:45 +01:00
|
|
|
protected $monthAndDayFormat;
|
2016-01-27 19:35:00 +01:00
|
|
|
/** @var string */
|
2016-01-24 15:58:16 +01:00
|
|
|
protected $monthFormat;
|
2017-10-05 11:49:06 +02:00
|
|
|
/** @var string */
|
2017-09-29 16:53:09 +02:00
|
|
|
protected $redirectUri = '/';
|
2016-01-09 07:48:45 +01:00
|
|
|
|
2015-04-28 15:26:30 +02:00
|
|
|
/**
|
2016-01-08 16:01:21 +01: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:
|
2015-04-28 15:26:30 +02:00
|
|
|
View::share('hideBudgets', false);
|
|
|
|
View::share('hideCategories', false);
|
|
|
|
View::share('hideBills', false);
|
|
|
|
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;
|
|
|
|
View::share('IS_DEMO_SITE', $isDemoSite);
|
2016-12-28 11:34:00 +01:00
|
|
|
View::share('DEMO_USERNAME', env('DEMO_USERNAME', ''));
|
|
|
|
View::share('DEMO_PASSWORD', env('DEMO_PASSWORD', ''));
|
2017-09-12 19:57:41 +02:00
|
|
|
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');
|
|
|
|
|
2017-07-15 21:40:42 +02:00
|
|
|
// get shown-intro-preference:
|
2017-07-15 22:17:24 +02:00
|
|
|
if (auth()->check()) {
|
2017-07-22 10:50:30 +02:00
|
|
|
// some routes have a "what" parameter, which indicates a special page:
|
2017-11-15 12:25:49 +01:00
|
|
|
$specificPage = null === Route::current()->parameter('what') ? '' : '_' . Route::current()->parameter('what');
|
2017-07-22 10:50:30 +02:00
|
|
|
$page = str_replace('.', '_', Route::currentRouteName());
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
$shownDemo = true;
|
|
|
|
|
|
|
|
// either must be array and either must be > 0
|
|
|
|
if ((is_array($intro) || is_array($specialIntro)) && (count($intro) > 0 || count($specialIntro) > 0)) {
|
|
|
|
$shownDemo = Preferences::get($key, false)->data;
|
|
|
|
Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %d', $key, $shownDemo));
|
2017-07-16 07:35:08 +02:00
|
|
|
}
|
2017-07-22 10:50:30 +02:00
|
|
|
|
2017-07-15 22:17:24 +02:00
|
|
|
View::share('shownDemo', $shownDemo);
|
2017-07-22 10:50:30 +02:00
|
|
|
View::share('current_route_name', $page);
|
|
|
|
View::share('original_route_name', Route::currentRouteName());
|
2017-07-15 22:17:24 +02:00
|
|
|
}
|
2017-07-15 21:40:42 +02:00
|
|
|
|
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
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Functionality:.
|
2017-02-05 08:26:54 +01:00
|
|
|
*
|
|
|
|
* - If the $identifier contains the word "delete" then a remembered uri with the text "/show/" in it will not be returned but instead the index (/)
|
|
|
|
* will be returned.
|
|
|
|
* - If the remembered uri contains "javascript/" the remembered uri will not be returned but instead the index (/) will be returned.
|
|
|
|
*
|
|
|
|
* @param string $identifier
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getPreviousUri(string $identifier): string
|
|
|
|
{
|
|
|
|
$uri = strval(session($identifier));
|
2017-11-15 12:25:49 +01:00
|
|
|
if (!(false === strpos($identifier, 'delete')) && !(false === strpos($uri, '/show/'))) {
|
2017-09-29 16:53:09 +02:00
|
|
|
$uri = $this->redirectUri;
|
2017-02-05 08:26:54 +01:00
|
|
|
}
|
2017-11-15 12:25:49 +01:00
|
|
|
if (!(false === strpos($uri, 'jscript'))) {
|
2017-09-29 16:53:09 +02:00
|
|
|
$uri = $this->redirectUri;
|
2017-02-05 08:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $uri;
|
|
|
|
}
|
2016-11-22 19:10:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isOpeningBalance(TransactionJournal $journal): bool
|
|
|
|
{
|
2017-11-15 12:25:49 +01:00
|
|
|
return TransactionType::OPENING_BALANCE === $journal->transactionTypeStr();
|
2016-11-22 19:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
protected function redirectToAccount(TransactionJournal $journal)
|
|
|
|
{
|
|
|
|
$valid = [AccountType::DEFAULT, AccountType::ASSET];
|
|
|
|
$transactions = $journal->transactions;
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$account = $transaction->account;
|
|
|
|
if (in_array($account->accountType->type, $valid)) {
|
|
|
|
return redirect(route('accounts.show', [$account->id]));
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 20:53:44 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-11-22 19:10:17 +01:00
|
|
|
Session::flash('error', strval(trans('firefly.cannot_redirect_to_account')));
|
|
|
|
|
|
|
|
return redirect(route('index'));
|
2017-03-18 20:53:44 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-11-22 19:10:17 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:26:54 +01:00
|
|
|
/**
|
|
|
|
* @param string $identifier
|
|
|
|
*/
|
|
|
|
protected function rememberPreviousUri(string $identifier)
|
|
|
|
{
|
|
|
|
Session::put($identifier, URL::previous());
|
|
|
|
}
|
2015-02-06 04:39:52 +01:00
|
|
|
}
|