| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * General.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * 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/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-01 22:44:35 +02:00
										 |  |  | namespace FireflyIII\Support\Twig; | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 10:01:47 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  | use FireflyIII\Models\TransactionCurrency; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:20:46 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-07-31 19:28:56 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							|  |  |  | use FireflyIII\Support\Facades\Steam; | 
					
						
							| 
									
										
										
										
											2020-08-27 20:22:52 +02:00
										 |  |  | use FireflyIII\Support\Search\OperatorQuerySearch; | 
					
						
							| 
									
										
										
										
											2021-10-01 05:30:14 +02:00
										 |  |  | use League\CommonMark\GithubFlavoredMarkdownConverter; | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  | use Route; | 
					
						
							| 
									
										
										
										
											2019-12-28 09:44:56 +01:00
										 |  |  | use Twig\Extension\AbstractExtension; | 
					
						
							|  |  |  | use Twig\TwigFilter; | 
					
						
							|  |  |  | use Twig\TwigFunction; | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class TwigSupport. | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-12-28 09:44:56 +01:00
										 |  |  | class General extends AbstractExtension | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-02-06 10:15:07 +01:00
										 |  |  |     public function getFilters(): array | 
					
						
							| 
									
										
										
										
											2015-05-01 20:17:06 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-06-06 17:40:41 +02:00
										 |  |  |         return [ | 
					
						
							|  |  |  |             $this->balance(), | 
					
						
							| 
									
										
										
										
											2015-07-19 11:47:56 +02:00
										 |  |  |             $this->formatFilesize(), | 
					
						
							|  |  |  |             $this->mimeIcon(), | 
					
						
							| 
									
										
										
										
											2018-08-06 19:14:30 +02:00
										 |  |  |             $this->markdown(), | 
					
						
							| 
									
										
										
										
											2021-10-24 07:25:51 +02:00
										 |  |  |             $this->phpHostName(), | 
					
						
							| 
									
										
										
										
											2015-06-06 17:40:41 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show account balance. Only used on the front page of Firefly III. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function balance(): TwigFilter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFilter( | 
					
						
							|  |  |  |             'balance', | 
					
						
							|  |  |  |             static function (?Account $account): string { | 
					
						
							|  |  |  |                 if (null === $account) { | 
					
						
							|  |  |  |                     return '0'; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 /** @var Carbon $date */ | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                 $date            = session('end', today(config('app.timezone'))->endOfMonth()); | 
					
						
							|  |  |  |                 $info            = Steam::finalAccountBalance($account, $date); | 
					
						
							|  |  |  |                 $currency        = Steam::getAccountCurrency($account); | 
					
						
							| 
									
										
										
										
											2024-12-25 07:13:41 +01:00
										 |  |  |                 $default         = Amount::getDefaultCurrency(); | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                 $convertToNative = app('preferences')->get('convert_to_native', false)->data; | 
					
						
							| 
									
										
										
										
											2024-12-25 07:13:41 +01:00
										 |  |  |                 $useNative       = $convertToNative && $default->id !== $currency->id; | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                 $strings         = []; | 
					
						
							|  |  |  |                 foreach ($info as $key => $balance) { | 
					
						
							|  |  |  |                     if ('balance' === $key) { | 
					
						
							|  |  |  |                         // balance in account currency.
 | 
					
						
							| 
									
										
										
										
											2024-12-24 10:29:07 +01:00
										 |  |  |                         if (!$useNative) { | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                             $strings[] = app('amount')->formatAnything($currency, $balance, false); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2024-12-25 07:13:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                         continue; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if ('native_balance' === $key) { | 
					
						
							|  |  |  |                         // balance in native currency.
 | 
					
						
							| 
									
										
										
										
											2024-12-24 10:29:07 +01:00
										 |  |  |                         if ($useNative) { | 
					
						
							|  |  |  |                             $strings[] = app('amount')->formatAnything($default, $balance, false); | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                         continue; | 
					
						
							| 
									
										
										
										
											2024-09-28 17:43:18 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-12-24 10:29:07 +01:00
										 |  |  |                     // for multi currency accounts.
 | 
					
						
							| 
									
										
										
										
											2024-12-24 16:56:31 +01:00
										 |  |  |                     if ($useNative && $key !== $default->code) { | 
					
						
							| 
									
										
										
										
											2024-12-22 19:42:06 +01:00
										 |  |  |                         $strings[] = app('amount')->formatAnything(TransactionCurrency::where('code', $key)->first(), $balance, false); | 
					
						
							| 
									
										
										
										
											2024-09-28 17:43:18 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2024-07-31 13:09:55 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-08-05 05:06:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 13:09:55 +02:00
										 |  |  |                 return implode(', ', $strings); | 
					
						
							| 
									
										
										
										
											2024-08-05 05:06:53 +02:00
										 |  |  |                 // return app('steam')->balance($account, $date);
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Used to convert 1024 to 1kb etc. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function formatFilesize(): TwigFilter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFilter( | 
					
						
							|  |  |  |             'filesize', | 
					
						
							|  |  |  |             static function (int $size): string { | 
					
						
							|  |  |  |                 // less than one GB, more than one MB
 | 
					
						
							|  |  |  |                 if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                     return round($size / (1024 * 1024), 2).' MB'; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // less than one MB
 | 
					
						
							|  |  |  |                 if ($size < (1024 * 1024)) { | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                     return round($size / 1024, 2).' KB'; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 return $size.' bytes'; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show icon with attachment. | 
					
						
							| 
									
										
										
										
											2023-12-22 20:12:38 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @SuppressWarnings(PHPMD.CyclomaticComplexity) | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function mimeIcon(): TwigFilter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFilter( | 
					
						
							|  |  |  |             'mimeIcon', | 
					
						
							|  |  |  |             static function (string $string): string { | 
					
						
							|  |  |  |                 switch ($string) { | 
					
						
							|  |  |  |                     default: | 
					
						
							|  |  |  |                         return 'fa-file-o'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'application/pdf': | 
					
						
							|  |  |  |                         return 'fa-file-pdf-o'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                         // image
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'image/png': | 
					
						
							|  |  |  |                     case 'image/jpeg': | 
					
						
							|  |  |  |                     case 'image/svg+xml': | 
					
						
							|  |  |  |                     case 'image/heic': | 
					
						
							|  |  |  |                     case 'image/heic-sequence': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.image': | 
					
						
							|  |  |  |                         return 'fa-file-image-o'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                         // MS word
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'application/msword': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.wordprocessingml.template': | 
					
						
							|  |  |  |                     case 'application/x-iwork-pages-sffpages': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.writer': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.writer.template': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.writer.global': | 
					
						
							|  |  |  |                     case 'application/vnd.stardivision.writer': | 
					
						
							|  |  |  |                     case 'application/vnd.stardivision.writer-global': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.text': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.text-template': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.text-web': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.text-master': | 
					
						
							|  |  |  |                         return 'fa-file-word-o'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                         // MS excel
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'application/vnd.ms-excel': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.spreadsheetml.template': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.calc': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.calc.template': | 
					
						
							|  |  |  |                     case 'application/vnd.stardivision.calc': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.spreadsheet': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.spreadsheet-template': | 
					
						
							|  |  |  |                         return 'fa-file-excel-o'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                         // MS powerpoint
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'application/vnd.ms-powerpoint': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.presentationml.presentation': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.presentationml.template': | 
					
						
							|  |  |  |                     case 'application/vnd.openxmlformats-officedocument.presentationml.slideshow': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.impress': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.impress.template': | 
					
						
							|  |  |  |                     case 'application/vnd.stardivision.impress': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.presentation': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.presentation-template': | 
					
						
							|  |  |  |                         return 'fa-file-powerpoint-o'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                         // calc
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'application/vnd.sun.xml.draw': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.draw.template': | 
					
						
							|  |  |  |                     case 'application/vnd.stardivision.draw': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.chart': | 
					
						
							|  |  |  |                         return 'fa-paint-brush'; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     case 'application/vnd.oasis.opendocument.graphics': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.graphics-template': | 
					
						
							|  |  |  |                     case 'application/vnd.sun.xml.math': | 
					
						
							|  |  |  |                     case 'application/vnd.stardivision.math': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.formula': | 
					
						
							|  |  |  |                     case 'application/vnd.oasis.opendocument.database': | 
					
						
							|  |  |  |                         return 'fa-calculator'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             ['is_safe' => ['html']] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function markdown(): TwigFilter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFilter( | 
					
						
							|  |  |  |             'markdown', | 
					
						
							|  |  |  |             static function (string $text): string { | 
					
						
							|  |  |  |                 $converter = new GithubFlavoredMarkdownConverter( | 
					
						
							|  |  |  |                     [ | 
					
						
							|  |  |  |                         'allow_unsafe_links' => false, | 
					
						
							| 
									
										
										
										
											2024-06-11 15:04:15 +02:00
										 |  |  |                         'max_nesting_level'  => 5, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                         'html_input'         => 'escape', | 
					
						
							|  |  |  |                     ] | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-28 17:17:29 +02:00
										 |  |  |                 return (string) $converter->convert($text); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             ['is_safe' => ['html']] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show URL host name | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function phpHostName(): TwigFilter | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFilter( | 
					
						
							|  |  |  |             'phphost', | 
					
						
							|  |  |  |             static function (string $string): string { | 
					
						
							| 
									
										
										
										
											2024-09-28 17:17:29 +02:00
										 |  |  |                 $proto = (string) parse_url($string, PHP_URL_SCHEME); | 
					
						
							|  |  |  |                 $host  = (string) parse_url($string, PHP_URL_HOST); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return e(sprintf('%s://%s', $proto, $host)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 20:11:09 +01:00
										 |  |  |     public function getFunctions(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             $this->phpdate(), | 
					
						
							|  |  |  |             $this->activeRouteStrict(), | 
					
						
							|  |  |  |             $this->activeRoutePartial(), | 
					
						
							|  |  |  |             $this->activeRoutePartialObjectType(), | 
					
						
							|  |  |  |             $this->menuOpenRoutePartial(), | 
					
						
							|  |  |  |             $this->formatDate(), | 
					
						
							|  |  |  |             $this->getMetaField(), | 
					
						
							|  |  |  |             $this->hasRole(), | 
					
						
							|  |  |  |             $this->getRootSearchOperator(), | 
					
						
							|  |  |  |             $this->carbonize(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Basic example thing for some views. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function phpdate(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'phpdate', | 
					
						
							|  |  |  |             static function (string $str): string { | 
					
						
							|  |  |  |                 return date($str); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Will return "active" when the current route matches the given argument | 
					
						
							|  |  |  |      * exactly. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function activeRouteStrict(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'activeRouteStrict', | 
					
						
							|  |  |  |             static function (): string { | 
					
						
							|  |  |  |                 $args  = func_get_args(); | 
					
						
							|  |  |  |                 $route = $args[0]; // name of the route.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 if (\Route::getCurrentRoute()->getName() === $route) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                     return 'active'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return ''; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Will return "active" when a part of the route matches the argument. | 
					
						
							|  |  |  |      * ie. "accounts" will match "accounts.index". | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function activeRoutePartial(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'activeRoutePartial', | 
					
						
							|  |  |  |             static function (): string { | 
					
						
							|  |  |  |                 $args  = func_get_args(); | 
					
						
							|  |  |  |                 $route = $args[0]; // name of the route.
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 $name  = \Route::getCurrentRoute()->getName() ?? ''; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 if (str_contains($name, $route)) { | 
					
						
							|  |  |  |                     return 'active'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return ''; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * This function will return "active" when the current route matches the first argument (even partly) | 
					
						
							|  |  |  |      * but, the variable $objectType has been set and matches the second argument. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function activeRoutePartialObjectType(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'activeRoutePartialObjectType', | 
					
						
							|  |  |  |             static function ($context): string { | 
					
						
							|  |  |  |                 [, $route, $objectType] = func_get_args(); | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 $activeObjectType       = $context['objectType'] ?? false; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 05:55:57 +02:00
										 |  |  |                 if ($objectType === $activeObjectType | 
					
						
							|  |  |  |                     && false !== stripos( | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                         \Route::getCurrentRoute()->getName(), | 
					
						
							| 
									
										
										
										
											2023-06-21 05:55:57 +02:00
										 |  |  |                         $route | 
					
						
							|  |  |  |                     )) { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                     return 'active'; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return ''; | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             ['needs_context' => true] | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Will return "menu-open" when a part of the route matches the argument. | 
					
						
							|  |  |  |      * ie. "accounts" will match "accounts.index". | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function menuOpenRoutePartial(): TwigFunction | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             'menuOpenRoutePartial', | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |             static function (): string { | 
					
						
							|  |  |  |                 $args  = func_get_args(); | 
					
						
							|  |  |  |                 $route = $args[0]; // name of the route.
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |                 $name  = \Route::getCurrentRoute()->getName() ?? ''; | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 if (str_contains($name, $route)) { | 
					
						
							|  |  |  |                     return 'menu-open'; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return ''; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Formats a string as a thing by converting it to a Carbon first. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function formatDate(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'formatDate', | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |             static function (string $date, string $format): string { | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |                 $carbon = new Carbon($date); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $carbon->isoFormat($format); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-01-04 07:44:52 +01:00
										 |  |  |      * TODO Remove me when v2 hits. | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function getMetaField(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'accountGetMetaField', | 
					
						
							|  |  |  |             static function (Account $account, string $field): string { | 
					
						
							|  |  |  |                 /** @var AccountRepositoryInterface $repository */ | 
					
						
							|  |  |  |                 $repository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |                 $result     = $repository->getMetaValue($account, $field); | 
					
						
							|  |  |  |                 if (null === $result) { | 
					
						
							|  |  |  |                     return ''; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $result; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Will return true if the user is of role X. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function hasRole(): TwigFunction | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							|  |  |  |             'hasRole', | 
					
						
							|  |  |  |             static function (string $role): bool { | 
					
						
							|  |  |  |                 $repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |                 if ($repository->hasRole(auth()->user(), $role)) { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function getRootSearchOperator(): TwigFunction | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             'getRootSearchOperator', | 
					
						
							|  |  |  |             static function (string $operator): string { | 
					
						
							|  |  |  |                 $result = OperatorQuerySearch::getRootOperator($operator); | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 return str_replace('-', 'not_', $result); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     protected function carbonize(): TwigFunction | 
					
						
							| 
									
										
										
										
											2020-08-27 20:22:52 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return new TwigFunction( | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             'carbonize', | 
					
						
							|  |  |  |             static function (string $date): Carbon { | 
					
						
							|  |  |  |                 return new Carbon($date, config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2020-08-27 20:22:52 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-05 12:51:57 +02:00
										 |  |  | } |