From 711a1a1d4fe31ccbc03f6b4c937220cb60418f04 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 19 Feb 2017 09:07:14 +0100 Subject: [PATCH] Final modifiers. --- app/Support/Search/Modifier.php | 74 ++++++++++++++++++++++++++++++++- app/Support/Search/Search.php | 12 ++++-- config/firefly.php | 5 ++- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/app/Support/Search/Modifier.php b/app/Support/Search/Modifier.php index a79744097a..fe6bb2d851 100644 --- a/app/Support/Search/Modifier.php +++ b/app/Support/Search/Modifier.php @@ -12,6 +12,8 @@ declare(strict_types = 1); namespace FireflyIII\Support\Search; +use Carbon\Carbon; +use Exception; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Transaction; use Log; @@ -71,12 +73,82 @@ class Modifier $res = Modifier::budget($transaction, $modifier['value']); Log::debug(sprintf('Budget is %s? %s', $modifier['value'], var_export($res, true))); break; - + case 'bill': + $res = Modifier::stringCompare(strval($transaction->bill_name), $modifier['value']); + Log::debug(sprintf('Bill is %s? %s', $modifier['value'], var_export($res, true))); + break; + case 'type': + $res = Modifier::stringCompare($transaction->transaction_type_type, $modifier['value']); + Log::debug(sprintf('Transaction type is %s? %s', $modifier['value'], var_export($res, true))); + break; + case 'date': + $res = Modifier::sameDate($transaction->date, $modifier['value']); + Log::debug(sprintf('Date is %s? %s', $modifier['value'], var_export($res, true))); + break; + case 'date_before': + $res = Modifier::dateBefore($transaction->date, $modifier['value']); + Log::debug(sprintf('Date is %s? %s', $modifier['value'], var_export($res, true))); + break; + case 'date_after': + $res = Modifier::dateAfter($transaction->date, $modifier['value']); + Log::debug(sprintf('Date is %s? %s', $modifier['value'], var_export($res, true))); + break; } return $res; } + /** + * @param Carbon $date + * @param string $compare + * + * @return bool + */ + public static function dateAfter(Carbon $date, string $compare): bool + { + try { + $compareDate = new Carbon($compare); + } catch (Exception $e) { + return false; + } + + return $date->greaterThanOrEqualTo($compareDate); + } + + /** + * @param Carbon $date + * @param string $compare + * + * @return bool + */ + public static function dateBefore(Carbon $date, string $compare): bool + { + try { + $compareDate = new Carbon($compare); + } catch (Exception $e) { + return false; + } + + return $date->lessThanOrEqualTo($compareDate); + } + + /** + * @param Carbon $date + * @param string $compare + * + * @return bool + */ + public static function sameDate(Carbon $date, string $compare): bool + { + try { + $compareDate = new Carbon($compare); + } catch (Exception $e) { + return false; + } + + return $compareDate->isSameDay($date); + } + /** * @param string $haystack * @param string $needle diff --git a/app/Support/Search/Search.php b/app/Support/Search/Search.php index add2d3fc7d..4e96e3cb5b 100644 --- a/app/Support/Search/Search.php +++ b/app/Support/Search/Search.php @@ -75,7 +75,7 @@ class Search implements SearchInterface public function parseQuery(string $query) { $filteredQuery = $query; - $pattern = '/[a-z_]*:[0-9a-z.]*/i'; + $pattern = '/[a-z_]*:[0-9a-z-.]*/i'; $matches = []; preg_match_all($pattern, $query, $matches); @@ -84,7 +84,9 @@ class Search implements SearchInterface $filteredQuery = str_replace($match, '', $filteredQuery); } $filteredQuery = trim(str_replace(['"', "'"], '', $filteredQuery)); - $this->words = array_map('trim', explode(' ', $filteredQuery)); + if (strlen($filteredQuery) > 0) { + $this->words = array_map('trim', explode(' ', $filteredQuery)); + } } /** @@ -194,9 +196,10 @@ class Search implements SearchInterface $collector = app(JournalCollectorInterface::class); $collector->setUser($this->user); $collector->setAllAssetAccounts()->setLimit($pageSize)->setPage($page); - if($this->hasModifiers()) { + if ($this->hasModifiers()) { $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); } + $collector->disableInternalFilter(); $set = $collector->getPaginatedJournals()->getCollection(); $words = $this->words; @@ -287,7 +290,8 @@ class Search implements SearchInterface Log::debug(sprintf('Now at transaction #%d', $transaction->id)); // first "modifier" is always the text of the search: // check descr of journal: - if (!$this->strpos_arr(strtolower(strval($transaction->description)), $this->words) + if (count($this->words) > 0 + && !$this->strpos_arr(strtolower(strval($transaction->description)), $this->words) && !$this->strpos_arr(strtolower(strval($transaction->transaction_description)), $this->words) ) { Log::debug('Description does not match', $this->words); diff --git a/config/firefly.php b/config/firefly.php index 6844fa7a10..3d4d28d6d7 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -209,6 +209,7 @@ return [ ], 'default_currency' => 'EUR', 'default_language' => 'en_US', - 'search_modifiers' => ['amount_is', 'amount_less', 'amount_more', 'source', 'destination', 'category', 'budget', 'tag', 'bill', 'type', 'date_on', - 'date_before', 'date_after', 'has_attachments', 'notes',], + 'search_modifiers' => ['amount_is', 'amount_less', 'amount_more', 'source', 'destination', 'category', 'budget', 'bill', 'type', 'date', + 'date_before', 'date_after'], + // tag notes has_attachments ];