diff --git a/.gitignore b/.gitignore index 5cd3d2901..4b114324f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .sass-cache -.DS_Store \ No newline at end of file +.DS_Store diff --git a/css/DataGrid.css b/css/DataGrid.css deleted file mode 100644 index 124a0fbb9..000000000 --- a/css/DataGrid.css +++ /dev/null @@ -1,17 +0,0 @@ -/** Core styles for the basic DataGrid form field without any specific style. @package sapphire @subpackage scss */ -.ss-datagrid { border: none; } -.ss-datagrid table { width: 100%; border-collapse: collapse; border-spacing: 0; background: #fff; border: 1px solid #c1c1c1; } -.ss-datagrid thead { color: #5a5a5a; background: #dadada; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f3f3f3), color-stop(100%, #dadada)); background-image: -webkit-linear-gradient(#f3f3f3, #dadada); background-image: -moz-linear-gradient(#f3f3f3, #dadada); background-image: -o-linear-gradient(#f3f3f3, #dadada); background-image: -ms-linear-gradient(#f3f3f3, #dadada); background-image: linear-gradient(#f3f3f3, #dadada); } -.ss-datagrid thead th { font-weight: bold; padding: 8px 24px 8px 8px; position: relative; border: 1px solid #c1c1c1; border-width: 0 1px 1px 0; } -.ss-datagrid thead th.ss-datagrid-sortable.hover { color: #747474; cursor: pointer; background: #f3f3f3; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #ffffff), color-stop(100%, #e7e7e7)); background-image: -webkit-linear-gradient(#ffffff, #e7e7e7); background-image: -moz-linear-gradient(#ffffff, #e7e7e7); background-image: -o-linear-gradient(#ffffff, #e7e7e7); background-image: -ms-linear-gradient(#ffffff, #e7e7e7); background-image: linear-gradient(#ffffff, #e7e7e7); } -.ss-datagrid thead th.ss-datagrid-sorted { background: #e7e7e7; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #dadada), color-stop(100%, #f3f3f3)); background-image: -webkit-linear-gradient(#dadada, #f3f3f3); background-image: -moz-linear-gradient(#dadada, #f3f3f3); background-image: -o-linear-gradient(#dadada, #f3f3f3); background-image: -ms-linear-gradient(#dadada, #f3f3f3); background-image: linear-gradient(#dadada, #f3f3f3); } -.ss-datagrid thead th.ss-datagrid-sorted.hover { background: #f3f3f3; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #e7e7e7), color-stop(100%, #ffffff)); background-image: -webkit-linear-gradient(#e7e7e7, #ffffff); background-image: -moz-linear-gradient(#e7e7e7, #ffffff); background-image: -o-linear-gradient(#e7e7e7, #ffffff); background-image: -ms-linear-gradient(#e7e7e7, #ffffff); background-image: linear-gradient(#e7e7e7, #ffffff); } -.ss-datagrid thead th .ui-icon { position: absolute; top: 5px; right: 0; } -.ss-datagrid thead th.ss-datagrid-desc .ui-icon { background-position: 0 -48px; } -.ss-datagrid thead th.ss-datagrid-asc .ui-icon { background-position: -64px -48px; } -.ss-datagrid td { padding: 8px; border-right: 1px solid #f3f3f3; } -.ss-datagrid td.ss-datagrid-last { border-right: none; } -.ss-datagrid tr.ss-datagrid-even { border: 1px solid #c6e5f6; border-width: 1px 0; background: #f2f9fd; } -.ss-datagrid tr.ss-datagrid-even.ss-datagrid-last { border-bottom: none; } -.ss-datagrid tr.ss-datagrid-even td { border-right: 1px solid #dceffa; } -.ss-datagrid tr.ss-datagrid-even td.ss-datagrid-last { border-right: none; } diff --git a/css/GridField.css b/css/GridField.css new file mode 100644 index 000000000..f8644546e --- /dev/null +++ b/css/GridField.css @@ -0,0 +1,17 @@ +/** Core styles for the basic GridField form field without any specific style. @package sapphire @subpackage scss */ +.ss-gridfield { border: none; } +.ss-gridfield table { width: 100%; border-collapse: collapse; border-spacing: 0; background: #fff; border: 1px solid #c1c1c1; } +.ss-gridfield thead { color: #5a5a5a; background: #dadada; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f3f3f3), color-stop(100%, #dadada)); background-image: -webkit-linear-gradient(#f3f3f3, #dadada); background-image: -moz-linear-gradient(#f3f3f3, #dadada); background-image: -o-linear-gradient(#f3f3f3, #dadada); background-image: -ms-linear-gradient(#f3f3f3, #dadada); background-image: linear-gradient(#f3f3f3, #dadada); } +.ss-gridfield thead th { font-weight: bold; padding: 8px 24px 8px 8px; position: relative; border: 1px solid #c1c1c1; border-width: 0 1px 1px 0; } +.ss-gridfield thead th.ss-gridfield-sortable.hover { color: #747474; cursor: pointer; background: #f3f3f3; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #ffffff), color-stop(100%, #e7e7e7)); background-image: -webkit-linear-gradient(#ffffff, #e7e7e7); background-image: -moz-linear-gradient(#ffffff, #e7e7e7); background-image: -o-linear-gradient(#ffffff, #e7e7e7); background-image: -ms-linear-gradient(#ffffff, #e7e7e7); background-image: linear-gradient(#ffffff, #e7e7e7); } +.ss-gridfield thead th.ss-gridfield-sorted { background: #e7e7e7; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #dadada), color-stop(100%, #f3f3f3)); background-image: -webkit-linear-gradient(#dadada, #f3f3f3); background-image: -moz-linear-gradient(#dadada, #f3f3f3); background-image: -o-linear-gradient(#dadada, #f3f3f3); background-image: -ms-linear-gradient(#dadada, #f3f3f3); background-image: linear-gradient(#dadada, #f3f3f3); } +.ss-gridfield thead th.ss-gridfield-sorted.hover { background: #f3f3f3; background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #e7e7e7), color-stop(100%, #f9f9f9)); background-image: -webkit-linear-gradient(#e7e7e7, #f9f9f9); background-image: -moz-linear-gradient(#e7e7e7, #f9f9f9); background-image: -o-linear-gradient(#e7e7e7, #f9f9f9); background-image: -ms-linear-gradient(#e7e7e7, #f9f9f9); background-image: linear-gradient(#e7e7e7, #f9f9f9); } +.ss-gridfield thead th .ui-icon { position: absolute; top: 5px; right: 0; } +.ss-gridfield thead th.ss-gridfield-desc .ui-icon { background-position: 0 -48px; } +.ss-gridfield thead th.ss-gridfield-asc .ui-icon { background-position: -64px -48px; } +.ss-gridfield td { padding: 8px; border-right: 1px solid #f3f3f3; } +.ss-gridfield td.ss-gridfield-last { border-right: none; } +.ss-gridfield tr.ss-gridfield-even { border: 1px solid #c6e5f6; border-width: 1px 0; background: #f2f9fd; } +.ss-gridfield tr.ss-gridfield-even.ss-gridfield-last { border-bottom: none; } +.ss-gridfield tr.ss-gridfield-even td { border-right: 1px solid #dceffa; } +.ss-gridfield tr.ss-gridfield-even td.ss-gridfield-last { border-right: none; } diff --git a/forms/Datagrid.php b/forms/Datagrid.php deleted file mode 100644 index 7b9b34e9f..000000000 --- a/forms/Datagrid.php +++ /dev/null @@ -1,129 +0,0 @@ -setDatasource($datasource); - if ($dataPresenterClassName) $this->setDataPresenter($dataPresenterClassName); - } - - /** - * - * @param string $modelClassName - */ - function setModelClass($modelClassName) { - $this->modelClassName = $modelClassName; - return $this; - } - - /** - * - * @param string $dataPresenterClassName - * @throws Exception - */ - function setDataPresenter($dataPresenterClassName) { - if(!$dataPresenterClassName){ - throw new Exception('Datapresenter for Datagrid must be set with a class'); - } - if(!class_exists($dataPresenterClassName)){ - throw new Exception('Datapresenter for Datagrid must be set with an existing class'); - } - - if($dataPresenterClassName !='DatagridPresenter' && !ClassInfo::is_subclass_of($dataPresenterClassName, 'DatagridPresenter')){ - throw new Exception('Datapresenter "$dataPresenterClassName" must inherit DatagridPresenter' ); - } - $this->dataPresenterClassName = $dataPresenterClassName; - return $this; - } - - /** - * - * @return type - */ - 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'); - } - - /** - * Set the datasource - * - * @param SS_List $datasource - */ - public function setDatasource(SS_List $datasource) { - $this->datasource = $datasource; - return $this; - } - - /** - * Get the datasource - * - * @return SS_list - */ - public function getDatasource() { - return $this->datasource; - } - - /** - * - * @return DatagridPresenter - */ - public function getDatagridPresenter(){ - if(!$this->datagridPresenter) { - $this->datagridPresenter = new $this->dataPresenterClassName(); - $this->datagridPresenter->setDatagrid($this); - } - return $this->datagridPresenter; - } - - /** - * - * @return string - html for the form - */ - function FieldHolder() { - return $this->getDatagridPresenter()->render(); - } -} \ No newline at end of file diff --git a/forms/GridField.php b/forms/GridField.php new file mode 100644 index 000000000..c000137ef --- /dev/null +++ b/forms/GridField.php @@ -0,0 +1,140 @@ +setDatasource($datasource); + } + + $this->setPresenter($dataPresenterClassName); + } + + /** + * @param string $modelClassName + */ + public function setModelClass($modelClassName) { + $this->modelClassName = $modelClassName; + + return $this; + } + + /** + * @throws Exception + * @return string + */ + public 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'); + } + + /** + * @param string|GridFieldPresenter + * + * @throws Exception + */ + public function setPresenter($presenter) { + if(!$presenter){ + throw new Exception('setPresenter() for GridField must be set with a class'); + } + + if(is_object($presenter)) { + $this->presenter = $presenter; + $this->presenter->setGridField($this); + + return; + } + + if(!class_exists($presenter)){ + throw new Exception('DataPresenter for GridField must be set with an existing class, '.$presenter.' does not exists.'); + } + + if($presenter !='GridFieldPresenter' && !ClassInfo::is_subclass_of($presenter, 'GridFieldPresenter')) { + throw new Exception(sprintf( + 'DataPresenter "%s" must subclass GridFieldPresenter', $presenter + )); + } + + $this->presenter = new $presenter; + $this->presenter->setGridField($this); + + return $this; + } + + /** + * @return GridFieldPresenter + */ + public function getPresenter(){ + return $this->presenter; + } + + /** + * Set the datasource + * + * @param SS_List $datasource + */ + public function setDataSource(SS_List $datasource) { + $this->datasource = $datasource; + + return $this; + } + + /** + * Get the datasource + * + * @return SS_List + */ + public function getDataSource() { + return $this->datasource; + } + + /** + * @return string - html for the form + */ + function FieldHolder() { + return $this->getPresenter()->render(); + } +} + \ No newline at end of file diff --git a/forms/DatagridPresenter.php b/forms/GridFieldPresenter.php similarity index 57% rename from forms/DatagridPresenter.php rename to forms/GridFieldPresenter.php index aa91eaf4e..306ba4098 100644 --- a/forms/DatagridPresenter.php +++ b/forms/GridFieldPresenter.php @@ -1,45 +1,49 @@ datagrid = $datagrid; + public function setGridField(GridField $grid){ + $this->GridField = $grid; } /** - * - * @return Datagrid + * @return GridField */ - public function getDatagrid(){ - return $this->datagrid; + public function getGridField(){ + return $this->GridField; } /** - * Return a ArrayList of Datagrid_Item objects, suitable for display in the template. + * Sort the grid by columns + * + * @param string $column + * @param string $direction + */ + public function sort($column, $direction = 'asc') { + $this->sorting[$column] = $direction; + + return $this; + } + + /** + * Return an {@link ArrayList} of {@link GridField_Item} objects, suitable for display in the template. * * @return ArrayList */ public function Items() { - $fieldItems = new ArrayList(); - if($items = $this->getDatagrid()->getDatasource()) { + $items = new ArrayList(); + + if($this->sorting) { + $this->setSorting($this->sorting); + } + + if($sources = $this->getGridField()->getDataSource()) { $counter = 0; - foreach($items as $item) { - if(!$item) { + + foreach($sources as $source) { + if(!$source) { continue; } - $datagridPresenterItem = new $this->itemClass($item, $this); - $datagridPresenterItem->iteratorProperties($counter++, $items->count()); - $fieldItems->push($datagridPresenterItem); + + $itemPresenter = new $this->itemClass($source, $this); + $itemPresenter->iteratorProperties($counter++, $sources->count()); + + $items->push($itemPresenter); } } - return $fieldItems; + + return $items; } /** * 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' - * ) + * + * + * array( + * 'FirstName' => 'First name', + * 'Description' => 'A nice description' + * ) + * * * @return ArrayList * @throws Exception */ public function Headers() { - if(!$this->getDatasource()) { - throw new Exception(get_class($this->getDatagrid()). ' needs an data source to be able to render the form'); + throw new Exception(sprintf( + '%s needs an data source to be able to render the form', get_class($this->getGridField()) + )); } $summaryFields = singleton($this->getModelClass())->summaryFields(); + return $this->summaryFieldsToList($summaryFields); } /** - * * @return SS_List */ protected function getDataSource() { - return $this->getDatagrid()->getDatasource(); + return $this->getGridField()->getDatasource(); } /** - * * @return string - name of model */ protected function getModelClass() { - return $this->getDatagrid()->getModelClass(); + return $this->getGridField()->getModelClass(); } /** + * Add the combined sorting on the datasource + * + * If the sorting isn't set in one go on the datasource, only the latest sort + * will be executed.s * + * @param array $sortColumns + */ + protected function setSorting(array $sortColumns) { + $resultColumns = array(); + + foreach($sortColumns as $column => $sortOrder) { + $resultColumns[] = sprintf("%s %s", $column ,$sortOrder); + } + + $sort = implode(', ', $resultColumns); + $this->getDataSource()->sort($sort); + } + + /** * @return array */ public function FieldList() { @@ -134,30 +178,41 @@ class DatagridPresenter extends ViewableData { * by the Form renderer * * @param array $summaryFields + * * @return ArrayList */ protected function summaryFieldsToList($summaryFields) { - $fieldHeaders = new ArrayList(); - if (is_array($summaryFields)){ + $headers = new ArrayList(); + + if(is_array($summaryFields)) { $counter = 0; - foreach ($summaryFields as $name=>$title){ - $arrayData = new ArrayData(array( - 'Name'=>$name, - 'Title'=>$title, - 'IsSortable'=>true, - 'IsSorted'=>false, - 'SortedDirection'=>'desc') + + foreach ($summaryFields as $name => $title) { + $data = array( + 'Name' => $name, + 'Title' => $title, + 'IsSortable' => true, + 'IsSorted' => false, + 'SortedDirection' => 'asc' ); - $arrayData->iteratorProperties($counter++, count($summaryFields)); - $fieldHeaders->push($arrayData); + + if(array_key_exists($name, $this->sorting)) { + $data['IsSorted'] = true; + $data['SortedDirection'] = $this->sorting[$name]; + } + + $result = new ArrayData($data); + $result->iteratorProperties($counter++, count($summaryFields)); + + $headers->push($result); } } - return $fieldHeaders; + + return $headers; } /** - * - * @param type $casting + * @param array $casting */ function setFieldCasting($casting) { $this->fieldCasting = $casting; @@ -172,7 +227,6 @@ class DatagridPresenter extends ViewableData { } /** - * * @return string - html */ function render(){ @@ -181,36 +235,36 @@ class DatagridPresenter extends ViewableData { } /** - * A single record in a Datagrid. - * @package forms - * @see Datagrid + * A single record in a GridField. + * + * @package sapphire + * @see GridField */ -class DatagridPresenter_Item extends ViewableData { +class GridFieldPresenter_Item extends ViewableData { /** - * @var DataObject The underlying data record, - * usually an element of {@link Datagrid->datasource()}. + * @var Object The underlying record, usually an element of + * {@link GridField->datasource()}. */ protected $item; /** - * @var DatagridPresenter + * @var GridFieldPresenter */ protected $parent; /** - * - * @param type $item - * @param type $parent + * @param Object $item + * @param GridFieldPresenter $parent */ public function __construct($item, $parent) { $this->failover = $this->item = $item; $this->parent = $parent; + parent::__construct(); } /** - * * @return int */ public function ID() { @@ -218,7 +272,6 @@ class DatagridPresenter_Item extends ViewableData { } /** - * * @return type */ public function Parent() { @@ -227,14 +280,14 @@ class DatagridPresenter_Item extends ViewableData { /** - * * @param bool $xmlSafe + * * @return ArrayList */ public function Fields($xmlSafe = true) { $list = $this->parent->FieldList(); - $counter = 0; + foreach($list as $fieldName => $fieldTitle) { $value = ""; @@ -242,10 +295,12 @@ class DatagridPresenter_Item extends ViewableData { // This supports simple FieldName syntax if(strpos($fieldName,'.') === false) { $value = ($this->item->XML_val($fieldName) && $xmlSafe) ? $this->item->XML_val($fieldName) : $this->item->RAW_val($fieldName); + // This support the syntax fieldName = Relation.RelatedField } else { $fieldNameParts = explode('.', $fieldName) ; $tmpItem = $this->item; + for($j=0;$jparent->getDatagrid()->fieldEscape){ + if($escape = $this->parent->getGridField()->fieldEscape){ foreach($escape as $search => $replace){ $value = str_replace($search, $replace, $value); } @@ -288,6 +343,7 @@ class DatagridPresenter_Item extends ViewableData { $arrayData->iteratorProperties($counter++, count($list)); $fields[] = $arrayData; } + return new ArrayList($fields); } } diff --git a/scss/DataGrid.scss b/scss/GridField.scss similarity index 84% rename from scss/DataGrid.scss rename to scss/GridField.scss index 4274def13..d98b3f705 100644 --- a/scss/DataGrid.scss +++ b/scss/GridField.scss @@ -1,5 +1,5 @@ /** - * Core styles for the basic DataGrid form field without any specific style. + * Core styles for the basic GridField form field without any specific style. * * @package sapphire * @subpackage scss @@ -10,7 +10,7 @@ $base: #dadada; $zebra: #f2f9fd; -.ss-datagrid { +.ss-gridfield { border: none; table { @@ -38,7 +38,7 @@ $zebra: #f2f9fd; border: 1px solid darken($base, 10%); border-width: 0 1px 1px 0; - &.ss-datagrid-sortable { + &.ss-gridfield-sortable { &.hover { color: darken($base, 40%); @@ -52,7 +52,7 @@ $zebra: #f2f9fd; } } - &.ss-datagrid-sorted { + &.ss-gridfield-sorted { background: lighten($base, 5%); @include background-image(linear-gradient( @@ -74,11 +74,11 @@ $zebra: #f2f9fd; right: 0; } - &.ss-datagrid-desc .ui-icon { + &.ss-gridfield-desc .ui-icon { background-position: 0 -48px; } - &.ss-datagrid-asc .ui-icon { + &.ss-gridfield-asc .ui-icon { background-position: -64px -48px; } } @@ -92,26 +92,26 @@ $zebra: #f2f9fd; padding: 8px; border-right: 1px solid lighten($base, 10%); - &.ss-datagrid-last { + &.ss-gridfield-last { border-right: none; } } tr { - &.ss-datagrid-even { + &.ss-gridfield-even { border: 1px solid darken($zebra, 10%); border-width: 1px 0; background: $zebra; - &.ss-datagrid-last { + &.ss-gridfield-last { border-bottom: none; } td { border-right: 1px solid darken($zebra, 5%); - &.ss-datagrid-last { + &.ss-gridfield-last { border-right: none; } } diff --git a/templates/GridField.ss b/templates/GridField.ss new file mode 100644 index 000000000..ee875c0d4 --- /dev/null +++ b/templates/GridField.ss @@ -0,0 +1,25 @@ +<% require css(sapphire/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css) %> +<% require css(sapphire/css/GridField.css) %> + +
+ + + + <% control Headings %> + + <% end_control %> + + + + + <% control Items %> + <% include GridField_Item %> + <% end_control %> + + + + + +
+ $Title
+
\ No newline at end of file diff --git a/templates/DatagridPresenter.ss b/templates/GridFieldPresenter.ss similarity index 50% rename from templates/DatagridPresenter.ss rename to templates/GridFieldPresenter.ss index f6dc538b1..e919d32d6 100644 --- a/templates/DatagridPresenter.ss +++ b/templates/GridFieldPresenter.ss @@ -1,12 +1,12 @@ <% require css(sapphire/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css) %> -<% require css(sapphire/css/DataGrid.css) %> +<% require css(sapphire/css/GridField.css) %> -
+
<% control Headers %> - <% end_control %> @@ -14,7 +14,7 @@ <% control Items %> - <% include DataGrid_Item %> + <% include GridField_Item %> <% end_control %> diff --git a/templates/Includes/DataGrid_Item.ss b/templates/Includes/DataGrid_Item.ss deleted file mode 100644 index 27f02e7e5..000000000 --- a/templates/Includes/DataGrid_Item.ss +++ /dev/null @@ -1,5 +0,0 @@ - - <% control Fields %> - - <% end_control %> - \ No newline at end of file diff --git a/templates/Includes/GridField_Item.ss b/templates/Includes/GridField_Item.ss new file mode 100644 index 000000000..882f1caa2 --- /dev/null +++ b/templates/Includes/GridField_Item.ss @@ -0,0 +1,5 @@ + + <% control Fields %> + + <% end_control %> + \ No newline at end of file diff --git a/tests/forms/DatagridFunctionalTest.php b/tests/forms/DatagridFunctionalTest.php deleted file mode 100644 index a1c98ee1c..000000000 --- a/tests/forms/DatagridFunctionalTest.php +++ /dev/null @@ -1,45 +0,0 @@ -objFromFixture('DatagridTest_Person', 'first'); - $response = $this->get("DatagridFunctionalTest_Controller/"); - $this->assertContains($firstPerson->Name, $response->getBody()); - } -} - -class DatagridFunctionalTest_Controller extends Controller { - - protected $template = 'BlankPage'; - - function Link($action = null) { - return Controller::join_links('DatagridFunctionalTest_Controller', $action); - } - - public function index() { - $grid = new Datagrid('testgrid'); - $dataSource = DataList::create("DatagridTest_Person")->sort("Name"); - $grid->setDataSource($dataSource); - $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/DatagridPresenterTest.php b/tests/forms/DatagridPresenterTest.php deleted file mode 100644 index 7fe55d732..000000000 --- a/tests/forms/DatagridPresenterTest.php +++ /dev/null @@ -1,44 +0,0 @@ -assertTrue(new DatagridPresenter instanceof DatagridPresenter, 'Trying to find an instance of DatagridPresenter'); - } - - public function testHeaders() { - $presenter = new DatagridPresenter(); - $grid = new Datagrid('testgrid', 'testgrid', new DataList('DatagridTest_Person')); - $presenter->setDatagrid($grid); - $headers = $presenter->Headers()->first(); - - $this->assertEquals(1, count($headers)); - $this->assertEquals('Name', $headers->Name ); - } - - public function testItemsReturnCorrectNumberOfItems() { - $presenter = new DatagridPresenter(); - $grid = new Datagrid('testgrid', 'testgrid', new DataList('DatagridTest_Person')); - $presenter->setDatagrid($grid); - $this->assertEquals(2, $presenter->Items()->count()); - } - -} \ No newline at end of file diff --git a/tests/forms/GridFieldFunctionalTest.php b/tests/forms/GridFieldFunctionalTest.php new file mode 100644 index 000000000..0050b8896 --- /dev/null +++ b/tests/forms/GridFieldFunctionalTest.php @@ -0,0 +1,45 @@ +objFromFixture('GridFieldTest_Person', 'first'); + $response = $this->get("GridFieldFunctionalTest_Controller/"); + $this->assertContains($firstPerson->Name, $response->getBody()); + } +} + +class GridFieldFunctionalTest_Controller extends Controller { + + protected $template = 'BlankPage'; + + function Link($action = null) { + return Controller::join_links('GridFieldFunctionalTest_Controller', $action); + } + + public function index() { + $grid = new GridField('testgrid'); + $dataSource = DataList::create("GridFieldTest_Person")->sort("Name"); + $grid->setDataSource($dataSource); + $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/GridFieldPresenterTest.php b/tests/forms/GridFieldPresenterTest.php new file mode 100644 index 000000000..3421e14c5 --- /dev/null +++ b/tests/forms/GridFieldPresenterTest.php @@ -0,0 +1,55 @@ +assertTrue(new GridFieldPresenter instanceof GridFieldPresenter, 'Trying to find an instance of GridFieldPresenter'); + } + + public function testHeaders() { + $presenter = new GridFieldPresenter(); + $grid = new GridField('testgrid', 'testgrid', new DataList('GridFieldTest_Person')); + $presenter->setGridField($grid); + $headers = $presenter->Headers()->first(); + + $this->assertEquals(1, count($headers)); + $this->assertEquals('Name', $headers->Name ); + } + + public function testItemsReturnCorrectNumberOfItems() { + $presenter = new GridFieldPresenter(); + $grid = new GridField('testgrid', 'testgrid', new DataList('GridFieldTest_Person')); + $presenter->setGridField($grid); + $this->assertEquals(2, $presenter->Items()->count()); + } + + public function testSorting(){ + $presenter = new GridFieldPresenter(); + $GridField = new GridField('testgrid', 'testgrid', new DataList('GridFieldTest_Person')); + $presenter->setGridField($GridField); + $presenter->sort('Name','desc'); + $data = $presenter->Items()->map('ID','Name'); + $this->assertEquals(array(2=>'Second Person', 1=>'First Person'), $data); + $presenter->sort('Name','asc'); + $data = $presenter->Items()->map('ID','Name'); + $this->assertEquals(array(1=>'First Person', 2=>'Second Person'), $data); + } +} \ No newline at end of file diff --git a/tests/forms/DatagridTest.php b/tests/forms/GridFieldTest.php similarity index 54% rename from tests/forms/DatagridTest.php rename to tests/forms/GridFieldTest.php index 5dfec990a..e45ae97fc 100644 --- a/tests/forms/DatagridTest.php +++ b/tests/forms/GridFieldTest.php @@ -1,31 +1,31 @@ assertTrue(new Datagrid('Testgrid') instanceof FormField, 'Datagrid should be a FormField'); + $this->assertTrue(new GridField('Testgrid') instanceof FormField, 'GridField should be a FormField'); } public function testSetDataSource() { - $grid = new Datagrid('Testgrid'); + $grid = new GridField('Testgrid'); $source = new ArrayList(); $grid->setDatasource($source); $this->assertEquals($source, $grid->getDatasource()); @@ -33,29 +33,29 @@ class DatagridTest extends SapphireTest { function testSetEmptyDataPresenter() { $this->setExpectedException('Exception'); - $grid = new Datagrid('Testgrid'); - $grid->setDataPresenter(''); + $grid = new GridField('Testgrid'); + $grid->setPresenter(''); } function testSetNonExistingDataPresenter() { $this->setExpectedException('Exception'); - $grid = new Datagrid('Testgrid'); - $grid->setDataPresenter('ifThisClassExistsIWouldBeSurprised'); + $grid = new GridField('Testgrid'); + $grid->setPresenter('ifThisClassExistsIWouldBeSurprised'); } function testSetDataPresenterWithDataObject() { $this->setExpectedException('Exception'); - $grid = new Datagrid('Testgrid'); - $grid->setDataPresenter('DataObject'); + $grid = new GridField('Testgrid'); + $grid->setPresenter('DataObject'); } function testSetDataPresenter() { - $grid = new Datagrid('Testgrid'); - $grid->setDataPresenter('DatagridPresenter'); + $grid = new GridField('Testgrid'); + $grid->setPresenter('GridFieldPresenter'); } function testSetDataclass() { - $grid = new Datagrid('Testgrid'); + $grid = new GridField('Testgrid'); $grid->setModelClass('SiteTree'); $this->assertEquals('SiteTree', $grid->getModelClass()); } @@ -65,23 +65,23 @@ class DatagridTest extends SapphireTest { */ function testFieldHolderWithoutDataSource() { $this->setExpectedException('Exception'); - $grid = new Datagrid('Testgrid'); + $grid = new GridField('Testgrid'); $this->assertNotNull($grid->FieldHolder()); } /** - * This is better tested in the DatagridFunctionalTest + * This is better tested in the GridFieldFunctionalTest * - * @see DatagridFunctionalTest + * @see GridFieldFunctionalTest */ function testFieldHolder() { - $grid = new Datagrid('Testgrid'); - $grid->setDatasource(new DataList('DatagridTest_Person')); + $grid = new GridField('Testgrid'); + $grid->setDatasource(new DataList('GridFieldTest_Person')); $this->assertNotNull($grid->FieldHolder()); } } -class DatagridTest_Person extends Dataobject implements TestOnly { +class GridFieldTest_Person extends Dataobject implements TestOnly { public static $db = array( 'Name' => 'Varchar' diff --git a/tests/forms/DatagridTest.yml b/tests/forms/GridFieldTest.yml similarity index 74% rename from tests/forms/DatagridTest.yml rename to tests/forms/GridFieldTest.yml index bc8493604..a1d123193 100644 --- a/tests/forms/DatagridTest.yml +++ b/tests/forms/GridFieldTest.yml @@ -1,4 +1,4 @@ -DatagridTest_Person: +GridFieldTest_Person: first: Name: First Person second:
+ $Title
class="ss-datagrid-{$FirstLast}"<% end_if %>>$Value
class="ss-gridfield-{$FirstLast}"<% end_if %>>$Value