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

105 lines
3.0 KiB
PHP
Raw Normal View History

2016-05-20 08:57:45 +02:00
<?php
/**
* SearchController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-05-20 08:57:45 +02:00
namespace FireflyIII\Http\Controllers;
2015-02-27 11:09:23 +01:00
2017-10-26 19:56:03 +02:00
use FireflyIII\Support\CacheProperties;
2015-02-27 11:09:23 +01:00
use FireflyIII\Support\Search\SearchInterface;
2016-11-06 14:52:31 +01:00
use Illuminate\Http\Request;
2017-11-25 08:54:52 +01:00
use Illuminate\Support\Collection;
2017-07-15 17:19:12 +02:00
use Response;
use View;
2015-02-27 11:09:23 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class SearchController.
2015-02-27 11:09:23 +01:00
*/
2015-03-10 17:26:31 +01:00
class SearchController extends Controller
{
/**
* SearchController constructor.
*/
2016-01-08 18:29:47 +01:00
public function __construct()
{
2016-01-08 20:40:48 +01:00
parent::__construct();
2016-10-29 07:44:46 +02:00
$this->middleware(
function ($request, $next) {
2017-12-16 19:46:36 +01:00
app('view')->share('mainTitleIcon', 'fa-search');
app('view')->share('title', trans('firefly.search'));
return $next($request);
}
);
2016-01-08 18:29:47 +01:00
}
2015-02-27 11:09:23 +01:00
/**
2016-11-18 20:06:08 +01:00
* @param Request $request
2015-05-03 12:58:55 +02:00
* @param SearchInterface $searcher
*
* @return View
2015-02-27 11:09:23 +01:00
*/
2016-11-06 14:52:31 +01:00
public function index(Request $request, SearchInterface $searcher)
2015-02-27 11:09:23 +01:00
{
2017-09-16 06:52:53 +02:00
$fullQuery = strval($request->get('q'));
2017-07-15 16:41:07 +02:00
2017-07-15 17:19:12 +02:00
// parse search terms:
$searcher->parseQuery($fullQuery);
$query = $searcher->getWordsAsString();
$subTitle = trans('breadcrumbs.search_result', ['query' => $query]);
return view('search.index', compact('query', 'fullQuery', 'subTitle'));
2015-02-27 11:09:23 +01:00
}
2017-12-17 14:30:53 +01:00
/**
* @param Request $request
* @param SearchInterface $searcher
*
* @return \Illuminate\Http\JsonResponse
* @throws \Throwable
*/
2017-07-15 17:19:12 +02:00
public function search(Request $request, SearchInterface $searcher)
{
2017-11-25 08:54:52 +01:00
$fullQuery = strval($request->get('query'));
$transactions = new Collection;
2017-10-26 19:56:03 +02:00
// cache
$cache = new CacheProperties;
$cache->addProperty('search');
$cache->addProperty($fullQuery);
if ($cache->has()) {
2017-12-17 14:30:53 +01:00
$transactions = $cache->get(); // @codeCoverageIgnore
2017-10-26 19:56:03 +02:00
}
if (!$cache->has()) {
// parse search terms:
$searcher->parseQuery($fullQuery);
2017-10-30 16:25:39 +01:00
$searcher->setLimit(intval(env('SEARCH_RESULT_LIMIT', 50)));
2017-10-26 19:56:03 +02:00
$transactions = $searcher->searchTransactions();
$cache->store($transactions);
}
$html = view('search.search', compact('transactions'))->render();
2017-07-15 17:19:12 +02:00
return Response::json(['count' => $transactions->count(), 'html' => $html]);
}
2015-02-27 11:09:23 +01:00
}