Files
firefly-iii/app/Support/Search/Search.php

150 lines
3.9 KiB
PHP
Raw Normal View History

2015-02-27 11:09:23 +01:00
<?php
/**
* Search.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-02-27 11:09:23 +01:00
namespace FireflyIII\Support\Search;
2015-03-20 18:21:14 +01:00
use Auth;
2015-02-27 11:09:23 +01:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
2015-03-20 18:21:14 +01:00
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
2015-02-27 11:09:23 +01:00
/**
* Class Search
*
* @package FireflyIII\Search
*/
class Search implements SearchInterface
{
/**
* @param array $words
*
* @return Collection
*/
2016-02-06 10:15:07 +01:00
public function searchAccounts(array $words): Collection
2015-02-27 11:09:23 +01:00
{
2015-03-20 18:21:14 +01:00
return Auth::user()->accounts()->with('accounttype')->where(
2015-06-06 23:09:12 +02:00
function (EloquentBuilder $q) use ($words) {
2015-02-27 11:09:23 +01:00
foreach ($words as $word) {
$q->orWhere('name', 'LIKE', '%' . e($word) . '%');
}
}
)->get();
}
/**
* @param array $words
*
* @return Collection
*/
2016-02-06 10:15:07 +01:00
public function searchBudgets(array $words): Collection
2015-02-27 11:09:23 +01:00
{
/** @var Collection $set */
2015-03-20 18:21:14 +01:00
$set = Auth::user()->budgets()->get();
2015-02-27 11:09:23 +01:00
$newSet = $set->filter(
2015-06-06 23:09:12 +02:00
function (Budget $b) use ($words) {
2015-02-27 11:09:23 +01:00
$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
*/
2016-02-06 10:15:07 +01:00
public function searchCategories(array $words): Collection
2015-02-27 11:09:23 +01:00
{
/** @var Collection $set */
2015-03-20 18:21:14 +01:00
$set = Auth::user()->categories()->get();
2015-02-27 11:09:23 +01:00
$newSet = $set->filter(
2015-06-06 23:09:12 +02:00
function (Category $c) use ($words) {
2015-02-27 11:09:23 +01:00
$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
*/
2016-02-06 10:15:07 +01:00
public function searchTags(array $words): Collection
2015-02-27 11:09:23 +01:00
{
return new Collection;
}
/**
* @param array $words
*
* @return Collection
*/
2016-02-06 10:15:07 +01:00
public function searchTransactions(array $words): Collection
2015-02-27 11:09:23 +01:00
{
2015-03-20 18:21:14 +01:00
// decrypted transaction journals:
$decrypted = Auth::user()->transactionJournals()->expanded()->where('transaction_journals.encrypted', 0)->where(
2015-06-06 23:09:12 +02:00
function (EloquentBuilder $q) use ($words) {
2015-02-27 11:09:23 +01:00
foreach ($words as $word) {
2016-04-04 19:49:11 +02:00
$q->orWhere('transaction_journals.description', 'LIKE', '%' . e($word) . '%');
2015-02-27 11:09:23 +01:00
}
}
)->get(TransactionJournal::queryFields());
2015-03-20 18:21:14 +01:00
// encrypted
$all = Auth::user()->transactionJournals()->expanded()->where('transaction_journals.encrypted', 1)->get(TransactionJournal::queryFields());
2015-03-29 21:27:51 +02:00
$set = $all->filter(
2015-06-06 23:09:12 +02:00
function (TransactionJournal $journal) use ($words) {
2015-03-20 18:21:14 +01:00
foreach ($words as $word) {
$haystack = strtolower($journal->description);
$word = strtolower($word);
if (!(strpos($haystack, $word) === false)) {
return $journal;
}
}
2015-05-05 10:23:01 +02:00
return null;
2015-03-20 18:21:14 +01:00
}
);
2015-03-20 18:30:17 +01:00
$filtered = $set->merge($decrypted);
2015-06-17 06:11:35 +02:00
$filtered = $filtered->sortBy(
2015-06-06 23:09:12 +02:00
function (TransactionJournal $journal) {
2015-03-20 18:30:17 +01:00
return intval($journal->date->format('U'));
}
);
$filtered = $filtered->reverse();
return $filtered;
2015-02-27 11:09:23 +01:00
}
}