2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\Classes;
|
|
|
|
|
2016-11-08 07:02:31 +11:00
|
|
|
use October\Rain\Database\Model;
|
2015-03-25 19:35:00 +11:00
|
|
|
use October\Rain\Html\Helper as HtmlHelper;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* List Columns definition
|
|
|
|
* A translation of the list column configuration
|
|
|
|
*
|
|
|
|
* @package october\backend
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class ListColumn
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string List column name.
|
|
|
|
*/
|
|
|
|
public $columnName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string List column label.
|
|
|
|
*/
|
|
|
|
public $label;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Display mode. Text, number
|
|
|
|
*/
|
|
|
|
public $type = 'text';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool Specifies if this column can be searched.
|
|
|
|
*/
|
|
|
|
public $searchable = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool Specifies if this column is hidden by default.
|
|
|
|
*/
|
|
|
|
public $invisible = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool Specifies if this column can be sorted.
|
|
|
|
*/
|
|
|
|
public $sortable = true;
|
|
|
|
|
2015-12-05 10:05:42 +11:00
|
|
|
/**
|
|
|
|
* @var bool If set to false, disables the default click behavior when the column is clicked.
|
|
|
|
*/
|
|
|
|
public $clickable = true;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
2014-08-16 14:08:51 +10:00
|
|
|
* @var string Model attribute to use for the display value, this will
|
2017-03-15 18:03:26 +11:00
|
|
|
* override any `$sqlSelect` definition.
|
2014-08-16 14:08:51 +10:00
|
|
|
*/
|
2014-09-17 19:08:49 +10:00
|
|
|
public $valueFrom;
|
2014-08-16 14:08:51 +10:00
|
|
|
|
2015-06-27 10:39:07 +10:00
|
|
|
/**
|
|
|
|
* @var string Specifies a default value when value is empty.
|
|
|
|
*/
|
|
|
|
public $defaults;
|
|
|
|
|
2014-08-16 14:08:51 +10:00
|
|
|
/**
|
|
|
|
* @var string Custom SQL for selecting this record display value,
|
2017-03-15 18:03:26 +11:00
|
|
|
* the `@` symbol is replaced with the table name.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public $sqlSelect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Relation name, if this column represents a model relationship.
|
|
|
|
*/
|
|
|
|
public $relation;
|
|
|
|
|
2015-02-21 13:49:32 +11:00
|
|
|
/**
|
|
|
|
* @var string sets the column width, can be specified in percents (10%) or pixels (50px).
|
|
|
|
* There could be a single column without width specified, it will be stretched to take the
|
|
|
|
* available space.
|
|
|
|
*/
|
|
|
|
public $width;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
2014-07-31 19:34:38 +10:00
|
|
|
* @var string Specify a CSS class to attach to the list cell element.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public $cssClass;
|
|
|
|
|
|
|
|
/**
|
2014-08-01 17:42:00 +10:00
|
|
|
* @var string Specify a format or style for the column value, such as a Date.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public $format;
|
|
|
|
|
2014-08-01 17:42:00 +10:00
|
|
|
/**
|
|
|
|
* @var string Specifies a path for partial-type fields.
|
|
|
|
*/
|
|
|
|
public $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Raw field configuration.
|
|
|
|
*/
|
|
|
|
public $config;
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
2014-09-17 19:20:44 +10:00
|
|
|
* Constructor.
|
|
|
|
* @param string $columnName
|
|
|
|
* @param string $label
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function __construct($columnName, $label)
|
|
|
|
{
|
|
|
|
$this->columnName = $columnName;
|
|
|
|
$this->label = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies a list column rendering mode. Supported modes are:
|
|
|
|
* - text - text column, aligned left
|
|
|
|
* - number - numeric column, aligned right
|
|
|
|
* @param string $type Specifies a render mode as described above
|
|
|
|
*/
|
2014-07-31 19:34:38 +10:00
|
|
|
public function displayAs($type, $config)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2014-07-31 19:34:38 +10:00
|
|
|
$this->type = strtolower($type) ?: $this->type;
|
|
|
|
$this->config = $this->evalConfig($config);
|
2014-05-14 23:24:20 +10:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-31 19:34:38 +10:00
|
|
|
* Process options and apply them to this object.
|
|
|
|
* @param array $config
|
|
|
|
* @return array
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2014-07-31 19:34:38 +10:00
|
|
|
protected function evalConfig($config)
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
2015-02-21 13:49:32 +11:00
|
|
|
if (isset($config['width'])) {
|
|
|
|
$this->width = $config['width'];
|
|
|
|
}
|
2014-10-10 23:12:50 +02:00
|
|
|
if (isset($config['cssClass'])) {
|
|
|
|
$this->cssClass = $config['cssClass'];
|
|
|
|
}
|
|
|
|
if (isset($config['searchable'])) {
|
|
|
|
$this->searchable = $config['searchable'];
|
|
|
|
}
|
|
|
|
if (isset($config['sortable'])) {
|
|
|
|
$this->sortable = $config['sortable'];
|
|
|
|
}
|
2015-12-05 10:05:42 +11:00
|
|
|
if (isset($config['clickable'])) {
|
|
|
|
$this->clickable = $config['clickable'];
|
|
|
|
}
|
2014-10-10 23:12:50 +02:00
|
|
|
if (isset($config['invisible'])) {
|
|
|
|
$this->invisible = $config['invisible'];
|
|
|
|
}
|
|
|
|
if (isset($config['valueFrom'])) {
|
|
|
|
$this->valueFrom = $config['valueFrom'];
|
|
|
|
}
|
2015-06-27 10:39:07 +10:00
|
|
|
if (isset($config['default'])) {
|
|
|
|
$this->defaults = $config['default'];
|
|
|
|
}
|
2014-10-10 23:12:50 +02:00
|
|
|
if (isset($config['select'])) {
|
|
|
|
$this->sqlSelect = $config['select'];
|
|
|
|
}
|
|
|
|
if (isset($config['relation'])) {
|
|
|
|
$this->relation = $config['relation'];
|
|
|
|
}
|
|
|
|
if (isset($config['format'])) {
|
|
|
|
$this->format = $config['format'];
|
|
|
|
}
|
|
|
|
if (isset($config['path'])) {
|
|
|
|
$this->path = $config['path'];
|
|
|
|
}
|
|
|
|
|
2014-07-31 19:34:38 +10:00
|
|
|
return $config;
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
2015-03-25 19:35:00 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a HTML valid name for the column name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return HtmlHelper::nameToId($this->columnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a value suitable for the column id property.
|
|
|
|
* @param string $suffix Specify a suffix string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId($suffix = null)
|
|
|
|
{
|
|
|
|
$id = 'column';
|
|
|
|
|
|
|
|
$id .= '-'.$this->columnName;
|
|
|
|
|
|
|
|
if ($suffix) {
|
|
|
|
$id .= '-'.$suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HtmlHelper::nameToId($id);
|
|
|
|
}
|
2016-11-08 07:02:31 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this columns value from a supplied data set, which can be
|
|
|
|
* an array or a model or another generic collection.
|
|
|
|
* @param mixed $data
|
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getValueFromData($data, $default = null)
|
|
|
|
{
|
|
|
|
$columnName = $this->valueFrom ?: $this->columnName;
|
|
|
|
return $this->getColumnNameFromData($columnName, $data, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal method to extract the value of a column name from a data set.
|
|
|
|
* @param string $columnName
|
|
|
|
* @param mixed $data
|
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getColumnNameFromData($columnName, $data, $default = null)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Array column name, eg: column[key][key2][key3]
|
|
|
|
*/
|
|
|
|
$keyParts = HtmlHelper::nameToArray($columnName);
|
|
|
|
$result = $data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop the column key parts and build a value.
|
|
|
|
* To support relations only the last column should return the
|
|
|
|
* relation value, all others will look up the relation object as normal.
|
|
|
|
*/
|
|
|
|
foreach ($keyParts as $key) {
|
|
|
|
if ($result instanceof Model && $result->hasRelation($key)) {
|
|
|
|
$result = $result->{$key};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!isset($result->{$key})) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $result->{$key};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|