Add row and cell generator methods

This commit is contained in:
Christopher Pitt 2015-04-17 12:07:51 +12:00
parent b33a660604
commit 2d9a003168

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Displays a {@link SS_List} in a grid format. * Displays a {@link SS_List} in a grid format.
* *
@ -31,18 +32,21 @@ class GridField extends FormField {
/** /**
* The datasource * The datasource
*
* @var SS_List * @var SS_List
*/ */
protected $list = null; protected $list = null;
/** /**
* The classname of the DataObject that the GridField will display. Defaults to the value of $this->list->dataClass * The classname of the DataObject that the GridField will display. Defaults to the value of $this->list->dataClass
*
* @var string * @var string
*/ */
protected $modelClassName = ''; protected $modelClassName = '';
/** /**
* the current state of the GridField * the current state of the GridField
*
* @var GridState * @var GridState
*/ */
protected $state = null; protected $state = null;
@ -114,6 +118,7 @@ class GridField extends FormField {
* this modelclass $summary_fields * this modelclass $summary_fields
* *
* @param string $modelClassName * @param string $modelClassName
*
* @see GridFieldDataColumns::getDisplayFields() * @see GridFieldDataColumns::getDisplayFields()
*/ */
public function setModelClass($modelClassName) { public function setModelClass($modelClassName) {
@ -149,6 +154,7 @@ class GridField extends FormField {
/** /**
* @param GridFieldConfig $config * @param GridFieldConfig $config
*
* @return GridField * @return GridField
*/ */
public function setConfig(GridFieldConfig $config) { public function setConfig(GridFieldConfig $config) {
@ -165,6 +171,7 @@ class GridField extends FormField {
* *
* @param $value * @param $value
* @param $castingDefinition * @param $castingDefinition
*
* @todo refactor this into GridFieldComponent * @todo refactor this into GridFieldComponent
*/ */
public function getCastedValue($value, $castingDefinition) { public function getCastedValue($value, $castingDefinition) {
@ -345,26 +352,18 @@ class GridField extends FormField {
$rowContent = ''; $rowContent = '';
foreach($this->getColumns() as $column) { foreach($this->getColumns() as $column) {
$colContent = $this->getColumnContent($record, $column); $colContent = $this->getColumnContent($record, $column);
// A return value of null means this columns should be skipped altogether. // A return value of null means this columns should be skipped altogether.
if($colContent === null) continue; if($colContent === null) {
$colAttributes = $this->getColumnAttributes($record, $column); continue;
$rowContent .= FormField::create_tag('td', $colAttributes, $colContent);
} }
$classes = array('ss-gridfield-item');
if ($idx == 0) $classes[] = 'first'; $colAttributes = $this->getColumnAttributes($record, $column);
if ($idx == $total-1) $classes[] = 'last';
$classes[] = ($idx % 2) ? 'even' : 'odd'; $rowContent .= $this->newCell($this, $total, $idx, $record, $colAttributes, $colContent);
$row = FormField::create_tag( }
'tr',
array( $rows[] = $this->newRow($this, $total, $idx, $record, array(), $rowContent);
"class" => implode(' ', $classes),
'data-id' => $record->ID,
// TODO Allow per-row customization similar to GridFieldDataColumns
'data-class' => $record->ClassName,
),
$rowContent
);
$rows[] = $row;
} }
$content['body'] = implode("\n", $rows); $content['body'] = implode("\n", $rows);
} }
@ -422,6 +421,58 @@ class GridField extends FormField {
); );
} }
/**
* @param GridField $gridfield
* @param int $total
* @param int $index
* @param DataObject $record
* @param array $attributes
* @param string $content
*
* @return string
*/
protected function newCell($gridfield, $total, $index, $record, $attributes, $content) {
return FormField::create_tag(
'td',
$attributes,
$content
);
}
/**
* @param GridField $gridfield
* @param int $total
* @param int $index
* @param DataObject $record
* @param array $attributes
* @param string $content
*
* @return string
*/
protected function newRow($gridfield, $total, $index, $record, $attributes, $content) {
$classes = array('ss-gridfield-item');
if($index == 0) {
$classes[] = 'first';
}
if($index == $total - 1) {
$classes[] = 'last';
}
$classes[] = ($index % 2) ? 'even' : 'odd';
return FormField::create_tag(
'tr',
array(
'class' => implode(' ', $classes),
'data-id' => $record->ID,
'data-class' => $record->ClassName,
),
$content
);
}
public function Field($properties = array()) { public function Field($properties = array()) {
return $this->FieldHolder($properties); return $this->FieldHolder($properties);
} }
@ -452,6 +503,7 @@ class GridField extends FormField {
* *
* @param DataObject $record * @param DataObject $record
* @param string $column * @param string $column
*
* @return string * @return string
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
@ -512,6 +564,7 @@ class GridField extends FormField {
* *
* @param DataObject $record * @param DataObject $record
* @param string $column * @param string $column
*
* @return array * @return array
* @throws LogicException * @throws LogicException
* @throws InvalidArgumentException * @throws InvalidArgumentException
@ -546,6 +599,7 @@ class GridField extends FormField {
* Get metadata for a column, example array('Title'=>'Email address') * Get metadata for a column, example array('Title'=>'Email address')
* *
* @param string $column * @param string $column
*
* @return array * @return array
* @throws LogicException * @throws LogicException
* @throws InvalidArgumentException * @throws InvalidArgumentException
@ -610,6 +664,7 @@ class GridField extends FormField {
* This is the action that gets executed when a GridField_AlterAction gets clicked. * This is the action that gets executed when a GridField_AlterAction gets clicked.
* *
* @param array $data * @param array $data
*
* @return string * @return string
*/ */
public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) { public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) {
@ -661,6 +716,7 @@ class GridField extends FormField {
* @param string $actionName * @param string $actionName
* @param mixed $args * @param mixed $args
* @param arrray $data - send data from a form * @param arrray $data - send data from a form
*
* @return type * @return type
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
@ -723,7 +779,8 @@ class GridField extends FormField {
} }
if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result) if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result)
&& $result instanceof RequestHandler) { && $result instanceof RequestHandler
) {
$returnValue = $result->handleRequest($request, $model); $returnValue = $result->handleRequest($request, $model);
@ -860,6 +917,7 @@ class GridField_FormAction extends FormAction {
* Calculate the name of the gridfield relative to the Form * Calculate the name of the gridfield relative to the Form
* *
* @param GridField $base * @param GridField $base
*
* @return string * @return string
*/ */
protected function getNameFromParent() { protected function getNameFromParent() {