| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-28 06:49:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * RuleFormRequest.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01: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-01-14 18:09:20 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | namespace FireflyIII\Http\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 19:24:07 +02:00
										 |  |  | use FireflyIII\Models\Rule; | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  | use FireflyIII\Support\Request\ChecksLogin; | 
					
						
							| 
									
										
										
										
											2020-07-18 08:42:13 +02:00
										 |  |  | use FireflyIII\Support\Request\ConvertsDataTypes; | 
					
						
							| 
									
										
										
										
											2020-08-21 08:23:44 +02:00
										 |  |  | use FireflyIII\Support\Request\GetRuleConfiguration; | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  | use Illuminate\Foundation\Http\FormRequest; | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class RuleFormRequest. | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  | class RuleFormRequest extends FormRequest | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-04 14:18:49 +01:00
										 |  |  |     use ChecksLogin; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |     use ConvertsDataTypes; | 
					
						
							|  |  |  |     use GetRuleConfiguration; | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-28 06:49:40 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Get all data for controller. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getRuleData(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             'title'           => $this->convertString('title'), | 
					
						
							|  |  |  |             'rule_group_id'   => $this->convertInteger('rule_group_id'), | 
					
						
							|  |  |  |             'active'          => $this->boolean('active'), | 
					
						
							|  |  |  |             'trigger'         => $this->convertString('trigger'), | 
					
						
							|  |  |  |             'description'     => $this->stringWithNewlines('description'), | 
					
						
							|  |  |  |             'stop_processing' => $this->boolean('stop_processing'), | 
					
						
							|  |  |  |             'strict'          => $this->boolean('strict'), | 
					
						
							|  |  |  |             'triggers'        => $this->getRuleTriggerData(), | 
					
						
							|  |  |  |             'actions'         => $this->getRuleActionData(), | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function getRuleTriggerData(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return      = []; | 
					
						
							|  |  |  |         $triggerData = $this->get('triggers'); | 
					
						
							|  |  |  |         if (is_array($triggerData)) { | 
					
						
							|  |  |  |             foreach ($triggerData as $trigger) { | 
					
						
							|  |  |  |                 $stopProcessing = $trigger['stop_processing'] ?? '0'; | 
					
						
							|  |  |  |                 $prohibited     = $trigger['prohibited'] ?? '0'; | 
					
						
							|  |  |  |                 $set            = [ | 
					
						
							|  |  |  |                     'type'            => $trigger['type'] ?? 'invalid', | 
					
						
							|  |  |  |                     'value'           => $trigger['value'] ?? '', | 
					
						
							|  |  |  |                     'stop_processing' => 1 === (int)$stopProcessing, | 
					
						
							|  |  |  |                     'prohibited'      => 1 === (int)$prohibited, | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |                 $set            = self::replaceAmountTrigger($set); | 
					
						
							|  |  |  |                 $return[]       = $set; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param array $array | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-28 06:49:40 +01:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function replaceAmountTrigger(array $array): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // do some sneaky search and replace.
 | 
					
						
							|  |  |  |         $amountFields = [ | 
					
						
							|  |  |  |             'amount_is', | 
					
						
							|  |  |  |             'amount', | 
					
						
							|  |  |  |             'amount_exactly', | 
					
						
							|  |  |  |             'amount_less', | 
					
						
							|  |  |  |             'amount_max', | 
					
						
							|  |  |  |             'amount_more', | 
					
						
							|  |  |  |             'amount_min', | 
					
						
							|  |  |  |             'foreign_amount_is', | 
					
						
							|  |  |  |             'foreign_amount', | 
					
						
							|  |  |  |             'foreign_amount_less', | 
					
						
							|  |  |  |             'foreign_amount_max', | 
					
						
							|  |  |  |             'foreign_amount_more', | 
					
						
							|  |  |  |             'foreign_amount_min', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         if (in_array($array['type'], $amountFields, true) && '0' === $array['value']) { | 
					
						
							|  |  |  |             $array['value'] = '0.00'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $array; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function getRuleActionData(): array | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $return     = []; | 
					
						
							|  |  |  |         $actionData = $this->get('actions'); | 
					
						
							|  |  |  |         if (is_array($actionData)) { | 
					
						
							|  |  |  |             foreach ($actionData as $action) { | 
					
						
							|  |  |  |                 $stopProcessing = $action['stop_processing'] ?? '0'; | 
					
						
							|  |  |  |                 $return[]       = [ | 
					
						
							|  |  |  |                     'type'            => $action['type'] ?? 'invalid', | 
					
						
							|  |  |  |                     'value'           => $action['value'] ?? '', | 
					
						
							|  |  |  |                     'stop_processing' => 1 === (int)$stopProcessing, | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2020-10-24 07:55:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:10:16 +02:00
										 |  |  |      * Rules for this request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-30 17:49:14 +02:00
										 |  |  |     public function rules(): array | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-21 08:23:44 +02:00
										 |  |  |         $validTriggers = $this->getTriggers(); | 
					
						
							| 
									
										
										
										
											2016-04-26 21:40:15 +02:00
										 |  |  |         $validActions  = array_keys(config('firefly.rule-actions')); | 
					
						
							| 
									
										
										
										
											2016-01-14 19:20:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-14 17:38:26 +02:00
										 |  |  |         // some actions require text (aka context):
 | 
					
						
							|  |  |  |         $contextActions = implode(',', config('firefly.context-rule-actions')); | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-14 17:38:26 +02:00
										 |  |  |         // some triggers require text (aka context):
 | 
					
						
							| 
									
										
										
										
											2020-08-21 08:23:44 +02:00
										 |  |  |         $contextTriggers = implode(',', $this->getTriggersWithContext()); | 
					
						
							| 
									
										
										
										
											2018-08-07 19:24:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-14 17:38:26 +02:00
										 |  |  |         // initial set of rules:
 | 
					
						
							| 
									
										
										
										
											2016-01-14 19:20:02 +01:00
										 |  |  |         $rules = [ | 
					
						
							| 
									
										
										
										
											2018-12-09 20:54:11 +01:00
										 |  |  |             'title'            => 'required|between:1,100|uniqueObjectForUser:rules,title', | 
					
						
							|  |  |  |             'description'      => 'between:1,5000|nullable', | 
					
						
							|  |  |  |             'stop_processing'  => 'boolean', | 
					
						
							|  |  |  |             'rule_group_id'    => 'required|belongsToUser:rule_groups', | 
					
						
							|  |  |  |             'trigger'          => 'required|in:store-journal,update-journal', | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             'triggers.*.type'  => 'required|in:' . implode(',', $validTriggers), | 
					
						
							| 
									
										
										
										
											2023-04-26 06:17:04 +02:00
										 |  |  |             'triggers.*.value' => sprintf('required_if:triggers.*.type,%s|max:1024|min:1|ruleTriggerValue', $contextTriggers), | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             'actions.*.type'   => 'required|in:' . implode(',', $validActions), | 
					
						
							| 
									
										
										
										
											2023-04-26 06:17:04 +02:00
										 |  |  |             'actions.*.value'  => sprintf('required_if:actions.*.type,%s|min:0|max:1024|ruleActionValue', $contextActions), | 
					
						
							| 
									
										
										
										
											2018-12-09 20:54:11 +01:00
										 |  |  |             'strict'           => 'in:0,1', | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-10-14 17:38:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 16:55:16 +01:00
										 |  |  |         /** @var Rule|null $rule */ | 
					
						
							| 
									
										
										
										
											2018-10-14 17:38:26 +02:00
										 |  |  |         $rule = $this->route()->parameter('rule'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (null !== $rule) { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |             $rules['title'] = 'required|between:1,100|uniqueObjectForUser:rules,title,' . $rule->id; | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-15 23:12:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 21:34:17 +01:00
										 |  |  |         return $rules; | 
					
						
							| 
									
										
										
										
											2016-01-14 18:09:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |