firefly-iii/app/Http/Requests/RuleFormRequest.php

146 lines
4.9 KiB
PHP
Raw Normal View History

2016-01-14 11:09:20 -06:00
<?php
/**
* RuleFormRequest.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2016-01-14 11:09:20 -06: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.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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.
2017-10-21 01:40:00 -05: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 11:09:20 -06:00
*/
declare(strict_types=1);
2016-01-14 11:09:20 -06:00
namespace FireflyIII\Http\Requests;
2018-08-07 12:24:07 -05:00
use FireflyIII\Models\Rule;
2020-10-24 00:55:09 -05:00
use FireflyIII\Support\Request\ChecksLogin;
2020-07-18 01:42:13 -05:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Support\Request\GetRuleConfiguration;
2020-10-24 00:55:09 -05:00
use Illuminate\Foundation\Http\FormRequest;
2016-01-14 11:09:20 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class RuleFormRequest.
2016-01-14 11:09:20 -06:00
*/
2020-10-24 00:55:09 -05:00
class RuleFormRequest extends FormRequest
2016-01-14 11:09:20 -06:00
{
2022-10-30 08:24:28 -05:00
use ConvertsDataTypes;
use GetRuleConfiguration;
use ChecksLogin;
2016-01-14 11:09:20 -06:00
2016-10-23 05:10:22 -05:00
/**
2018-07-22 01:10:16 -05:00
* Get all data for controller.
*
2016-10-23 05:10:22 -05:00
* @return array
2018-07-20 07:34:56 -05:00
*
2016-10-23 05:10:22 -05:00
*/
public function getRuleData(): array
{
2020-10-23 12:11:25 -05:00
return [
2022-05-02 12:35:35 -05:00
'title' => $this->convertString('title'),
2022-09-30 13:07:01 -05:00
'rule_group_id' => $this->convertInteger('rule_group_id'),
2018-06-30 10:49:14 -05:00
'active' => $this->boolean('active'),
2022-05-02 12:35:35 -05:00
'trigger' => $this->convertString('trigger'),
2021-04-06 06:30:09 -05:00
'description' => $this->stringWithNewlines('description'),
2018-08-05 08:34:20 -05:00
'stop_processing' => $this->boolean('stop_processing'),
2018-06-30 10:49:14 -05:00
'strict' => $this->boolean('strict'),
2018-12-09 13:54:11 -06:00
'triggers' => $this->getRuleTriggerData(),
'actions' => $this->getRuleActionData(),
2016-10-23 05:10:22 -05:00
];
}
2020-10-24 00:55:09 -05:00
/**
* @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 = [
2020-10-24 00:55:09 -05:00
'type' => $trigger['type'] ?? 'invalid',
'value' => $trigger['value'] ?? '',
2022-03-29 07:58:06 -05:00
'stop_processing' => 1 === (int) $stopProcessing,
'prohibited' => 1 === (int) $prohibited,
2020-10-24 00:55:09 -05:00
];
$return[] = $set;
2020-10-24 00:55:09 -05:00
}
}
return $return;
}
/**
* @return array
*/
private function getRuleActionData(): array
{
$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'] ?? '',
2022-03-29 07:58:06 -05:00
'stop_processing' => 1 === (int) $stopProcessing,
2020-10-24 00:55:09 -05:00
];
}
}
return $return;
}
2016-01-14 11:09:20 -06:00
/**
2018-07-22 01:10:16 -05:00
* Rules for this request.
*
2016-01-14 11:09:20 -06:00
* @return array
*/
2018-06-30 10:49:14 -05:00
public function rules(): array
2016-01-14 11:09:20 -06:00
{
$validTriggers = $this->getTriggers();
2016-04-26 14:40:15 -05:00
$validActions = array_keys(config('firefly.rule-actions'));
2018-10-14 10:38:26 -05:00
// some actions require text (aka context):
$contextActions = implode(',', config('firefly.context-rule-actions'));
2016-01-14 11:09:20 -06:00
2018-10-14 10:38:26 -05:00
// some triggers require text (aka context):
$contextTriggers = implode(',', $this->getTriggersWithContext());
2018-08-07 12:24:07 -05:00
2018-10-14 10:38:26 -05:00
// initial set of rules:
$rules = [
2018-12-09 13:54:11 -06: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',
2018-12-18 00:08:46 -06:00
'triggers.*.type' => 'required|in:' . implode(',', $validTriggers),
'triggers.*.value' => sprintf('required_if:triggers.*.type,%s|min:1|ruleTriggerValue', $contextTriggers),
'actions.*.type' => 'required|in:' . implode(',', $validActions),
2019-11-17 04:34:53 -06:00
'actions.*.value' => sprintf('required_if:actions.*.type,%s|min:0|max:255|ruleActionValue', $contextActions),
2018-12-09 13:54:11 -06:00
'strict' => 'in:0,1',
2016-01-14 11:09:20 -06:00
];
2018-10-14 10:38:26 -05:00
/** @var Rule $rule */
$rule = $this->route()->parameter('rule');
if (null !== $rule) {
$rules['title'] = 'required|between:1,100|uniqueObjectForUser:rules,title,' . $rule->id;
2016-01-14 14:34:17 -06:00
}
2016-01-15 16:12:52 -06:00
2016-01-14 14:34:17 -06:00
return $rules;
2016-01-14 11:09:20 -06:00
}
}