1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models\DataComponents; |
4
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Facades\UrlHelper; |
6
|
|
|
use hamburgscleanest\DataTables\Models\DataComponent; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DataScout |
11
|
|
|
* @package hamburgscleanest\DataTables\Models\DataComponents |
12
|
|
|
*/ |
13
|
|
|
class DataScout extends DataComponent { |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
private $_searchQueries = []; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
private $_searchableFields; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $_buttonText = 'Search'; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $_placeholder = 'Search..'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* DataScout constructor. |
29
|
|
|
* @param array $searchableFields |
30
|
|
|
* @param bool $remember |
31
|
|
|
*/ |
32
|
7 |
|
public function __construct(array $searchableFields = [], $remember = false) |
33
|
|
|
{ |
34
|
7 |
|
$this->_searchableFields = $searchableFields; |
35
|
7 |
|
$this->_rememberKey = 'data-scout'; |
36
|
7 |
|
$this->_rememberState = $remember; |
37
|
7 |
|
} |
38
|
|
|
|
39
|
7 |
|
protected function _afterInit() |
40
|
|
|
{ |
41
|
7 |
|
$search = $this->_request->get('search'); |
42
|
7 |
|
if (!empty($search)) |
43
|
|
|
{ |
44
|
1 |
|
$this->_searchQueries += \explode(',', $search); |
45
|
|
|
} |
46
|
7 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Builder |
50
|
|
|
*/ |
51
|
5 |
|
public function _shapeData(): Builder |
52
|
|
|
{ |
53
|
5 |
|
if (\count($this->_searchQueries) === 0) |
54
|
|
|
{ |
55
|
1 |
|
return $this->_queryBuilder; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$this->_queryBuilder->where(function ($query) |
59
|
|
|
{ |
60
|
4 |
|
foreach ($this->_searchQueries as $value) |
61
|
|
|
{ |
62
|
4 |
|
foreach ($this->_searchableFields as $field) |
63
|
|
|
{ |
64
|
4 |
|
$query->orWhere($field, 'like', '%' . $value . '%'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
4 |
|
}); |
68
|
|
|
|
69
|
4 |
|
return $this->_queryBuilder; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add a query programmatically. |
74
|
|
|
* |
75
|
|
|
* @param string $value |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
3 |
|
public function addQuery(string $value) |
79
|
|
|
{ |
80
|
3 |
|
$this->_searchQueries[] = $value; |
81
|
|
|
|
82
|
3 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the text for the search button. |
87
|
|
|
* |
88
|
|
|
* @param string $text |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
1 |
|
public function buttonText(string $text) |
92
|
|
|
{ |
93
|
1 |
|
$this->_buttonText = $text; |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the placeholder for the input. |
100
|
|
|
* |
101
|
|
|
* @param string $text |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
1 |
|
public function placeholder(string $text) |
105
|
|
|
{ |
106
|
1 |
|
$this->_placeholder = $text; |
107
|
|
|
|
108
|
1 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $field |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
1 |
|
public function makeSearchable(string $field) |
116
|
|
|
{ |
117
|
1 |
|
$this->_searchableFields[] = $field; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
2 |
|
private function _buildSearchUrl() |
126
|
|
|
{ |
127
|
2 |
|
$parameters = UrlHelper::parameterizeQuery($this->_request->getQueryString()); |
128
|
2 |
|
$parameters['search'] = \implode(',', $this->_searchQueries); |
129
|
|
|
|
130
|
2 |
|
return $this->_request->url() . '?' . \http_build_query($parameters); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
2 |
|
public function render(): string |
137
|
|
|
{ |
138
|
2 |
|
return '<form method="get" action="' . $this->_buildSearchUrl() . |
139
|
2 |
|
'"><div class="row"><div class="col-md-10"><input name="search" class="form-control data-scout-input" placeholder="' . |
140
|
2 |
|
$this->_placeholder . '"/></div><div class="col-md-2"><button type="submit" class="btn btn-primary">' . |
141
|
2 |
|
$this->_buttonText . '</button></div></div></form>'; |
142
|
|
|
} |
143
|
|
|
} |