2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* SearchController.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:27:31 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 01:57:45 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-27 04:09:23 -06:00
|
|
|
|
2017-10-26 12:56:03 -05:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2015-02-27 04:09:23 -06:00
|
|
|
use FireflyIII\Support\Search\SearchInterface;
|
2018-07-08 05:28:42 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2016-11-06 07:52:31 -06:00
|
|
|
use Illuminate\Http\Request;
|
2017-11-25 01:54:52 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-08-04 10:30:06 -05:00
|
|
|
use Log;
|
|
|
|
use Throwable;
|
2015-02-27 04:09:23 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class SearchController.
|
2015-02-27 04:09:23 -06:00
|
|
|
*/
|
2015-03-10 11:26:31 -05:00
|
|
|
class SearchController extends Controller
|
|
|
|
{
|
2016-01-09 01:20:55 -06:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2017-02-18 13:10:03 -06:00
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2017-12-16 12:46:36 -06:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-search');
|
2018-07-15 02:38:49 -05:00
|
|
|
app('view')->share('title', (string)trans('firefly.search'));
|
2017-02-18 13:10:03 -06:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2016-01-08 11:29:47 -06:00
|
|
|
}
|
|
|
|
|
2015-02-27 04:09:23 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Do the search.
|
|
|
|
*
|
2016-11-18 13:06:08 -06:00
|
|
|
* @param Request $request
|
2015-05-03 05:58:55 -05:00
|
|
|
* @param SearchInterface $searcher
|
|
|
|
*
|
2018-07-08 05:08:53 -05:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\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
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
$fullQuery = (string)$request->get('q');
|
2017-07-15 09:41:07 -05:00
|
|
|
|
2017-07-15 10:19:12 -05:00
|
|
|
// parse search terms:
|
|
|
|
$searcher->parseQuery($fullQuery);
|
|
|
|
$query = $searcher->getWordsAsString();
|
2019-03-02 07:12:09 -06:00
|
|
|
$modifiers = $searcher->getModifiers();
|
2018-07-15 02:38:49 -05:00
|
|
|
$subTitle = (string)trans('breadcrumbs.search_result', ['query' => $query]);
|
2017-07-15 10:19:12 -05:00
|
|
|
|
2019-03-02 07:12:09 -06:00
|
|
|
return view('search.index', compact('query','modifiers', 'fullQuery', 'subTitle'));
|
2015-02-27 04:09:23 -06:00
|
|
|
}
|
|
|
|
|
2017-12-17 07:30:53 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* JSON request that does the work.
|
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @param Request $request
|
|
|
|
* @param SearchInterface $searcher
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2018-07-08 05:28:42 -05:00
|
|
|
public function search(Request $request, SearchInterface $searcher): JsonResponse
|
2017-07-15 10:19:12 -05:00
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
$fullQuery = (string)$request->get('query');
|
2017-10-26 12:56:03 -05:00
|
|
|
|
2019-02-27 12:08:09 -06:00
|
|
|
$searcher->parseQuery($fullQuery);
|
|
|
|
$searcher->setLimit((int)config('firefly.search_result_limit'));
|
|
|
|
$transactions = $searcher->searchTransactions();
|
2019-03-02 07:12:09 -06:00
|
|
|
$searchTime = $searcher->searchTime(); // in seconds
|
2019-02-27 12:08:09 -06:00
|
|
|
|
2018-08-04 10:30:06 -05:00
|
|
|
try {
|
2019-03-02 07:12:09 -06:00
|
|
|
$html = view('search.search', compact('transactions','searchTime'))->render();
|
2018-08-30 13:58:07 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-08-04 10:30:06 -05:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
Log::error(sprintf('Cannot render search.search: %s', $e->getMessage()));
|
|
|
|
$html = 'Could not render view.';
|
|
|
|
}
|
2019-02-13 10:38:41 -06:00
|
|
|
|
2018-08-30 13:58:07 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2017-07-15 10:19:12 -05:00
|
|
|
|
2018-03-10 13:30:09 -06:00
|
|
|
return response()->json(['count' => $transactions->count(), 'html' => $html]);
|
2017-07-15 10:19:12 -05:00
|
|
|
}
|
2015-02-27 04:09:23 -06:00
|
|
|
}
|