2018-07-20 14:34:56 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2018-07-20 14:34:56 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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;
|
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-07-20 14:34:56 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait RuleManagement
|
|
|
|
*/
|
|
|
|
trait RuleManagement
|
|
|
|
{
|
|
|
|
/**
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws FireflyException
|
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');
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($oldInput)) {
|
2018-08-05 15:34:20 +02:00
|
|
|
foreach ($oldInput as $oldAction) {
|
|
|
|
try {
|
2022-01-29 14:11:12 +01:00
|
|
|
$triggers[] = view(
|
2018-08-05 15:34:20 +02:00
|
|
|
'rules.partials.action',
|
|
|
|
[
|
2018-12-21 09:01:21 +01:00
|
|
|
'oldAction' => $oldAction['type'],
|
2023-09-06 05:45:35 +02:00
|
|
|
'oldValue' => $oldAction['value'] ?? '',
|
2024-12-22 08:43:12 +01:00
|
|
|
'oldChecked' => 1 === (int) ($oldAction['stop_processing'] ?? '0'),
|
2018-08-05 15:34:20 +02:00
|
|
|
'count' => $index + 1,
|
|
|
|
]
|
|
|
|
)->render();
|
2024-12-22 20:37:54 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('Throwable was thrown in getPreviousActions(): %s', $e->getMessage()));
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-08-30 06:40:12 +02:00
|
|
|
throw new FireflyException(sprintf('Could not render: %s', $e->getMessage()), 0, $e);
|
2018-08-05 15:34:20 +02:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
++$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws FireflyException
|
2018-07-20 14:34:56 +02:00
|
|
|
*/
|
|
|
|
protected function getPreviousTriggers(Request $request): array
|
|
|
|
{
|
2022-10-30 11:43:17 +01:00
|
|
|
// TODO duplicated code.
|
2024-12-22 20:37:54 +01:00
|
|
|
$operators = config('search.operators');
|
|
|
|
$triggers = [];
|
2020-08-27 06:19:16 +02:00
|
|
|
foreach ($operators as $key => $operator) {
|
|
|
|
if ('user_action' !== $key && false === $operator['alias']) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
|
2020-08-27 06:19:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
asort($triggers);
|
|
|
|
|
|
|
|
$index = 0;
|
|
|
|
$renderedEntries = [];
|
|
|
|
$oldInput = $request->old('triggers');
|
2019-06-22 13:09:25 +02:00
|
|
|
if (is_array($oldInput)) {
|
2018-08-05 15:34:20 +02:00
|
|
|
foreach ($oldInput as $oldTrigger) {
|
|
|
|
try {
|
2022-01-29 14:11:12 +01:00
|
|
|
$renderedEntries[] = view(
|
2018-08-05 15:34:20 +02:00
|
|
|
'rules.partials.trigger',
|
|
|
|
[
|
2022-10-01 07:34:30 +02:00
|
|
|
'oldTrigger' => OperatorQuerySearch::getRootOperator($oldTrigger['type']),
|
2023-12-19 09:39:36 +01:00
|
|
|
'oldValue' => $oldTrigger['value'] ?? '',
|
2024-12-22 08:43:12 +01:00
|
|
|
'oldChecked' => 1 === (int) ($oldTrigger['stop_processing'] ?? '0'),
|
|
|
|
'oldProhibited' => 1 === (int) ($oldTrigger['prohibited'] ?? '0'),
|
2022-10-01 07:34:30 +02:00
|
|
|
'count' => $index + 1,
|
|
|
|
'triggers' => $triggers,
|
2018-08-05 15:34:20 +02:00
|
|
|
]
|
|
|
|
)->render();
|
2024-12-22 20:37:54 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-08-30 06:40:12 +02:00
|
|
|
throw new FireflyException(sprintf('Could not render: %s', $e->getMessage()), 0, $e);
|
2018-08-05 15:34:20 +02:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
++$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
|
|
|
}
|
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
/**
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws FireflyException
|
2021-03-21 09:15:40 +01:00
|
|
|
*/
|
|
|
|
protected function parseFromOperators(array $submittedOperators): array
|
|
|
|
{
|
2022-10-30 11:43:17 +01:00
|
|
|
// TODO duplicated code.
|
2022-03-20 07:42:39 +01:00
|
|
|
$operators = config('search.operators');
|
2021-03-21 09:15:40 +01:00
|
|
|
$renderedEntries = [];
|
|
|
|
$triggers = [];
|
|
|
|
foreach ($operators as $key => $operator) {
|
|
|
|
if ('user_action' !== $key && false === $operator['alias']) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
|
2021-03-21 09:15:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
asort($triggers);
|
|
|
|
|
2024-12-22 20:37:54 +01:00
|
|
|
$index = 0;
|
2021-03-21 09:15:40 +01:00
|
|
|
foreach ($submittedOperators as $operator) {
|
2023-11-25 19:03:51 +01:00
|
|
|
$rootOperator = OperatorQuerySearch::getRootOperator($operator['type']);
|
2024-12-22 08:43:12 +01:00
|
|
|
$needsContext = (bool) config(sprintf('search.operators.%s.needs_context', $rootOperator));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
try {
|
2022-01-29 14:11:12 +01:00
|
|
|
$renderedEntries[] = view(
|
2021-03-21 09:15:40 +01:00
|
|
|
'rules.partials.trigger',
|
|
|
|
[
|
2023-11-25 19:03:51 +01:00
|
|
|
'oldTrigger' => $rootOperator,
|
|
|
|
'oldValue' => $needsContext ? $operator['value'] : '',
|
2023-10-15 12:45:27 +02:00
|
|
|
'oldChecked' => false,
|
|
|
|
'oldProhibited' => $operator['prohibited'] ?? false,
|
|
|
|
'count' => $index + 1,
|
|
|
|
'triggers' => $triggers,
|
2021-03-21 09:15:40 +01:00
|
|
|
]
|
|
|
|
)->render();
|
2024-12-22 20:37:54 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error($e->getTraceAsString());
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-08-30 06:40:12 +02:00
|
|
|
throw new FireflyException(sprintf('Could not render: %s', $e->getMessage()), 0, $e);
|
2021-03-21 09:15:40 +01:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
++$index;
|
2021-03-21 09:15:40 +01: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 = [
|
2024-12-22 08:43:12 +01: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);
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 20:32:02 +02:00
|
|
|
}
|