firefly-iii/app/Http/Controllers/SearchController.php

98 lines
2.8 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* SearchController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-27 04:09:23 -06:00
use FireflyIII\Support\Search\SearchInterface;
2016-11-06 07:52:31 -06:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use View;
2015-02-27 04:09:23 -06:00
/**
* Class SearchController
*
* @package FireflyIII\Http\Controllers
*/
2015-03-10 11:26:31 -05:00
class SearchController extends Controller
{
/**
* SearchController constructor.
*/
2016-01-08 11:29:47 -06:00
public function __construct()
{
2016-01-08 13:40:48 -06:00
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
View::share('mainTitleIcon', 'fa-search');
View::share('title', trans('firefly.search'));
return $next($request);
}
);
2016-01-08 11:29:47 -06:00
}
2015-02-27 04:09:23 -06:00
/**
2016-11-18 13:06:08 -06:00
* @param Request $request
2015-05-03 05:58:55 -05:00
* @param SearchInterface $searcher
*
* @return View
2015-02-27 04:09:23 -06:00
*/
2016-11-06 07:52:31 -06:00
public function index(Request $request, SearchInterface $searcher)
2015-02-27 04:09:23 -06:00
{
// yes, hard coded values:
$minSearchLen = 1;
$limit = 20;
// ui stuff:
2017-02-24 22:57:01 -06:00
$subTitle = '';
2016-11-06 07:52:31 -06:00
// query stuff
$query = null;
$result = [];
$rawQuery = $request->get('q');
$hasModifiers = true;
$modifiers = [];
2016-11-06 07:52:31 -06:00
if (!is_null($request->get('q')) && strlen($request->get('q')) >= $minSearchLen) {
// parse query, find modifiers:
// set limit for search
$searcher->setLimit($limit);
$searcher->parseQuery($request->get('q'));
$transactions = $searcher->searchTransactions();
$accounts = new Collection;
$categories = new Collection;
$tags = new Collection;
$budgets = new Collection;
// no special search thing?
if (!$searcher->hasModifiers()) {
$hasModifiers = false;
$accounts = $searcher->searchAccounts();
$categories = $searcher->searchCategories();
$budgets = $searcher->searchBudgets();
$tags = $searcher->searchTags();
}
$query = $searcher->getWordsAsString();
$subTitle = trans('firefly.search_results_for', ['query' => $query]);
$result = ['transactions' => $transactions, 'accounts' => $accounts, 'categories' => $categories, 'budgets' => $budgets, 'tags' => $tags];
2015-02-27 04:09:23 -06:00
}
return view('search.index', compact('rawQuery', 'hasModifiers', 'modifiers', 'subTitle', 'limit', 'query', 'result'));
2015-02-27 04:09:23 -06:00
}
}