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
/**
* Displays a {@link SS_List} in a grid format.
*
@ -31,18 +32,21 @@ class GridField extends FormField {
/**
* The datasource
*
* @var SS_List
*/
protected $list = null;
/**
* The classname of the DataObject that the GridField will display. Defaults to the value of $this->list->dataClass
*
* @var string
*/
protected $modelClassName = '';
/**
* the current state of the GridField
*
* @var GridState
*/
protected $state = null;
@ -114,6 +118,7 @@ class GridField extends FormField {
* this modelclass $summary_fields
*
* @param string $modelClassName
*
* @see GridFieldDataColumns::getDisplayFields()
*/
public function setModelClass($modelClassName) {
@ -128,8 +133,8 @@ class GridField extends FormField {
* @return string
*/
public function getModelClass() {
if ($this->modelClassName) return $this->modelClassName;
if ($this->list && method_exists($this->list, 'dataClass')) {
if($this->modelClassName) return $this->modelClassName;
if($this->list && method_exists($this->list, 'dataClass')) {
$class = $this->list->dataClass();
if($class) return $class;
}
@ -149,6 +154,7 @@ class GridField extends FormField {
/**
* @param GridFieldConfig $config
*
* @return GridField
*/
public function setConfig(GridFieldConfig $config) {
@ -165,6 +171,7 @@ class GridField extends FormField {
*
* @param $value
* @param $castingDefinition
*
* @todo refactor this into GridFieldComponent
*/
public function getCastedValue($value, $castingDefinition) {
@ -176,16 +183,16 @@ class GridField extends FormField {
$castingParams = array();
}
if(strpos($castingDefinition,'->') === false) {
if(strpos($castingDefinition, '->') === false) {
$castingFieldType = $castingDefinition;
$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 {
$fieldTypeParts = explode('->', $castingDefinition);
$castingFieldType = $fieldTypeParts[0];
$castingMethod = $fieldTypeParts[1];
$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;
@ -293,7 +300,7 @@ class GridField extends FormField {
$fragmentDefined = array('header' => true, 'footer' => true, 'before' => true, 'after' => true);
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)) {
foreach($matches[1] as $match) {
$fragmentName = strtolower($match);
@ -345,26 +352,18 @@ class GridField extends FormField {
$rowContent = '';
foreach($this->getColumns() as $column) {
$colContent = $this->getColumnContent($record, $column);
// A return value of null means this columns should be skipped altogether.
if($colContent === null) continue;
$colAttributes = $this->getColumnAttributes($record, $column);
$rowContent .= FormField::create_tag('td', $colAttributes, $colContent);
if($colContent === null) {
continue;
}
$classes = array('ss-gridfield-item');
if ($idx == 0) $classes[] = 'first';
if ($idx == $total-1) $classes[] = 'last';
$classes[] = ($idx % 2) ? 'even' : 'odd';
$row = FormField::create_tag(
'tr',
array(
"class" => implode(' ', $classes),
'data-id' => $record->ID,
// TODO Allow per-row customization similar to GridFieldDataColumns
'data-class' => $record->ClassName,
),
$rowContent
);
$rows[] = $row;
$colAttributes = $this->getColumnAttributes($record, $column);
$rowContent .= $this->newCell($this, $total, $idx, $record, $colAttributes, $colContent);
}
$rows[] = $this->newRow($this, $total, $idx, $record, array(), $rowContent);
}
$content['body'] = implode("\n", $rows);
}
@ -417,11 +416,63 @@ class GridField extends FormField {
return
FormField::create_tag('fieldset', $attrs,
$content['before'] .
FormField::create_tag('table', $tableAttrs, $head."\n".$foot."\n".$body) .
FormField::create_tag('table', $tableAttrs, $head . "\n" . $foot . "\n" . $body) .
$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()) {
return $this->FieldHolder($properties);
}
@ -452,6 +503,7 @@ class GridField extends FormField {
*
* @param DataObject $record
* @param string $column
*
* @return string
* @throws InvalidArgumentException
*/
@ -512,6 +564,7 @@ class GridField extends FormField {
*
* @param DataObject $record
* @param string $column
*
* @return array
* @throws LogicException
* @throws InvalidArgumentException
@ -546,6 +599,7 @@ class GridField extends FormField {
* Get metadata for a column, example array('Title'=>'Email address')
*
* @param string $column
*
* @return array
* @throws LogicException
* @throws InvalidArgumentException
@ -610,6 +664,7 @@ class GridField extends FormField {
* This is the action that gets executed when a GridField_AlterAction gets clicked.
*
* @param array $data
*
* @return string
*/
public function gridFieldAlterAction($data, $form, SS_HTTPRequest $request) {
@ -661,6 +716,7 @@ class GridField extends FormField {
* @param string $actionName
* @param mixed $args
* @param arrray $data - send data from a form
*
* @return type
* @throws InvalidArgumentException
*/
@ -671,7 +727,7 @@ class GridField extends FormField {
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);
}
}
@ -704,7 +760,7 @@ class GridField extends FormField {
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
if($params = $request->match($rule, true)) {
// 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(!$action) {
$action = "index";
@ -723,7 +779,8 @@ class GridField extends FormField {
}
if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result)
&& $result instanceof RequestHandler) {
&& $result instanceof RequestHandler
) {
$returnValue = $result->handleRequest($request, $model);
@ -826,7 +883,7 @@ class GridField_FormAction extends FormAction {
* @param string $val
*/
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
$id = 'gf_'.substr(md5(serialize($state)), 0, 8);
$id = 'gf_' . substr(md5(serialize($state)), 0, 8);
Session::set($id, $state);
$actionData['StateID'] = $id;
@ -850,7 +907,7 @@ class GridField_FormAction extends FormAction {
array(
// Note: This field needs to be less than 65 chars, otherwise Suhosin security patch
// 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(),
)
);
@ -860,6 +917,7 @@ class GridField_FormAction extends FormAction {
* Calculate the name of the gridfield relative to the Form
*
* @param GridField $base
*
* @return string
*/
protected function getNameFromParent() {
@ -869,7 +927,7 @@ class GridField_FormAction extends FormAction {
do {
array_unshift($name, $base->getName());
$base = $base->getForm();
} while ($base && !($base instanceof Form));
} while($base && !($base instanceof Form));
return implode('.', $name);
}