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

139 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
* Controller.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-05 21:39:52 -06:00
namespace FireflyIII\Http\Controllers;
2016-12-28 04:34:00 -06:00
use FireflyConfig;
2016-11-22 12:10:17 -06:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2015-06-13 03:02:36 -05:00
use Illuminate\Foundation\Bus\DispatchesJobs;
2015-02-05 21:39:52 -06:00
use Illuminate\Foundation\Validation\ValidatesRequests;
2015-02-11 00:35:10 -06:00
use Illuminate\Routing\Controller as BaseController;
2016-11-22 12:10:17 -06:00
use Session;
2017-02-05 01:26:54 -06:00
use URL;
2016-01-09 00:48:45 -06:00
use View;
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* Class Controller
*
* @package FireflyIII\Http\Controllers
*/
class Controller extends BaseController
2015-02-11 00:35:10 -06:00
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
2015-05-14 06:41:21 -05:00
2016-03-01 14:31:25 -06:00
/** @var string */
protected $dateTimeFormat;
/** @var string */
2016-01-09 00:48:45 -06:00
protected $monthAndDayFormat;
/** @var string */
2016-01-24 08:58:16 -06:00
protected $monthFormat;
2016-01-09 00:48:45 -06:00
2015-04-28 08:26:30 -05:00
/**
* Controller constructor.
2015-04-28 08:26:30 -05:00
*/
public function __construct()
{
View::share('hideBudgets', false);
View::share('hideCategories', false);
View::share('hideBills', false);
View::share('hideTags', false);
2016-12-26 02:18:45 -06:00
$isDemoSite = FireflyConfig::get('is_demo_site', config('firefly.configuration.is_demo_site'))->data;
View::share('IS_DEMO_SITE', $isDemoSite);
2016-12-28 04:34:00 -06:00
View::share('DEMO_USERNAME', env('DEMO_USERNAME', ''));
View::share('DEMO_PASSWORD', env('DEMO_PASSWORD', ''));
2016-10-29 00:44:46 -05:00
// translations:
$this->middleware(
function ($request, $next) {
$this->monthFormat = (string)trans('config.month');
$this->monthAndDayFormat = (string)trans('config.month_and_day');
$this->dateTimeFormat = (string)trans('config.date_time');
return $next($request);
}
);
2015-05-14 06:41:21 -05:00
2015-04-28 08:26:30 -05:00
}
2015-12-31 10:20:54 -06:00
2017-02-05 01:26:54 -06:00
/**
* Functionality:
*
* - 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));
if (!(strpos($identifier, 'delete') === false) && !(strpos($uri, '/show/') === false)) {
$uri = route('index');
}
if (!(strpos($uri, 'javascript') === false)) {
$uri = route('index');
}
return $uri;
}
2016-11-22 12:10:17 -06:00
/**
* @param TransactionJournal $journal
*
* @return bool
*/
protected function isOpeningBalance(TransactionJournal $journal): bool
{
return $journal->transactionTypeStr() === TransactionType::OPENING_BALANCE;
2016-11-22 12:10:17 -06: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 14:53:44 -05:00
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
Session::flash('error', strval(trans('firefly.cannot_redirect_to_account')));
return redirect(route('index'));
2017-03-18 14:53:44 -05:00
// @codeCoverageIgnoreEnd
2016-11-22 12:10:17 -06:00
}
2017-02-05 01:26:54 -06:00
/**
* @param string $identifier
*/
protected function rememberPreviousUri(string $identifier)
{
Session::put($identifier, URL::previous());
}
2015-02-05 21:39:52 -06:00
}