2016-01-14 11:09:20 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RuleFormRequest.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-01-14 11:09:20 -06:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-01-14 11:09:20 -06:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:41:23 -05:00
|
|
|
|
2016-01-14 11:09:20 -06:00
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
2017-09-03 08:57:13 -05:00
|
|
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
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
|
|
|
*/
|
|
|
|
class RuleFormRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Verify the request.
|
|
|
|
*
|
2016-01-14 11:09:20 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-06-30 10:49:14 -05:00
|
|
|
public function authorize(): bool
|
2016-01-14 11:09:20 -06:00
|
|
|
{
|
|
|
|
// Only allow logged in users
|
2016-09-16 05:07:45 -05:00
|
|
|
return auth()->check();
|
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
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2016-10-23 05:10:22 -05:00
|
|
|
*/
|
|
|
|
public function getRuleData(): array
|
|
|
|
{
|
2018-06-30 10:49:14 -05:00
|
|
|
$data = [
|
|
|
|
'title' => $this->string('title'),
|
|
|
|
'rule_group_id' => $this->integer('rule_group_id'),
|
|
|
|
'active' => $this->boolean('active'),
|
|
|
|
'trigger' => $this->string('trigger'),
|
|
|
|
'description' => $this->string('description'),
|
|
|
|
'stop-processing' => $this->boolean('stop_processing'),
|
|
|
|
'strict' => $this->boolean('strict'),
|
|
|
|
'rule-triggers' => [],
|
|
|
|
'rule-actions' => [],
|
2016-10-23 05:10:22 -05:00
|
|
|
];
|
2018-06-30 10:49:14 -05:00
|
|
|
$triggers = $this->get('rule-trigger');
|
|
|
|
$triggerValues = $this->get('rule-trigger-value');
|
|
|
|
$triggerStop = $this->get('rule-trigger-stop');
|
|
|
|
|
|
|
|
$actions = $this->get('rule-action');
|
|
|
|
$actionValues = $this->get('rule-action-value');
|
|
|
|
$actionStop = $this->get('rule-action-stop');
|
|
|
|
|
|
|
|
if (\is_array($triggers)) {
|
|
|
|
foreach ($triggers as $index => $value) {
|
|
|
|
$data['rule-triggers'][] = [
|
|
|
|
'name' => $value,
|
|
|
|
'value' => $triggerValues[$index] ?? '',
|
2018-07-08 00:59:58 -05:00
|
|
|
'stop-processing' => 1 === (int)($triggerStop[$index] ?? 0),
|
2018-06-30 10:49:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\is_array($actions)) {
|
|
|
|
foreach ($actions as $index => $value) {
|
|
|
|
$data['rule-actions'][] = [
|
|
|
|
'name' => $value,
|
|
|
|
'value' => $actionValues[$index] ?? '',
|
2018-07-08 00:59:58 -05:00
|
|
|
'stop-processing' => 1 === (int)($actionStop[$index] ?? 0),
|
2018-06-30 10:49:14 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2016-10-23 05:10:22 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-09-03 08:57:13 -05:00
|
|
|
/** @var RuleRepositoryInterface $repository */
|
|
|
|
$repository = app(RuleRepositoryInterface::class);
|
2016-04-26 14:40:15 -05:00
|
|
|
$validTriggers = array_keys(config('firefly.rule-triggers'));
|
|
|
|
$validActions = array_keys(config('firefly.rule-actions'));
|
2016-01-14 12:20:02 -06:00
|
|
|
|
|
|
|
// some actions require text:
|
2018-03-28 12:37:59 -05:00
|
|
|
$contextActions = implode(',', config('firefly.rule-actions-text'));
|
2016-01-14 11:09:20 -06:00
|
|
|
|
2017-09-03 03:38:41 -05:00
|
|
|
$titleRule = 'required|between:1,100|uniqueObjectForUser:rules,title';
|
2018-07-22 14:09:57 -05:00
|
|
|
if (null !== $repository->find((int)$this->get('id'))) {
|
2018-04-02 08:10:40 -05:00
|
|
|
$titleRule = 'required|between:1,100|uniqueObjectForUser:rules,title,' . (int)$this->get('id');
|
2016-01-14 11:09:20 -06:00
|
|
|
}
|
2016-01-14 12:20:02 -06:00
|
|
|
$rules = [
|
|
|
|
'title' => $titleRule,
|
2017-09-12 14:44:31 -05:00
|
|
|
'description' => 'between:1,5000|nullable',
|
2016-01-14 12:20:02 -06:00
|
|
|
'stop_processing' => 'boolean',
|
2016-01-14 14:34:17 -06:00
|
|
|
'rule_group_id' => 'required|belongsToUser:rule_groups',
|
2016-01-14 12:20:02 -06:00
|
|
|
'trigger' => 'required|in:store-journal,update-journal',
|
2018-03-28 12:37:59 -05:00
|
|
|
'rule-trigger.*' => 'required|in:' . implode(',', $validTriggers),
|
2016-01-14 14:34:17 -06:00
|
|
|
'rule-trigger-value.*' => 'required|min:1|ruleTriggerValue',
|
2018-03-28 12:37:59 -05:00
|
|
|
'rule-action.*' => 'required|in:' . implode(',', $validActions),
|
2018-04-14 13:31:31 -05:00
|
|
|
'strict' => 'in:0,1',
|
2016-01-14 11:09:20 -06:00
|
|
|
];
|
2016-01-14 14:34:17 -06:00
|
|
|
// since Laravel does not support this stuff yet, here's a trick.
|
2017-11-15 05:25:49 -06:00
|
|
|
for ($i = 0; $i < 10; ++$i) {
|
2016-01-14 14:34:17 -06:00
|
|
|
$rules['rule-action-value.' . $i] = 'required_if:rule-action.' . $i . ',' . $contextActions . '|ruleActionValue';
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|