| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankForm.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +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-08-10 15:09:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Form; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							|  |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class PiggyBankForm | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-10-30 11:43:17 +01:00
										 |  |  |  * TODO cleanup and describe. | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class PiggyBankForm | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     use FormSupport; | 
					
						
							| 
									
										
										
										
											2019-08-10 16:50:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-10-30 11:43:17 +01:00
										 |  |  |      * TODO cleanup and describe | 
					
						
							| 
									
										
										
										
											2019-08-10 16:50:37 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @param mixed $value | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-03-18 20:25:30 +01:00
										 |  |  |     public function piggyBankList(string $name, $value = null, ?array $options = null): string | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         // make repositories
 | 
					
						
							|  |  |  |         /** @var PiggyBankRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(PiggyBankRepositoryInterface::class); | 
					
						
							|  |  |  |         $piggyBanks = $repository->getPiggyBanksWithAmount(); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $title      = (string) trans('firefly.default_group_title_name'); | 
					
						
							| 
									
										
										
										
											2020-06-27 13:08:51 +02:00
										 |  |  |         $array      = []; | 
					
						
							|  |  |  |         $subList    = [ | 
					
						
							|  |  |  |             0 => [ | 
					
						
							|  |  |  |                 'group'   => [ | 
					
						
							|  |  |  |                     'title' => $title, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 'piggies' => [ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |                     (string) trans('firefly.none_in_select_list'), | 
					
						
							| 
									
										
										
										
											2020-06-27 13:08:51 +02:00
										 |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         /** @var PiggyBank $piggy */ | 
					
						
							|  |  |  |         foreach ($piggyBanks as $piggy) { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $group                                       = $piggy->objectGroups->first(); | 
					
						
							|  |  |  |             $groupTitle                                  = null; | 
					
						
							|  |  |  |             $groupOrder                                  = 0; | 
					
						
							| 
									
										
										
										
											2020-06-27 13:08:51 +02:00
										 |  |  |             if (null !== $group) { | 
					
						
							|  |  |  |                 $groupTitle = $group->title; | 
					
						
							|  |  |  |                 $groupOrder = $group->order; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $subList[$groupOrder] ??= [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |                 'group'   => [ | 
					
						
							|  |  |  |                     'title' => $groupTitle, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 'piggies' => [], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2020-06-27 13:08:51 +02:00
										 |  |  |             $subList[$groupOrder]['piggies'][$piggy->id] = $piggy->name; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ksort($subList); | 
					
						
							|  |  |  |         foreach ($subList as $info) { | 
					
						
							|  |  |  |             $groupTitle         = $info['group']['title']; | 
					
						
							|  |  |  |             $array[$groupTitle] = $info['piggies']; | 
					
						
							| 
									
										
										
										
											2019-08-10 15:09:44 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->select($name, $array, $value, $options); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-17 12:09:03 +02:00
										 |  |  | } |