Skip to content

Commit 214e633

Browse files
authored
Merge pull request #30 from oitmain-public/oitmain
Allow data and response formatting
2 parents 02df094 + 22d49a2 commit 214e633

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

src/DataTableAction.php

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
*/
2323
class DataTableAction extends Action
2424
{
25+
/**
26+
* GET or POST
27+
*
28+
* @var string
29+
*/
30+
public $requestMethod = "GET";
31+
2532
/**
2633
* @var ActiveQuery
2734
*/
@@ -43,44 +50,68 @@ class DataTableAction extends Action
4350
*/
4451
public $applyFilter;
4552

53+
/**
54+
* Format data
55+
* Signature is following:
56+
* function ($query, $columns)
57+
* @var callable
58+
*/
59+
public $formatData;
60+
61+
/**
62+
* Format response
63+
* Signature is following:
64+
* function ($response)
65+
* @var callable
66+
*/
67+
public $formatResponse;
68+
4669
public function init()
4770
{
4871
if ($this->query === null) {
4972
throw new InvalidConfigException(get_class($this) . '::$query must be set.');
5073
}
5174
}
5275

76+
protected function getParam($name, $defaultValue = null)
77+
{
78+
return $this->requestMethod == 'GET' ?
79+
Yii::$app->request->getQueryParam($name, $defaultValue) :
80+
Yii::$app->request->getBodyParam($name, $defaultValue);
81+
}
82+
5383
public function run()
5484
{
5585
/** @var ActiveQuery $originalQuery */
5686
$originalQuery = $this->query;
5787
$filterQuery = clone $originalQuery;
58-
$draw = Yii::$app->request->getQueryParam('draw');
88+
$draw = $this->getParam('draw');
5989
$filterQuery->where = null;
60-
$search = Yii::$app->request->getQueryParam('search', ['value' => null, 'regex' => false]);
61-
$columns = Yii::$app->request->getQueryParam('columns', []);
62-
$order = Yii::$app->request->getQueryParam('order', []);
90+
$search = $this->getParam('search', ['value' => null, 'regex' => false]);
91+
$columns = $this->getParam('columns', []);
92+
$order = $this->getParam('order', []);
6393
$filterQuery = $this->applyFilter($filterQuery, $columns, $search);
6494
$filterQuery = $this->applyOrder($filterQuery, $columns, $order);
6595
if (!empty($originalQuery->where)) {
6696
$filterQuery->andWhere($originalQuery->where);
6797
}
6898
$filterQuery
69-
->offset(Yii::$app->request->getQueryParam('start', 0))
70-
->limit(Yii::$app->request->getQueryParam('length', -1));
99+
->offset($this->getParam('start', 0))
100+
->limit($this->getParam('length', -1));
71101
$dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)]]);
72102
Yii::$app->response->format = Response::FORMAT_JSON;
73103
try {
74104
$response = [
75105
'draw' => (int)$draw,
76106
'recordsTotal' => (int)$originalQuery->count(),
77107
'recordsFiltered' => (int)$dataProvider->getTotalCount(),
78-
'data' => $filterQuery->all(),
108+
'data' => $this->formatData($filterQuery, $columns),
79109
];
80110
} catch (\Exception $e) {
81111
return ['error' => $e->getMessage()];
82112
}
83-
return $response;
113+
114+
return $this->formatResponse($response);
84115
}
85116

86117
/**
@@ -129,4 +160,31 @@ public function applyOrder(ActiveQuery $query, $columns, $order)
129160
}
130161
return $query;
131162
}
163+
164+
/**
165+
* @param ActiveQuery $query
166+
* @param array $columns
167+
* @return ActiveQuery
168+
*/
169+
public function formatData(ActiveQuery $query, $columns)
170+
{
171+
if ($this->formatData !== null) {
172+
return call_user_func($this->formatData, $query, $columns);
173+
}
174+
175+
return $query->all();
176+
}
177+
178+
/**
179+
* @param array $response
180+
* @return ActiveQuery
181+
*/
182+
public function formatResponse($response)
183+
{
184+
if ($this->formatResponse !== null) {
185+
return call_user_func($this->formatResponse, $response);
186+
}
187+
188+
return $response;
189+
}
132190
}

0 commit comments

Comments
 (0)