1 | <?php |
||
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) |
|
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) |
|
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) |
|
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) |
|
110 | |||
111 | /** |
||
112 | * @param string $field |
||
113 | * @return $this |
||
114 | */ |
||
115 | 1 | public function makeSearchable(string $field) |
|
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 2 | private function _buildSearchUrl() |
|
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | 2 | public function render(): string |
|
143 | } |