197 lines
4.9 KiB
PHP
Raw Normal View History

2015-01-03 11:57:50 +11:00
<?php namespace Backend\FormWidgets;
use Lang;
use Backend\Widgets\Table;
use Backend\Classes\FormWidgetBase;
use October\Rain\Html\Helper as HtmlHelper;
2015-01-28 18:03:35 +11:00
use ApplicationException;
2015-01-03 11:57:50 +11:00
/**
* Data Table
* Renders a table field.
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
class DataTable extends FormWidgetBase
{
//
// Configurable properties
//
2015-01-03 11:57:50 +11:00
/**
* @var string Table size
2015-01-03 11:57:50 +11:00
*/
public $size = 'large';
2015-01-03 11:57:50 +11:00
/**
* @var bool Allow rows to be sorted
* @todo Not implemented...
*/
public $rowSorting = false;
//
// Object properties
//
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
2015-01-03 11:57:50 +11:00
*/
protected $defaultAlias = 'datatable';
2015-01-03 11:57:50 +11:00
/**
* @var Backend\Widgets\Table Table widget
*/
protected $table;
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
2015-01-03 11:57:50 +11:00
*/
public function init()
{
$this->fillFromConfig([
'size',
'rowSorting',
]);
2015-01-03 11:57:50 +11:00
$this->table = $this->makeTableWidget();
$this->table->bindToController();
}
/**
* @return Backend\Widgets\Table The table to be displayed.
*/
public function getTable()
{
return $this->table;
}
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
2015-01-03 11:57:50 +11:00
*/
public function render()
{
$this->prepareVars();
return $this->makePartial('datatable');
}
/**
* Prepares the list data
*/
public function prepareVars()
{
$this->populateTableWidget();
$this->vars['table'] = $this->table;
$this->vars['size'] = $this->size;
$this->vars['rowSorting'] = $this->rowSorting;
}
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
*/
public function getLoadValue()
{
$value = (array) parent::getLoadValue();
// Sync the array keys as the ID to make the
// table widget happy!
foreach ($value as $key => $_value) {
$value[$key] = ['id' => $key] + (array) $_value;
}
return $value;
2015-01-03 11:57:50 +11:00
}
/**
2017-03-16 06:26:14 +11:00
* @inheritDoc
2015-01-03 11:57:50 +11:00
*/
2015-01-05 09:43:39 +11:00
public function getSaveValue($value)
2015-01-03 11:57:50 +11:00
{
// TODO: provide a streaming implementation of saving
// data to the model. The current implementation returns
2015-01-04 20:32:34 -08:00
// all records at once. -ab
2015-01-03 11:57:50 +11:00
$dataSource = $this->table->getDataSource();
$result = [];
while ($records = $dataSource->readRecords()) {
$result = array_merge($result, $records);
2015-01-03 11:57:50 +11:00
}
// We should be dealing with a simple array, so
// strip out the id columns in the final array.
foreach ($result as $key => $_result) {
unset($result[$key]['id']);
}
2015-01-03 11:57:50 +11:00
return $result;
}
/*
* Populate data
*/
protected function populateTableWidget()
{
$dataSource = $this->table->getDataSource();
2015-01-04 20:32:34 -08:00
// TODO: provide a streaming implementation of loading
2015-01-04 20:32:34 -08:00
// data from the model. The current implementation loads
// all records at once. -ab
2015-01-05 09:45:04 +11:00
$records = $this->getLoadValue() ?: [];
2015-03-25 19:33:19 +11:00
$dataSource->purge();
2015-01-03 11:57:50 +11:00
$dataSource->initRecords((array) $records);
}
protected function makeTableWidget()
{
$config = $this->makeConfig((array) $this->config);
$config->dataSource = 'client';
if (isset($this->getParentForm()->arrayName)) {
$config->alias = studly_case(HtmlHelper::nameToId($this->getParentForm()->arrayName . '[' . $this->fieldName . ']')) . 'datatable';
$config->fieldName = $this->getParentForm()->arrayName . '[' . $this->fieldName . ']';
} else {
$config->alias = studly_case(HtmlHelper::nameToId($this->fieldName)) . 'datatable';
$config->fieldName = $this->fieldName;
}
2015-01-03 11:57:50 +11:00
$table = new Table($this->controller, $config);
$table->bindEvent('table.getDropdownOptions', [$this, 'getDataTableOptions']);
return $table;
}
/**
* Looks at the model for getXXXDataTableOptions or getDataTableOptions methods
* to obtain values for autocomplete field types.
* @param string $field Table field name
* @param string $data Data for the entire table
* @return array
*/
public function getDataTableOptions($field, $data)
{
$methodName = 'get'.studly_case($this->fieldName).'DataTableOptions';
if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getDataTableOptions')) {
throw new ApplicationException(Lang::get('backend::lang.model.missing_method', ['class' => get_class($this->model), 'method' => 'getDataTableOptions']));
}
if ($this->model->methodExists($methodName)) {
$result = $this->model->$methodName($field, $data);
}
else {
$result = $this->model->getDataTableOptions($this->fieldName, $field, $data);
}
if (!is_array($result)) {
$result = [];
}
return $result;
}
}