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

175 lines
5.8 KiB
PHP
Raw Normal View History

2018-07-20 14:34:56 +02:00
<?php
/**
* RuleManagement.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
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;
2020-01-25 06:32:15 +01:00
use FireflyIII\Models\TransactionJournal;
2018-07-20 14:34:56 +02:00
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Illuminate\Http\Request;
use Log;
use Throwable;
/**
* Trait RuleManagement
*
*/
trait RuleManagement
{
/**
*
*/
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,
2018-07-20 14:34:56 +02:00
'title' => (string)trans('firefly.default_rule_name'),
'description' => (string)trans('firefly.default_rule_description'),
'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',
2018-07-20 14:34:56 +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',
2018-07-20 14:34:56 +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',
2018-07-20 14:34:56 +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',
2018-07-20 14:34:56 +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'],
'oldChecked' => 1 === (int)($oldAction['stop_processing'] ?? '0'),
'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
{
2018-08-05 15:34:20 +02:00
$index = 0;
2018-07-20 14:34:56 +02:00
$triggers = [];
2018-12-09 20:54:11 +01:00
$oldInput = $request->old('triggers');
if (is_array($oldInput)) {
2018-08-05 15:34:20 +02:00
foreach ($oldInput as $oldTrigger) {
try {
$triggers[] = view(
'rules.partials.trigger',
[
2018-12-21 09:01:21 +01:00
'oldTrigger' => $oldTrigger['type'],
2018-08-05 15:34:20 +02:00
'oldValue' => $oldTrigger['value'],
'oldChecked' => 1 === (int)($oldTrigger['stop_processing'] ?? '0'),
'count' => $index + 1,
]
)->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
}
}
return $triggers;
}
/**
*
*/
private function createDefaultRuleGroup(): void
{
/** @var RuleGroupRepositoryInterface $repository */
$repository = app(RuleGroupRepositoryInterface::class);
if (0 === $repository->count()) {
$data = [
'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);
}
}
}