Skip to content

Firefly III: a personal finances manager

Include trigger that responds to tags

James Cole7688d7c619ba
diff --git a/app/Rules/Triggers/TagIs.php b/app/Rules/Triggers/TagIs.php
new file mode 100644
index 0000000000..684d806760
--- /dev/null
+++ b/app/Rules/Triggers/TagIs.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * TagIs.php
+ * Copyright (c) 2017 [email protected]
+ * This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types = 1);
+
+namespace FireflyIII\Rules\Triggers;
+
+use FireflyIII\Models\Tag;
+use FireflyIII\Models\TransactionJournal;
+use Log;
+
+/**
+ * Class TagIs
+ *
+ * @package FireflyIII\Rules\Triggers
+ */
+final class TagIs extends AbstractTrigger implements TriggerInterface
+{
+
+ /**
+ * A trigger is said to "match anything", or match any given transaction,
+ * when the trigger value is very vague or has no restrictions. Easy examples
+ * are the "AmountMore"-trigger combined with an amount of 0: any given transaction
+ * has an amount of more than zero! Other examples are all the "Description"-triggers
+ * which have hard time handling empty trigger values such as "" or "*" (wild cards).
+ *
+ * If the user tries to create such a trigger, this method MUST return true so Firefly III
+ * can stop the storing / updating the trigger. If the trigger is in any way restrictive
+ * (even if it will still include 99.9% of the users transactions), this method MUST return
+ * false.
+ *
+ * @param null $value
+ *
+ * @return bool
+ */
+ public static function willMatchEverything($value = null)
+ {
+ if (!is_null($value)) {
+ return false;
+ }
+ Log::error(sprintf('Cannot use %s with a null value.', self::class));
+
+ return true;
+ }
+
+ /**
+ * @param TransactionJournal $journal
+ *
+ * @return bool
+ */
+ public function triggered(TransactionJournal $journal): bool
+ {
+ $tags = $journal->tags()->get();
+ /** @var Tag $tag */
+ foreach ($tags as $tag) {
+ $name = strtolower($tag->tag);
+ // match on journal:
+ if ($name === strtolower($this->triggerValue)) {
+ Log::debug(sprintf('RuleTrigger TagIs for journal #%d: is tagged with "%s", return true.', $journal->id, $name));
+
+ return true;
+ }
+ }
+ Log::debug(sprintf('RuleTrigger TagIs for journal #%d: is not tagged with "%s", return false.', $journal->id, $this->triggerValue));
+
+ return false;
+ }
+}
diff --git a/config/firefly.php b/config/firefly.php
index a68e72bb74..d3b9e7a268 100644
--- a/config/firefly.php
+++ b/config/firefly.php
@@ -166,7 +166,6 @@ return [
'to_account_ends' => 'FireflyIII\Rules\Triggers\ToAccountEnds',
'to_account_is' => 'FireflyIII\Rules\Triggers\ToAccountIs',
'to_account_contains' => 'FireflyIII\Rules\Triggers\ToAccountContains',
- 'transaction_type' => 'FireflyIII\Rules\Triggers\TransactionType',
'amount_less' => 'FireflyIII\Rules\Triggers\AmountLess',
'amount_exactly' => 'FireflyIII\Rules\Triggers\AmountExactly',
'amount_more' => 'FireflyIII\Rules\Triggers\AmountMore',
@@ -174,8 +173,10 @@ return [
'description_ends' => 'FireflyIII\Rules\Triggers\DescriptionEnds',
'description_contains' => 'FireflyIII\Rules\Triggers\DescriptionContains',
'description_is' => 'FireflyIII\Rules\Triggers\DescriptionIs',
+ 'transaction_type' => 'FireflyIII\Rules\Triggers\TransactionType',
'category_is' => 'FireflyIII\Rules\Triggers\CategoryIs',
'budget_is' => 'FireflyIII\Rules\Triggers\BudgetIs',
+ 'tag_is' => 'FireflyIII\Rules\Triggers\TagIs',
],
'rule-actions' => [
'set_category' => 'FireflyIII\Rules\Actions\SetCategory',
diff --git a/public/js/ff/rules/create-edit.js b/public/js/ff/rules/create-edit.js
index ebee001611..c47ccdf140 100644
--- a/public/js/ff/rules/create-edit.js
+++ b/public/js/ff/rules/create-edit.js
@@ -268,6 +268,16 @@ function updateTriggerInput(selectList) {
case 'to_account_contains':
createAutoComplete(input, 'json/all-accounts');
break;
+ case 'tag_is':
+ // also make tag thing?
+ createAutoComplete(input, 'json/tags');
+ break;
+ case 'budget_is':
+ createAutoComplete(input, 'json/budgets');
+ break;
+ case 'category_is':
+ createAutoComplete(input, 'json/categories');
+ break;
case 'transaction_type':
createAutoComplete(input, 'json/transaction-types');
break;
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index abee277d95..d54d34d4a6 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -262,6 +262,7 @@ return [
'rule_trigger_description_is_choice' => 'Description is..',
'rule_trigger_category_is_choice' => 'Category is..',
'rule_trigger_budget_is_choice' => 'Budget is..',
+ 'rule_trigger_tag_is_choice' => '(A) tag is..',
'rule_trigger_store_journal' => 'When a transaction is created',
'rule_trigger_update_journal' => 'When a transaction is updated',
'rule_action_set_category' => 'Set category to ":action_value"',