. */ declare(strict_types=1); namespace FireflyIII\Http\Controllers; use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Search\SearchInterface; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Log; use Throwable; /** * Class SearchController. */ class SearchController extends Controller { /** * SearchController constructor. */ public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { app('view')->share('mainTitleIcon', 'fa-search'); app('view')->share('title', (string)trans('firefly.search')); return $next($request); } ); } /** * Do the search. * * @param Request $request * @param SearchInterface $searcher * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function index(Request $request, SearchInterface $searcher) { $fullQuery = (string)$request->get('q'); // parse search terms: $searcher->parseQuery($fullQuery); $query = $searcher->getWordsAsString(); $modifiers = $searcher->getModifiers(); $subTitle = (string)trans('breadcrumbs.search_result', ['query' => $query]); return view('search.index', compact('query','modifiers', 'fullQuery', 'subTitle')); } /** * JSON request that does the work. * * @param Request $request * @param SearchInterface $searcher * * @return \Illuminate\Http\JsonResponse */ public function search(Request $request, SearchInterface $searcher): JsonResponse { $fullQuery = (string)$request->get('query'); $searcher->parseQuery($fullQuery); $searcher->setLimit((int)config('firefly.search_result_limit')); $transactions = $searcher->searchTransactions(); $searchTime = $searcher->searchTime(); // in seconds try { $html = view('search.search', compact('transactions','searchTime'))->render(); // @codeCoverageIgnoreStart } catch (Throwable $e) { Log::error(sprintf('Cannot render search.search: %s', $e->getMessage())); $html = 'Could not render view.'; } // @codeCoverageIgnoreEnd return response()->json(['count' => $transactions->count(), 'html' => $html]); } }