From afec8480fbdec69aaf135b3d10d129d7f1a5a881 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 Jan 2016 14:12:46 +0100 Subject: [PATCH] More working triggers. --- app/Rules/Triggers/ToAccountContains.php | 64 ++++++++++++++++++++ app/Rules/Triggers/ToAccountEnds.php | 74 ++++++++++++++++++++++++ app/Rules/Triggers/ToAccountIs.php | 60 +++++++++++++++++++ app/Rules/Triggers/ToAccountStarts.php | 62 ++++++++++++++++++++ app/Rules/Triggers/TransactionType.php | 59 +++++++++++++++++++ app/Rules/Triggers/UserAction.php | 1 + config/firefly.php | 10 ++-- 7 files changed, 325 insertions(+), 5 deletions(-) create mode 100644 app/Rules/Triggers/ToAccountContains.php create mode 100644 app/Rules/Triggers/ToAccountEnds.php create mode 100644 app/Rules/Triggers/ToAccountIs.php create mode 100644 app/Rules/Triggers/ToAccountStarts.php create mode 100644 app/Rules/Triggers/TransactionType.php diff --git a/app/Rules/Triggers/ToAccountContains.php b/app/Rules/Triggers/ToAccountContains.php new file mode 100644 index 0000000000..5e06383f04 --- /dev/null +++ b/app/Rules/Triggers/ToAccountContains.php @@ -0,0 +1,64 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $toAccountName = strtolower($this->journal->destination_account->name); + $search = strtolower($this->trigger->trigger_value); + $strpos = strpos($toAccountName, $search); + + if (!($strpos === false)) { + // found something + Log::debug('"' . $toAccountName . '" contains the text "' . $search . '". Return true.'); + + return true; + } + + // found nothing. + Log::debug('"' . $toAccountName . '" does not contain the text "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/ToAccountEnds.php b/app/Rules/Triggers/ToAccountEnds.php new file mode 100644 index 0000000000..67bb9750f1 --- /dev/null +++ b/app/Rules/Triggers/ToAccountEnds.php @@ -0,0 +1,74 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $toAccountName = strtolower($this->journal->destination_account->name); + $toAccountNameLength = strlen($toAccountName); + $search = strtolower($this->trigger->trigger_value); + $searchLength = strlen($search); + + // if the string to search for is longer than the account name, + // shorten the search string. + if ($searchLength > $toAccountNameLength) { + Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $toAccountName . '" (' . $toAccountNameLength . '). '); + $search = substr($search, ($toAccountNameLength * -1)); + $searchLength = strlen($search); + Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.'); + } + + + $part = substr($toAccountName, $searchLength * -1); + + if ($part == $search) { + Log::debug('"' . $toAccountName . '" ends with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $toAccountName . '" does not end with "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/ToAccountIs.php b/app/Rules/Triggers/ToAccountIs.php new file mode 100644 index 0000000000..09f4c66f6a --- /dev/null +++ b/app/Rules/Triggers/ToAccountIs.php @@ -0,0 +1,60 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $toAccountName = strtolower($this->journal->destination_account->name); + $search = strtolower($this->trigger->trigger_value); + + if ($toAccountName == $search) { + Log::debug('"' . $toAccountName . '" equals "' . $search . '" exactly. Return true.'); + + return true; + } + Log::debug('"' . $toAccountName . '" does not equal "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/ToAccountStarts.php b/app/Rules/Triggers/ToAccountStarts.php new file mode 100644 index 0000000000..0af5fc4491 --- /dev/null +++ b/app/Rules/Triggers/ToAccountStarts.php @@ -0,0 +1,62 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $toAccountName = strtolower($this->journal->destination_account->name); + $search = strtolower($this->trigger->trigger_value); + + $part = substr($toAccountName, 0, strlen($search)); + + if ($part == $search) { + Log::debug('"' . $toAccountName . '" starts with "' . $search . '". Return true.'); + + return true; + } + Log::debug('"' . $toAccountName . '" does not start with "' . $search . '". Return false.'); + + return false; + + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/TransactionType.php b/app/Rules/Triggers/TransactionType.php new file mode 100644 index 0000000000..9bba776f42 --- /dev/null +++ b/app/Rules/Triggers/TransactionType.php @@ -0,0 +1,59 @@ +trigger = $trigger; + $this->journal = $journal; + } + + /** + * @return bool + */ + public function triggered() + { + $type = strtolower($this->journal->transactionType->type); + $search = strtolower($this->trigger->trigger_value); + + if ($type == $search) { + Log::debug('Journal is of type "' . $type . '" which matches with "' . $search . '". Return true'); + + return true; + } + Log::debug('Journal is of type "' . $type . '" which does not match with "' . $search . '". Return false'); + + return false; + } +} \ No newline at end of file diff --git a/app/Rules/Triggers/UserAction.php b/app/Rules/Triggers/UserAction.php index 48c9932a33..bdde712e36 100644 --- a/app/Rules/Triggers/UserAction.php +++ b/app/Rules/Triggers/UserAction.php @@ -48,6 +48,7 @@ class UserAction implements TriggerInterface public function triggered() { Log::debug('user_action always returns true.'); + return true; } diff --git a/config/firefly.php b/config/firefly.php index ef85e627f6..eb78cf5102 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -174,11 +174,11 @@ return [ 'from_account_ends' => 'FireflyIII\Rules\Triggers\FromAccountEnds', 'from_account_is' => 'FireflyIII\Rules\Triggers\FromAccountIs', 'from_account_contains' => 'FireflyIII\Rules\Triggers\FromAccountContains', - 'to_account_starts' => 'FireflyIII\Rules\Triggers', - 'to_account_ends' => 'FireflyIII\Rules\Triggers', - 'to_account_is' => 'FireflyIII\Rules\Triggers', - 'to_account_contains' => 'FireflyIII\Rules\Triggers', - 'transaction_type' => 'FireflyIII\Rules\Triggers', + 'to_account_starts' => 'FireflyIII\Rules\Triggers\ToAccountStarts', + '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', 'amount_exactly' => 'FireflyIII\Rules\Triggers', 'amount_exactly_not' => 'FireflyIII\Rules\Triggers',