MINOR Cleaned up the GridFieldDataColumns to be easier on the eyes and fixed the some method docblocks for the interface

This commit is contained in:
Stig Lindqvist 2012-05-09 10:31:39 +12:00
parent 592d42e477
commit 0af2d87bb3
2 changed files with 92 additions and 55 deletions

View File

@ -38,36 +38,36 @@ interface GridField_ColumnProvider extends GridFieldComponent {
* Modify the list of columns displayed in the table. * Modify the list of columns displayed in the table.
* See {@link GridFieldDataColumns->getDisplayFields()} and {@link GridFieldDataColumns}. * See {@link GridFieldDataColumns->getDisplayFields()} and {@link GridFieldDataColumns}.
* *
* @param GridField * @param GridField $gridField
* @param Array List reference of all column names. * @param array - List reference of all column names.
*/ */
function augmentColumns($gridField, &$columns); function augmentColumns($gridField, &$columns);
/** /**
* Names of all columns which are affected by this component. * Names of all columns which are affected by this component.
* *
* @param GridField * @param GridField $gridField
* @return Array * @return array
*/ */
function getColumnsHandled($gridField); function getColumnsHandled($gridField);
/** /**
* HTML for the column, content of the <td> element. * HTML for the column, content of the <td> element.
* *
* @param GridField * @param GridField $gridField
* @param DataObject Record displayed in this row * @param DataObject $record - Record displayed in this row
* @param String * @param string $columnName
* @return String HTML for the column. Return NULL to skip. * @return string - HTML for the column. Return NULL to skip.
*/ */
function getColumnContent($gridField, $record, $columnName); function getColumnContent($gridField, $record, $columnName);
/** /**
* Attributes for the element containing the content returned by {@link getColumnContent()}. * Attributes for the element containing the content returned by {@link getColumnContent()}.
* *
* @param GridField * @param GridField $gridField
* @param DataObject Record displayed in this row * @param DataObject $record displayed in this row
* @param String * @param string $columnName
* @return Array * @return array
*/ */
function getColumnAttributes($gridField, $record, $columnName); function getColumnAttributes($gridField, $record, $columnName);
@ -75,9 +75,9 @@ interface GridField_ColumnProvider extends GridFieldComponent {
* Additional metadata about the column which can be used by other components, * Additional metadata about the column which can be used by other components,
* e.g. to set a title for a search column header. * e.g. to set a title for a search column header.
* *
* @param GridField * @param GridField $gridField
* @param String * @param string $columnName
* @return Array Map of arbitrary metadata identifiers to their values. * @return array - Map of arbitrary metadata identifiers to their values.
*/ */
function getColumnMetadata($gridField, $columnName); function getColumnMetadata($gridField, $columnName);
} }

View File

@ -21,11 +21,24 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
*/ */
protected $displayFields = array(); protected $displayFields = array();
/**
* 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.
*/
public function augmentColumns($gridField, &$columns) { public function augmentColumns($gridField, &$columns) {
$baseColumns = array_keys($this->getDisplayFields($gridField)); $baseColumns = array_keys($this->getDisplayFields($gridField));
foreach($baseColumns as $col) $columns[] = $col; foreach($baseColumns as $col) $columns[] = $col;
} }
/**
* Names of all columns which are affected by this component.
*
* @param GridField $gridField
* @return array
*/
public function getColumnsHandled($gridField) { public function getColumnsHandled($gridField) {
return array_keys($this->getDisplayFields($gridField)); return array_keys($this->getDisplayFields($gridField));
} }
@ -97,50 +110,48 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
} }
/** /**
* HTML for the column, content of the <td> element.
* *
* @param string $fieldName * @param GridField
* @param string $value * @param DataObject - Record displayed in this row
* @param boolean $xmlSafe * @param string
* @return type * @return string HTML for the column. Return NULL to skip.
*/ */
public function getColumnContent($gridField, $item, $column) { public function getColumnContent($gridField, $record, $columnName) {
// Find the data column for the given named column
$fieldName = $column;
$xmlSafe = true;
// This supports simple FieldName syntax // This supports simple FieldName syntax
if(strpos($fieldName, '.') === false) { if(strpos($columnName, '.') === false) {
$value = ($item->XML_val($fieldName) && $xmlSafe) ? $item->XML_val($fieldName) : $item->RAW_val($fieldName); $value = $record->XML_val($columnName);
} else { } else {
$fieldNameParts = explode('.', $fieldName); $value = $this->getValueFromRelation($record, $columnName);
$tmpItem = $item;
for($idx = 0; $idx < sizeof($fieldNameParts); $idx++) {
$relationMethod = $fieldNameParts[$idx];
// Last value for value
if($idx == sizeof($fieldNameParts) - 1) {
if($tmpItem) {
$value = ($tmpItem->XML_val($relationMethod) && $xmlSafe) ? $tmpItem->XML_val($relationMethod) : $tmpItem->RAW_val($relationMethod);
}
// else get the object for the next iteration
} else {
if($tmpItem) {
$tmpItem = $tmpItem->$relationMethod();
}
}
}
} }
$value = $this->castValue($gridField, $column, $value); $value = $this->castValue($gridField, $columnName, $value);
$value = $this->formatValue($gridField, $item, $column, $value); $value = $this->formatValue($gridField, $record, $columnName, $value);
$value = $this->escapeValue($gridField, $value); $value = $this->escapeValue($gridField, $value);
return $value; return $value;
} }
public function getColumnAttributes($gridField, $item, $column) { /**
return array('class' => 'col-' . preg_replace('/[^\w]/', '-', $column)); * 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));
} }
/**
* 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.
*/
public function getColumnMetadata($gridField, $column) { public function getColumnMetadata($gridField, $column) {
$columns = $this->getDisplayFields($gridField); $columns = $this->getDisplayFields($gridField);
return array( return array(
@ -149,10 +160,33 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
} }
/** /**
* Translate a Object.RelationName.ColumnName $columnName into the value that ColumnName returns
* *
* @param type $fieldName * @param DataObject $record
* @param type $value * @param string $columnName
* @return type * @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;
}
/**
*
* @param GridField $gridField
* @param string $fieldName
* @param string $value
* @return string
*/ */
protected function castValue($gridField, $fieldName, $value) { protected function castValue($gridField, $fieldName, $value) {
if(array_key_exists($fieldName, $this->fieldCasting)) { if(array_key_exists($fieldName, $this->fieldCasting)) {
@ -165,9 +199,11 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
/** /**
* *
* @param type $fieldName * @param GridField $gridField
* @param type $value * @param DataObject $item
* @return type * @param string $fieldName
* @param string $value
* @return string
*/ */
protected function formatValue($gridField, $item, $fieldName, $value) { protected function formatValue($gridField, $item, $fieldName, $value) {
if(!array_key_exists($fieldName, $this->fieldFormatting)) { if(!array_key_exists($fieldName, $this->fieldFormatting)) {
@ -189,8 +225,9 @@ class GridFieldDataColumns implements GridField_ColumnProvider {
/** /**
* Remove values from a value using FieldEscape setter * Remove values from a value using FieldEscape setter
* *
* @param type $value * @param GridField $gridField
* @return type * @param string $value
* @return string
*/ */
protected function escapeValue($gridField, $value) { protected function escapeValue($gridField, $value) {
if(!$escape = $gridField->FieldEscape) { if(!$escape = $gridField->FieldEscape) {