Skip to content

Commit 02302d2

Browse files
committed
Merge remote-tracking branch 'upstream/master'
Conflicts: src/Chumper/Datatable/Engines/QueryEngine.php
2 parents dfb7998 + 2b5f7bb commit 02302d2

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

src/Chumper/Datatable/Engines/BaseEngine.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,21 @@ public function showColumns($cols)
230230
}
231231

232232
/**
233-
* @return \Illuminate\Http\JsonResponse
233+
* Used to handle all the inputs directly from an engine, instead of from Datatables.
234+
* @see QueryEngine
234235
*/
235-
public function make()
236+
protected function prepareEngine()
236237
{
237-
//TODO Handle all inputs
238238
$this->handleInputs();
239239
$this->prepareSearchColumns();
240+
}
241+
242+
/**
243+
* @return \Illuminate\Http\JsonResponse
244+
*/
245+
public function make()
246+
{
247+
$this->prepareEngine();
240248

241249
$output = array(
242250
"aaData" => $this->internalMake($this->columns, $this->searchColumns)->toArray(),

src/Chumper/Datatable/Engines/QueryEngine.php

+53-13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class QueryEngine extends BaseEngine {
4242
'noGroupByOnCount' => false,
4343
'distinctCountGroup'=> false,
4444
'emptyAtEnd' => false,
45+
'returnQuery' => false,
4546
);
4647

4748
function __construct($builder)
@@ -96,9 +97,9 @@ public function setSearchOperator($value = "LIKE")
9697
return $this;
9798
}
9899

99-
public function setSearchWithAlias()
100+
public function setSearchWithAlias($value = true)
100101
{
101-
$this->options['searchWithAlias'] = true;
102+
$this->options['searchWithAlias'] = (bool)$value;
102103
return $this;
103104
}
104105

@@ -108,9 +109,9 @@ public function setEmptyAtEnd()
108109
return $this;
109110
}
110111

111-
public function setNoGroupByOnCount()
112+
public function setNoGroupByOnCount($value = true)
112113
{
113-
$this->options['noGroupByOnCount'] = true;
114+
$this->options['noGroupByOnCount'] = (bool)$value;
114115
return $this;
115116
}
116117

@@ -121,14 +122,40 @@ public function setNoGroupByOnCount()
121122
*
122123
* Instead of counting all of the rows, the distinct rows in the group by will be counted instead.
123124
*
125+
* @param bool $value should this option be enabled?
124126
* @return $this
125127
*/
126128
public function setDistinctCountGroup($value = true)
127129
{
128-
$this->options['distinctCountGroup'] = $value;
130+
$this->options['distinctCountGroup'] = (bool)$value;
129131
return $this;
130132
}
131133

134+
/**
135+
* Let internalMake return a QueryBuilder, instead of a collection.
136+
*
137+
* @param bool $value
138+
* @return $this
139+
*/
140+
public function setReturnQuery($value = true)
141+
{
142+
$this->options['returnQuery'] = $value;
143+
return $this;
144+
}
145+
146+
/**
147+
* Get a Builder object back from the engine. Don't return a collection.
148+
*
149+
* @return Query\Builder
150+
*/
151+
public function getQueryBuilder()
152+
{
153+
$this->prepareEngine();
154+
$this->setReturnQuery();
155+
156+
return $this->internalMake($this->columns, $this->searchColumns);
157+
}
158+
132159
//--------PRIVATE FUNCTIONS
133160

134161
protected function internalMake(Collection $columns, array $searchColumns = array())
@@ -165,6 +192,10 @@ protected function internalMake(Collection $columns, array $searchColumns = arra
165192
}
166193

167194
$builder = $this->doInternalOrder($builder, $columns);
195+
196+
if ($this->options['returnQuery'])
197+
return $this->getQuery($builder);
198+
168199
$collection = $this->compile($builder, $columns);
169200

170201
return $collection;
@@ -174,19 +205,27 @@ protected function internalMake(Collection $columns, array $searchColumns = arra
174205
* @param $builder
175206
* @return Collection
176207
*/
177-
private function getCollection($builder)
208+
private function getQuery($builder)
178209
{
179-
if($this->collection == null)
180-
{
181-
if($this->skip > 0)
182-
{
210+
// dd($builder->toSql());
211+
if ($this->collection == null) {
212+
if ($this->skip > 0) {
183213
$builder = $builder->skip($this->skip);
184214
}
185-
if($this->limit > 0)
186-
{
215+
if ($this->limit > 0) {
187216
$builder = $builder->take($this->limit);
188217
}
189-
//dd($this->builder->toSql());
218+
}
219+
220+
return $builder;
221+
}
222+
223+
private function getCollection($builder)
224+
{
225+
$builder = $this->getQuery($builder);
226+
227+
if ($this->collection == null)
228+
{
190229
$this->collection = $builder->get();
191230

192231
if(is_array($this->collection))
@@ -207,6 +246,7 @@ private function doInternalSearch($builder, $columns)
207246

208247
return $builder;
209248
}
249+
210250
private function buildSearchQuery($builder, $columns)
211251
{
212252
$like = $this->options['searchOperator'];

0 commit comments

Comments
 (0)