From 062adcb93705b638a5a5cdd464229ff52aa2263b Mon Sep 17 00:00:00 2001 From: Hermawan Ramadhan Date: Wed, 5 May 2021 14:34:00 +0700 Subject: [PATCH] version 0.4.0 - cleaner code - new feature postQuery --- src/Column.php | 14 + src/DataTable.php | 180 ++++++++++++- src/DataTableColumnDefs.php | 376 +++++++++++++++++++++++++++ src/DataTableQuery.php | 496 +++++++----------------------------- src/DataTableServerSide.php | 162 ------------ src/Request.php | 28 ++ 6 files changed, 685 insertions(+), 571 deletions(-) create mode 100644 src/Column.php create mode 100644 src/DataTableColumnDefs.php delete mode 100644 src/DataTableServerSide.php create mode 100644 src/Request.php diff --git a/src/Column.php b/src/Column.php new file mode 100644 index 0000000..34961d4 --- /dev/null +++ b/src/Column.php @@ -0,0 +1,14 @@ +query = new DataTableQuery($builder); + $this->columnDefs = new DataTableColumnDefs($builder); + + return $this; + } + /** * Make a DataTable instance from builder. * * Builder from CodeIgniter Query Builder * @param Builder $builder - * @return DataTableServerSide */ public static function of($builder) { - return new DataTableServerSide($builder); + return new self($builder); } + /** + * postQuery + * @param Closure $postQuery + */ + public function postQuery($postQuery) + { + $this->query->postQuery($postQuery); + return $this; + } /** - * Get DataTable Request - * - * @param String $requestName - * @return String|Array + * custom Filter + * @param Closure function */ + public function filter($filterFunction) + { + $this->query->filter($filterFunction); + return $this; + } - public static function request($requestName = NULL) + /** + * Add numbering to first column + * @param String $column + */ + public function addNumbering($column = NULL) + { + $this->columnDefs->addNumbering($column); + return $this; + } + + + /** + * Add extra column + * + * @param String $column + * @param Closure $callback + * @param String|int $position + */ + public function add($column, $callback, $position = 'last') { - $request = Services::request(); - if($requestName !== NULL) - return $request->getGetPost($requestName); + $this->columnDefs->add($column, $callback, $position); + return $this; + } + + /** + * Edit column + * + * @param String $column + * @param Closure $callback + */ + public function edit($column, $callback) + { + $this->columnDefs->edit($column, $callback); + return $this; + } + + /** + * Format column + * + * @param String $column + * @param Closure $callback + */ + public function format($column, $callback) + { + $this->columnDefs->format($column, $callback); + return $this; + } + + /** + * Hide column + * + * @param String $column + */ + public function hide($column) + { + $this->columnDefs->remove($column); + return $this; + } + + /** + * Set Searchable columns + * @param String|Array + */ + public function setSearchableColumns($columns) + { + $this->columnDefs->setSearchable($columns); + return $this; + } + + /** + * Add Searchable columns + * @param String|Array + */ + public function addSearchableColumns($columns) + { + $this->columnDefs->addSearchable($columns); + return $this; + } + + + + /** + * Return JSON output + * + * @param bool $returnAsObject + * @return JSON + */ + public function toJson($returnAsObject = NULL) + { + + if(! Request::get('draw')) + { + return self::throwError('no datatable request detected'); + } + + if($returnAsObject !== NULL) + $this->columnDefs->returnAsObject($returnAsObject); + + $this->query->setColumnDefs($this->columnDefs); - return (Object) (($request->getMethod() == 'get') ? $request->getGet() : $request->getPost()); + $response = Services::response(); + return $response->setJSON([ + 'draw' => Request::get('draw'), + 'recordsTotal' => $this->query->countAll(), + 'recordsFiltered' => $this->query->countFiltered(), + 'data' => $this->query->getDataResult(), + + ]); } + + + /** + * Throw Error + * + * @param String $message + * @return Error + */ + public static function throwError($message) + { + $response = Services::response(); + return $response->setJSON([ + 'error' => $message, + + ]); + } + + } // End of DataTables Library Class. diff --git a/src/DataTableColumnDefs.php b/src/DataTableColumnDefs.php new file mode 100644 index 0000000..22b5a2e --- /dev/null +++ b/src/DataTableColumnDefs.php @@ -0,0 +1,376 @@ +initFromBuilder($builder); + } + + public function returnAsObject($returnAsObject) + { + $this->returnAsObject = $returnAsObject; + return $this; + } + + + public function addNumbering($key) + { + $column = new Column(); + + $column->key = $key; + $column->alias = $key; + $column->type = 'numbering'; + $column->searchable = FALSE; + $column->orderable = FALSE; + + array_unshift($this->columns, $column); + + } + + public function add($key, $callback, $position) + { + $column = new Column(); + + $column->key = $key; + $column->alias = $key; + $column->type = 'add'; + $column->searchable = FALSE; + $column->orderable = FALSE; + $column->callback = $callback; + + switch ($position) { + + case 'first': + array_unshift($this->columns, $column); + break; + + case 'last': + $this->columns[] = $column; + break; + + default: + array_splice( $this->columns, $position, 0, $column); + break; + } + + + } + + public function edit($alias, $callback) + { + if($alias) + { + $column = $this->getColumnBy('alias', $alias); + if(is_object($column)) + { + $column->type = 'edit'; + $column->callback = $callback; + } + + } + + } + + public function format($alias, $callback) + { + if($alias) + { + $column = $this->getColumnBy('alias', $alias); + if(is_object($column)) + { + $column->type = 'format'; + $column->callback = $callback; + } + + } + + } + + public function remove($alias) + { + if(! is_array($alias)) + $aliases = [$alias]; + + foreach ($this->columns as $index => $column) + { + if(in_array($column->alias, $aliases)) + { + unset($this->columns[$index]); + } + } + + $this->columns = array_values($this->columns); + } + + public function setSearchable($searchable) + { + $this->searchableColumns = $searchable; + return $this; + } + + public function addSearchable($searchable) + { + if(is_array($searchable)) + $this->searchableColumns = array_merge($this->searchableColumns, $searchable); + else + $this->searchableColumns[] = $searchable; + } + + public function getColumns() + { + return $this->columns; + } + + public function getOrderables() + { + $orderableColumns = []; + + if($this->returnAsObject) + { + foreach (Request::get('columns') as $columnRequest) + { + if($columnRequest['name']) + $orderableColumns[] = $columnRequest['name']; + + elseif($column = $this->getColumnBy('alias', $columnRequest['data'])) + $orderableColumns[] = $column->alias; + + else + $orderableColumns[] = $columnRequest['data']; + + } + + } + else + { + foreach ($this->columns as $column) + $orderableColumns[] = $column->orderable ? $column->alias : NULL; + } + + return $orderableColumns; + + } + + public function getSearchRequest($index, $request) + { + + if($this->returnAsObject) + { + if($request['name']) + return trim($request['name']); + + elseif($column = $this->getColumnBy('alias', $request['data'])) + return $column->key; + + else + return $request['data']; + } + else + { + return $this->columns[$index]->key; + } + } + + public function getSearchable() + { + if($this->searchableColumns !== NULL) + { + return $this->searchableColumns; + } + else + { + $searchableColumns = []; + + foreach (Request::get('columns') as $index => $request) + { + if($request['searchable'] == 'true') + { + if($this->returnAsObject) + { + if($request['name']) + $searchableColumns[] = trim($request['name']); + + elseif($column = $this->getColumnBy('alias', $request['data'])) + { + if($column->searchable) + $searchableColumns[] = $column->key; + } + + else + $searchableColumns[] = $request['data']; + } + else + { + $column = $this->columns[$index]; + if($column->searchable) + $searchableColumns[] = $column->key; + + } + + } + } + + return $searchableColumns; + + } + + + } + + public function getNumbering() + { + $this->number = $this->number === NULL ? Request::get('start')+1 : $this->number+1; + return $this->number; + } + + + public function initFromBuilder($builder) + { + + $QBSelect = Helper::getObjectPropertyValue($builder, 'QBSelect'); + + if( ! empty($QBSelect) ) + { + $baseSQL = $builder->getCompiledSelect(false); + $parser = new \PHPSQLParser\PHPSQLParser(); + $sqlParsed = $parser->parse($baseSQL); + + foreach ($sqlParsed['SELECT'] as $index => $selectParsed) + { + + $column = new Column(); + + // if select column + if ($selectParsed['expr_type'] == 'colref') + { + + //if have select all (*) query + if(strpos($selectParsed['base_expr'], '*') !== FALSE) + { + $fieldData = $builder->db()->getFieldData($selectParsed['no_quotes']['parts'][0]); + foreach ($fieldData as $field) + { + + $key = $selectParsed['no_quotes']['parts'][0].'.'.$field->name; + $alias = $field->name; + + } + + } + else + { + + $alias = ! empty($selectParsed['alias']) ? end($selectParsed['alias']['no_quotes']['parts']) : end($selectParsed['no_quotes']['parts']); + + $key = $selectParsed['base_expr']; + $alias = $alias; + + } + + } + elseif ($selectParsed['expr_type'] == 'function') + { + + $key = $selectParsed['base_expr']; + $key .= '('; + + $arrayKey = []; + + foreach ($selectParsed['sub_tree'] as $sub_tree) + $arrayKey[] = $sub_tree['base_expr']; + + $key .= implode($selectParsed['delim'].' ', $arrayKey); + $key .= ')'; + + $alias = ! empty($selectParsed['alias']) ? end($selectParsed['alias']['no_quotes']['parts']) : $key; + + + } + else + { + + if( ! empty($selectParsed['alias']) ) + { + $key = substr($QBSelect[$index], 0,-1*(strlen($selectParsed['alias']['base_expr']))); + $alias = end($selectParsed['alias']['no_quotes']['parts']); + } + else + { + $key = $QBSelect[$index]; + $alias = $key; + + } + + } + + $column->key = $key; + $column->alias = $alias; + $this->columns[] = $column; + + } + + } + else + { + + $QBFrom = Helper::getObjectPropertyValue($builder, 'QBFrom'); + $QBJoin = Helper::getObjectPropertyValue($builder, 'QBJoin'); + + foreach ($QBFrom as $table) + { + $fieldData = $builder->db()->getFieldData($table); + foreach ($fieldData as $field) + { + + $column = new Column(); + + $column->key = $table.'.'.$field->name; + $column->alias = $field->name; + + $this->columns[] = $column; + + } + } + + foreach ($QBJoin as $table) + { + $fieldData = $builder->db()->getFieldData($table); + foreach ($fieldData as $field) + { + $column = new Column(); + + $column->key = $table.'.'.$field->name; + $column->alias = $field->name; + + $this->columns[] = $column; + } + } + + } + + return $this; + } + + public function getColumnBy($by, $value) + { + foreach ($this->columns as $column) + { + if($column->$by == $value) + return $column; + } + return NULL; + } + + +} // End of DataTableColumnDefs Class. diff --git a/src/DataTableQuery.php b/src/DataTableQuery.php index b39641f..0d609d4 100644 --- a/src/DataTableQuery.php +++ b/src/DataTableQuery.php @@ -1,99 +1,44 @@ builder = $builder; - } + private $columnDefs; - /* Modified column */ + private $filter; - public function addNumbering($column) - { - $this->numbering = TRUE; - $this->numberingColumn = $column; + private $postQuery; - if($column !== NULL) - $this->extraColumn[] = $column; - - } - - public function filter($filterFunction) - { - $this->filterFunction = $filterFunction; - } - - public function setSearchableColumns($columns) - { - $this->searchableColumns = $columns; - } + private $countResult; + + private $doQueryFilter = FALSE; - public function addSearchableColumns($columns) - { - if(is_array($columns)) - $this->addedSearchableColumns = array_merge($this->addedSearchableColumns, $columns); - else - $this->addedSearchableColumns[] = $columns; - } - public function addColumn($column, $callback, $position) + public function __construct($builder) { - $this->columnsAdded[] = [ - 'name' => $column, - 'callback' => $callback, - 'position' => $position - ]; - - $this->extraColumn[] = $column; + $this->builder = $builder; } - public function editColumn($column, $callback) + public function setColumnDefs($columnDefs) { - $this->columnsEdited[$column] = $callback; + $this->columnDefs = $columnDefs; + return $this; } - public function formatColumn($column, $callback) + public function postQuery($postQuery) { - $this->columnsFormatted[$column] = $callback; + $this->postQuery = $postQuery; } - public function removeColumn($column) + public function filter($filter) { - if(is_array($column)) - $this->columnsRemoved = array_merge($this->columnsRemoved, $column); - else - $this->columnsRemoved[] = $column; + $this->filter = $filter; } - public function returnAsObject($returnAsObject) - { - $this->returnAsObject = $returnAsObject; - } /* End Modified column */ @@ -111,104 +56,63 @@ public function countAll() public function countFiltered() { - $builder = clone $this->builder; - $doQuerying = $this->queryFilterSearch($builder); + $builder = clone $this->builder; + + $this->queryFilterSearch($builder); - $this->countResult = ($this->countResult !== NULL && ! $doQuerying) ? $this->countResult : $builder->countAllResults(); + $this->countResult = ($this->countResult !== NULL && ! $this->doQueryFilter) ? $this->countResult : $builder->countAllResults(); return $this->countResult; } public function getDataResult() { - $result = $this->queryResult(); - - $dataResult = []; + $queryResult = $this->queryResult(); + $result = []; - if($this->returnAsObject) + foreach ($queryResult as $row) { - $number = DataTable::request('start'); - foreach (DataTable::request('columns') as $column) - $columns[] = $column['data']; + $data = []; + $columns = $this->columnDefs->getColumns(); - foreach ($result as $row) + foreach ($columns as $column) { - $number++; - $data = []; - - if($this->numbering && $this->numberingColumn !== NULL) - $data[$this->numberingColumn] = $number; - - foreach ($row as $key => $value) - { - if(! in_array($key, $this->columnsRemoved)) - { - if(isset($this->columnsEdited[$key])) - $data[$key] = $this->columnsEdited[$key]($row); - elseif(isset($this->columnsFormatted[$key])) - $data[$key] = $this->columnsFormatted[$key]($value); - else - $data[$key] = $value; - } - + switch ($column->type) { + case 'numbering': + $value = $this->columnDefs->getNumbering(); + break; + + case 'add': + $callback = $column->callback; + $value = $callback($row); + break; + + case 'edit': + $callback = $column->callback; + $value = $callback($row); + break; + + case 'format': + $callback = $column->callback; + $value = $callback($row->{$column->alias}); + break; + + default: + $value = $row->{$column->alias}; + break; } - foreach ($this->columnsAdded as $column) - $data[$column['name']] = $column['callback']($row); - - $dataResult[] = $data; - - } - } - else - { - $number = DataTable::request('start'); - foreach ($result as $row) - { - $number++; + if($this->columnDefs->returnAsObject) + $data[$column->alias] = $value; + else + $data[] = $value; - $data = []; - foreach ($row as $key => $value) - { - if(! in_array($key, $this->columnsRemoved)) - { - if(isset($this->columnsEdited[$key])) - $data[] = $this->columnsEdited[$key]($row); - elseif(isset($this->columnsFormatted[$key])) - $data[] = $this->columnsFormatted[$key]($value); - else - $data[] = $value; - } - - } - - foreach ($this->columnsAdded as $column) - { - switch ($column['position']) { - case 'first': - array_unshift($data, $column['callback']($row)); - break; - case 'last': - $data[] = $column['callback']($row); - break; - default: - array_splice( $data, $column['position'], 0, [$column['callback']($row)]); - break; - } - - } - - if($this->numbering) - array_unshift($data, $number); - - $data = array_values($data); - - $dataResult[] = $data; } + $result[] = $data; } - - return $dataResult; + + return $result; } /* End Generating result */ @@ -218,42 +122,20 @@ public function getDataResult() /* Querying */ private function queryOrder($builder) - { - $dtOrders = DataTable::request('order'); - if($dtOrders) - { - - $orderableColumns = []; - $columns = $this->getColumns(); - - if($this->returnAsObject) - { - foreach (DataTable::request('columns') as $dtColumn) - { - if($dtColumn['name']) - $orderableColumns[] = $dtColumn['name']; - - elseif(in_array($dtColumn['data'], $columns)) - $orderableColumns[] = array_search($dtColumn['data'], $columns); - - else - $orderableColumns[] = $dtColumn['data']; - - } - } - else - { - foreach ($columns as $column => $alias) - $orderableColumns[] = $column; - } + { - foreach ($dtOrders as $order) + $orderables = $this->columnDefs->getOrderables(); + $oderColumnRequests = Request::get('order'); + + if($oderColumnRequests) + { + foreach ($oderColumnRequests as $request) { + $dir = ($request['dir'] == 'desc') ? 'desc' : 'asc'; + $column = $orderables[$request['column']] ?? NULL; - $orderIndex = $order['column'] - ($this->numbering && ! $this->returnAsObject ? 1 : 0); - - if( $orderIndex >= 0 && $orderIndex < count($orderableColumns) && ! in_array($orderableColumns[$orderIndex], $this->extraColumn)) - $builder->orderBy($orderableColumns[$orderIndex], $order['dir']); + if( $column !== NULL) + $builder->orderBy($column, $dir); } } @@ -261,261 +143,79 @@ private function queryOrder($builder) private function queryFilterSearch($builder) { - $doQuerying = FALSE; - - $columns = $this->getColumns(); //individual column search (multi column search) - foreach (DataTable::request('columns') as $index => $dtColumn) + $columnRequests = Request::get('columns'); + foreach ($columnRequests as $index => $request) { - if($dtColumn['search']['value'] != '') + if($request['search']['value'] != '') { + $column = $this->columnDefs->getSearchRequest($index, $request); + $this->doQueryFilter = TRUE; - if($this->returnAsObject) - { - if($dtColumn['name']) - $column = trim($dtColumn['name']); - elseif(in_array($dtColumn['data'], $columns)) - $column = array_search($dtColumn['data'], $columns); - - else - $column = $dtColumn['data']; - } - else - { - $keyColumns = array_keys($columns); - $column = $keyColumns[$index]; - } - - - - $builder->like(trim($column), $dtColumn['search']['value']); - $doQuerying = TRUE; + $builder->like($column, $request['search']['value']); } } //global search - $dtSearch = DataTable::request('search'); - $searchValue = $dtSearch['value']; + $searchRequest = Request::get('search'); - if($searchValue != '') + if($searchRequest['value'] != '') { + $searchable = $this->columnDefs->getSearchable(); - if($this->searchableColumns !== NULL) + if(! empty($searchable)) { - $searchableColumns = is_array($this->searchableColumns) - ? $this->searchableColumns - : explode(",",$this->searchableColumns); - } - else - { - - if($this->returnAsObject) - { - foreach (DataTable::request('columns') as $dtColumn) - { - if($dtColumn['name']) - $searchableColumns[] = $dtColumn['name']; - - elseif(in_array($dtColumn['data'], $columns)) - $searchableColumns[] = array_search($dtColumn['data'], $columns); - - else - $searchableColumns[] = $dtColumn['data']; - - } - } - else - { - foreach ($columns as $column => $alias) - $searchableColumns[] = $column; - } - - if(! empty($this->addedSearchableColumns)) - $searchableColumns = array_merge($searchableColumns, $this->addedSearchableColumns); - } - - if(! empty($searchableColumns)) - { - $doQuerying = TRUE; - $dtColumns = DataTable::request('columns'); + $this->doQueryFilter = TRUE; $builder->groupStart(); - foreach ($searchableColumns as $index => $column) - { - if($column != '' - && ! in_array($column, $this->extraColumn) - && ($this->searchableColumns !== NULL - || $dtColumns[$index]['searchable'] === 'true')) - { - $builder->orLike(trim($column), $searchValue); - } - } + foreach ($searchable as $column) + $builder->orLike(trim($column), $searchRequest['value']); + $builder->groupEnd(); } } - if($this->filterFunction !== NULL) + $this->queryFilter($builder); + } + + private function queryFilter($builder) + { + if($this->filter !== NULL) { $testBuilder = clone $builder; - $callback = $this->filterFunction; - $callback($builder, DataTable::request()); + $callback = $this->filter; + $callback($builder, Request::get()); if($testBuilder != $builder) - $doQuerying = TRUE; + $this->doQueryFilter = TRUE; } - - return $doQuerying; } private function queryResult() { $builder = clone $this->builder; + $this->queryOrder($builder); - if(DataTable::request('length') != -1) - $builder->limit(DataTable::request('length'), DataTable::request('start')); + if(Request::get('length') != -1) + $builder->limit(Request::get('length'), Request::get('start')); $this->queryFilterSearch($builder); - return $builder->get()->getResult(); - } - - /* End Querying */ - - private function getBaseSQL() - { - if($this->baseSQL === NULL) - $this->baseSQL = $this->builder->getCompiledSelect(false); - - return $this->baseSQL; - } - - private function getBaseSQLParsed() - { - if($this->baseSQLParsed === NULL) + if($this->postQuery !== NULL) { - $baseSQL = $this->getBaseSQL(); - $parser = new PHPSQLParser(); - - $this->baseSQLParsed = $parser->parse($baseSQL); + $callback = $this->postQuery; + $callback($builder); } - - return $this->baseSQLParsed; - } - - private function getColumns() //for query or for result - { - - if($this->columns !== NULL) - return $this->columns; - - $columns = []; - - $builder = $this->builder; - $QBSelect = Helper::getObjectPropertyValue($builder, 'QBSelect'); - $QBFrom = Helper::getObjectPropertyValue($builder, 'QBFrom'); - $QBJoin = Helper::getObjectPropertyValue($builder, 'QBJoin'); - - if( ! empty($QBSelect) ) - { - - $sqlParsed = $this->getBaseSQLParsed(); - - foreach ($sqlParsed['SELECT'] as $index => $select) - { - - // if select column - if ($select['expr_type'] == 'colref') - { - //if have select all (*) query - if(strpos($select['base_expr'], '*') !== FALSE) - { - $fieldData = $this->getTableField($select['no_quotes']['parts'][0]); - foreach ($fieldData as $field) - { - if( ! in_array($field->name, $this->columnsRemoved)) - $columns[$table.'.'.$field->name] = $field->name; - } - - } - else - { - - $fieldName = (! empty($select['alias']) ? end($select['alias']['no_quotes']['parts']) : end($select['no_quotes']['parts']) ); - - if( ! in_array($fieldName, $this->columnsRemoved)) - $columns[$select['base_expr']] = $fieldName; - } - - } - else - { - $column = $QBSelect[$index]; - - if(! empty($select['alias']) - && substr($column, -1*(strlen($select['alias']['base_expr']))) == $select['alias']['base_expr']) - { - $column = substr($column, 0,-1*(strlen($select['alias']['base_expr']))); - $alias = end($select['alias']['no_quotes']['parts']); - }else - $alias = $column; - - $columns[$column] = $alias; - } - - } - - } - else - { - - foreach ($QBFrom as $table) - { - $fieldData = $this->getTableField($table); - foreach ($fieldData as $field) - { - if( ! in_array($field->name, $this->columnsRemoved)) - $columns[$table.'.'.$field->name] = $field->name; - } - } - - foreach ($QBJoin as $table) - { - $fieldData = $this->getTableField($table); - foreach ($fieldData as $field) - { - if( ! in_array($field->name, $this->columnsRemoved)) - $columns[$table.'.'.$field->name] = $field->name; - } - } - - } - - - foreach ($this->columnsAdded as $column) - { - $columns[$column['name']] = $column['name']; - } - - $this->columns = $columns; - return $columns; + return $builder->get()->getResult(); } - private function getTableField($table) - { - if( ! array_key_exists($table, $this->tableField)) - { - $db = \Config\Database::connect(); - $this->tableField[$table] = $db->getFieldData($table); - } - return $this->tableField[$table]; - - } + /* End Querying */ -} // End of DataTableQuery Library Class. +} // End of DataTableQuery Class. diff --git a/src/DataTableServerSide.php b/src/DataTableServerSide.php deleted file mode 100644 index 1966a12..0000000 --- a/src/DataTableServerSide.php +++ /dev/null @@ -1,162 +0,0 @@ -query = new DataTableQuery($builder); - return $this; - } - - /** - * Add numbering to first column - * @param String $column - */ - public function addNumbering($column = NULL) - { - $this->query->addNumbering($column); - return $this; - } - - /** - * custom Filter - * @param callback function - */ - public function filter($filterFunction) - { - $this->query->filter($filterFunction); - return $this; - } - - - /** - * Set Searchable columns - * @param String|Array - */ - public function setSearchableColumns($columns) - { - $this->query->setSearchableColumns($columns); - return $this; - } - - /** - * Add Searchable columns - * @param String|Array - */ - public function addSearchableColumns($columns) - { - $this->query->addSearchableColumns($columns); - return $this; - } - - /** - * Add extra column - * - * @param String $column - * @param Callback $callback - * @param String|int $position - */ - public function add($column, $callback, $position = 'last') - { - $this->query->addColumn($column, $callback, $position); - return $this; - } - - /** - * Edit column - * - * @param String $column - * @param Callback $callback - */ - public function edit($column, $callback) - { - $this->query->editColumn($column, $callback); - return $this; - } - - /** - * Format column - * - * @param String $column - * @param Callback $callback - */ - public function format($column, $callback) - { - $this->query->formatColumn($column, $callback); - return $this; - } - - /** - * Hide column - * - * @param String $column - */ - public function hide($column) - { - $this->query->removeColumn($column); - return $this; - } - - - /** - * Return JSON output - * - * @param bool $returnAsObject - * @return JSON - */ - public function toJson($returnAsObject = NULL) - { - - if(! DataTable::request('draw')) - { - return DataTableServerSide::throwError('no datatable request detected'); - } - - if($returnAsObject !== NULL) - $this->query->returnAsObject($returnAsObject); - - $response = Services::response(); - - return $response->setJSON([ - 'draw' => DataTable::request('draw'), - 'recordsTotal' => $this->query->countAll(), - 'recordsFiltered' => $this->query->countFiltered(), - 'data' => $this->query->getDataResult(), - - ]); - } - - - /** - * Throw Error - * - * @param String $message - * @return Error - */ - public static function throwError($message) - { - $response = Services::response(); - return $response->setJSON([ - 'error' => $message, - - ]); - } - - -} // End of DataTableServerSide Class. diff --git a/src/Request.php b/src/Request.php new file mode 100644 index 0000000..94239e9 --- /dev/null +++ b/src/Request.php @@ -0,0 +1,28 @@ +getGetPost($requestName); + + return (Object) (($request->getMethod() == 'get') ? $request->getGet() : $request->getPost()); + + } + + +} \ No newline at end of file