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) {
@ -128,8 +133,8 @@ class GridField extends FormField {
* @return string * @return string
*/ */
public function getModelClass() { public function getModelClass() {
if ($this->modelClassName) return $this->modelClassName; if($this->modelClassName) return $this->modelClassName;
if ($this->list && method_exists($this->list, 'dataClass')) { if($this->list && method_exists($this->list, 'dataClass')) {
$class = $this->list->dataClass(); $class = $this->list->dataClass();
if($class) return $class; if($class) return $class;
} }
@ -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) {
@ -176,16 +183,16 @@ class GridField extends FormField {
$castingParams = array(); $castingParams = array();
} }
if(strpos($castingDefinition,'->') === false) { if(strpos($castingDefinition, '->') === false) {
$castingFieldType = $castingDefinition; $castingFieldType = $castingDefinition;
$castingField = DBField::create_field($castingFieldType, $value); $castingField = DBField::create_field($castingFieldType, $value);
$value = call_user_func_array(array($castingField,'XML'),$castingParams); $value = call_user_func_array(array($castingField, 'XML'), $castingParams);
} else { } else {
$fieldTypeParts = explode('->', $castingDefinition); $fieldTypeParts = explode('->', $castingDefinition);
$castingFieldType = $fieldTypeParts[0]; $castingFieldType = $fieldTypeParts[0];
$castingMethod = $fieldTypeParts[1]; $castingMethod = $fieldTypeParts[1];
$castingField = DBField::create_field($castingFieldType, $value); $castingField = DBField::create_field($castingFieldType, $value);
$value = call_user_func_array(array($castingField,$castingMethod),$castingParams); $value = call_user_func_array(array($castingField, $castingMethod), $castingParams);
} }
return $value; return $value;
@ -293,7 +300,7 @@ class GridField extends FormField {
$fragmentDefined = array('header' => true, 'footer' => true, 'before' => true, 'after' => true); $fragmentDefined = array('header' => true, 'footer' => true, 'before' => true, 'after' => true);
reset($content); reset($content);
while(list($k,$v) = each($content)) { while(list($k, $v) = each($content)) {
if(preg_match_all('/\$DefineFragment\(([a-z0-9\-_]+)\)/i', $v, $matches)) { if(preg_match_all('/\$DefineFragment\(([a-z0-9\-_]+)\)/i', $v, $matches)) {
foreach($matches[1] as $match) { foreach($matches[1] as $match) {
$fragmentName = strtolower($match); $fragmentName = strtolower($match);
@ -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);
} }
@ -417,11 +416,63 @@ class GridField extends FormField {
return return
FormField::create_tag('fieldset', $attrs, FormField::create_tag('fieldset', $attrs,
$content['before'] . $content['before'] .
FormField::create_tag('table', $tableAttrs, $head."\n".$foot."\n".$body) . FormField::create_tag('table', $tableAttrs, $head . "\n" . $foot . "\n" . $body) .
$content['after'] $content['after']
); );
} }
/**
* @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
*/ */
@ -671,7 +727,7 @@ class GridField extends FormField {
continue; continue;
} }
if(in_array($actionName, array_map('strtolower', (array)$component->getActions($this)))) { if(in_array($actionName, array_map('strtolower', (array) $component->getActions($this)))) {
return $component->handleAction($this, $actionName, $args, $data); return $component->handleAction($this, $actionName, $args, $data);
} }
} }
@ -704,7 +760,7 @@ class GridField extends FormField {
if($urlHandlers) foreach($urlHandlers as $rule => $action) { if($urlHandlers) foreach($urlHandlers as $rule => $action) {
if($params = $request->match($rule, true)) { if($params = $request->match($rule, true)) {
// Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action', // Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action',
if($action[0] == '$') $action = $params[substr($action,1)]; if($action[0] == '$') $action = $params[substr($action, 1)];
if(!method_exists($component, 'checkAccessAction') || $component->checkAccessAction($action)) { if(!method_exists($component, 'checkAccessAction') || $component->checkAccessAction($action)) {
if(!$action) { if(!$action) {
$action = "index"; $action = "index";
@ -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);
@ -826,7 +883,7 @@ class GridField_FormAction extends FormAction {
* @param string $val * @param string $val
*/ */
public function _nameEncode($match) { public function _nameEncode($match) {
return '%'.dechex(ord($match[0])); return '%' . dechex(ord($match[0]));
} }
/** /**
@ -841,7 +898,7 @@ class GridField_FormAction extends FormAction {
); );
// Ensure $id doesn't contain only numeric characters // Ensure $id doesn't contain only numeric characters
$id = 'gf_'.substr(md5(serialize($state)), 0, 8); $id = 'gf_' . substr(md5(serialize($state)), 0, 8);
Session::set($id, $state); Session::set($id, $state);
$actionData['StateID'] = $id; $actionData['StateID'] = $id;
@ -850,7 +907,7 @@ class GridField_FormAction extends FormAction {
array( array(
// Note: This field needs to be less than 65 chars, otherwise Suhosin security patch // Note: This field needs to be less than 65 chars, otherwise Suhosin security patch
// will strip it from the requests // will strip it from the requests
'name' => 'action_gridFieldAlterAction'. '?' . http_build_query($actionData), 'name' => 'action_gridFieldAlterAction' . '?' . http_build_query($actionData),
'data-url' => $this->gridField->Link(), 'data-url' => $this->gridField->Link(),
) )
); );
@ -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() {
@ -869,7 +927,7 @@ class GridField_FormAction extends FormAction {
do { do {
array_unshift($name, $base->getName()); array_unshift($name, $base->getName());
$base = $base->getForm(); $base = $base->getForm();
} while ($base && !($base instanceof Form)); } while($base && !($base instanceof Form));
return implode('.', $name); return implode('.', $name);
} }