Files
firefly-iii/app/Api/V1/Requests/Models/Rule/UpdateRequest.php

248 lines
9.0 KiB
PHP
Raw Normal View History

2018-06-30 16:46:51 +02:00
<?php
/**
* RuleUpdateRequest.php
* Copyright (c) 2019 james@firefly-iii.org
2018-06-30 16:46:51 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-06-30 16:46:51 +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.
2018-06-30 16:46:51 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-06-30 16:46:51 +02:00
* 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.
2018-06-30 16:46:51 +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/>.
2018-06-30 16:46:51 +02:00
*/
declare(strict_types=1);
2021-03-06 17:43:06 +01:00
namespace FireflyIII\Api\V1\Requests\Models\Rule;
2018-06-30 16:46:51 +02:00
2022-10-31 05:39:22 +01:00
use FireflyIII\Models\Rule;
2018-12-07 16:03:05 +01:00
use FireflyIII\Rules\IsBoolean;
2020-11-08 13:36:13 +01:00
use FireflyIII\Support\Request\ChecksLogin;
2020-07-18 08:34:00 +02:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Support\Request\GetRuleConfiguration;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
2022-10-30 14:23:00 +01:00
2018-06-30 16:46:51 +02:00
/**
2021-03-06 17:43:06 +01:00
* Class UpdateRequest
2018-06-30 16:46:51 +02:00
*/
2021-03-06 17:43:06 +01:00
class UpdateRequest extends FormRequest
2018-06-30 16:46:51 +02:00
{
2023-11-04 14:18:49 +01:00
use ChecksLogin;
2022-10-30 14:23:00 +01:00
use ConvertsDataTypes;
use GetRuleConfiguration;
2020-11-08 13:36:13 +01:00
2018-06-30 16:46:51 +02:00
/**
* Get all data from the request.
2018-06-30 16:46:51 +02:00
*/
public function getAll(): array
{
2021-03-12 06:11:34 +01:00
$fields = [
2022-05-04 05:53:47 +02:00
'title' => ['title', 'convertString'],
2021-04-06 13:30:09 +02:00
'description' => ['description', 'stringWithNewlines'],
2022-10-01 05:29:42 +02:00
'rule_group_id' => ['rule_group_id', 'convertInteger'],
2022-05-04 05:53:47 +02:00
'trigger' => ['trigger', 'convertString'],
2021-03-12 06:11:34 +01:00
'strict' => ['strict', 'boolean'],
'stop_processing' => ['stop_processing', 'boolean'],
'active' => ['active', 'boolean'],
2022-10-01 05:29:42 +02:00
'order' => ['order', 'convertInteger'],
2021-03-12 06:11:34 +01:00
];
$return = $this->getAllData($fields);
$triggers = $this->getRuleTriggers();
$actions = $this->getRuleActions();
2021-03-21 09:15:40 +01:00
if (null !== $triggers) {
2021-03-12 06:11:34 +01:00
$return['triggers'] = $triggers;
2018-12-07 16:03:05 +01:00
}
2021-03-21 09:15:40 +01:00
if (null !== $actions) {
2021-03-12 06:11:34 +01:00
$return['actions'] = $actions;
2018-12-07 16:03:05 +01:00
}
2021-03-12 06:11:34 +01:00
return $return;
2018-06-30 16:46:51 +02:00
}
/**
* The rules that the incoming request must be matched against.
2018-06-30 16:46:51 +02:00
*/
public function rules(): array
{
$validTriggers = $this->getTriggers();
2018-06-30 16:46:51 +02:00
$validActions = array_keys(config('firefly.rule-actions'));
2022-10-31 05:39:22 +01:00
/** @var Rule $rule */
2022-12-29 19:41:57 +01:00
$rule = $this->route()->parameter('rule');
2018-06-30 16:46:51 +02:00
// some triggers and actions require text:
$contextTriggers = implode(',', $this->getTriggersWithContext());
$contextActions = implode(',', config('firefly.context-rule-actions'));
2020-03-15 08:16:16 +01:00
return [
'title' => sprintf('between:1,100|uniqueObjectForUser:rules,title,%d', $rule->id),
2018-12-16 13:55:19 +01:00
'description' => 'between:1,5000|nullable',
'rule_group_id' => 'belongsToUser:rule_groups',
'rule_group_title' => 'nullable|between:1,255|belongsToUser:rule_groups,title',
'trigger' => 'in:store-journal,update-journal',
2023-12-20 19:35:52 +01:00
'triggers.*.type' => 'required|in:'.implode(',', $validTriggers),
'triggers.*.value' => 'required_if:actions.*.type,'.$contextTriggers.'|min:1|ruleTriggerValue|max:1024',
2022-10-30 14:23:00 +01:00
'triggers.*.stop_processing' => [new IsBoolean()],
'triggers.*.active' => [new IsBoolean()],
2023-12-20 19:35:52 +01:00
'actions.*.type' => 'required|in:'.implode(',', $validActions),
'actions.*.value' => 'required_if:actions.*.type,'.$contextActions.'|ruleActionValue',
2022-10-30 14:23:00 +01:00
'actions.*.stop_processing' => [new IsBoolean()],
'actions.*.active' => [new IsBoolean()],
'strict' => [new IsBoolean()],
'stop_processing' => [new IsBoolean()],
'active' => [new IsBoolean()],
'order' => 'numeric|between:1,1337',
2018-06-30 16:46:51 +02:00
];
}
/**
* Configure the validator instance.
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$this->atLeastOneTrigger($validator);
$this->atLeastOneValidTrigger($validator);
$this->atLeastOneAction($validator);
$this->atLeastOneValidAction($validator);
}
);
}
2023-05-29 13:56:55 +02:00
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*/
protected function atLeastOneTrigger(Validator $validator): void
{
$data = $validator->getData();
$triggers = $data['triggers'] ?? null;
2019-09-04 17:39:39 +02:00
// need at least one trigger
2022-10-30 14:44:49 +01:00
if (is_array($triggers) && 0 === count($triggers)) {
2022-12-29 19:41:57 +01:00
$validator->errors()->add('title', (string)trans('validation.at_least_one_trigger'));
}
}
/**
* Adds an error to the validator when there are no repetitions in the array of data.
*/
2023-05-29 13:56:55 +02:00
protected function atLeastOneValidTrigger(Validator $validator): void
{
$data = $validator->getData();
2023-05-29 13:56:55 +02:00
$triggers = $data['triggers'] ?? [];
$allInactive = true;
$inactiveIndex = 0;
2023-05-29 13:56:55 +02:00
// need at least one trigger
if (is_array($triggers) && 0 === count($triggers)) {
return;
}
2023-05-29 13:56:55 +02:00
foreach ($triggers as $index => $trigger) {
$active = array_key_exists('active', $trigger) ? $trigger['active'] : true; // assume true
if (true === $active) {
$allInactive = false;
}
if (false === $active) {
$inactiveIndex = $index;
}
}
if (true === $allInactive) {
2023-05-29 13:56:55 +02:00
$validator->errors()->add(sprintf('triggers.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_trigger'));
}
}
/**
2023-06-21 12:34:58 +02:00
* Adds an error to the validator when there are no repetitions in the array of data.
2023-05-29 13:56:55 +02:00
*/
2023-06-21 12:34:58 +02:00
protected function atLeastOneAction(Validator $validator): void
2023-05-29 13:56:55 +02:00
{
2023-06-21 12:34:58 +02:00
$data = $validator->getData();
$actions = $data['actions'] ?? null;
// need at least one action
if (is_array($actions) && 0 === count($actions)) {
$validator->errors()->add('title', (string)trans('validation.at_least_one_action'));
2023-05-29 13:56:55 +02:00
}
}
/**
2023-06-21 12:34:58 +02:00
* Adds an error to the validator when there are no repetitions in the array of data.
2023-05-29 13:56:55 +02:00
*/
2023-06-21 12:34:58 +02:00
protected function atLeastOneValidAction(Validator $validator): void
2023-05-29 13:56:55 +02:00
{
2023-06-21 12:34:58 +02:00
$data = $validator->getData();
$actions = $data['actions'] ?? [];
$allInactive = true;
$inactiveIndex = 0;
// need at least one action
if (is_array($actions) && 0 === count($actions)) {
return;
2023-05-29 13:56:55 +02:00
}
2023-06-21 12:34:58 +02:00
foreach ($actions as $index => $action) {
$active = array_key_exists('active', $action) ? $action['active'] : true; // assume true
if (true === $active) {
$allInactive = false;
}
if (false === $active) {
$inactiveIndex = $index;
2023-05-29 13:56:55 +02:00
}
}
2023-06-21 12:34:58 +02:00
if (true === $allInactive) {
$validator->errors()->add(sprintf('actions.%d.active', $inactiveIndex), (string)trans('validation.at_least_one_active_action'));
}
}
2023-12-20 19:35:52 +01:00
private function getRuleTriggers(): ?array
{
if (!$this->has('triggers')) {
return null;
}
$triggers = $this->get('triggers');
$return = [];
if (is_array($triggers)) {
foreach ($triggers as $trigger) {
$active = array_key_exists('active', $trigger) ? $trigger['active'] : true;
$stopProcessing = array_key_exists('stop_processing', $trigger) ? $trigger['stop_processing'] : false;
$return[] = [
'type' => $trigger['type'],
'value' => $trigger['value'],
'active' => $active,
'stop_processing' => $stopProcessing,
];
}
}
return $return;
}
private function getRuleActions(): ?array
{
if (!$this->has('actions')) {
return null;
}
$actions = $this->get('actions');
$return = [];
if (is_array($actions)) {
foreach ($actions as $action) {
$return[] = [
'type' => $action['type'],
'value' => $action['value'],
'active' => $this->convertBoolean((string)($action['active'] ?? 'false')),
'stop_processing' => $this->convertBoolean((string)($action['stop_processing'] ?? 'false')),
];
}
}
return $return;
}
}