Skip to content

Firefly III: a personal finances manager

Search controller.

James Cole8f578ed95a48
diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php
index bc743f4b7b..139fb63887 100644
--- a/app/Http/Controllers/BillController.php
+++ b/app/Http/Controllers/BillController.php
@@ -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));
}
/**
diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php
index 2ef107071c..8bc05e7bc8 100644
--- a/app/Http/Controllers/CategoryController.php
+++ b/app/Http/Controllers/CategoryController.php
@@ -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'));
}
/**
diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php
index 53d4a6e0d5..318e2736d3 100644
--- a/app/Http/Controllers/CurrencyController.php
+++ b/app/Http/Controllers/CurrencyController.php
@@ -74,7 +74,7 @@ class CurrencyController extends Controller
}
- return View::make('currency.delete', compact('currency'));
+ return view('currency.delete', compact('currency'));
}
/**
diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php
index 589dc52c8e..9e2a7ef9f5 100644
--- a/app/Http/Controllers/PiggyBankController.php
+++ b/app/Http/Controllers/PiggyBankController.php
@@ -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'));
}
diff --git a/app/Http/Controllers/PreferencesController.php b/app/Http/Controllers/PreferencesController.php
index 50548858c0..c8c1379149 100644
--- a/app/Http/Controllers/PreferencesController.php
+++ b/app/Http/Controllers/PreferencesController.php
@@ -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
);
}
diff --git a/app/Http/Controllers/RepeatedExpenseController.php b/app/Http/Controllers/RepeatedExpenseController.php
index 7813844479..8569b067a9 100644
--- a/app/Http/Controllers/RepeatedExpenseController.php
+++ b/app/Http/Controllers/RepeatedExpenseController.php
@@ -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'));
}
/**
diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php
new file mode 100644
index 0000000000..aa56c5e993
--- /dev/null
+++ b/app/Http/Controllers/SearchController.php
@@ -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);
+ }
+
+}
diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php
index e3d111dac7..549df90c5a 100644
--- a/app/Providers/FireflyServiceProvider.php
+++ b/app/Providers/FireflyServiceProvider.php
@@ -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');
diff --git a/app/Support/Search/Search.php b/app/Support/Search/Search.php
new file mode 100644
index 0000000000..7e0cd1de39
--- /dev/null
+++ b/app/Support/Search/Search.php
@@ -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();
+ }
+}
diff --git a/app/Support/Search/SearchInterface.php b/app/Support/Search/SearchInterface.php
new file mode 100644
index 0000000000..50d7c6ad08
--- /dev/null
+++ b/app/Support/Search/SearchInterface.php
@@ -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);
+}
\ No newline at end of file
diff --git a/resources/views/search/index.blade.php b/resources/views/search/index.blade.php
new file mode 100644
index 0000000000..3a2c4aa1aa
--- /dev/null
+++ b/resources/views/search/index.blade.php
@@ -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