mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-26 19:00:22 -06:00
Include trigger that responds to tags
This commit is contained in:
parent
e8303bd059
commit
7688d7c619
74
app/Rules/Triggers/TagIs.php
Normal file
74
app/Rules/Triggers/TagIs.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/**
|
||||
* TagIs.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -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',
|
||||
|
@ -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;
|
||||
|
@ -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"',
|
||||
|
Loading…
Reference in New Issue
Block a user