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

244 lines
9.1 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* HomeController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-05 21:39:52 -06:00
2015-07-26 08:51:07 -05:00
use Artisan;
2015-02-06 23:49:24 -06:00
use Carbon\Carbon;
2017-12-29 02:05:35 -06:00
use Exception;
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Exceptions\FireflyException;
2016-11-20 01:57:48 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2017-12-19 12:25:50 -06:00
use FireflyIII\Http\Middleware\IsDemoUser;
use FireflyIII\Http\Middleware\IsSandStormUser;
2017-12-15 05:59:21 -06:00
use FireflyIII\Models\AccountType;
2017-03-05 06:21:36 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-10-16 12:01:26 -05:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2016-06-11 00:38:30 -05:00
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
2016-05-13 08:53:39 -05:00
use Illuminate\Support\Collection;
2016-06-11 00:38:30 -05:00
use Log;
2015-02-06 23:49:24 -06:00
use Preferences;
use Route as RouteFacade;
2016-10-29 00:44:46 -05:00
use View;
2015-02-06 14:23:14 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class HomeController.
2015-02-06 14:23:14 -06:00
*/
2015-02-06 00:23:26 -06:00
class HomeController extends Controller
{
/**
* HomeController constructor.
*/
2016-01-08 11:29:47 -06:00
public function __construct()
{
2016-01-08 12:13:51 -06:00
parent::__construct();
2017-12-16 12:46:36 -06:00
app('view')->share('title', 'Firefly III');
app('view')->share('mainTitleIcon', 'fa-fire');
2017-12-19 12:25:50 -06:00
$this->middleware(IsDemoUser::class)->except(['dateRange', 'index']);
$this->middleware(IsSandStormUser::class)->only('routes');
2016-01-08 11:29:47 -06:00
}
2015-02-05 21:39:52 -06:00
2016-06-11 00:38:30 -05:00
/**
* @param Request $request
2017-12-15 05:59:21 -06:00
*
* @return \Illuminate\Http\JsonResponse
2016-06-11 00:38:30 -05:00
*/
public function dateRange(Request $request)
2015-03-02 06:19:13 -06:00
{
2016-06-11 00:38:30 -05:00
$start = new Carbon($request->get('start'));
$end = new Carbon($request->get('end'));
$label = $request->get('label');
2016-04-29 01:56:56 -05:00
$isCustomRange = false;
2016-04-25 14:37:08 -05:00
2016-06-11 00:38:30 -05:00
Log::debug('Received dateRange', ['start' => $request->get('start'), 'end' => $request->get('end'), 'label' => $request->get('label')]);
2016-04-25 14:37:08 -05:00
// check if the label is "everything" or "Custom range" which will betray
// a possible problem with the budgets.
2018-04-02 08:10:40 -05:00
if ($label === (string)trans('firefly.everything') || $label === (string)trans('firefly.customRange')) {
2016-04-29 01:56:56 -05:00
$isCustomRange = true;
2016-06-11 00:38:30 -05:00
Log::debug('Range is now marked as "custom".');
2016-04-25 14:37:08 -05:00
}
2015-03-02 06:19:13 -06:00
$diff = $start->diffInDays($end);
if ($diff > 50) {
2018-04-02 08:10:40 -05:00
$request->session()->flash('warning', (string)trans('firefly.warning_much_data', ['days' => $diff]));
2015-03-02 06:19:13 -06:00
}
2015-03-02 05:35:14 -06:00
2017-12-15 05:59:21 -06:00
$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')));
2018-03-10 13:30:09 -06:00
return response()->json(['ok' => 'ok']);
2015-03-02 05:35:14 -06:00
}
2017-11-01 14:23:28 -05:00
/**
* @throws FireflyException
*/
2016-02-17 14:14:32 -06:00
public function displayError()
{
2017-09-03 09:09:27 -05:00
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.');
2016-02-17 14:14:32 -06:00
throw new FireflyException('A very simple test error.');
}
2015-04-07 13:54:43 -05:00
/**
2017-02-12 05:21:44 -06:00
* @param Request $request
2016-03-12 07:20:45 -06:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-04-07 13:54:43 -05:00
*/
2017-02-12 05:21:44 -06:00
public function flush(Request $request)
2015-04-07 11:26:14 -05:00
{
2015-07-05 07:37:36 -05:00
Preferences::mark();
2017-02-16 23:42:36 -06:00
$request->session()->forget(['start', 'end', '_previous', 'viewRange', 'range', 'is_custom_range']);
2017-12-10 11:09:36 -06:00
Log::debug('Call cache:clear...');
2015-07-22 15:13:40 -05:00
Artisan::call('cache:clear');
2017-12-10 11:09:36 -06:00
Log::debug('Call config:clear...');
Artisan::call('config:clear');
Log::debug('Call route:clear...');
Artisan::call('route:clear');
Log::debug('Call twig:clean...');
2017-12-11 07:52:30 -06:00
try {
Artisan::call('twig:clean');
2017-12-29 02:05:35 -06:00
} catch (Exception $e) {
2017-12-11 07:52:30 -06:00
// dont care
2018-03-27 12:29:58 -05:00
Log::debug('Called twig:clean.');
2017-12-11 07:52:30 -06:00
}
2017-12-10 11:09:36 -06:00
Log::debug('Call view:clear...');
Artisan::call('view:clear');
Log::debug('Done! Redirecting...');
2015-04-07 11:26:14 -05:00
2015-07-06 09:27:21 -05:00
return redirect(route('index'));
2015-03-31 13:46:37 -05:00
}
2015-02-06 00:23:26 -06:00
/**
2017-03-05 06:21:36 -06:00
* @param AccountRepositoryInterface $repository
*
2016-11-05 12:43:18 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2015-02-06 00:23:26 -06:00
*/
2017-03-05 06:21:36 -06:00
public function index(AccountRepositoryInterface $repository)
2015-02-06 00:23:26 -06:00
{
2016-04-26 14:40:15 -05:00
$types = config('firefly.accountTypesByIdentifier.asset');
$count = $repository->count($types);
2015-06-01 11:13:54 -05:00
2017-11-15 05:25:49 -06:00
if (0 === $count) {
2015-07-06 09:27:21 -05:00
return redirect(route('new-user.index'));
2015-06-01 11:13:54 -05:00
}
2016-10-29 00:44:46 -05:00
$subTitle = trans('firefly.welcomeBack');
$transactions = [];
$frontPage = Preferences::get(
2017-11-15 03:52:29 -06:00
'frontPageAccounts',
$repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray()
2016-05-13 10:22:24 -05:00
);
2016-02-05 08:41:40 -06:00
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
2016-02-05 08:41:40 -06:00
/** @var Carbon $end */
2017-09-16 02:24:48 -05:00
$end = session('end', Carbon::now()->endOfMonth());
$accounts = $repository->getAccountsById($frontPage->data);
2017-11-17 12:31:48 -06:00
$today = new Carbon;
2017-10-16 12:01:26 -05:00
// zero bills? Hide some elements from view.
/** @var BillRepositoryInterface $billRepository */
$billRepository = app(BillRepositoryInterface::class);
$billCount = $billRepository->getBills()->count();
2015-02-06 23:49:24 -06:00
foreach ($accounts as $account) {
2016-11-20 01:57:48 -06:00
$collector = app(JournalCollectorInterface::class);
2016-11-05 04:42:31 -05:00
$collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit(10)->setPage(1);
2016-11-25 09:55:04 -06:00
$set = $collector->getJournals();
$transactions[] = [$set, $account];
2015-02-06 23:49:24 -06:00
}
2015-06-03 14:25:11 -05:00
2017-12-28 12:03:15 -06:00
// fire check update event:
event(new RequestedVersionCheckStatus(auth()->user()));
2015-07-12 05:45:41 -05:00
return view(
2017-11-15 03:52:29 -06:00
'index',
2018-03-08 22:44:35 -06:00
compact('count', 'subTitle', 'transactions', 'billCount', 'start', 'end', 'today')
2015-07-12 05:45:41 -05:00
);
2015-02-06 00:23:26 -06:00
}
2015-06-29 08:23:50 -05:00
/**
* @return string
*/
public function routes()
{
$set = RouteFacade::getRoutes();
2017-10-05 04:49:06 -05:00
$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',
2018-01-10 00:29:55 -06:00
'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',
2017-10-02 09:24:31 -05:00
];
$return = '&nbsp;';
/** @var Route $route */
foreach ($set as $route) {
$name = $route->getName();
2017-11-15 05:25:49 -06:00
if (null !== $name && in_array('GET', $route->methods()) && strlen($name) > 0) {
2018-01-10 00:29:55 -06:00
$found = false;
foreach ($ignore as $string) {
2018-01-10 00:29:55 -06:00
if (!(false === stripos($name, $string))) {
$found = true;
2018-01-10 00:29:55 -06:00
break;
}
}
2018-01-10 00:29:55 -06:00
if ($found === false) {
$return .= 'touch ' . $route->getName() . '.md;';
}
}
}
return $return;
}
2016-10-20 09:51:05 -05:00
/**
2017-12-17 07:30:53 -06:00
* @param Request $request
*
2016-10-20 09:51:05 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2017-12-15 05:59:21 -06:00
public function testFlash(Request $request)
2016-10-20 09:51:05 -05:00
{
2017-12-15 05:59:21 -06:00
$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!');
2016-10-22 02:33:03 -05:00
2016-10-20 09:51:05 -05:00
return redirect(route('home'));
}
2015-02-05 21:39:52 -06:00
}