This commit is contained in:
James Cole 2020-08-27 06:19:16 +02:00
parent 483e7256f7
commit a16ac479d5
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
3 changed files with 61 additions and 34 deletions

View File

@ -34,6 +34,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\Search\OperatorQuerySearch;
use Log; use Log;
use Throwable; use Throwable;
@ -64,9 +65,9 @@ trait RenderPartialViews
foreach ($revenue as $otherAccount) { foreach ($revenue as $otherAccount) {
if ( if (
( (
($otherAccount->name === $account->name) ($otherAccount->name === $account->name)
|| ||
(null !== $account->iban && null !== $otherAccount->iban && $otherAccount->iban === $account->iban) (null !== $account->iban && null !== $otherAccount->iban && $otherAccount->iban === $account->iban)
) )
&& $otherAccount->id !== $account->id && $otherAccount->id !== $account->id
) { ) {
@ -102,10 +103,10 @@ trait RenderPartialViews
/** @var BudgetRepositoryInterface $budgetRepository */ /** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class); $budgetRepository = app(BudgetRepositoryInterface::class);
$budget = $budgetRepository->findNull((int)$attributes['budgetId']); $budget = $budgetRepository->findNull((int) $attributes['budgetId']);
$accountRepos = app(AccountRepositoryInterface::class); $accountRepos = app(AccountRepositoryInterface::class);
$account = $accountRepos->findNull((int)$attributes['accountId']); $account = $accountRepos->findNull((int) $attributes['accountId']);
$journals = $popupHelper->balanceForBudget($budget, $account, $attributes); $journals = $popupHelper->balanceForBudget($budget, $account, $attributes);
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
@ -159,7 +160,7 @@ trait RenderPartialViews
/** @var PopupReportInterface $popupHelper */ /** @var PopupReportInterface $popupHelper */
$popupHelper = app(PopupReportInterface::class); $popupHelper = app(PopupReportInterface::class);
$budget = $budgetRepository->findNull((int)$attributes['budgetId']); $budget = $budgetRepository->findNull((int) $attributes['budgetId']);
if (null === $budget) { if (null === $budget) {
$budget = new Budget; $budget = new Budget;
} }
@ -191,7 +192,7 @@ trait RenderPartialViews
/** @var CategoryRepositoryInterface $categoryRepository */ /** @var CategoryRepositoryInterface $categoryRepository */
$categoryRepository = app(CategoryRepositoryInterface::class); $categoryRepository = app(CategoryRepositoryInterface::class);
$category = $categoryRepository->findNull((int)$attributes['categoryId']); $category = $categoryRepository->findNull((int) $attributes['categoryId']);
$journals = $popupHelper->byCategory($category, $attributes); $journals = $popupHelper->byCategory($category, $attributes);
if (null === $category) { if (null === $category) {
return 'This is an unknown category. Apologies.'; return 'This is an unknown category. Apologies.';
@ -247,7 +248,7 @@ trait RenderPartialViews
/** @var PopupReportInterface $popupHelper */ /** @var PopupReportInterface $popupHelper */
$popupHelper = app(PopupReportInterface::class); $popupHelper = app(PopupReportInterface::class);
$account = $accountRepository->findNull((int)$attributes['accountId']); $account = $accountRepository->findNull((int) $attributes['accountId']);
if (null === $account) { if (null === $account) {
return 'This is an unknown account. Apologies.'; return 'This is an unknown account. Apologies.';
@ -315,8 +316,20 @@ trait RenderPartialViews
*/ */
protected function getCurrentTriggers(Rule $rule): array // get info from object and present. protected function getCurrentTriggers(Rule $rule): array // get info from object and present.
{ {
$index = 0; // TODO duplicated code.
$triggers = []; $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 = [];
// todo must be repos // todo must be repos
$currentTriggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get(); $currentTriggers = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
/** @var RuleTrigger $entry */ /** @var RuleTrigger $entry */
@ -324,13 +337,14 @@ trait RenderPartialViews
if ('user_action' !== $entry->trigger_type) { if ('user_action' !== $entry->trigger_type) {
$count = ($index + 1); $count = ($index + 1);
try { try {
$triggers[] = view( $renderedEntries[] = view(
'rules.partials.trigger', 'rules.partials.trigger',
[ [
'oldTrigger' => $entry->trigger_type, 'oldTrigger' => OperatorQuerySearch::getRootOperator($entry->trigger_type),
'oldValue' => $entry->trigger_value, 'oldValue' => $entry->trigger_value,
'oldChecked' => $entry->stop_processing, 'oldChecked' => $entry->stop_processing,
'count' => $count, 'count' => $count,
'triggers' => $triggers,
] ]
)->render(); )->render();
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
@ -343,7 +357,7 @@ trait RenderPartialViews
} }
} }
return $triggers; return $renderedEntries;
} }
/** /**
@ -360,7 +374,7 @@ trait RenderPartialViews
/** @var PopupReportInterface $popupHelper */ /** @var PopupReportInterface $popupHelper */
$popupHelper = app(PopupReportInterface::class); $popupHelper = app(PopupReportInterface::class);
$account = $accountRepository->findNull((int)$attributes['accountId']); $account = $accountRepository->findNull((int) $attributes['accountId']);
if (null === $account) { if (null === $account) {
return 'This is an unknown category. Apologies.'; return 'This is an unknown category. Apologies.';

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers; namespace FireflyIII\Support\Http\Controllers;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\Support\Search\OperatorQuerySearch;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Log; use Log;
use Throwable; use Throwable;
@ -48,21 +48,21 @@ trait RuleManagement
$data = [ $data = [
'rule_group_id' => $ruleRepository->getFirstRuleGroup()->id, 'rule_group_id' => $ruleRepository->getFirstRuleGroup()->id,
'stop_processing' => 0, 'stop_processing' => 0,
'title' => (string)trans('firefly.default_rule_name'), 'title' => (string) trans('firefly.default_rule_name'),
'description' => (string)trans('firefly.default_rule_description'), 'description' => (string) trans('firefly.default_rule_description'),
'trigger' => 'store-journal', 'trigger' => 'store-journal',
'strict' => true, 'strict' => true,
'active' => true, 'active' => true,
'triggers' => [ 'triggers' => [
[ [
'type' => 'description_is', 'type' => 'description_is',
'value' => (string)trans('firefly.default_rule_trigger_description'), 'value' => (string) trans('firefly.default_rule_trigger_description'),
'stop_processing' => false, 'stop_processing' => false,
], ],
[ [
'type' => 'from_account_is', 'type' => 'from_account_is',
'value' => (string)trans('firefly.default_rule_trigger_from_account'), 'value' => (string) trans('firefly.default_rule_trigger_from_account'),
'stop_processing' => false, 'stop_processing' => false,
], ],
@ -71,12 +71,12 @@ trait RuleManagement
'actions' => [ 'actions' => [
[ [
'type' => 'prepend_description', 'type' => 'prepend_description',
'value' => (string)trans('firefly.default_rule_action_prepend'), 'value' => (string) trans('firefly.default_rule_action_prepend'),
'stop_processing' => false, 'stop_processing' => false,
], ],
[ [
'type' => 'set_category', 'type' => 'set_category',
'value' => (string)trans('firefly.default_rule_action_set_category'), 'value' => (string) trans('firefly.default_rule_action_set_category'),
'stop_processing' => false, 'stop_processing' => false,
], ],
], ],
@ -105,7 +105,7 @@ trait RuleManagement
[ [
'oldAction' => $oldAction['type'], 'oldAction' => $oldAction['type'],
'oldValue' => $oldAction['value'], 'oldValue' => $oldAction['value'],
'oldChecked' => 1 === (int)($oldAction['stop_processing'] ?? '0'), 'oldChecked' => 1 === (int) ($oldAction['stop_processing'] ?? '0'),
'count' => $index + 1, 'count' => $index + 1,
] ]
)->render(); )->render();
@ -128,19 +128,31 @@ trait RuleManagement
*/ */
protected function getPreviousTriggers(Request $request): array protected function getPreviousTriggers(Request $request): array
{ {
$index = 0; // TODO duplicated code.
$triggers = []; $operators = config('firefly.search.operators');
$oldInput = $request->old('triggers'); $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)) { if (is_array($oldInput)) {
foreach ($oldInput as $oldTrigger) { foreach ($oldInput as $oldTrigger) {
try { try {
$triggers[] = view( $renderedEntries[] = view(
'rules.partials.trigger', 'rules.partials.trigger',
[ [
'oldTrigger' => $oldTrigger['type'], 'oldTrigger' => OperatorQuerySearch::getRootOperator($oldTrigger['type']),
'oldValue' => $oldTrigger['value'], 'oldValue' => $oldTrigger['value'],
'oldChecked' => 1 === (int)($oldTrigger['stop_processing'] ?? '0'), 'oldChecked' => 1 === (int) ($oldTrigger['stop_processing'] ?? '0'),
'count' => $index + 1, 'count' => $index + 1,
'triggers' => $triggers,
] ]
)->render(); )->render();
} catch (Throwable $e) { } catch (Throwable $e) {
@ -151,7 +163,7 @@ trait RuleManagement
} }
} }
return $triggers; return $renderedEntries;
} }
/** /**
@ -163,8 +175,8 @@ trait RuleManagement
$repository = app(RuleGroupRepositoryInterface::class); $repository = app(RuleGroupRepositoryInterface::class);
if (0 === $repository->count()) { if (0 === $repository->count()) {
$data = [ $data = [
'title' => (string)trans('firefly.default_rule_group_name'), 'title' => (string) trans('firefly.default_rule_group_name'),
'description' => (string)trans('firefly.default_rule_group_description'), 'description' => (string) trans('firefly.default_rule_group_description'),
'active' => true, 'active' => true,
]; ];

View File

@ -234,7 +234,7 @@ class OperatorQuerySearch implements SearchInterface
if ($this->updateCollector($operator, $value)) { if ($this->updateCollector($operator, $value)) {
$this->operators->push( $this->operators->push(
[ [
'type' => $this->getRootOperator($operator), 'type' => self::getRootOperator($operator),
'value' => $value, 'value' => $value,
] ]
); );
@ -256,7 +256,7 @@ class OperatorQuerySearch implements SearchInterface
Log::debug(sprintf('updateCollector(%s, %s)', $operator, $value)); Log::debug(sprintf('updateCollector(%s, %s)', $operator, $value));
// check if alias, replace if necessary: // check if alias, replace if necessary:
$operator = $this->getRootOperator($operator); $operator = self::getRootOperator($operator);
switch ($operator) { switch ($operator) {
default: default:
@ -687,8 +687,9 @@ class OperatorQuerySearch implements SearchInterface
/** /**
* @param string $operator * @param string $operator
* @return string * @return string
* @throws FireflyException
*/ */
private function getRootOperator(string $operator): string public static function getRootOperator(string $operator): string
{ {
$config = config(sprintf('firefly.search.operators.%s', $operator)); $config = config(sprintf('firefly.search.operators.%s', $operator));
if (null === $config) { if (null === $config) {