1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SearchController.php |
4
|
|
|
* Copyright (c) 2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Http\Controllers; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Support\CacheProperties; |
27
|
|
|
use FireflyIII\Support\Search\SearchInterface; |
28
|
|
|
use Illuminate\Http\Request; |
29
|
|
|
use Response; |
30
|
|
|
use View; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class SearchController |
34
|
|
|
* |
35
|
|
|
* @package FireflyIII\Http\Controllers |
36
|
|
|
*/ |
37
|
|
|
class SearchController extends Controller |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* SearchController constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
parent::__construct(); |
45
|
|
|
|
46
|
|
|
$this->middleware( |
47
|
|
|
function ($request, $next) { |
48
|
|
|
View::share('mainTitleIcon', 'fa-search'); |
49
|
|
|
View::share('title', trans('firefly.search')); |
50
|
|
|
|
51
|
|
|
return $next($request); |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Request $request |
59
|
|
|
* @param SearchInterface $searcher |
60
|
|
|
* |
61
|
|
|
* @return View |
62
|
|
|
*/ |
63
|
|
|
public function index(Request $request, SearchInterface $searcher) |
64
|
|
|
{ |
65
|
|
|
$fullQuery = strval($request->get('q')); |
66
|
|
|
|
67
|
|
|
// parse search terms: |
68
|
|
|
$searcher->parseQuery($fullQuery); |
69
|
|
|
$query = $searcher->getWordsAsString(); |
70
|
|
|
$subTitle = trans('breadcrumbs.search_result', ['query' => $query]); |
71
|
|
|
|
72
|
|
|
return view('search.index', compact('query', 'fullQuery', 'subTitle')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function search(Request $request, SearchInterface $searcher) |
76
|
|
|
{ |
77
|
|
|
$fullQuery = strval($request->get('query')); |
78
|
|
|
|
79
|
|
|
// cache |
80
|
|
|
$cache = new CacheProperties; |
81
|
|
|
$cache->addProperty('search'); |
82
|
|
|
$cache->addProperty($fullQuery); |
83
|
|
|
|
84
|
|
|
if ($cache->has()) { |
85
|
|
|
$transactions = $cache->get(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!$cache->has()) { |
89
|
|
|
// parse search terms: |
90
|
|
|
$searcher->parseQuery($fullQuery); |
91
|
|
|
$searcher->setLimit(intval(env('SEARCH_RESULT_LIMIT', 50))); |
92
|
|
|
$transactions = $searcher->searchTransactions(); |
93
|
|
|
$cache->store($transactions); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$html = view('search.search', compact('transactions'))->render(); |
97
|
|
|
|
98
|
|
|
return Response::json(['count' => $transactions->count(), 'html' => $html]); |
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: