Search controller.

This commit is contained in:
James Cole 2015-02-27 11:09:23 +01:00
parent fc5c339e27
commit 8f578ed95a
11 changed files with 328 additions and 17 deletions

View File

@ -70,7 +70,7 @@ class BillController extends Controller
{
$periods = \Config::get('firefly.periods_to_text');
return View::make('bills.edit')->with('periods', $periods)->with('bill', $bill)->with('subTitle', 'Edit "' . e($bill->name) . '"');
return view('bills.edit')->with('periods', $periods)->with('bill', $bill)->with('subTitle', 'Edit "' . e($bill->name) . '"');
}
/**
@ -92,7 +92,7 @@ class BillController extends Controller
}
);
return View::make('bills.index', compact('bills'));
return view('bills.index', compact('bills'));
}
/**
@ -140,7 +140,7 @@ class BillController extends Controller
$hideBill = true;
return View::make('bills.show', compact('journals', 'hideBill', 'bill'))->with('subTitle', e($bill->name));
return view('bills.show', compact('journals', 'hideBill', 'bill'))->with('subTitle', e($bill->name));
}
/**

View File

@ -73,7 +73,7 @@ class CategoryController extends Controller
->get(['transaction_journals.*']);
$subTitle = 'Transactions without a category in ' . $start->format('F Y');
return View::make('categories.noCategory', compact('list', 'subTitle'));
return view('categories.noCategory', compact('list', 'subTitle'));
}
/**

View File

@ -74,7 +74,7 @@ class CurrencyController extends Controller
}
return View::make('currency.delete', compact('currency'));
return view('currency.delete', compact('currency'));
}
/**

View File

@ -52,7 +52,7 @@ class PiggyBankController extends Controller
\Log::debug('Now going to view for piggy bank #' . $piggyBank->id . ' (' . $piggyBank->name . ')');
return View::make('piggy-banks.add', compact('piggyBank', 'maxAmount'));
return view('piggy-banks.add', compact('piggyBank', 'maxAmount'));
}
/**
@ -66,7 +66,7 @@ class PiggyBankController extends Controller
$subTitle = 'Create new piggy bank';
$subTitleIcon = 'fa-plus';
return View::make('piggy-banks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon'));
return view('piggy-banks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon'));
}
/**
@ -78,7 +78,7 @@ class PiggyBankController extends Controller
{
$subTitle = 'Delete "' . e($piggyBank->name) . '"';
return View::make('piggy_banks.delete', compact('piggyBank', 'subTitle'));
return view('piggy_banks.delete', compact('piggyBank', 'subTitle'));
}
/**
@ -128,7 +128,7 @@ class PiggyBankController extends Controller
];
Session::flash('preFilled', $preFilled);
return View::make('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'periods', 'preFilled'));
return view('piggy-banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'periods', 'preFilled'));
}
@ -242,7 +242,7 @@ class PiggyBankController extends Controller
*/
public function remove(PiggyBank $piggyBank)
{
return View::make('piggy-banks.remove', compact('piggyBank'));
return view('piggy-banks.remove', compact('piggyBank'));
}
/**
@ -261,7 +261,7 @@ class PiggyBankController extends Controller
$subTitle = e($piggyBank->name);
return View::make('piggy-banks.show', compact('piggyBank', 'events', 'subTitle'));
return view('piggy-banks.show', compact('piggyBank', 'events', 'subTitle'));
}

View File

@ -39,7 +39,7 @@ class PreferencesController extends Controller {
$budgetMax = Preferences::get('budgetMaximum', 1000);
$budgetMaximum = $budgetMax->data;
return View::make('preferences.index', compact('budgetMaximum'))->with('accounts', $accounts)->with('frontPageAccounts', $frontPage)->with(
return view('preferences.index', compact('budgetMaximum'))->with('accounts', $accounts)->with('frontPageAccounts', $frontPage)->with(
'viewRange', $viewRangeValue
);
}

View File

@ -38,7 +38,7 @@ class RepeatedExpenseController extends Controller
$periods = Config::get('firefly.piggy_bank_periods');
$accounts = ExpandedForm::makeSelectList(Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->get(['accounts.*']));
return View::make('repeatedExpense.create', compact('accounts', 'periods'))->with('subTitle', 'Create new repeated expense')->with(
return view('repeatedExpense.create', compact('accounts', 'periods'))->with('subTitle', 'Create new repeated expense')->with(
'subTitleIcon', 'fa-plus'
);
}
@ -52,7 +52,7 @@ class RepeatedExpenseController extends Controller
{
$subTitle = 'Delete "' . e($repeatedExpense->name) . '"';
return View::make('repeatedExpense.delete', compact('repeatedExpense', 'subTitle'));
return view('repeatedExpense.delete', compact('repeatedExpense', 'subTitle'));
}
/**
@ -98,7 +98,7 @@ class RepeatedExpenseController extends Controller
];
Session::flash('preFilled', $preFilled);
return View::make('repeatedExpense.edit', compact('subTitle', 'subTitleIcon', 'repeatedExpense', 'accounts', 'periods', 'preFilled'));
return view('repeatedExpense.edit', compact('subTitle', 'subTitleIcon', 'repeatedExpense', 'accounts', 'periods', 'preFilled'));
}
/**
@ -116,7 +116,7 @@ class RepeatedExpenseController extends Controller
}
);
return View::make('repeatedExpense.index', compact('expenses', 'subTitle'));
return view('repeatedExpense.index', compact('expenses', 'subTitle'));
}
/**
@ -136,7 +136,7 @@ class RepeatedExpenseController extends Controller
}
);
return View::make('repeatedExpense.show', compact('repetitions', 'repeatedExpense', 'today', 'subTitle'));
return view('repeatedExpense.show', compact('repetitions', 'repeatedExpense', 'today', 'subTitle'));
}
/**

View File

@ -0,0 +1,44 @@
<?php namespace FireflyIII\Http\Controllers;
use FireflyIII\Http\Requests;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\Search\SearchInterface;
use Illuminate\Http\Request;
use Input;
/**
* Class SearchController
*
* @package FireflyIII\Http\Controllers
*/
class SearchController extends Controller {
/**
* Results always come in the form of an array [results, count, fullCount]
*/
public function index(SearchInterface $searcher)
{
$subTitle = null;
$rawQuery = null;
$result = [];
if (!is_null(Input::get('q')) && strlen(Input::get('q')) > 0) {
$rawQuery = trim(Input::get('q'));
$words = explode(' ', $rawQuery);
$subTitle = 'Results for "' . e($rawQuery) . '"';
$transactions = $searcher->searchTransactions($words);
$accounts = $searcher->searchAccounts($words);
$categories = $searcher->searchCategories($words);
$budgets = $searcher->searchBudgets($words);
$tags = $searcher->searchTags($words);
$result = ['transactions' => $transactions, 'accounts' => $accounts, 'categories' => $categories, 'budgets' => $budgets, 'tags' => $tags];
}
return view('search.index')->with('title', 'Search')->with('subTitle', $subTitle)->with(
'mainTitleIcon', 'fa-search'
)->with('query', $rawQuery)->with('result', $result);
}
}

View File

@ -62,6 +62,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Repositories\Journal\JournalRepositoryInterface', 'FireflyIII\Repositories\Journal\JournalRepository');
$this->app->bind('FireflyIII\Repositories\Bill\BillRepositoryInterface', 'FireflyIII\Repositories\Bill\BillRepository');
$this->app->bind('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface', 'FireflyIII\Repositories\PiggyBank\PiggyBankRepository');
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
$this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper');
$this->app->bind('FireflyIII\Helpers\Report\ReportQueryInterface', 'FireflyIII\Helpers\Report\ReportQuery');

View File

@ -0,0 +1,112 @@
<?php
namespace FireflyIII\Support\Search;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
/**
* Class Search
*
* @package FireflyIII\Search
*/
class Search implements SearchInterface
{
/**
* @param array $words
*
* @return Collection
*/
public function searchAccounts(array $words)
{
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)
{
/** @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)
{
/** @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;
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param array $words
*
* @return Collection
*/
public function searchTags(array $words)
{
return new Collection;
}
/**
* @param array $words
*
* @return Collection
*/
public function searchTransactions(array $words)
{
return \Auth::user()->transactionjournals()->withRelevantData()->where(
function (EloquentBuilder $q) use ($words) {
foreach ($words as $word) {
$q->orWhere('description', 'LIKE', '%' . e($word) . '%');
}
}
)->get();
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace FireflyIII\Support\Search;
use Illuminate\Support\Collection;
/**
* Interface SearchInterface
*
* @package FireflyIII\Support\Search
*/
interface SearchInterface {
/**
* @param array $words
*
* @return Collection
*/
public function searchAccounts(array $words);
/**
* @param array $words
*
* @return Collection
*/
public function searchBudgets(array $words);
/**
* @param array $words
*
* @return Collection
*/
public function searchCategories(array $words);
/**
*
* @param array $words
*
* @return Collection
*/
public function searchTags(array $words);
/**
* @param array $words
*
* @return Collection
*/
public function searchTransactions(array $words);
}

View File

@ -0,0 +1,106 @@
@extends('layouts.default')
@section('content')
{!! Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $query) !!}
@if(!is_null($query))
<div class="row">
@if(isset($result['transactions']) && $result['transactions']->count() > 0)
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-repeat"></i> Transactions ({{$result['transactions']->count()}})
</div>
<div class="panel-body">
@include('list.journals-small',['journals' => $result['transactions']])
</div>
</div>
</div>
@endif
@if(isset($result['categories']) && $result['categories']->count() > 0)
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bar-chart"></i> Categories ({{$result['categories']->count()}})
</div>
<div class="panel-body">
<div class="list-group">
@foreach($result['categories'] as $category)
<a class="list-group-item" title="{{$category->name}}" href="{{route('categories.show',$category->id)}}">
{{{$category->name}}}
</a>
@endforeach
</div>
</div>
</div>
</div>
@endif
@if(isset($result['tags']) && $result['tags']->count() > 0)
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bar-chart"></i> Tags ({{$result['tags']->count()}})
</div>
<div class="panel-body">
<p>Bla bla</p>
</div>
</div>
</div>
@endif
@if(isset($result['accounts']) && $result['accounts']->count() > 0)
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-credit-card"></i> Accounts ({{$result['accounts']->count()}})
</div>
<div class="panel-body">
<div class="list-group">
@foreach($result['accounts'] as $account)
<a class="list-group-item" title="{{$account->name}}" href="{{route('accounts.show',$account->id)}}">
{{{$account->name}}}
</a>
@endforeach
</div>
</div>
</div>
</div>
@endif
@if(isset($result['budgets']) && $result['budgets']->count() > 0)
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-tasks"></i> Budgets ({{$result['budgets']->count()}})
</div>
<div class="panel-body">
<div class="list-group">
@foreach($result['budgets'] as $budget)
<a class="list-group-item" title="{{$budget->name}}" href="{{route('budgets.show',$budget->id)}}">
{{{$budget->name}}}
</a>
@endforeach
</div>
</div>
</div>
</div>
@endif
<!--
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-search-plus"></i> Other results
</div>
<div class="panel-body">
<p>Bla bla</p>
</div>
</div>
</div>
-->
</div>
@endif
@stop
@section('scripts')
<script type="text/javascript">
var query = '{{{$query}}}';
</script>
@stop