2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see GridField
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-12-06 01:56:24 +01:00
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
2012-03-09 02:07:40 +01:00
|
|
|
class GridFieldDataColumns implements GridField_ColumnProvider {
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-04-19 01:25:37 +02:00
|
|
|
/** @var array */
|
|
|
|
public $fieldCasting = array();
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
public $fieldFormatting = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}.
|
|
|
|
*
|
|
|
|
* @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));
|
2011-12-06 01:56:24 +01:00
|
|
|
foreach($baseColumns as $col) $columns[] = $col;
|
|
|
|
}
|
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
|
|
|
* Names of all columns which are affected by this component.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the default behaviour of showing the models summaryFields with
|
|
|
|
* these fields instead
|
|
|
|
* Example: array( 'Name' => 'Members name', 'Email' => 'Email address')
|
|
|
|
*
|
|
|
|
* @param array $fields
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* Caution: Make sure to escape special php-characters like in a normal php-statement.
|
|
|
|
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'.
|
|
|
|
* Alternatively, pass a anonymous function, which takes two parameters:
|
2012-07-26 09:18:22 +02:00
|
|
|
* The value and the original list item.
|
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.
|
|
|
|
*
|
|
|
|
* @param GridField
|
|
|
|
* @param DataObject - Record displayed in this row
|
|
|
|
* @param string
|
|
|
|
* @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];
|
|
|
|
|
|
|
|
// Allow callbacks
|
|
|
|
if(is_array($columnInfo) && isset($columnInfo['callback'])) {
|
|
|
|
$method = $columnInfo['callback'];
|
2012-07-26 09:18:22 +02:00
|
|
|
$value = $method($record);
|
2012-05-29 00:06:30 +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
|
|
|
}
|
|
|
|
|
2012-05-09 00:31:39 +02:00
|
|
|
/**
|
|
|
|
* Attributes for the element containing the content returned by {@link getColumnContent()}.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @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);
|
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'];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2012-05-09 00:31:39 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $item
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param string $value
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|