Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new feature: using model methods for table render with custom formatting. #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 121 additions & 93 deletions src/Adapters/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,140 @@

use DataTables\ParamsParser;

abstract class AdapterInterface {

protected $parser = null;
protected $columns = [];
protected $lentgh = 30;

public function __construct($length) {
$this->length = $length;
}

abstract public function getResponse();

public function setParser(ParamsParser $parser) {
$this->parser = $parser;
}

public function setColumns(array $columns) {
$this->columns = $columns;
}

public function getColumns() {
return $this->columns;
}

public function columnExists($column) {
return in_array($column, $this->columns);
}

public function getParser() {
return $this->parser;
}

public function formResponse($options) {
$defaults = [
'total' => 0,
'filtered' => 0,
'data' => []
];
$options += $defaults;

$response = [];
$response['draw'] = $this->parser->getDraw();
$response['recordsTotal'] = $options['total'];
$response['recordsFiltered'] = $options['filtered'];

if (count($options['data'])) {
foreach($options['data'] as $item) {
if (isset($item['id'])) {
$item['DT_RowId'] = $item['id'];
}
abstract class AdapterInterface
{

protected $parser = null;
protected $columns = [];
protected $lentgh = 30;

$response['data'][] = $item;
}
} else {
$response['data'] = [];
public function __construct($length)
{
$this->length = $length;
}

return $response;
}
abstract public function getResponse();

public function sanitaze($string) {
return mb_substr($string, 0, $this->length);
}
public function setParser(ParamsParser $parser)
{
$this->parser = $parser;
}

public function bind($case, $closure) {
switch($case) {
case "global_search":
$search = $this->parser->getSearchValue();
if (!mb_strlen($search)) return;
public function setColumns(array $columns)
{
$this->columns = $columns;
}

foreach($this->parser->getSearchableColumns() as $column) {
if (!$this->columnExists($column)) continue;
$closure($column, $this->sanitaze($search));
}
break;
case "column_search":
$columnSearch = $this->parser->getColumnsSearch();
if (!$columnSearch) return;

foreach($columnSearch as $key => $column) {
if (!$this->columnExists($column['data'])) continue;
$closure($column['data'], $this->sanitaze($column['search']['value']));
}
break;
case "order":
$order = $this->parser->getOrder();
if (!$order) return;
public function getColumns()
{
return $this->columns;
}

$orderArray = [];
public function columnExists($column)
{
$notFakeField = true;
if (isset($this->originalColumns) && is_array($this->originalColumns)) {
foreach ($this->originalColumns as $columnDefinition) {
if (!is_array($columnDefinition) || array_keys($columnDefinition)[0] != $column) {
continue;
}
if (!empty($columnDefinition[$column]['fake'])) {
$notFakeField = false;
}
}
}

foreach($order as $orderBy) {
if (!isset($orderBy['dir']) || !isset($orderBy['column'])) continue;
$orderDir = $orderBy['dir'];
return in_array($column, $this->columns) && $notFakeField;
}

$column = $this->parser->getColumnById($orderBy['column']);
if (is_null($column) || !$this->columnExists($column)) continue;
public function getParser()
{
return $this->parser;
}

$orderArray[] = "{$column} {$orderDir}";
public function formResponse($options)
{
$defaults = [
'total' => 0,
'filtered' => 0,
'data' => []
];
$options += $defaults;

$response = [];
$response['draw'] = $this->parser->getDraw();
$response['recordsTotal'] = $options['total'];
$response['recordsFiltered'] = $options['filtered'];

if (count($options['data'])) {
foreach ($options['data'] as $item) {
if (isset($item['id'])) {
$item['DT_RowId'] = $item['id'];
}

$response['data'][] = $item;
}
} else {
$response['data'] = [];
}

$closure($orderArray);
break;
default:
throw new \Exception('Unknown bind type');
return $response;
}

}
public function sanitaze($string)
{
return mb_substr($string, 0, $this->length);
}

public function bind($case, $closure)
{
switch ($case) {
case "global_search":
$search = $this->parser->getSearchValue();
if (!mb_strlen($search))
return;

foreach ($this->parser->getSearchableColumns() as $column) {
if (!$this->columnExists($column))
continue;
$closure($column, $this->sanitaze($search));
}
break;
case "column_search":
$columnSearch = $this->parser->getColumnsSearch();
if (!$columnSearch)
return;

foreach ($columnSearch as $key => $column) {
if (!$this->columnExists($column['data']))
continue;
$closure($column['data'], $this->sanitaze($column['search']['value']));
}
break;
case "order":
$order = $this->parser->getOrder();
if (!$order)
return;

$orderArray = [];

foreach ($order as $orderBy) {
if (!isset($orderBy['dir']) || !isset($orderBy['column']))
continue;
$orderDir = $orderBy['dir'];

$column = $this->parser->getColumnById($orderBy['column']);
if (is_null($column) || !$this->columnExists($column))
continue;

$orderArray[] = "{$column} {$orderDir}";
}

$closure($orderArray);
break;
default:
throw new \Exception('Unknown bind type');
}
}

}
Loading