1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\TechTips; |
4
|
|
|
|
5
|
|
|
use App\TechTips; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
|
9
|
|
|
class SearchTips |
10
|
|
|
{ |
11
|
|
|
protected $perPage = 10; |
12
|
|
|
|
13
|
12 |
|
public function execute($request) |
14
|
|
|
{ |
15
|
12 |
|
$searchData = $request->search; |
16
|
12 |
|
$this->perPage = $request->pagination['perPage']; |
17
|
|
|
|
18
|
12 |
|
if($searchData) |
19
|
|
|
{ |
20
|
10 |
|
$results = $this->searchFor($searchData); |
21
|
|
|
} |
22
|
|
|
else |
23
|
|
|
{ |
24
|
2 |
|
$results = $this->getAllTips(); |
25
|
|
|
} |
26
|
|
|
|
27
|
12 |
|
Log::debug('Tech Tip Search performed for ', $request->toArray()); |
28
|
12 |
|
Log::debug('Tech Tip Search results - ', $results != null ? $results->toArray() : []); |
29
|
12 |
|
return $results; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// Return all tips - no search paramaters |
33
|
2 |
|
protected function getAllTips() |
34
|
|
|
{ |
35
|
2 |
|
return TechTips::orderBy('sticky', 'DESC') |
36
|
2 |
|
->orderBy('created_at', 'DESC') |
37
|
2 |
|
->with('SystemTypes') |
38
|
2 |
|
->with('TechTipTypes') |
39
|
2 |
|
->paginate($this->perPage); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Process all of the search filters to search for a tech tip |
43
|
10 |
|
protected function searchFor($search) |
44
|
|
|
{ |
45
|
10 |
|
$searchText = isset($search['text']) ? explode(' ', $search['text']) : null; |
46
|
10 |
|
$type = isset($search['type']) ? $search['type'] : null; |
47
|
10 |
|
$sys = isset($search['sys_id']) ? $search['sys_id'] : null; |
48
|
|
|
|
49
|
10 |
|
return TechTips::orderBy('sticky', 'DESC') |
50
|
10 |
|
->orderBy('created_at', 'DESC') |
51
|
|
|
// Search text fields |
52
|
|
|
->when(!empty($searchText), function($q) use ($searchText) |
53
|
|
|
{ |
54
|
6 |
|
foreach($searchText as $text) |
55
|
|
|
{ |
56
|
6 |
|
$q->orWhere('subject', 'like', '%'.$text.'%') |
57
|
6 |
|
->orWhere('tip_id', 'like', '%'.$text.'%') |
58
|
6 |
|
->orWhere('description', 'like', '%'.$text.'%'); |
59
|
|
|
} |
60
|
10 |
|
}) |
61
|
|
|
// Search Article Type fields |
62
|
|
|
->when($type, function($q) use ($type) |
63
|
|
|
{ |
64
|
2 |
|
$q->whereIn('tip_type_id', $type); |
65
|
10 |
|
}) |
66
|
|
|
// Search equipment type fields |
67
|
|
|
->when($sys, function($q) use ($sys) |
68
|
|
|
{ |
69
|
|
|
$q->whereHas('SystemTypes', function($q2) use ($sys) |
70
|
|
|
{ |
71
|
2 |
|
$q2->whereIn('system_types.sys_id', $sys); |
72
|
2 |
|
}); |
73
|
10 |
|
}) |
74
|
10 |
|
->with('SystemTypes') |
75
|
10 |
|
->with('TechTipTypes') |
76
|
10 |
|
->paginate($this->perPage); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|