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
|
|
|
/** |
40
|
|
|
* @return Builder |
41
|
|
|
*/ |
42
|
5 |
|
public function _shapeData() : Builder |
43
|
|
|
{ |
44
|
5 |
|
if (\count($this->_searchQueries) === 0) |
45
|
|
|
{ |
46
|
1 |
|
return $this->_queryBuilder; |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
$this->_queryBuilder->where( |
50
|
4 |
|
function($query) |
51
|
|
|
{ |
52
|
4 |
|
foreach ($this->_searchQueries as $value) |
53
|
|
|
{ |
54
|
4 |
|
foreach ($this->_searchableFields as $field) |
55
|
|
|
{ |
56
|
4 |
|
$query->orWhere($field, 'like', '%' . $value . '%'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
4 |
|
} |
60
|
|
|
); |
61
|
|
|
|
62
|
4 |
|
return $this->_queryBuilder; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Add a query programmatically. |
67
|
|
|
* |
68
|
|
|
* @param string $value |
69
|
|
|
* @return DataScout |
70
|
|
|
*/ |
71
|
3 |
|
public function addQuery(string $value) : DataScout |
72
|
|
|
{ |
73
|
3 |
|
$this->_searchQueries[] = $value; |
74
|
|
|
|
75
|
3 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the text for the search button. |
80
|
|
|
* |
81
|
|
|
* @param string $text |
82
|
|
|
* @return DataScout |
83
|
|
|
*/ |
84
|
1 |
|
public function buttonText(string $text) : DataScout |
85
|
|
|
{ |
86
|
1 |
|
$this->_buttonText = $text; |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the placeholder for the input. |
93
|
|
|
* |
94
|
|
|
* @param string $text |
95
|
|
|
* @return DataScout |
96
|
|
|
*/ |
97
|
1 |
|
public function placeholder(string $text) : DataScout |
98
|
|
|
{ |
99
|
1 |
|
$this->_placeholder = $text; |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Make the field searchable. |
106
|
|
|
* |
107
|
|
|
* @param string $field |
108
|
|
|
* @return DataScout |
109
|
|
|
*/ |
110
|
1 |
|
public function makeSearchable(string $field) : DataScout |
111
|
|
|
{ |
112
|
1 |
|
$this->_searchableFields[] = $field; |
113
|
|
|
|
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
2 |
|
public function render() : string |
121
|
|
|
{ |
122
|
2 |
|
return '<form method="get" action="' . $this->_buildSearchUrl() . |
123
|
2 |
|
'"><div class="row"><div class="col-md-10"><input name="search" class="form-control data-scout-input" placeholder="' . |
124
|
2 |
|
$this->_placeholder . '"/></div><div class="col-md-2"><button type="submit" class="btn btn-primary">' . |
125
|
2 |
|
$this->_buttonText . '</button></div></div></form>'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
2 |
|
private function _buildSearchUrl() : string |
132
|
|
|
{ |
133
|
2 |
|
$parameters = UrlHelper::queryParameters(); |
134
|
2 |
|
$parameters['search'] = \implode(',', $this->_searchQueries); |
135
|
|
|
|
136
|
2 |
|
return \request()->url() . '?' . \http_build_query($parameters); |
137
|
|
|
} |
138
|
|
|
|
139
|
7 |
|
protected function _afterInit() : void |
140
|
|
|
{ |
141
|
7 |
|
$search = \request()->get('search'); |
142
|
7 |
|
if (!empty($search)) |
143
|
|
|
{ |
144
|
1 |
|
$this->_searchQueries += \explode(',', $search); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |