firefly-iii/app/Http/Controllers/HomeController.php
2018-01-10 12:42:32 +01:00

244 lines
9.1 KiB
PHP

<?php
/**
* HomeController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use Artisan;
use Carbon\Carbon;
use Exception;
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Http\Middleware\IsSandStormUser;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Collection;
use Log;
use Preferences;
use Response;
use Route as RouteFacade;
use View;
/**
* Class HomeController.
*/
class HomeController extends Controller
{
/**
* HomeController constructor.
*/
public function __construct()
{
parent::__construct();
app('view')->share('title', 'Firefly III');
app('view')->share('mainTitleIcon', 'fa-fire');
$this->middleware(IsDemoUser::class)->except(['dateRange', 'index']);
$this->middleware(IsSandStormUser::class)->only('routes');
}
/**
* @param Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function dateRange(Request $request)
{
$start = new Carbon($request->get('start'));
$end = new Carbon($request->get('end'));
$label = $request->get('label');
$isCustomRange = false;
Log::debug('Received dateRange', ['start' => $request->get('start'), 'end' => $request->get('end'), 'label' => $request->get('label')]);
// check if the label is "everything" or "Custom range" which will betray
// a possible problem with the budgets.
if ($label === strval(trans('firefly.everything')) || $label === strval(trans('firefly.customRange'))) {
$isCustomRange = true;
Log::debug('Range is now marked as "custom".');
}
$diff = $start->diffInDays($end);
if ($diff > 50) {
$request->session()->flash('warning', strval(trans('firefly.warning_much_data', ['days' => $diff])));
}
$request->session()->put('is_custom_range', $isCustomRange);
Log::debug(sprintf('Set is_custom_range to %s', var_export($isCustomRange, true)));
$request->session()->put('start', $start);
Log::debug(sprintf('Set start to %s', $start->format('Y-m-d H:i:s')));
$request->session()->put('end', $end);
Log::debug(sprintf('Set end to %s', $end->format('Y-m-d H:i:s')));
return Response::json(['ok' => 'ok']);
}
/**
* @throws FireflyException
*/
public function displayError()
{
Log::debug('This is a test message at the DEBUG level.');
Log::info('This is a test message at the INFO level.');
Log::notice('This is a test message at the NOTICE level.');
Log::warning('This is a test message at the WARNING level.');
Log::error('This is a test message at the ERROR level.');
Log::critical('This is a test message at the CRITICAL level.');
Log::alert('This is a test message at the ALERT level.');
Log::emergency('This is a test message at the EMERGENCY level.');
throw new FireflyException('A very simple test error.');
}
/**
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function flush(Request $request)
{
Preferences::mark();
$request->session()->forget(['start', 'end', '_previous', 'viewRange', 'range', 'is_custom_range']);
Log::debug('Call cache:clear...');
Artisan::call('cache:clear');
Log::debug('Call config:clear...');
Artisan::call('config:clear');
Log::debug('Call route:clear...');
Artisan::call('route:clear');
Log::debug('Call twig:clean...');
try {
Artisan::call('twig:clean');
} catch (Exception $e) {
// dont care
}
Log::debug('Call view:clear...');
Artisan::call('view:clear');
Log::debug('Done! Redirecting...');
return redirect(route('index'));
}
/**
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
*/
public function index(AccountRepositoryInterface $repository)
{
$types = config('firefly.accountTypesByIdentifier.asset');
$count = $repository->count($types);
if (0 === $count) {
return redirect(route('new-user.index'));
}
$subTitle = trans('firefly.welcomeBack');
$transactions = [];
$frontPage = Preferences::get(
'frontPageAccounts',
$repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray()
);
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$accounts = $repository->getAccountsById($frontPage->data);
$showDeps = Preferences::get('showDepositsFrontpage', false)->data;
$today = new Carbon;
// zero bills? Hide some elements from view.
/** @var BillRepositoryInterface $billRepository */
$billRepository = app(BillRepositoryInterface::class);
$billCount = $billRepository->getBills()->count();
foreach ($accounts as $account) {
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit(10)->setPage(1);
$set = $collector->getJournals();
$transactions[] = [$set, $account];
}
// fire check update event:
event(new RequestedVersionCheckStatus(auth()->user()));
return view(
'index',
compact('count', 'subTitle', 'transactions', 'showDeps', 'billCount', 'start', 'end', 'today')
);
}
/**
* @return string
*/
public function routes()
{
$set = RouteFacade::getRoutes();
$ignore = ['chart.', 'javascript.', 'json.', 'report-data.', 'popup.', 'debugbar.', 'attachments.download', 'attachments.preview',
'bills.rescan', 'budgets.income', 'currencies.def', 'error', 'flush', 'help.show', 'import.file',
'login', 'logout', 'password.reset', 'profile.confirm-email-change', 'profile.undo-email-change',
'register', 'report.options', 'routes', 'rule-groups.down', 'rule-groups.up', 'rules.up', 'rules.down',
'rules.select', 'search.search', 'test-flash', 'transactions.link.delete', 'transactions.link.switch',
'two-factor.lost', 'reports.options', 'debug', 'import.create-job', 'import.download', 'import.start', 'import.status.json',
'preferences.delete-code', 'rules.test-triggers', 'piggy-banks.remove-money', 'piggy-banks.add-money',
'accounts.reconcile.transactions', 'accounts.reconcile.overview','export.download',
'transactions.clone','two-factor.index'
];
$return = '&nbsp;';
/** @var Route $route */
foreach ($set as $route) {
$name = $route->getName();
if (null !== $name && in_array('GET', $route->methods()) && strlen($name) > 0) {
$found = false;
foreach ($ignore as $string) {
if (!(false === stripos($name, $string))) {
$found = true;
break;
}
}
if ($found === false) {
$return .= 'touch ' . $route->getName() . '.md;';
}
}
}
return $return;
}
/**
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function testFlash(Request $request)
{
$request->session()->flash('success', 'This is a success message.');
$request->session()->flash('info', 'This is an info message.');
$request->session()->flash('warning', 'This is a warning.');
$request->session()->flash('error', 'This is an error!');
return redirect(route('home'));
}
}