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

141 lines
4.8 KiB
PHP
Raw Normal View History

2016-01-14 18:09:20 +01:00
<?php
/**
* 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
*
* 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 08:40:00 +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
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +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
*/
declare(strict_types=1);
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;
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
{
2020-10-24 07:55:09 +02:00
use ConvertsDataTypes, GetRuleConfiguration, ChecksLogin;
2016-01-14 18:09:20 +01:00
2016-10-23 12:10:22 +02:00
/**
2018-07-22 08:10:16 +02:00
* Get all data for controller.
*
2016-10-23 12:10:22 +02:00
* @return array
2018-07-20 14:34:56 +02:00
*
2016-10-23 12:10:22 +02:00
*/
public function getRuleData(): array
{
2020-10-23 19:11:25 +02:00
return [
2022-05-02 19:35:35 +02:00
'title' => $this->convertString('title'),
2022-09-30 20:07:01 +02:00
'rule_group_id' => $this->convertInteger('rule_group_id'),
2018-06-30 17:49:14 +02:00
'active' => $this->boolean('active'),
2022-05-02 19:35:35 +02:00
'trigger' => $this->convertString('trigger'),
2021-04-06 13:30:09 +02:00
'description' => $this->stringWithNewlines('description'),
2018-08-05 15:34:20 +02:00
'stop_processing' => $this->boolean('stop_processing'),
2018-06-30 17:49:14 +02:00
'strict' => $this->boolean('strict'),
2018-12-09 20:54:11 +01:00
'triggers' => $this->getRuleTriggerData(),
'actions' => $this->getRuleActionData(),
2016-10-23 12:10:22 +02:00
];
}
2020-10-24 07:55:09 +02: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';
$return[] = [
'type' => $trigger['type'] ?? 'invalid',
'value' => $trigger['value'] ?? '',
2022-03-29 14:58:06 +02:00
'stop_processing' => 1 === (int) $stopProcessing,
2020-10-24 07:55:09 +02: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 14:58:06 +02:00
'stop_processing' => 1 === (int) $stopProcessing,
2020-10-24 07:55:09 +02:00
];
}
}
return $return;
}
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
{
$validTriggers = $this->getTriggers();
2016-04-26 21:40:15 +02:00
$validActions = array_keys(config('firefly.rule-actions'));
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):
$contextTriggers = implode(',', $this->getTriggersWithContext());
2018-08-07 19:24:07 +02:00
2018-10-14 17:38:26 +02:00
// initial set of rules:
$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',
2018-12-18 07:08:46 +01: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 11:34:53 +01:00
'actions.*.value' => sprintf('required_if:actions.*.type,%s|min:0|max:255|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
/** @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 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
}
}