diff --git a/src/DataTableAction.php b/src/DataTableAction.php index 8f726e1..90ce666 100644 --- a/src/DataTableAction.php +++ b/src/DataTableAction.php @@ -22,6 +22,13 @@ */ class DataTableAction extends Action { + /** + * GET or POST + * + * @var string + */ + public $requestMethod = "GET"; + /** * @var ActiveQuery */ @@ -43,6 +50,22 @@ class DataTableAction extends Action */ public $applyFilter; + /** + * Format data + * Signature is following: + * function ($query, $columns) + * @var callable + */ + public $formatData; + + /** + * Format response + * Signature is following: + * function ($response) + * @var callable + */ + public $formatResponse; + public function init() { if ($this->query === null) { @@ -50,24 +73,31 @@ public function init() } } + protected function getParam($name, $defaultValue = null) + { + return $this->requestMethod == 'GET' ? + Yii::$app->request->getQueryParam($name, $defaultValue) : + Yii::$app->request->getBodyParam($name, $defaultValue); + } + public function run() { /** @var ActiveQuery $originalQuery */ $originalQuery = $this->query; $filterQuery = clone $originalQuery; - $draw = Yii::$app->request->getQueryParam('draw'); + $draw = $this->getParam('draw'); $filterQuery->where = null; - $search = Yii::$app->request->getQueryParam('search', ['value' => null, 'regex' => false]); - $columns = Yii::$app->request->getQueryParam('columns', []); - $order = Yii::$app->request->getQueryParam('order', []); + $search = $this->getParam('search', ['value' => null, 'regex' => false]); + $columns = $this->getParam('columns', []); + $order = $this->getParam('order', []); $filterQuery = $this->applyFilter($filterQuery, $columns, $search); $filterQuery = $this->applyOrder($filterQuery, $columns, $order); if (!empty($originalQuery->where)) { $filterQuery->andWhere($originalQuery->where); } $filterQuery - ->offset(Yii::$app->request->getQueryParam('start', 0)) - ->limit(Yii::$app->request->getQueryParam('length', -1)); + ->offset($this->getParam('start', 0)) + ->limit($this->getParam('length', -1)); $dataProvider = new ActiveDataProvider(['query' => $filterQuery, 'pagination' => ['pageSize' => Yii::$app->request->getQueryParam('length', 10)]]); Yii::$app->response->format = Response::FORMAT_JSON; try { @@ -75,12 +105,13 @@ public function run() 'draw' => (int)$draw, 'recordsTotal' => (int)$originalQuery->count(), 'recordsFiltered' => (int)$dataProvider->getTotalCount(), - 'data' => $filterQuery->all(), + 'data' => $this->formatData($filterQuery, $columns), ]; } catch (\Exception $e) { return ['error' => $e->getMessage()]; } - return $response; + + return $this->formatResponse($response); } /** @@ -129,4 +160,31 @@ public function applyOrder(ActiveQuery $query, $columns, $order) } return $query; } + + /** + * @param ActiveQuery $query + * @param array $columns + * @return ActiveQuery + */ + public function formatData(ActiveQuery $query, $columns) + { + if ($this->formatData !== null) { + return call_user_func($this->formatData, $query, $columns); + } + + return $query->all(); + } + + /** + * @param array $response + * @return ActiveQuery + */ + public function formatResponse($response) + { + if ($this->formatResponse !== null) { + return call_user_func($this->formatResponse, $response); + } + + return $response; + } }