ENHANCEMENT Datagrid renders ArrayList according to Datagrid#setModelClass()

This commit is contained in:
Stig Lindqvist 2011-09-29 13:47:56 +13:00
parent 1811fc2746
commit b1b1615a58
3 changed files with 82 additions and 49 deletions

View File

@ -13,22 +13,22 @@ class Datagrid extends FormField {
*/ */
protected $datasource = null; protected $datasource = null;
/**
*
* @var array
*/
protected $fieldList = null;
/** /**
* *
* @var string * @var string
*/ */
protected $dataPresenterClassName = "DatagridPresenter"; protected $dataPresenterClassName = "DatagridPresenter";
/**
*
* @var DatagridPresenter
*/
protected $datagridPresenter = null;
/** /**
* @var string - the name of the DataObject that the Datagrid will display * @var string - the name of the DataObject that the Datagrid will display
*/ */
protected $dataClass = ''; protected $modelClassName = '';
/** /**
* Creates a new datagrid field * Creates a new datagrid field
@ -47,10 +47,11 @@ class Datagrid extends FormField {
/** /**
* *
* @param string $dataClass * @param string $modelClassName
*/ */
function setDataclass($dataClass) { function setModelClass($modelClassName) {
$this->dataClass = $dataClass; $this->modelClassName = $modelClassName;
return $this;
} }
/** /**
@ -70,16 +71,21 @@ class Datagrid extends FormField {
throw new Exception('Datapresenter "$dataPresenterClassName" must inherit DatagridPresenter' ); throw new Exception('Datapresenter "$dataPresenterClassName" must inherit DatagridPresenter' );
} }
$this->dataPresenterClassName = $dataPresenterClassName; $this->dataPresenterClassName = $dataPresenterClassName;
return $this;
} }
/** /**
* *
* @return type * @return type
*/ */
function getDataclass() { function getModelClass() {
if ($this->dataClass) return $this->dataClass; if ($this->modelClassName) {
if ($this->datasource->dataClass) return $this->datasource->dataClass; return $this->modelClassName;
throw new Exception(get_class($this).' does not have a dataclass'); }
if ($this->datasource->dataClass) {
return $this->datasource->dataClass;
}
throw new Exception(get_class($this).' does not have a modelClassName');
} }
/** /**
@ -89,9 +95,7 @@ class Datagrid extends FormField {
*/ */
public function setDatasource(SS_List $datasource) { public function setDatasource(SS_List $datasource) {
$this->datasource = $datasource; $this->datasource = $datasource;
if($datasource->dataClass){ return $this;
$this->fieldList = singleton($datasource->dataClass)->summaryFields();
}
} }
/** /**
@ -104,12 +108,15 @@ class Datagrid extends FormField {
} }
/** /**
* Returns the list of fields, or the 'column header' names of this grid
* *
* @return array - e.g array('ID'=>'ID', 'Name'=>'Name) * @return DatagridPresenter
*/ */
function FieldList() { public function getDatagridPresenter(){
return $this->fieldList; if(!$this->datagridPresenter) {
$this->datagridPresenter = new $this->dataPresenterClassName();
$this->datagridPresenter->setDatagrid($this);
}
return $this->datagridPresenter;
} }
/** /**
@ -117,8 +124,6 @@ class Datagrid extends FormField {
* @return string - html for the form * @return string - html for the form
*/ */
function FieldHolder() { function FieldHolder() {
$dataPresenter = new $this->dataPresenterClassName(); return $this->getDatagridPresenter()->render();
$dataPresenter->setDatagrid($this);
return $dataPresenter->render();
} }
} }

View File

