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;
/**
*
* @var array
*/
protected $fieldList = null;
/**
*
* @var string
*/
protected $dataPresenterClassName = "DatagridPresenter";
/**
*
* @var DatagridPresenter
*/
protected $datagridPresenter = null;
/**
* @var string - the name of the DataObject that the Datagrid will display
*/
protected $dataClass = '';
protected $modelClassName = '';
/**
* Creates a new datagrid field
@ -47,10 +47,11 @@ class Datagrid extends FormField {
/**
*
* @param string $dataClass
* @param string $modelClassName
*/
function setDataclass($dataClass) {
$this->dataClass = $dataClass;
function setModelClass($modelClassName) {
$this->modelClassName = $modelClassName;
return $this;
}
/**
@ -70,16 +71,21 @@ class Datagrid extends FormField {
throw new Exception('Datapresenter "$dataPresenterClassName" must inherit DatagridPresenter' );
}
$this->dataPresenterClassName = $dataPresenterClassName;
return $this;
}
/**
*
* @return type
*/
function getDataclass() {
if ($this->dataClass) return $this->dataClass;
if ($this->datasource->dataClass) return $this->datasource->dataClass;
throw new Exception(get_class($this).' does not have a dataclass');
function getModelClass() {
if ($this->modelClassName) {
return $this->modelClassName;
}
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) {
$this->datasource = $datasource;
if($datasource->dataClass){
$this->fieldList = singleton($datasource->dataClass)->summaryFields();
}
return $this;
}
/**
@ -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() {
return $this->fieldList;
public function getDatagridPresenter(){
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
*/
function FieldHolder() {
$dataPresenter = new $this->dataPresenterClassName();
$dataPresenter->setDatagrid($this);
return $dataPresenter->render();
return $this->getDatagridPresenter()->render();
}
}

View File

@ -90,36 +90,58 @@ class DatagridPresenter extends ViewableData {
* )
*
* @return ArrayList
* @throws Exception
*/
public function Headers() {
if($this->datagrid->datasource instanceof DataList ) {
return $this->getHeadersFromDatalist();
} else {
if(!$this->datasource) {
return array();
}
$firstItem = $this->datasource->first();
if(!$firstItem) {
return array();
}
return array_combine(array_keys($firstItem),array_keys($firstItem));
if(!$this->getDatasource()) {
throw new Exception(get_class($this->getDatagrid()). ' needs an data source to be able to render the form');
}
$summaryFields = singleton($this->getModelClass())->summaryFields();
return $this->summaryFieldsToList($summaryFields);
}
/**
*
* @return ArrayList
* @return SS_List
*/
protected function getHeadersFromDatalist(){
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
*/
protected function summaryFieldsToList($summaryFields) {
$fieldHeaders = new ArrayList();
$fieldHeadersSummaryFields = singleton($this->datagrid->datasource->dataClass)->summaryFields();
if (is_array($fieldHeadersSummaryFields)){
foreach ($fieldHeadersSummaryFields as $name=>$title){
if (is_array($summaryFields)){
foreach ($summaryFields as $name=>$title){
$fieldHeaders->push(new ArrayData(array('Name'=>$name, 'Title'=>$title)));
}
}
return $fieldHeaders;
}
}
/**
*
@ -160,7 +182,7 @@ class DatagridPresenter_Item extends ViewableData {
protected $item;
/**
* @var Datagrid
* @var DatagridPresenter
*/
protected $parent;
@ -197,7 +219,9 @@ class DatagridPresenter_Item extends ViewableData {
* @return ArrayList
*/
public function Fields($xmlSafe = true) {
$list = $this->parent->getDatagrid()->FieldList();
$list = $this->parent->FieldList();
foreach($list as $fieldName => $fieldTitle) {
$value = "";

View File

@ -54,16 +54,19 @@ class DatagridTest extends SapphireTest {
$grid->setDataPresenter('DatagridPresenter');
}
function testFieldListIsNullWithoutDataSource() {
function testSetDataclass() {
$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->setDatasource(new DataList('DatagridTest_Person'));
$this->assertNotNull($grid->FieldList());
$this->assertEquals(array('Name'=>'Name','ID'=>'ID'), $grid->FieldList());
$this->assertNotNull($grid->FieldHolder());
}
/**
@ -73,6 +76,7 @@ class DatagridTest extends SapphireTest {
*/
function testFieldHolder() {
$grid = new Datagrid('Testgrid');
$grid->setDatasource(new DataList('DatagridTest_Person'));
$this->assertNotNull($grid->FieldHolder());
}
}