diff --git a/forms/Datagrid.php b/forms/Datagrid.php new file mode 100644 index 000000000..481cb4425 --- /dev/null +++ b/forms/Datagrid.php @@ -0,0 +1,68 @@ +datasource = $datasource; + } + + /** + * Get the datasource + * + * @return SS_list + */ + public function getDatasource() { + return $this->datasource; + } + + /** + * Get the headers or column names for this grid + * + * The returning array will have the format of + * array( + * 'FirstName' => 'First name', + * 'Description' => 'A nice description' + * ) + * + * @return array + */ + public function getHeaders() { + + if($this->datasource instanceof DataList ) { + return singleton($this->datasource->dataClass)->summaryFields(); + } else { + $firstItem = $this->datasource->first(); + if(!$firstItem) { + return array(); + } + return array_combine(array_keys($firstItem),array_keys($firstItem)); + } + } +} diff --git a/tests/forms/DatagridFunctionalTest.php b/tests/forms/DatagridFunctionalTest.php new file mode 100644 index 000000000..77288be27 --- /dev/null +++ b/tests/forms/DatagridFunctionalTest.php @@ -0,0 +1,26 @@ +assertTrue(new Datagrid('testgrid') instanceof Datagrid, 'Trying to find an instance of Datagrid.'); + } + + public function testAddToForm() { + $response = $this->get("DatagridFunctionalTest_Controller/"); + $this->assertContains("form", $response->getBody()); + } +} + +class DatagridFunctionalTest_Controller extends ContentController { + + public function index() { + $grid = new Datagrid('testgrid'); + $form = new Form($this, 'gridform', new FieldList($grid), new FieldList(new FormAction('rerender', 'rerender'))); + return array('Form'=>$form); + } +} \ No newline at end of file diff --git a/tests/forms/DatagridTest.php b/tests/forms/DatagridTest.php new file mode 100644 index 000000000..1fbaebab0 --- /dev/null +++ b/tests/forms/DatagridTest.php @@ -0,0 +1,66 @@ +assertTrue(new Datagrid('Testgrid') instanceof FormField, 'Datagrid should be a FormField'); + } + + public function testSetDataSource() { + $grid = new Datagrid('Testgrid'); + $source = new ArrayList(); + $grid->setDatasource( $source ); + $this->assertEquals($source, $grid->getDatasource()); + } + + public function testGetDefaultHeadersFromEmptyArrayList() { + $grid = new Datagrid('Testgrid'); + $source = new ArrayList(); + $grid->setDatasource($source); + $this->assertEquals(array(), $grid->getHeaders()); + } + + public function testGetDefaultHeadersFromArrayList() { + $grid = new Datagrid('Testgrid'); + $source = new ArrayList(array(array('ID'=>1,'Name'=>'Aaron Aardwark'))); + $grid->setDatasource($source); + $this->assertEquals(array('ID'=>'ID','Name'=>'Name'), $grid->getHeaders()); + } + + public function testGetDefaultHeadersFromDataList() { + $grid = new Datagrid('Testgrid'); + $source = new DataList('DatagridTest_Person'); + $grid->setDatasource($source); + $this->assertEquals(array('Name'=>'Name','ID'=>'ID'), $grid->getHeaders()); + } +} + +class DatagridTest_Person extends Dataobject implements TestOnly { + + public static $db = array( + 'Name' => 'Varchar' + ); + + public static $summary_fields = array( + 'Name', + 'ID' + ); +} \ No newline at end of file diff --git a/tests/forms/DatagridTest.yml b/tests/forms/DatagridTest.yml new file mode 100644 index 000000000..d9ead4ba2 --- /dev/null +++ b/tests/forms/DatagridTest.yml @@ -0,0 +1,3 @@ +DatagridTest_Person: + first: + Name: First Person \ No newline at end of file