@ -90,31 +90,53 @@ class DatagridPresenter extends ViewableData {
* ) * )
* *
* @return ArrayList * @return ArrayList
* @throws Exception
*/ */
public function Headers() { public function Headers() {
if($this->datagrid->datasource instanceof DataList ) {
return $this->getHeadersFromDatalist(); if(!$this->getDatasource()) {
} else { throw new Exception(get_class($this->getDatagrid()). ' needs an data source to be able to render the form');
if(!$this->datasource) {
return array();
}
$firstItem = $this->datasource->first();
if(!$firstItem) {
return array();
}
return array_combine(array_keys($firstItem),array_keys($firstItem));
} }
$summaryFields = singleton($this->getModelClass())->summaryFields();
return $this->summaryFieldsToList($summaryFields);
} }
/** /**
* *
* @return SS_List
*/
protected function getDataSource() {
return $this->getDatagrid()->getDatasource();
}
/**
*
* @return string - name of model
*/
protected function getModelClass() {
return $this->getDatagrid()->getModelClass();
}
/**
*
* @return array
*/
public function FieldList() {
return singleton($this->getModelClass())->summaryFields();
}
/**
* Translate the summaryFields from a model into a format that is understood
* by the Form renderer
*
* @param array $summaryFields
* @return ArrayList * @return ArrayList
*/ */
protected function getHeadersFromDatalist(){ protected function summaryFieldsToList($summaryFields) {
$fieldHeaders = new ArrayList(); $fieldHeaders = new ArrayList();
$fieldHeadersSummaryFields = singleton($this->datagrid->datasource->dataClass)->summaryFields(); if (is_array($summaryFields)){
if (is_array($fieldHeadersSummaryFields)){ foreach ($summaryFields as $name=>$title){
foreach ($fieldHeadersSummaryFields as $name=>$title){
$fieldHeaders->push(new ArrayData(array('Name'=>$name, 'Title'=>$title))); $fieldHeaders->push(new ArrayData(array('Name'=>$name, 'Title'=>$title)));
} }
} }
@ -160,7 +182,7 @@ class DatagridPresenter_Item extends ViewableData {
protected $item; protected $item;
/** /**
* @var Datagrid * @var DatagridPresenter
*/ */
protected $parent; protected $parent;
@ -197,7 +219,9 @@ class DatagridPresenter_Item extends ViewableData {
* @return ArrayList * @return ArrayList
*/ */
public function Fields($xmlSafe = true) { public function Fields($xmlSafe = true) {
$list = $this->parent->getDatagrid()->FieldList(); $list = $this->parent->FieldList();
foreach($list as $fieldName => $fieldTitle) { foreach($list as $fieldName => $fieldTitle) {
$value = ""; $value = "";

View File

@ -54,16 +54,19 @@ class DatagridTest extends SapphireTest {
$grid->setDataPresenter('DatagridPresenter'); $grid->setDataPresenter('DatagridPresenter');
} }
function testFieldListIsNullWithoutDataSource() { function testSetDataclass() {
$grid = new Datagrid('Testgrid'); $grid = new Datagrid('Testgrid');
$this->assertNull($grid->FieldList()); $grid->setModelClass('SiteTree');
$this->assertEquals('SiteTree', $grid->getModelClass());
} }
function testFieldList() { /**
*
*/
function testFieldHolderWithoutDataSource() {
$this->setExpectedException('Exception');
$grid = new Datagrid('Testgrid'); $grid = new Datagrid('Testgrid');
$grid->setDatasource(new DataList('DatagridTest_Person')); $this->assertNotNull($grid->FieldHolder());
$this->assertNotNull($grid->FieldList());
$this->assertEquals(array('Name'=>'Name','ID'=>'ID'), $grid->FieldList());
} }
/** /**
@ -73,6 +76,7 @@ class DatagridTest extends SapphireTest {
*/ */
function testFieldHolder() { function testFieldHolder() {
$grid = new Datagrid('Testgrid'); $grid = new Datagrid('Testgrid');
$grid->setDatasource(new DataList('DatagridTest_Person'));
$this->assertNotNull($grid->FieldHolder()); $this->assertNotNull($grid->FieldHolder());
} }
} }