Skip to content
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
42 changes: 42 additions & 0 deletions src/Pike/DataTable/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Zend\View\Model\ViewModel;
use Pike\DataTable\DataSource\DataSourceInterface;
use Pike\DataTable\ColumnBag;
use Pike\DataTable;

abstract class AbstractAdapter implements AdapterInterface
{
Expand Down Expand Up @@ -69,6 +70,47 @@ public function __construct()
$this->setAutoEscapeFilter();
}

/**
* Get items should return the current page datatable
* data with callbacks and filters already being processed.
*
* @return array
*/
protected function getItems(DataTable $dataTable, $offset, $limit)
{
$items = $dataTable->getDataSource()->getItems($offset, $limit);

foreach ($items as &$row) {
$newRow = array();

foreach ($this->columnBag as $columnName => $column) {
$newRow[$columnName] = $column['data']($row);
}

$row = $newRow;
}

$items = $this->filterItems($items);

return $items;
}

/**
* @param array $items
*
* @return array
*/
protected function filterItems($items)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typehint array? Or update comment.

{
foreach ($items as &$item) {
foreach (array_values($item) as $index => $string) {
$item[$index] = $this->filter($string, $this->columnBag->getOffset($index));
}
}

return $items;
}

/**
* @return string
*/
Expand Down
26 changes: 5 additions & 21 deletions src/Pike/DataTable/Adapter/DataTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Pike\DataTable;
use Pike\DataTable\DataSource\DataSourceInterface;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\Json;

class DataTables extends AbstractAdapter
Expand Down Expand Up @@ -35,7 +36,7 @@ public function __construct()
}

/**
* @return string
* @return ViewModel
*/
public function render(DataTable $dataTable)
{
Expand All @@ -49,10 +50,7 @@ public function render(DataTable $dataTable)
$this->viewModel->setVariable('attributes', $this->getAttributes());

if (null !== $this->getOption('iDeferLoading')) {
$items = $dataTable->getDataSource()
->getItems(0, $this->getOption('iDeferLoading'));
$items = $this->filterItems($items);
$this->viewModel->setVariable('items', $items);
$this->viewModel->setVariable('items', $this->getItems($dataTable, 0, $this->getOption('iDeferLoading')));
}

return $this->viewModel;
Expand Down Expand Up @@ -84,31 +82,17 @@ public function getResponse(DataTable $dataTable)
}

$count = count($dataSource);
$items = $dataSource->getItems($offset, $limit);
$items = $this->getItems($dataTable, $offset, $limit);

$data = array();
$data['sEcho'] = (int) $this->parameters['sEcho'];
$data['iTotalRecords'] = $count;
$data['iTotalDisplayRecords'] = $count;
$data['aaData'] = $this->filterItems($items);
$data['aaData'] = $items;

return new JsonModel($data);
}

/**
* @param array $items
* @return array
*/
protected function filterItems($items)
{
foreach ($items as &$item) {
foreach (array_values($item) as $index => $string) {
$item[$index] = $this->filter($string, $this->columnBag->getOffset($index));
}
}

return $items;
}

/**
* {@inheritdoc}
Expand Down
87 changes: 70 additions & 17 deletions src/Pike/DataTable/ColumnBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,48 @@ public function __construct(array $columns = array())
/**
* Adds a column to the column bag
*
* @param string $column The column name
* @param string $label The friendly name used for this column as heading
* @param string $field The field name to be used when sorting is isseud
* @param integer $position The position number, can be any number
* @param boolean $display Show column
* @param string $columnName The column name
* @param string $label The friendly name used for this column as heading
* @param string $field The field name to be used when sorting is isseud
* @param integer $position The position number, can be any number
* @param boolean $display Show column
* @param Closure $data A callback which called everyrow this column needs to be drawed with row data as argument
*/
public function add($column, $label = null, $field = null, $position = null,
$display = true
public function add($columnName, $label = null, $field = null, $position = null,
$display = true, \Closure $data = null
) {
$label = isset($label) ? $label : $column;
$field = isset($field) ? $field : $column;
$label = isset($label) ? $label : $columnName;
$field = isset($field) ? $field : $columnName;
$position = isset($position) ? $position : count($this->columns) + 1;

$this->columns[$column] = array(
'column' => $column,
$column = array(
'column' => $columnName,
'label' => $label,
'field' => $field,
'position' => $position,
'display' => $display
'display' => $display,
'data' => $data,
);

if (!is_callable($data)) {
$column['data'] = function($row) use ($columnName, $data) {
if (is_string($data) || is_integer($data)) {
return $data;
}

if ($row[$columnName] instanceof \DateTime) {
return $row[$columnName]->format(\DateTime::ISO8601);
}

return $row[$columnName];
};
}

if ($this->has($columnName)) {
$column = array_merge($column,$this->get($columnName));
}

$this->columns[$columnName] = $column;
}

/**
Expand All @@ -60,7 +82,7 @@ public function setLabel($column, $label)
{
$this->get($column);

$this->columns[$column] = $label;
$this->columns[$column]['label'] = $label;

return $this;
}
Expand Down Expand Up @@ -89,7 +111,7 @@ public function setField($column, $field)
{
$this->get($column);

$this->columns[$column] = $field;
$this->columns[$column]['field'] = $field;

return $this;
}
Expand Down Expand Up @@ -118,7 +140,7 @@ public function setPosition($column, $position)
{
$this->get($column);

$this->columns[$column] = $position;
$this->columns[$column]['position'] = $position;

return $this;
}
Expand Down Expand Up @@ -147,7 +169,7 @@ public function setDisplay($column, $display)
{
$this->get($column);

$this->columns[$column] = $display;
$this->columns[$column]['display'] = $display;

return $this;
}
Expand All @@ -156,6 +178,7 @@ public function setDisplay($column, $display)
* Returns the display for the specified column
*
* @param string $column
*
* @return boolean
*/
public function getDisplay($column)
Expand All @@ -168,8 +191,10 @@ public function getDisplay($column)
/**
* Returns the specified column
*
* @param string $column
* @param string $column
*
* @return array
*
* @throws \Pike\Exception
*/
public function get($column)
Expand All @@ -185,6 +210,7 @@ public function get($column)
* Returns the column for the specified offset
*
* @param integer $offset
*
* @return array
*/
public function getOffset($offset)
Expand All @@ -199,6 +225,33 @@ public function getOffset($offset)
return current($columns);
}

/**
* Set the callback for a specific column.
*
* @param string $column
* @param \Closure $callback
*
* @return ColumnBag
*/
public function setDataCallback($column, \Closure $callback)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't typehint \Closure here. If you do, you disallow things like array($myObject, 'someMethod'). Do no typehint + is_callable check instead.

{
$this->columns[$column]['data'] = $callback;

return $this;
}

/**
* Get the closure for a given column.
*
* @return \Closure
*/
public function getDataCallback($column)
{
$column = $this->get($column);

return $column['data'];
}

/**
* Returns all columns
*
Expand Down
6 changes: 0 additions & 6 deletions src/Pike/DataTable/DataSource/Doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ public function getItems($offset, $limit)

$data = $paginateQuery->getArrayResult();

array_walk_recursive($data, function(&$item, $key) {
if ($item instanceof \DateTime) {
$item = $item->format('c');
}
});

return $data;
}

Expand Down