2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @see GridField
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
2012-03-09 02:07:40 +01:00
|
|
|
class GridFieldDataColumns implements GridField_ColumnProvider {
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
2013-05-20 12:18:07 +02:00
|
|
|
*/
|
2012-04-19 01:25:37 +02:00
|
|
|
public $fieldCasting = array();
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
2013-05-20 12:18:07 +02:00
|
|
|
*/
|
2012-04-19 01:25:37 +02:00
|
|
|
public $fieldFormatting = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-19 01:25:37 +02:00
|
|
|
/**
|
|
|
|
* This is the columns that will be visible
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $displayFields = array();
|
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
|
|
|
* Modify the list of columns displayed in the table.
|
|
|
|
* See {@link GridFieldDataColumns->getDisplayFields()} and {@link GridFieldDataColumns}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param array - List reference of all column names.
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function augmentColumns($gridField, &$columns) {
|
2012-04-19 01:25:37 +02:00
|
|
|
$baseColumns = array_keys($this->getDisplayFields($gridField));
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-09-01 11:50:36 +02:00
|
|
|
foreach($baseColumns as $col) {
|
|
|
|
$columns[] = $col;
|
|
|
|
}
|
|
|
|
|
|
|
|
$columns = array_unique($columns);
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
|
|
|
* Names of all columns which are affected by this component.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return array
|
2012-05-09 00:31:39 +02:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getColumnsHandled($gridField) {
|
2012-04-19 01:25:37 +02:00
|
|
|
return array_keys($this->getDisplayFields($gridField));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-19 01:25:37 +02:00
|
|
|
/**
|
|
|
|
* Override the default behaviour of showing the models summaryFields with
|
|
|
|
* these fields instead
|
|
|
|
* Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param array $fields
|
2012-04-19 01:25:37 +02:00
|
|
|
*/
|
|
|
|
public function setDisplayFields($fields) {
|
|
|
|
if(!is_array($fields)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new InvalidArgumentException('
|
|
|
|
Arguments passed to GridFieldDataColumns::setDisplayFields() must be an array');
|
2012-04-19 01:25:37 +02:00
|
|
|
}
|
|
|
|
$this->displayFields = $fields;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the DisplayFields
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-19 01:25:37 +02:00
|
|
|
* @return array
|
|
|
|
* @see GridFieldDataColumns::setDisplayFields
|
|
|
|
*/
|
|
|
|
public function getDisplayFields($gridField) {
|
|
|
|
if(!$this->displayFields) {
|
|
|
|
return singleton($gridField->getModelClass())->summaryFields();
|
|
|
|
}
|
|
|
|
return $this->displayFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify castings with fieldname as the key, and the desired casting as value.
|
|
|
|
* Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
|
|
|
|
*
|
|
|
|
* @param array $casting
|
|
|
|
*/
|
|
|
|
public function setFieldCasting($casting) {
|
|
|
|
$this->fieldCasting = $casting;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldCasting() {
|
|
|
|
return $this->fieldCasting;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-04 16:07:22 +02:00
|
|
|
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-07-04 16:07:22 +02:00
|
|
|
* Caution: Make sure to escape special php-characters like in a normal php-statement.
|
|
|
|
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-07-04 16:07:22 +02:00
|
|
|
* Alternatively, pass a anonymous function, which takes two parameters:
|
2013-09-24 10:45:55 +02:00
|
|
|
* The value and the original list item.
|
|
|
|
*
|
|
|
|
* Formatting is applied after field casting, so if you're modifying the string
|
|
|
|
* to include further data through custom formatting, ensure it's correctly escaped.
|
2012-07-04 16:07:22 +02:00
|
|
|
*
|
2012-04-19 01:25:37 +02:00
|
|
|
* @param array $formatting
|
|
|
|
*/
|
|
|
|
public function setFieldFormatting($formatting) {
|
|
|
|
$this->fieldFormatting = $formatting;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldFormatting() {
|
|
|
|
return $this->fieldFormatting;
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-09 00:31:39 +02:00
|
|
|
* HTML for the column, content of the <td> element.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField
|
|
|
|
* @param DataObject - Record displayed in this row
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string
|
2012-05-09 00:31:39 +02:00
|
|
|
* @return string HTML for the column. Return NULL to skip.
|
|
|
|
*/
|
|
|
|
public function getColumnContent($gridField, $record, $columnName) {
|
2012-05-29 00:06:30 +02:00
|
|
|
// Find the data column for the given named column
|
|
|
|
$columns = $this->getDisplayFields($gridField);
|
|
|
|
$columnInfo = $columns[$columnName];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-29 00:06:30 +02:00
|
|
|
// Allow callbacks
|
|
|
|
if(is_array($columnInfo) && isset($columnInfo['callback'])) {
|
|
|
|
$method = $columnInfo['callback'];
|
2012-07-26 09:18:22 +02:00
|
|
|
$value = $method($record);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
// This supports simple FieldName syntax
|
2012-01-09 11:36:50 +01:00
|
|
|
} else {
|
2012-07-26 09:18:22 +02:00
|
|
|
$value = $gridField->getDataFieldValue($record, $columnName);
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
2012-07-28 05:42:39 +02:00
|
|
|
// Turn $value, whatever it is, into a HTML embeddable string
|
2012-07-26 09:18:22 +02:00
|
|
|
$value = $this->castValue($gridField, $columnName, $value);
|
2012-07-28 05:42:39 +02:00
|
|
|
// Make any formatting tweaks
|
|
|
|
$value = $this->formatValue($gridField, $record, $columnName, $value);
|
|
|
|
// Do any final escaping
|
2011-12-06 01:56:24 +01:00
|
|
|
$value = $this->escapeValue($gridField, $value);
|
|
|
|
|
2012-07-28 05:42:39 +02:00
|
|
|
return $value;
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
|
|
|
* Attributes for the element containing the content returned by {@link getColumnContent()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $record displayed in this row
|
|
|
|
* @param string $columnName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumnAttributes($gridField, $record, $columnName) {
|
|
|
|
return array('class' => 'col-' . preg_replace('/[^\w]/', '-', $columnName));
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
|
|
|
* Additional metadata about the column which can be used by other components,
|
|
|
|
* e.g. to set a title for a search column header.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $columnName
|
|
|
|
* @return array - Map of arbitrary metadata identifiers to their values.
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getColumnMetadata($gridField, $column) {
|
2012-04-19 01:25:37 +02:00
|
|
|
$columns = $this->getDisplayFields($gridField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-29 00:06:30 +02:00
|
|
|
$title = null;
|
|
|
|
if(is_string($columns[$column])) {
|
|
|
|
$title = $columns[$column];
|
|
|
|
} else if(is_array($columns[$column]) && isset($columns[$column]['title'])) {
|
|
|
|
$title = $columns[$column]['title'];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
return array(
|
2012-05-29 00:06:30 +02:00
|
|
|
'title' => $title,
|
2011-12-06 01:56:24 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2012-05-09 00:31:39 +02:00
|
|
|
* Translate a Object.RelationName.ColumnName $columnName into the value that ColumnName returns
|
2011-12-06 01:56:24 +01:00
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param DataObject $record
|
|
|
|
* @param string $columnName
|
|
|
|
* @return string|null - returns null if it could not found a value
|
|
|
|
*/
|
|
|
|
protected function getValueFromRelation($record, $columnName) {
|
|
|
|
$fieldNameParts = explode('.', $columnName);
|
|
|
|
$tmpItem = clone($record);
|
|
|
|
for($idx = 0; $idx < sizeof($fieldNameParts); $idx++) {
|
|
|
|
$methodName = $fieldNameParts[$idx];
|
|
|
|
// Last mmethod call from $columnName return what that method is returning
|
|
|
|
if($idx == sizeof($fieldNameParts) - 1) {
|
|
|
|
return $tmpItem->XML_val($methodName);
|
|
|
|
}
|
|
|
|
// else get the object from this $methodName
|
|
|
|
$tmpItem = $tmpItem->$methodName();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
2012-07-28 05:42:39 +02:00
|
|
|
* Casts a field to a string which is safe to insert into HTML
|
2012-05-09 00:31:39 +02:00
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param string $value
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
protected function castValue($gridField, $fieldName, $value) {
|
2012-07-28 05:42:39 +02:00
|
|
|
// If a fieldCasting is specified, we assume the result is safe
|
2012-04-19 01:25:37 +02:00
|
|
|
if(array_key_exists($fieldName, $this->fieldCasting)) {
|
2012-07-28 05:42:39 +02:00
|
|
|
$value = $gridField->getCastedValue($value, $this->fieldCasting[$fieldName]);
|
2012-07-29 21:40:14 +02:00
|
|
|
} else if(is_object($value)) {
|
|
|
|
// If the value is an object, we do one of two things
|
|
|
|
if (method_exists($value, 'Nice')) {
|
|
|
|
// If it has a "Nice" method, call that & make sure the result is safe
|
|
|
|
$value = Convert::raw2xml($value->Nice());
|
|
|
|
} else {
|
|
|
|
// Otherwise call forTemplate - the result of this should already be safe
|
|
|
|
$value = $value->forTemplate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, just treat as a text string & make sure the result is safe
|
2012-07-28 05:42:39 +02:00
|
|
|
$value = Convert::raw2xml($value);
|
|
|
|
}
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
return $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $item
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param string $value
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
protected function formatValue($gridField, $item, $fieldName, $value) {
|
2012-04-19 01:25:37 +02:00
|
|
|
if(!array_key_exists($fieldName, $this->fieldFormatting)) {
|
2011-12-06 01:56:24 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2012-04-19 01:25:37 +02:00
|
|
|
$spec = $this->fieldFormatting[$fieldName];
|
2012-04-17 17:11:43 +02:00
|
|
|
if(is_callable($spec)) {
|
2012-05-30 01:46:32 +02:00
|
|
|
return $spec($value, $item);
|
2012-04-17 17:11:43 +02:00
|
|
|
} else {
|
|
|
|
$format = str_replace('$value', "__VAL__", $spec);
|
|
|
|
$format = preg_replace('/\$([A-Za-z0-9-_]+)/', '$item->$1', $format);
|
|
|
|
$format = str_replace('__VAL__', '$value', $format);
|
|
|
|
eval('$value = "' . $format . '";');
|
|
|
|
return $value;
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
|
|
|
* Remove values from a value using FieldEscape setter
|
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
protected function escapeValue($gridField, $value) {
|
|
|
|
if(!$escape = $gridField->FieldEscape) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($escape as $search => $replace) {
|
|
|
|
$value = str_replace($search, $replace, $value);
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|