Files
firefly-iii/app/Support/Http/Controllers/RuleManagement.php

227 lines
7.8 KiB
PHP
Raw Normal View History

2018-07-20 14:34:56 +02:00
<?php
/**
* RuleManagement.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-20 14:34:56 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-20 14:34:56 +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-07-20 14:34:56 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-20 14:34:56 +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-07-20 14:34:56 +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-07-20 14:34:56 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
2020-08-27 06:19:16 +02:00
use FireflyIII\Support\Search\OperatorQuerySearch;
2018-07-20 14:34:56 +02:00
use Illuminate\Http\Request;
use Log;
use Throwable;
/**
* Trait RuleManagement
*
*/
trait RuleManagement
{
2020-08-28 05:51:02 +02:00
/**
* @param array $submittedOperators
* @return array
*/
protected function parseFromOperators(array $submittedOperators): array
{
// TODO duplicated code.
$operators = config('firefly.search.operators');
$renderedEntries = [];
$triggers = [];
foreach ($operators as $key => $operator) {
if ('user_action' !== $key && false === $operator['alias']) {
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
}
}
asort($triggers);
$index = 0;
foreach ($submittedOperators as $operator) {
try {
$renderedEntries[] = view(
'rules.partials.trigger',
[
'oldTrigger' => OperatorQuerySearch::getRootOperator($operator['type']),
'oldValue' => $operator['value'],
2020-10-01 17:02:15 +02:00
'oldChecked' => false,
2020-08-28 05:51:02 +02:00
'count' => $index + 1,
'triggers' => $triggers,
]
)->render();
} catch (Throwable $e) {
Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
$index++;
}
return $renderedEntries;
}
2018-07-20 14:34:56 +02:00
/**
*
*/
protected function createDefaultRule(): void
{
/** @var RuleRepositoryInterface $ruleRepository */
$ruleRepository = app(RuleRepositoryInterface::class);
if (0 === $ruleRepository->count()) {
$data = [
'rule_group_id' => $ruleRepository->getFirstRuleGroup()->id,
2018-08-05 18:59:15 +02:00
'stop_processing' => 0,
2020-08-27 06:19:16 +02:00
'title' => (string) trans('firefly.default_rule_name'),
'description' => (string) trans('firefly.default_rule_description'),
2018-07-20 14:34:56 +02:00
'trigger' => 'store-journal',
'strict' => true,
2018-12-09 20:54:11 +01:00
'active' => true,
'triggers' => [
2018-07-20 14:34:56 +02:00
[
2018-12-21 09:01:21 +01:00
'type' => 'description_is',
2020-08-27 06:19:16 +02:00
'value' => (string) trans('firefly.default_rule_trigger_description'),
2018-08-05 18:59:15 +02:00
'stop_processing' => false,
2018-07-20 14:34:56 +02:00
],
[
2018-12-21 09:01:21 +01:00
'type' => 'from_account_is',
2020-08-27 06:19:16 +02:00
'value' => (string) trans('firefly.default_rule_trigger_from_account'),
2018-08-05 18:59:15 +02:00
'stop_processing' => false,
2018-07-20 14:34:56 +02:00
],
],
2018-12-09 20:54:11 +01:00
'actions' => [
2018-07-20 14:34:56 +02:00
[
2018-12-21 09:01:21 +01:00
'type' => 'prepend_description',
2020-08-27 06:19:16 +02:00
'value' => (string) trans('firefly.default_rule_action_prepend'),
2018-08-05 18:59:15 +02:00
'stop_processing' => false,
2018-07-20 14:34:56 +02:00
],
[
2018-12-21 09:01:21 +01:00
'type' => 'set_category',
2020-08-27 06:19:16 +02:00
'value' => (string) trans('firefly.default_rule_action_set_category'),
2018-08-05 18:59:15 +02:00
'stop_processing' => false,
2018-07-20 14:34:56 +02:00
],
],
];
$ruleRepository->store($data);
}
}
/**
* @param Request $request
*
* @return array
2019-07-26 17:48:24 +02:00
* @codeCoverageIgnore
2018-07-20 14:34:56 +02:00
*/
protected function getPreviousActions(Request $request): array
{
2018-08-05 15:34:20 +02:00
$index = 0;
$triggers = [];
2018-12-09 20:54:11 +01:00
$oldInput = $request->old('actions');
if (is_array($oldInput)) {
2018-08-05 15:34:20 +02:00
foreach ($oldInput as $oldAction) {
try {
$triggers[] = view(
'rules.partials.action',
[
2018-12-21 09:01:21 +01:00
'oldAction' => $oldAction['type'],
2018-08-05 15:34:20 +02:00
'oldValue' => $oldAction['value'],
2020-08-27 06:19:16 +02:00
'oldChecked' => 1 === (int) ($oldAction['stop_processing'] ?? '0'),
2018-08-05 15:34:20 +02:00
'count' => $index + 1,
]
)->render();
} catch (Throwable $e) {
Log::debug(sprintf('Throwable was thrown in getPreviousActions(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
$index++;
2018-07-20 14:34:56 +02:00
}
}
2018-08-05 15:34:20 +02:00
return $triggers;
2018-07-20 14:34:56 +02:00
}
/**
* @param Request $request
*
* @return array
2019-07-26 17:48:24 +02:00
* @codeCoverageIgnore
2018-07-20 14:34:56 +02:00
*/
protected function getPreviousTriggers(Request $request): array
{
2020-08-27 06:19:16 +02:00
// TODO duplicated code.
$operators = config('firefly.search.operators');
$triggers = [];
foreach ($operators as $key => $operator) {
if ('user_action' !== $key && false === $operator['alias']) {
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
}
}
asort($triggers);
$index = 0;
$renderedEntries = [];
$oldInput = $request->old('triggers');
if (is_array($oldInput)) {
2018-08-05 15:34:20 +02:00
foreach ($oldInput as $oldTrigger) {
try {
2020-08-27 06:19:16 +02:00
$renderedEntries[] = view(
2018-08-05 15:34:20 +02:00
'rules.partials.trigger',
[
2020-08-27 06:19:16 +02:00
'oldTrigger' => OperatorQuerySearch::getRootOperator($oldTrigger['type']),
2018-08-05 15:34:20 +02:00
'oldValue' => $oldTrigger['value'],
2020-08-27 06:19:16 +02:00
'oldChecked' => 1 === (int) ($oldTrigger['stop_processing'] ?? '0'),
2018-08-05 15:34:20 +02:00
'count' => $index + 1,
2020-08-27 06:19:16 +02:00
'triggers' => $triggers,
2018-08-05 15:34:20 +02:00
]
)->render();
} catch (Throwable $e) {
Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
$index++;
2018-07-20 14:34:56 +02:00
}
}
2020-08-27 06:19:16 +02:00
return $renderedEntries;
2018-07-20 14:34:56 +02:00
}
/**
*
*/
private function createDefaultRuleGroup(): void
{
/** @var RuleGroupRepositoryInterface $repository */
$repository = app(RuleGroupRepositoryInterface::class);
if (0 === $repository->count()) {
$data = [
2020-08-27 06:19:16 +02:00
'title' => (string) trans('firefly.default_rule_group_name'),
'description' => (string) trans('firefly.default_rule_group_description'),
2018-12-09 20:54:11 +01:00
'active' => true,
2018-07-20 14:34:56 +02:00
];
$repository->store($data);
}
}
}