1 | <?php |
||
2 | /** |
||
3 | * PreferencesController.php |
||
4 | * Copyright (c) 2017 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\Http\Controllers; |
||
24 | |||
25 | use FireflyIII\Models\AccountType; |
||
26 | use FireflyIII\Repositories\Account\AccountRepositoryInterface; |
||
27 | use Illuminate\Http\Request; |
||
28 | |||
29 | /** |
||
30 | * Class PreferencesController. |
||
31 | */ |
||
32 | class PreferencesController extends Controller |
||
33 | { |
||
34 | /** |
||
35 | * PreferencesController constructor. |
||
36 | */ |
||
37 | public function __construct() |
||
38 | { |
||
39 | parent::__construct(); |
||
40 | |||
41 | $this->middleware( |
||
42 | function ($request, $next) { |
||
43 | app('view')->share('title', (string)trans('firefly.preferences')); |
||
44 | app('view')->share('mainTitleIcon', 'fa-gear'); |
||
45 | |||
46 | return $next($request); |
||
47 | } |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Show overview of preferences. |
||
53 | * |
||
54 | * @param AccountRepositoryInterface $repository |
||
55 | * |
||
56 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
57 | */ |
||
58 | public function index(AccountRepositoryInterface $repository) |
||
59 | { |
||
60 | $accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); |
||
61 | $accountIds = $accounts->pluck('id')->toArray(); |
||
62 | $viewRangePref = app('preferences')->get('viewRange', '1M'); |
||
63 | /** @noinspection NullPointerExceptionInspection */ |
||
64 | $viewRange = $viewRangePref->data; |
||
65 | $frontPageAccounts = app('preferences')->get('frontPageAccounts', $accountIds); |
||
66 | $language = app('preferences')->get('language', config('firefly.default_language', 'en_US'))->data; |
||
67 | $listPageSize = app('preferences')->get('listPageSize', 50)->data; |
||
68 | $customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data; |
||
69 | $fiscalYearStartStr = app('preferences')->get('fiscalYearStart', '01-01')->data; |
||
70 | $fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr; |
||
71 | $tjOptionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data; |
||
72 | |||
73 | // an important fallback is that the frontPageAccount array gets refilled automatically |
||
74 | // when it turns up empty. |
||
75 | if (\count($frontPageAccounts->data) === 0) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
76 | $frontPageAccounts = $accountIds; |
||
77 | } |
||
78 | |||
79 | return view( |
||
80 | 'preferences.index', |
||
81 | compact( |
||
82 | 'language', |
||
83 | 'accounts', |
||
84 | 'frontPageAccounts', |
||
85 | 'tjOptionalFields', |
||
86 | 'viewRange', |
||
87 | 'customFiscalYear', |
||
88 | 'listPageSize', |
||
89 | 'fiscalYearStart' |
||
90 | ) |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Store new preferences. |
||
96 | * |
||
97 | * @param Request $request |
||
98 | * |
||
99 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||
100 | * |
||
101 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
102 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
103 | */ |
||
104 | public function postIndex(Request $request) |
||
105 | { |
||
106 | // front page accounts |
||
107 | $frontPageAccounts = []; |
||
108 | if (\is_array($request->get('frontPageAccounts')) && \count($request->get('frontPageAccounts')) > 0) { |
||
109 | foreach ($request->get('frontPageAccounts') as $id) { |
||
110 | $frontPageAccounts[] = (int)$id; |
||
111 | } |
||
112 | app('preferences')->set('frontPageAccounts', $frontPageAccounts); |
||
113 | } |
||
114 | |||
115 | // view range: |
||
116 | app('preferences')->set('viewRange', $request->get('viewRange')); |
||
117 | // forget session values: |
||
118 | session()->forget('start'); |
||
119 | session()->forget('end'); |
||
120 | session()->forget('range'); |
||
121 | |||
122 | // custom fiscal year |
||
123 | $customFiscalYear = 1 === (int)$request->get('customFiscalYear'); |
||
124 | $fiscalYearStart = date('m-d', strtotime((string)$request->get('fiscalYearStart'))); |
||
125 | app('preferences')->set('customFiscalYear', $customFiscalYear); |
||
126 | app('preferences')->set('fiscalYearStart', $fiscalYearStart); |
||
127 | |||
128 | // save page size: |
||
129 | app('preferences')->set('listPageSize', 50); |
||
130 | $listPageSize = (int)$request->get('listPageSize'); |
||
131 | if ($listPageSize > 0 && $listPageSize < 1337) { |
||
132 | app('preferences')->set('listPageSize', $listPageSize); |
||
133 | } |
||
134 | |||
135 | // language: |
||
136 | $lang = $request->get('language'); |
||
137 | if (array_key_exists($lang, config('firefly.languages'))) { |
||
138 | app('preferences')->set('language', $lang); |
||
139 | } |
||
140 | |||
141 | // optional fields for transactions: |
||
142 | $setOptions = $request->get('tj'); |
||
143 | $optionalTj = [ |
||
144 | 'interest_date' => isset($setOptions['interest_date']), |
||
145 | 'book_date' => isset($setOptions['book_date']), |
||
146 | 'process_date' => isset($setOptions['process_date']), |
||
147 | 'due_date' => isset($setOptions['due_date']), |
||
148 | 'payment_date' => isset($setOptions['payment_date']), |
||
149 | 'invoice_date' => isset($setOptions['invoice_date']), |
||
150 | 'internal_reference' => isset($setOptions['internal_reference']), |
||
151 | 'notes' => isset($setOptions['notes']), |
||
152 | 'attachments' => isset($setOptions['attachments']), |
||
153 | ]; |
||
154 | app('preferences')->set('transaction_journal_optional_fields', $optionalTj); |
||
155 | |||
156 | session()->flash('success', (string)trans('firefly.saved_preferences')); |
||
157 | app('preferences')->mark(); |
||
158 | |||
159 | return redirect(route('preferences.index')); |
||
160 | } |
||
161 | } |
||
162 |