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

141 lines
3.5 KiB
PHP
Raw Normal View History

2015-02-27 04:09:23 -06:00
<?php
namespace FireflyIII\Support\Search;
2015-03-20 12:21:14 -05:00
use Auth;
2015-02-27 04:09:23 -06:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
2015-03-20 12:21:14 -05:00
use FireflyIII\Models\TransactionJournal;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
2015-02-27 04:09:23 -06:00
/**
* Class Search
*
* @package FireflyIII\Search
*/
class Search implements SearchInterface
{
/**
* @param array $words
*
* @return Collection
*/
public function searchAccounts(array $words)
{
2015-03-20 12:21:14 -05:00
return Auth::user()->accounts()->with('accounttype')->where(
2015-06-06 16:09:12 -05:00
function (EloquentBuilder $q) use ($words) {
2015-02-27 04:09:23 -06:00
foreach ($words as $word) {
$q->orWhere('name', 'LIKE', '%' . e($word) . '%');
}
}
)->get();
}
/**
* @param array $words
*
* @return Collection
*/
public function searchBudgets(array $words)
{
/** @var Collection $set */
2015-03-20 12:21:14 -05:00
$set = Auth::user()->budgets()->get();
2015-02-27 04:09:23 -06:00
$newSet = $set->filter(
2015-06-06 16:09:12 -05:00
function (Budget $b) use ($words) {
2015-02-27 04:09:23 -06: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
*/
public function searchCategories(array $words)
{
/** @var Collection $set */
2015-03-20 12:21:14 -05:00
$set = Auth::user()->categories()->get();
2015-02-27 04:09:23 -06:00
$newSet = $set->filter(
2015-06-06 16:09:12 -05:00
function (Category $c) use ($words) {
2015-02-27 04:09:23 -06: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
*/
public function searchTags(array $words)
{
return new Collection;
}
/**
* @param array $words
*
* @return Collection
*/
public function searchTransactions(array $words)
{
2015-03-20 12:21:14 -05:00
// decrypted transaction journals:
$decrypted = Auth::user()->transactionjournals()->withRelevantData()->where('encrypted', 0)->where(
2015-06-06 16:09:12 -05:00
function (EloquentBuilder $q) use ($words) {
2015-02-27 04:09:23 -06:00
foreach ($words as $word) {
$q->orWhere('description', 'LIKE', '%' . e($word) . '%');
}
}
)->get();
2015-03-20 12:21:14 -05:00
// encrypted
2015-03-29 14:27:51 -05:00
$all = Auth::user()->transactionjournals()->withRelevantData()->where('encrypted', 1)->get();
$set = $all->filter(
2015-06-06 16:09:12 -05:00
function (TransactionJournal $journal) use ($words) {
2015-03-20 12:21:14 -05:00
foreach ($words as $word) {
$haystack = strtolower($journal->description);
$word = strtolower($word);
if (!(strpos($haystack, $word) === false)) {
return $journal;
}
}
2015-05-05 03:23:01 -05:00
return null;
2015-03-20 12:21:14 -05:00
}
);
2015-03-20 12:30:17 -05:00
$filtered = $set->merge($decrypted);
2015-06-16 23:11:35 -05:00
$filtered = $filtered->sortBy(
2015-06-06 16:09:12 -05:00
function (TransactionJournal $journal) {
2015-03-20 12:30:17 -05:00
return intval($journal->date->format('U'));
}
);
$filtered = $filtered->reverse();
return $filtered;
2015-02-27 04:09:23 -06:00
}
}