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

261 lines
7.7 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* HomeController.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-05-20 01:57:45 -05:00
declare(strict_types = 1);
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;
use FireflyIII\Exceptions\FireflyException;
2016-11-20 01:57:48 -06:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2016-05-13 10:22:24 -05:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Tag;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2016-03-12 07:20:45 -06:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2016-06-11 00:38:30 -05:00
use Illuminate\Http\Request;
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;
2015-04-07 11:26:14 -05:00
use Session;
2016-10-29 00:44:46 -05:00
use View;
2015-02-06 14:23:14 -06:00
/**
* Class HomeController
*
* @package FireflyIII\Http\Controllers
*/
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();
2016-10-29 00:44:46 -05:00
View::share('title', 'Firefly III');
View::share('mainTitleIcon', 'fa-fire');
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
*/
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.
if ($label === strval(trans('firefly.everything')) || $label === strval(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) {
2016-03-20 05:38:01 -05:00
Session::flash('warning', strval(trans('firefly.warning_much_data', ['days' => $diff])));
2015-03-02 06:19:13 -06:00
}
2015-03-02 05:35:14 -06:00
2016-04-29 01:56:56 -05:00
Session::put('is_custom_range', $isCustomRange);
2015-03-02 06:19:13 -06:00
Session::put('start', $start);
Session::put('end', $end);
2015-03-02 05:35:14 -06:00
}
/**
* @throws FireflyException
*/
2016-02-17 14:14:32 -06:00
public function displayError()
{
throw new FireflyException('A very simple test error.');
}
2015-04-07 13:54:43 -05:00
/**
2016-03-12 07:20:45 -06:00
* @param TagRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-04-07 13:54:43 -05:00
*/
2016-03-12 07:20:45 -06:00
public function flush(TagRepositoryInterface $repository)
2015-04-07 11:26:14 -05:00
{
2015-07-05 07:37:36 -05:00
Preferences::mark();
// get all tags.
// update all counts:
2016-03-12 07:20:45 -06:00
$tags = $repository->get();
/** @var Tag $tag */
foreach ($tags as $tag) {
foreach ($tag->transactionJournals()->get() as $journal) {
$count = $journal->tags()->count();
$journal->tag_count = $count;
$journal->save();
}
}
Session::forget(['start', 'end', 'viewRange', 'range', 'is_custom_range']);
2015-03-31 13:46:37 -05:00
Session::clear();
2015-07-22 15:13:40 -05:00
Artisan::call('cache:clear');
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
/**
2016-11-05 05:47:21 -05:00
* @param ARI $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
*/
2016-11-05 04:42:31 -05:00
public function index(ARI $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
2015-06-02 10:14:03 -05:00
if ($count == 0) {
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(
2016-10-10 00:49:39 -05: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 */
2016-10-14 13:01:17 -05:00
$end = session('end', Carbon::now()->endOfMonth());
$showTour = Preferences::get('tour', true)->data;
$accounts = $repository->getAccountsById($frontPage->data);
$showDepositsFrontpage = Preferences::get('showDepositsFrontpage', false)->data;
// 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
2015-07-12 05:45:41 -05:00
return view(
'index', compact('count', 'showTour', 'title', 'subTitle', 'mainTitleIcon', 'transactions', 'showDepositsFrontpage', 'billCount')
2015-07-12 05:45:41 -05:00
);
2015-02-06 00:23:26 -06:00
}
2015-06-29 08:23:50 -05:00
/**
* Display a list of named routes. Excludes some that cannot be "shown". This method
* is used to generate help files (down the road).
*/
public function routes()
{
2016-02-23 13:22:53 -06:00
// these routes are not relevant for the help pages:
2016-12-09 00:40:00 -06:00
$ignore = [
// login and two-factor routes:
'login',
'registe',
'password.rese',
'logout',
'two-fac',
'lost-two',
// test troutes
'test-flash',
'all-routes',
// json routes
'json.',
// routes that point to modals or that redirect immediately.
'piggy-banks.add',
'piggy-banks.remove',
'rules.rule.up',
'attachments.download',
'bills.rescan',
'rules.rule.down',
'rules.rule-group.up',
'rules.rule-group.down',
'popup.',
'error',
'flush',
//'preferences.',
'admin.users.domains.block-',
'help.',
// ajax routes:
'import.json',
// charts:
'chart.',
// report data:
'report-data.',
// others:
'debugbar',
'attachments.preview',
'budgets.income',
2016-12-14 11:59:12 -06:00
'currencies.default',
2016-12-09 00:40:00 -06:00
2016-02-23 13:22:53 -06:00
];
$routes = Route::getRoutes();
2016-11-20 00:24:18 -06:00
$return = '<pre>';
2016-10-23 10:33:53 -05:00
2016-02-23 13:22:53 -06:00
/** @var \Illuminate\Routing\Route $route */
foreach ($routes as $route) {
2016-02-23 13:22:53 -06:00
$name = $route->getName();
$methods = $route->getMethods();
2016-10-23 10:33:53 -05:00
if (!is_null($name) && strlen($name) > 0 && in_array('GET', $methods) && !$this->startsWithAny($ignore, $name)) {
2016-11-20 00:24:18 -06:00
$return .= sprintf('touch %s.md', $name) . "\n";
2016-02-23 13:22:53 -06:00
}
}
2016-11-20 00:24:18 -06:00
$return .= '</pre><hr />';
2016-02-23 13:22:53 -06:00
2016-11-20 00:24:18 -06:00
return $return;
}
2016-10-20 09:51:05 -05:00
/**
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function testFlash()
{
Session::flash('success', 'This is a success message.');
Session::flash('info', 'This is an info message.');
Session::flash('warning', 'This is a warning.');
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'));
}
2016-02-23 13:22:53 -06:00
/**
* @param array $array
* @param string $needle
*
* @return bool
*/
private function startsWithAny(array $array, string $needle): bool
2016-02-23 13:22:53 -06:00
{
foreach ($array as $entry) {
if ((substr($needle, 0, strlen($entry)) === $entry)) {
return true;
}
}
return false;
}
2016-09-15 23:48:38 -05:00
2015-02-05 21:39:52 -06:00
}