mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 20:16:22 +00:00
Expand search with a bunch of keywords for #510
This commit is contained in:
108
app/Support/Search/Modifier.php
Normal file
108
app/Support/Search/Modifier.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Modifier.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\Support\Search;
|
||||
|
||||
|
||||
use FireflyIII\Models\Transaction;
|
||||
use Log;
|
||||
use Steam;
|
||||
|
||||
class Modifier
|
||||
{
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
* @param int $expected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function amountCompare(Transaction $transaction, string $amount, int $expected): bool
|
||||
{
|
||||
$amount = Steam::positive($amount);
|
||||
$transactionAmount = Steam::positive($transaction->transaction_amount);
|
||||
|
||||
$compare = bccomp($amount, $transactionAmount);
|
||||
Log::debug(sprintf('%s vs %s is %d', $amount, $transactionAmount, $compare));
|
||||
|
||||
return $compare === $expected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function amountIs(Transaction $transaction, string $amount): bool
|
||||
{
|
||||
return self::amountCompare($transaction, $amount, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function amountLess(Transaction $transaction, string $amount): bool
|
||||
{
|
||||
return self::amountCompare($transaction, $amount, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function amountMore(Transaction $transaction, string $amount): bool
|
||||
{
|
||||
return self::amountCompare($transaction, $amount, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $destination
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function destination(Transaction $transaction, string $destination): bool
|
||||
{
|
||||
return self::stringCompare($transaction->opposing_account_name, $destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
* @param string $source
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function source(Transaction $transaction, string $source): bool
|
||||
{
|
||||
return self::stringCompare($transaction->account_name, $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $haystack
|
||||
* @param string $needle
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function stringCompare(string $haystack, string $needle): bool
|
||||
{
|
||||
$res = !(strpos(strtolower($haystack), strtolower($needle)) === false);
|
||||
Log::debug(sprintf('"%s" is in "%s"? %s', $needle, $haystack, var_export($res, true)));
|
||||
|
||||
return $res;
|
||||
|
||||
}
|
||||
}
|
@@ -14,6 +14,7 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Support\Search;
|
||||
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
@@ -34,19 +35,64 @@ class Search implements SearchInterface
|
||||
{
|
||||
/** @var int */
|
||||
private $limit = 100;
|
||||
/** @var Collection */
|
||||
private $modifiers;
|
||||
/** @var User */
|
||||
private $user;
|
||||
/** @var array */
|
||||
private $validModifiers = [];
|
||||
/** @var array */
|
||||
private $words = [];
|
||||
|
||||
/**
|
||||
* Search constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->modifiers = new Collection;
|
||||
$this->validModifiers = config('firefly.search_modifiers');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getWordsAsString(): string
|
||||
{
|
||||
return join(' ', $this->words);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasModifiers(): bool
|
||||
{
|
||||
return $this->modifiers->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $query
|
||||
*/
|
||||
public function parseQuery(string $query)
|
||||
{
|
||||
$filteredQuery = $query;
|
||||
$pattern = '/[a-z_]*:[0-9a-z.]*/i';
|
||||
$matches = [];
|
||||
preg_match_all($pattern, $query, $matches);
|
||||
|
||||
foreach ($matches[0] as $match) {
|
||||
$this->extractModifier($match);
|
||||
$filteredQuery = str_replace($match, '', $filteredQuery);
|
||||
}
|
||||
$filteredQuery = trim(str_replace(['"', "'"], '', $filteredQuery));
|
||||
$this->words = array_map('trim', explode(' ', $filteredQuery));
|
||||
}
|
||||
|
||||
/**
|
||||
* The search will assume that the user does not have so many accounts
|
||||
* that this search should be paginated.
|
||||
*
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchAccounts(array $words): Collection
|
||||
public function searchAccounts(): Collection
|
||||
{
|
||||
$words = $this->words;
|
||||
$accounts = $this->user->accounts()
|
||||
->accountTypeIn([AccountType::DEFAULT, AccountType::ASSET, AccountType::EXPENSE, AccountType::REVENUE, AccountType::BENEFICIARY])
|
||||
->get(['accounts.*']);
|
||||
@@ -67,14 +113,13 @@ class Search implements SearchInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchBudgets(array $words): Collection
|
||||
public function searchBudgets(): Collection
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = auth()->user()->budgets()->get();
|
||||
$set = auth()->user()->budgets()->get();
|
||||
$words = $this->words;
|
||||
/** @var Collection $result */
|
||||
$result = $set->filter(
|
||||
function (Budget $budget) use ($words) {
|
||||
@@ -92,14 +137,11 @@ class Search implements SearchInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Search assumes the user does not have that many categories. So no paginated search.
|
||||
*
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchCategories(array $words): Collection
|
||||
public function searchCategories(): Collection
|
||||
{
|
||||
$words = $this->words;
|
||||
$categories = $this->user->categories()->get();
|
||||
/** @var Collection $result */
|
||||
$result = $categories->filter(
|
||||
@@ -117,15 +159,12 @@ class Search implements SearchInterface
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTags(array $words): Collection
|
||||
public function searchTags(): Collection
|
||||
{
|
||||
$tags = $this->user->tags()->get();
|
||||
|
||||
$words = $this->words;
|
||||
$tags = $this->user->tags()->get();
|
||||
/** @var Collection $result */
|
||||
$result = $tags->filter(
|
||||
function (Tag $tag) use ($words) {
|
||||
@@ -142,11 +181,9 @@ class Search implements SearchInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTransactions(array $words): Collection
|
||||
public function searchTransactions(): Collection
|
||||
{
|
||||
$pageSize = 100;
|
||||
$processed = 0;
|
||||
@@ -156,20 +193,17 @@ class Search implements SearchInterface
|
||||
/** @var JournalCollectorInterface $collector */
|
||||
$collector = app(JournalCollectorInterface::class);
|
||||
$collector->setUser($this->user);
|
||||
$collector->setAllAssetAccounts()->setLimit($pageSize)->setPage($page);
|
||||
$set = $collector->getPaginatedJournals();
|
||||
$collector->setAllAssetAccounts()->setLimit($pageSize)->setPage($page)->withOpposingAccount();
|
||||
$set = $collector->getPaginatedJournals();
|
||||
$words = $this->words;
|
||||
|
||||
Log::debug(sprintf('Found %d journals to check. ', $set->count()));
|
||||
|
||||
// Filter transactions that match the given triggers.
|
||||
$filtered = $set->filter(
|
||||
function (Transaction $transaction) use ($words) {
|
||||
// check descr of journal:
|
||||
if ($this->strpos_arr(strtolower(strval($transaction->description)), $words)) {
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
// check descr of transaction
|
||||
if ($this->strpos_arr(strtolower(strval($transaction->transaction_description)), $words)) {
|
||||
if ($this->matchModifiers($transaction)) {
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
@@ -201,6 +235,8 @@ class Search implements SearchInterface
|
||||
} while (!$reachedEndOfList && !$foundEnough);
|
||||
|
||||
$result = $result->slice(0, $this->limit);
|
||||
var_dump($result->toArray());
|
||||
exit;
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -221,6 +257,79 @@ class Search implements SearchInterface
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
*/
|
||||
private function extractModifier(string $string)
|
||||
{
|
||||
$parts = explode(':', $string);
|
||||
if (count($parts) === 2 && strlen(trim(strval($parts[0]))) > 0 && strlen(trim(strval($parts[1])))) {
|
||||
$type = trim(strval($parts[0]));
|
||||
$value = trim(strval($parts[1]));
|
||||
if (in_array($type, $this->validModifiers)) {
|
||||
// filter for valid type
|
||||
$this->modifiers->push(['type' => $type, 'value' => $value,]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function matchModifiers(Transaction $transaction): bool
|
||||
{
|
||||
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)
|
||||
&& !$this->strpos_arr(strtolower(strval($transaction->transaction_description)), $this->words)
|
||||
) {
|
||||
Log::debug('Description does not match', $this->words);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// then a for-each and a switch for every possible other thingie.
|
||||
foreach ($this->modifiers as $modifier) {
|
||||
switch ($modifier['type']) {
|
||||
default:
|
||||
throw new FireflyException(sprintf('Search modifier "%s" is not (yet) supported. Sorry!', $modifier['type']));
|
||||
break;
|
||||
case 'amount_is':
|
||||
$res = Modifier::amountIs($transaction, $modifier['value']);
|
||||
Log::debug(sprintf('Amount is %s? %s', $modifier['value'], var_export($res, true)));
|
||||
break;
|
||||
case 'amount_less':
|
||||
$res = Modifier::amountLess($transaction, $modifier['value']);
|
||||
Log::debug(sprintf('Amount less than %s? %s', $modifier['value'], var_export($res, true)));
|
||||
break;
|
||||
case 'amount_more':
|
||||
$res = Modifier::amountMore($transaction, $modifier['value']);
|
||||
Log::debug(sprintf('Amount more than %s? %s', $modifier['value'], var_export($res, true)));
|
||||
break;
|
||||
case 'source':
|
||||
$res = Modifier::source($transaction, $modifier['value']);
|
||||
Log::debug(sprintf('Source is %s? %s', $modifier['value'], var_export($res, true)));
|
||||
break;
|
||||
case 'destination':
|
||||
$res = Modifier::destination($transaction, $modifier['value']);
|
||||
Log::debug(sprintf('Destination is %s? %s', $modifier['value'], var_export($res, true)));
|
||||
break;
|
||||
|
||||
}
|
||||
if ($res === false) {
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $haystack
|
||||
* @param array $needle
|
||||
|
@@ -24,40 +24,49 @@ use Illuminate\Support\Collection;
|
||||
interface SearchInterface
|
||||
{
|
||||
/**
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
* @return string
|
||||
*/
|
||||
public function searchAccounts(array $words): Collection;
|
||||
public function getWordsAsString(): string;
|
||||
|
||||
/**
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
* @return bool
|
||||
*/
|
||||
public function searchBudgets(array $words): Collection;
|
||||
public function hasModifiers(): bool;
|
||||
|
||||
/**
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
* @param string $query
|
||||
*/
|
||||
public function searchCategories(array $words): Collection;
|
||||
public function parseQuery(string $query);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTags(array $words): Collection;
|
||||
public function searchAccounts(): Collection;
|
||||
|
||||
/**
|
||||
* @param array $words
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTransactions(array $words): Collection;
|
||||
public function searchBudgets(): Collection;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchCategories(): Collection;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTags(): Collection;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchTransactions(): Collection;
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
*/
|
||||
public function setLimit(int $limit);
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
|
Reference in New Issue
Block a user