| 
									
										
										
										
											2016-05-20 08:57:45 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * SearchController.php | 
					
						
							| 
									
										
										
										
											2020-01-31 07:32:04 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-05-20 08:57:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Http\Controllers; | 
					
						
							| 
									
										
										
										
											2015-02-27 11:09:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-27 07:12:44 +02:00
										 |  |  | use FireflyIII\Repositories\Rule\RuleRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2015-02-27 11:09:23 +01:00
										 |  |  | use FireflyIII\Support\Search\SearchInterface; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\Contracts\View\Factory; | 
					
						
							| 
									
										
										
										
											2018-07-08 12:28:42 +02:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							| 
									
										
										
										
											2016-11-06 14:52:31 +01:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  | use Illuminate\View\View; | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:06 +02:00
										 |  |  | use Log; | 
					
						
							|  |  |  | use Throwable; | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-09 08:20:55 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * SearchController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-08 18:29:47 +01:00
										 |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-01-08 20:40:48 +01:00
										 |  |  |         parent::__construct(); | 
					
						
							| 
									
										
										
										
											2019-09-04 07:51:31 +02:00
										 |  |  |         app('view')->share('showCategory', true); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  |         $this->middleware( | 
					
						
							| 
									
										
										
										
											2019-09-04 07:51:31 +02:00
										 |  |  |             static function ($request, $next) { | 
					
						
							| 
									
										
										
										
											2017-12-16 19:46:36 +01:00
										 |  |  |                 app('view')->share('mainTitleIcon', 'fa-search'); | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  |                 app('view')->share('title', (string)trans('firefly.search')); | 
					
						
							| 
									
										
										
										
											2017-02-18 20:10:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2016-01-08 18:29:47 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 11:09:23 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:10:16 +02:00
										 |  |  |      * Do the search. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-11-18 20:06:08 +01:00
										 |  |  |      * @param Request         $request | 
					
						
							| 
									
										
										
										
											2015-05-03 12:58:55 +02:00
										 |  |  |      * @param SearchInterface $searcher | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @return Factory|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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-08-27 07:12:44 +02:00
										 |  |  |         // search params:
 | 
					
						
							| 
									
										
										
										
											2021-06-07 06:27:44 +02:00
										 |  |  |         $fullQuery = $request->get('search'); | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  |         if (is_array($request->get('search'))) { | 
					
						
							| 
									
										
										
										
											2021-06-06 17:47:44 +02:00
										 |  |  |             $fullQuery = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  |         $fullQuery   = (string)$fullQuery; | 
					
						
							|  |  |  |         $page        = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); | 
					
						
							|  |  |  |         $ruleId      = (int)$request->get('rule'); | 
					
						
							|  |  |  |         $ruleChanged = false; | 
					
						
							| 
									
										
										
										
											2020-08-27 07:12:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // find rule, check if query is different, offer to update.
 | 
					
						
							|  |  |  |         $ruleRepository = app(RuleRepositoryInterface::class); | 
					
						
							|  |  |  |         $rule           = $ruleRepository->find($ruleId); | 
					
						
							|  |  |  |         if (null !== $rule) { | 
					
						
							|  |  |  |             $originalQuery = $ruleRepository->getSearchQuery($rule); | 
					
						
							|  |  |  |             if ($originalQuery !== $fullQuery) { | 
					
						
							|  |  |  |                 $ruleChanged = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-07-15 17:19:12 +02:00
										 |  |  |         // parse search terms:
 | 
					
						
							|  |  |  |         $searcher->parseQuery($fullQuery); | 
					
						
							| 
									
										
										
										
											2020-08-27 07:12:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // words from query and operators:
 | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  |         $query            = $searcher->getWordsAsString(); | 
					
						
							|  |  |  |         $operators        = $searcher->getOperators(); | 
					
						
							|  |  |  |         $invalidOperators = $searcher->getInvalidOperators(); | 
					
						
							|  |  |  |         $subTitle         = (string)trans('breadcrumbs.search_result', ['query' => $fullQuery]); | 
					
						
							| 
									
										
										
										
											2020-08-27 07:12:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  |         return prefixView('search.index', compact('query', 'operators', 'page', 'rule', 'fullQuery', 'subTitle', 'ruleId', 'ruleChanged', 'invalidOperators')); | 
					
						
							| 
									
										
										
										
											2015-02-27 11:09:23 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 14:30:53 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 08:10:16 +02:00
										 |  |  |      * JSON request that does the work. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-12-17 14:30:53 +01:00
										 |  |  |      * @param Request         $request | 
					
						
							|  |  |  |      * @param SearchInterface $searcher | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-03-17 15:01:00 +01:00
										 |  |  |      * @return JsonResponse | 
					
						
							| 
									
										
										
										
											2017-12-17 14:30:53 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-08 12:28:42 +02:00
										 |  |  |     public function search(Request $request, SearchInterface $searcher): JsonResponse | 
					
						
							| 
									
										
										
										
											2017-07-15 17:19:12 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  |         $fullQuery = (string)$request->get('query'); | 
					
						
							|  |  |  |         $page      = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); | 
					
						
							| 
									
										
										
										
											2017-10-26 19:56:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-27 19:08:09 +01:00
										 |  |  |         $searcher->parseQuery($fullQuery); | 
					
						
							| 
									
										
										
										
											2020-10-17 08:53:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-08 20:21:44 +02:00
										 |  |  |         $searcher->setPage($page); | 
					
						
							|  |  |  |         $groups     = $searcher->searchTransactions(); | 
					
						
							|  |  |  |         $hasPages   = $groups->hasPages(); | 
					
						
							| 
									
										
										
										
											2019-08-21 04:37:17 +02:00
										 |  |  |         $searchTime = round($searcher->searchTime(), 3); // in seconds
 | 
					
						
							| 
									
										
										
										
											2019-09-08 20:21:44 +02:00
										 |  |  |         $parameters = ['search' => $fullQuery]; | 
					
						
							|  |  |  |         $url        = route('search.index') . '?' . http_build_query($parameters); | 
					
						
							|  |  |  |         $groups->setPath($url); | 
					
						
							| 
									
										
										
										
											2020-09-23 19:29:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:06 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-01-31 20:35:54 +01:00
										 |  |  |             $html = prefixView('search.search', compact('groups', 'hasPages', 'searchTime'))->render(); | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } catch (Throwable $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2018-08-04 17:30:06 +02:00
										 |  |  |             Log::error(sprintf('Cannot render search.search: %s', $e->getMessage())); | 
					
						
							|  |  |  |             $html = 'Could not render view.'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-30 20:02:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 05:32:30 +02:00
										 |  |  |         return response()->json(['count' => $groups->count(), 'html' => $html]); | 
					
						
							| 
									
										
										
										
											2017-07-15 17:19:12 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-27 11:09:23 +01:00
										 |  |  | } |