mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-27 17:31:09 -06:00
142 lines
3.6 KiB
PHP
142 lines
3.6 KiB
PHP
<?php
|
|
declare(strict_types = 1);
|
|
|
|
namespace FireflyIII\Support\Search;
|
|
|
|
|
|
use Auth;
|
|
use FireflyIII\Models\Budget;
|
|
use FireflyIII\Models\Category;
|
|
use FireflyIII\Models\TransactionJournal;
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Class Search
|
|
*
|
|
* @package FireflyIII\Search
|
|
*/
|
|
class Search implements SearchInterface
|
|
{
|
|
/**
|
|
* @param array $words
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function searchAccounts(array $words): Collection
|
|
{
|
|
return Auth::user()->accounts()->with('accounttype')->where(
|
|
function (EloquentBuilder $q) use ($words) {
|
|
foreach ($words as $word) {
|
|
$q->orWhere('name', 'LIKE', '%' . e($word) . '%');
|
|
}
|
|
}
|
|
)->get();
|
|
}
|
|
|
|
/**
|
|
* @param array $words
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function searchBudgets(array $words): Collection
|
|
{
|
|
/** @var Collection $set */
|
|
$set = Auth::user()->budgets()->get();
|
|
$newSet = $set->filter(
|
|
function (Budget $b) use ($words) {
|
|
$found = 0;
|
|
foreach ($words as $word) {
|
|
if (!(strpos(strtolower($b->name), strtolower($word)) === false)) {
|
|
$found++;
|
|
}
|
|
}
|
|
|
|
return $found > 0;
|
|
}
|
|
);
|
|
|
|
return $newSet;
|
|
}
|
|
|
|
/**
|
|
* @param array $words
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function searchCategories(array $words): Collection
|
|
{
|
|
/** @var Collection $set */
|
|
$set = Auth::user()->categories()->get();
|
|
$newSet = $set->filter(
|
|
function (Category $c) use ($words) {
|
|
$found = 0;
|
|
foreach ($words as $word) {
|
|
if (!(strpos(strtolower($c->name), strtolower($word)) === false)) {
|
|
$found++;
|
|
}
|
|
}
|
|
|
|
return $found > 0;
|
|
}
|
|
);
|
|
|
|
return $newSet;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param array $words
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function searchTags(array $words): Collection
|
|
{
|
|
return new Collection;
|
|
}
|
|
|
|
/**
|
|
* @param array $words
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function searchTransactions(array $words): Collection
|
|
{
|
|
// decrypted transaction journals:
|
|
$decrypted = Auth::user()->transactionjournals()->withRelevantData()->where('encrypted', 0)->where(
|
|
function (EloquentBuilder $q) use ($words) {
|
|
foreach ($words as $word) {
|
|
$q->orWhere('description', 'LIKE', '%' . e($word) . '%');
|
|
}
|
|
}
|
|
)->get();
|
|
|
|
// encrypted
|
|
$all = Auth::user()->transactionjournals()->withRelevantData()->where('encrypted', 1)->get();
|
|
$set = $all->filter(
|
|
function (TransactionJournal $journal) use ($words) {
|
|
foreach ($words as $word) {
|
|
$haystack = strtolower($journal->description);
|
|
$word = strtolower($word);
|
|
if (!(strpos($haystack, $word) === false)) {
|
|
return $journal;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
);
|
|
$filtered = $set->merge($decrypted);
|
|
$filtered = $filtered->sortBy(
|
|
function (TransactionJournal $journal) {
|
|
return intval($journal->date->format('U'));
|
|
}
|
|
);
|
|
|
|
$filtered = $filtered->reverse();
|
|
|
|
return $filtered;
|
|
}
|
|
}
|