firefly-iii/app/Http/Controllers/Controller.php
2016-11-22 19:10:17 +01:00

102 lines
2.7 KiB
PHP
Executable File

<?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.
*/
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Session;
use View;
/**
* Class Controller
*
* @package FireflyIII\Http\Controllers
*/
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/** @var string */
protected $dateTimeFormat;
/** @var string */
protected $monthAndDayFormat;
/** @var string */
protected $monthFormat;
/**
* Controller constructor.
*/
public function __construct()
{
View::share('hideBudgets', false);
View::share('hideCategories', false);
View::share('hideBills', false);
View::share('hideTags', false);
// 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);
}
);
}
/**
* @param TransactionJournal $journal
*
* @return bool
*/
protected function isOpeningBalance(TransactionJournal $journal): bool
{
return TransactionJournal::transactionTypeStr($journal) === TransactionType::OPENING_BALANCE;
}
/**
* @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]));
}
}
Session::flash('error', strval(trans('firefly.cannot_redirect_to_account')));
return redirect(route('index'));
}
}