| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * EditController.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\Category; | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  | use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Http\Requests\CategoryFormRequest; | 
					
						
							|  |  |  | use FireflyIII\Models\Category; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Category\CategoryRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							|  |  |  | use Illuminate\Http\RedirectResponse; | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Routing\Redirector; | 
					
						
							| 
									
										
										
										
											2023-12-30 10:14:35 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\View\View; | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class EditController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class EditController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-05 06:25:56 +01:00
										 |  |  |     private AttachmentHelperInterface   $attachments; | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  |     private CategoryRepositoryInterface $repository; | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * CategoryController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                 app('view')->share('title', (string) trans('firefly.categories')); | 
					
						
							| 
									
										
										
										
											2020-05-01 18:54:08 -03:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-bookmark'); | 
					
						
							| 
									
										
										
										
											2020-11-05 06:25:56 +01:00
										 |  |  |                 $this->repository  = app(CategoryRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2020-03-19 08:58:55 +01:00
										 |  |  |                 $this->attachments = app(AttachmentHelperInterface::class); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Edit a category. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @return Factory|View | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function edit(Request $request, Category $category) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $subTitle  = (string) trans('firefly.edit_category', ['name' => $category->name]); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // put previous url in session if not redirect from store (not "return_to_edit").
 | 
					
						
							|  |  |  |         if (true !== session('categories.edit.fromUpdate')) { | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |             $this->rememberPreviousUrl('categories.edit.url'); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         $request->session()->forget('categories.edit.fromUpdate'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 06:25:56 +01:00
										 |  |  |         $preFilled = [ | 
					
						
							|  |  |  |             'notes' => $request->old('notes') ?? $this->repository->getNoteText($category), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-29 14:11:12 +01:00
										 |  |  |         return view('categories.edit', compact('category', 'subTitle', 'preFilled')); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update category. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @return Redirector|RedirectResponse | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function update(CategoryFormRequest $request, Category $category) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $data     = $request->getCategoryData(); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |         $this->repository->update($category, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $request->session()->flash('success', (string) trans('firefly.updated_category', ['name' => $category->name])); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:14:49 +01:00
										 |  |  |         // store new attachment(s):
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         /** @var null|array $files */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $files    = $request->hasFile('attachments') ? $request->file('attachments') : null; | 
					
						
							| 
									
										
										
										
											2020-05-07 06:44:01 +02:00
										 |  |  |         if (null !== $files && !auth()->user()->hasRole('demo')) { | 
					
						
							|  |  |  |             $this->attachments->saveAttachmentsForModel($category, $files); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (null !== $files && auth()->user()->hasRole('demo')) { | 
					
						
							| 
									
										
										
										
											2024-01-09 20:48:17 +01:00
										 |  |  |             Log::channel('audit')->warning(sprintf('The demo user is trying to upload attachments in %s.', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             session()->flash('info', (string) trans('firefly.no_att_demo_user')); | 
					
						
							| 
									
										
										
										
											2020-05-07 06:44:01 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-19 09:14:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (count($this->attachments->getMessages()->get('attachments')) > 0) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); | 
					
						
							| 
									
										
										
										
											2020-03-19 09:14:49 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |         $redirect = redirect($this->getPreviousUrl('categories.edit.url')); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (1 === (int) $request->get('return_to_edit')) { | 
					
						
							| 
									
										
										
										
											2019-06-25 19:24:01 +02:00
										 |  |  |             $request->session()->put('categories.edit.fromUpdate', true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $redirect = redirect(route('categories.edit', [$category->id])); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $redirect; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |