Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT Datagrid renders ArrayList according to Datagrid#setModel…

…Class()
  • Loading branch information...
commit b1b1615a588c3ac7455ec9acb9afbdf5fe420578 1 parent 1811fc2
@slindqvist slindqvist authored
View
49 forms/Datagrid.php
@@ -15,20 +15,20 @@ class Datagrid extends FormField {
/**
*
- * @var array
+ * @var string
*/
- protected $fieldList = null;
+ protected $dataPresenterClassName = "DatagridPresenter";
/**
*
- * @var string
+ * @var DatagridPresenter
*/
- protected $dataPresenterClassName = "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 @@ function __construct($name, $title = null, SS_list $datasource = null, Form $for
/**
*
- * @param string $dataClass
+ * @param string $modelClassName
*/
- function setDataclass($dataClass) {
- $this->dataClass = $dataClass;
+ function setModelClass($modelClassName) {
+ $this->modelClassName = $modelClassName;
+ return $this;
}
/**
@@ -70,16 +71,21 @@ function setDataPresenter($dataPresenterClassName) {
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 @@ function getDataclass() {
*/
public function setDatasource(SS_List $datasource) {
$this->datasource = $datasource;
- if($datasource->dataClass){
- $this->fieldList = singleton($datasource->dataClass)->summaryFields();
- }
+ return $this;
}
/**
@@ -104,12 +108,15 @@ public function getDatasource() {
}
/**
- * 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 @@ function FieldList() {
* @return string - html for the form
*/
function FieldHolder() {
- $dataPresenter = new $this->dataPresenterClassName();
- $dataPresenter->setDatagrid($this);
- return $dataPresenter->render();
+ return $this->getDatagridPresenter()->render();
}
}
View
62 forms/DatagridPresenter.php
@@ -90,36 +90,58 @@ function Items() {
* )
*
* @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 @@ public function Parent() {
* @return ArrayList
*/
public function Fields($xmlSafe = true) {
- $list = $this->parent->getDatagrid()->FieldList();
+ $list = $this->parent->FieldList();
+
+
foreach($list as $fieldName => $fieldTitle) {
$value = "";
View
16 tests/forms/DatagridTest.php
@@ -54,16 +54,19 @@ function testSetDataPresenter() {
$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 @@ function testFieldList() {
*/
function testFieldHolder() {
$grid = new Datagrid('Testgrid');
+ $grid->setDatasource(new DataList('DatagridTest_Person'));
$this->assertNotNull($grid->FieldHolder());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.