This commit is contained in:
James Cole
2022-12-28 06:49:40 +01:00
parent 5e654786be
commit 32e89155a0
2 changed files with 53 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
<?php <?php
/** /**
* RuleFormRequest.php * RuleFormRequest.php
* Copyright (c) 2019 james@firefly-iii.org * Copyright (c) 2019 james@firefly-iii.org
@@ -66,17 +67,48 @@ class RuleFormRequest extends FormRequest
if (is_array($triggerData)) { if (is_array($triggerData)) {
foreach ($triggerData as $trigger) { foreach ($triggerData as $trigger) {
$stopProcessing = $trigger['stop_processing'] ?? '0'; $stopProcessing = $trigger['stop_processing'] ?? '0';
$return[] = [ $current = [
'type' => $trigger['type'] ?? 'invalid', 'type' => $trigger['type'] ?? 'invalid',
'value' => $trigger['value'] ?? '', 'value' => $trigger['value'] ?? '',
'stop_processing' => 1 === (int) $stopProcessing, 'stop_processing' => 1 === (int)$stopProcessing,
]; ];
$current = self::replaceAmountTrigger($current);
$return[] = $current;
} }
} }
return $return; return $return;
} }
/**
* @param array $array
* @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;
}
/** /**
* @return array * @return array
*/ */
@@ -90,7 +122,7 @@ class RuleFormRequest extends FormRequest
$return[] = [ $return[] = [
'type' => $action['type'] ?? 'invalid', 'type' => $action['type'] ?? 'invalid',
'value' => $action['value'] ?? '', 'value' => $action['value'] ?? '',
'stop_processing' => 1 === (int) $stopProcessing, 'stop_processing' => 1 === (int)$stopProcessing,
]; ];
} }
} }
@@ -121,9 +153,9 @@ class RuleFormRequest extends FormRequest
'stop_processing' => 'boolean', 'stop_processing' => 'boolean',
'rule_group_id' => 'required|belongsToUser:rule_groups', 'rule_group_id' => 'required|belongsToUser:rule_groups',
'trigger' => 'required|in:store-journal,update-journal', 'trigger' => 'required|in:store-journal,update-journal',
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers), 'triggers.*.type' => 'required|in:'.implode(',', $validTriggers),
'triggers.*.value' => sprintf('required_if:triggers.*.type,%s|min:1|ruleTriggerValue', $contextTriggers), 'triggers.*.value' => sprintf('required_if:triggers.*.type,%s|min:1|ruleTriggerValue', $contextTriggers),
'actions.*.type' => 'required|in:' . implode(',', $validActions), 'actions.*.type' => 'required|in:'.implode(',', $validActions),
'actions.*.value' => sprintf('required_if:actions.*.type,%s|min:0|max:255|ruleActionValue', $contextActions), 'actions.*.value' => sprintf('required_if:actions.*.type,%s|min:0|max:255|ruleActionValue', $contextActions),
'strict' => 'in:0,1', 'strict' => 'in:0,1',
]; ];
@@ -132,7 +164,7 @@ class RuleFormRequest extends FormRequest
$rule = $this->route()->parameter('rule'); $rule = $this->route()->parameter('rule');
if (null !== $rule) { if (null !== $rule) {
$rules['title'] = 'required|between:1,100|uniqueObjectForUser:rules,title,' . $rule->id; $rules['title'] = 'required|between:1,100|uniqueObjectForUser:rules,title,'.$rule->id;
} }
return $rules; return $rules;

View File

@@ -27,6 +27,7 @@ use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Exceptions\ValidationException; use FireflyIII\Exceptions\ValidationException;
use FireflyIII\Helpers\Help\HelpInterface; use FireflyIII\Helpers\Help\HelpInterface;
use FireflyIII\Http\Requests\RuleFormRequest;
use FireflyIII\Http\Requests\TestRuleFormRequest; use FireflyIII\Http\Requests\TestRuleFormRequest;
use FireflyIII\Support\Binder\AccountList; use FireflyIII\Support\Binder\AccountList;
use FireflyIII\User; use FireflyIII\User;
@@ -60,7 +61,7 @@ trait RequestInformation
/** /**
* Get a list of triggers. * Get a list of triggers.
* *
* @param TestRuleFormRequest $request * @param TestRuleFormRequest $request
* *
* @return array * @return array
*/ */
@@ -70,14 +71,15 @@ trait RequestInformation
$data = $request->get('triggers'); $data = $request->get('triggers');
if (is_array($data)) { if (is_array($data)) {
foreach ($data as $triggerInfo) { foreach ($data as $triggerInfo) {
$triggers[] = [ $current = [
'type' => $triggerInfo['type'] ?? '', 'type' => $triggerInfo['type'] ?? '',
'value' => $triggerInfo['value'] ?? '', 'value' => $triggerInfo['value'] ?? '',
'stop_processing' => 1 === (int) ($triggerInfo['stop_processing'] ?? '0'), 'stop_processing' => 1 === (int)($triggerInfo['stop_processing'] ?? '0'),
]; ];
$current = RuleFormRequest::replaceAmountTrigger($current);
$triggers[] = $current;
} }
} }
return $triggers; return $triggers;
} }
@@ -127,13 +129,13 @@ trait RequestInformation
*/ */
final protected function getSpecificPageName(): string // get request info final protected function getSpecificPageName(): string // get request info
{ {
return null === RouteFacade::current()->parameter('objectType') ? '' : '_' . RouteFacade::current()->parameter('objectType'); return null === RouteFacade::current()->parameter('objectType') ? '' : '_'.RouteFacade::current()->parameter('objectType');
} }
/** /**
* Check if date is outside session range. * Check if date is outside session range.
* *
* @param Carbon $date * @param Carbon $date
* *
* @return bool * @return bool
* *
@@ -159,7 +161,7 @@ trait RequestInformation
/** /**
* Parses attributes from URL * Parses attributes from URL
* *
* @param array $attributes * @param array $attributes
* *
* @return array * @return array
*/ */
@@ -189,9 +191,9 @@ trait RequestInformation
/** /**
* Validate users new password. * Validate users new password.
* *
* @param User $user * @param User $user
* @param string $current * @param string $current
* @param string $new * @param string $new
* *
* @return bool * @return bool
* *
@@ -200,11 +202,11 @@ trait RequestInformation
final protected function validatePassword(User $user, string $current, string $new): bool //get request info final protected function validatePassword(User $user, string $current, string $new): bool //get request info
{ {
if (!Hash::check($current, $user->password)) { if (!Hash::check($current, $user->password)) {
throw new ValidationException((string) trans('firefly.invalid_current_password')); throw new ValidationException((string)trans('firefly.invalid_current_password'));
} }
if ($current === $new) { if ($current === $new) {
throw new ValidationException((string) trans('firefly.should_change')); throw new ValidationException((string)trans('firefly.should_change'));
} }
return true; return true;
@@ -213,7 +215,7 @@ trait RequestInformation
/** /**
* Get a validator for an incoming registration request. * Get a validator for an incoming registration request.
* *
* @param array $data * @param array $data
* *
* @return ValidatorContract * @return ValidatorContract
* @codeCoverageIgnore * @codeCoverageIgnore