| 
									
										
										
										
											2019-07-21 17:15:06 +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-07-21 17:15:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers\RuleGroup; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Http\Controllers\Controller; | 
					
						
							|  |  |  | use FireflyIII\Http\Requests\RuleGroupFormRequest; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleGroup; | 
					
						
							|  |  |  | use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Http\RedirectResponse; | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Routing\Redirector; | 
					
						
							|  |  |  | use Illuminate\View\View; | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class EditController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class EditController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-04 09:19:36 +02:00
										 |  |  |     private RuleGroupRepositoryInterface $repository; | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * EditController 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.rules')); | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-random'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $this->repository = app(RuleGroupRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Edit a rule group. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Factory|View | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function edit(Request $request, RuleGroup $ruleGroup) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $subTitle    = (string) trans('firefly.edit_rule_group', ['title' => $ruleGroup->title]); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $hasOldInput = null !== $request->old('_token'); | 
					
						
							|  |  |  |         $preFilled   = [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'active' => $hasOldInput ? (bool) $request->old('active') : $ruleGroup->active, | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         // put previous url in session if not redirect from store (not "return_to_edit").
 | 
					
						
							|  |  |  |         if (true !== session('rule-groups.edit.fromUpdate')) { | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |             $this->rememberPreviousUrl('rule-groups.edit.url'); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         session()->forget('rule-groups.edit.fromUpdate'); | 
					
						
							|  |  |  |         session()->flash('preFilled', $preFilled); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return view('rules.rule-group.edit', compact('ruleGroup', 'subTitle')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |      * Move a rule group in either direction. | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |     public function moveGroup(Request $request): JsonResponse | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $groupId   = (int) $request->get('id'); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  |         $ruleGroup = $this->repository->find($groupId); | 
					
						
							| 
									
										
										
										
											2025-05-24 05:40:20 +02:00
										 |  |  |         if ($ruleGroup instanceof RuleGroup) { | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |             $direction = $request->get('direction'); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  |             if ('down' === $direction) { | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |                 $maxOrder = $this->repository->maxOrder(); | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |                 $order    = $ruleGroup->order; | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |                 if ($order < $maxOrder) { | 
					
						
							|  |  |  |                     $newOrder = $order + 1; | 
					
						
							|  |  |  |                     $this->repository->setOrder($ruleGroup, $newOrder); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-03-29 14:58:06 +02:00
										 |  |  |             if ('up' === $direction) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:55:39 +01:00
										 |  |  |                 $order = $ruleGroup->order; | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |                 if ($order > 1) { | 
					
						
							|  |  |  |                     $newOrder = $order - 1; | 
					
						
							|  |  |  |                     $this->repository->setOrder($ruleGroup, $newOrder); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-03-16 06:42:58 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-23 09:29:07 +02:00
										 |  |  |         return new JsonResponse(['OK']); | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-28 11:46:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the rule group. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @return $this|Redirector|RedirectResponse | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function update(RuleGroupFormRequest $request, RuleGroup $ruleGroup) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $data     = [ | 
					
						
							| 
									
										
										
										
											2022-05-02 19:35:35 +02:00
										 |  |  |             'title'       => $request->convertString('title'), | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |             'description' => $request->stringWithNewlines('description'), | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             'active'      => 1 === (int) $request->input('active'), | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->repository->update($ruleGroup, $data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         session()->flash('success', (string) trans('firefly.updated_rule_group', ['title' => $ruleGroup->title])); | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |         $redirect = redirect($this->getPreviousUrl('rule-groups.edit.url')); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         if (1 === (int) $request->get('return_to_edit')) { | 
					
						
							| 
									
										
										
										
											2019-07-21 17:15:06 +02:00
										 |  |  |             session()->put('rule-groups.edit.fromUpdate', true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $redirect = redirect(route('rule-groups.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // redirect to previous URL.
 | 
					
						
							|  |  |  |         return $redirect; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |