From 40cd66852e8d3a5d56c56b9d279cb89a98e3c16d Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Thu, 25 Jul 2019 14:20:12 -0300 Subject: [PATCH 1/3] BUGFIX: Fixed issue where multiple relationship sort order columns would be lost in favor of only the last relationship column in the sort order --- src/ORM/DataQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ORM/DataQuery.php b/src/ORM/DataQuery.php index 2b4224be7..6ca70c13e 100644 --- a/src/ORM/DataQuery.php +++ b/src/ORM/DataQuery.php @@ -403,7 +403,7 @@ class DataQuery // Find the first free "_SortColumnX" slot // and assign it to $key $i = 0; - while (isset($orderby[$key = "\"_SortColumn$i\""])) { + while (isset($newOrderby[$key = "\"_SortColumn$i\""]) || isset($orderby[$key = "\"_SortColumn$i\""])) { ++$i; } From c1ffc4edfb4c2c06da5a6a04ba5b9ea3fcc60f1a Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Mon, 29 Jul 2019 10:45:10 -0300 Subject: [PATCH 2/3] Added unit tests for multiple relationship sorting --- tests/php/ORM/DataQueryTest.php | 23 ++++++++++++++++++++ tests/php/ORM/DataQueryTest.yml | 28 +++++++++++++++++++++++++ tests/php/ORM/DataQueryTest/ObjectH.php | 21 +++++++++++++++++++ tests/php/ORM/DataQueryTest/ObjectI.php | 16 ++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 tests/php/ORM/DataQueryTest/ObjectH.php create mode 100644 tests/php/ORM/DataQueryTest/ObjectI.php diff --git a/tests/php/ORM/DataQueryTest.php b/tests/php/ORM/DataQueryTest.php index 0c8a6bf41..82e8ef3a0 100644 --- a/tests/php/ORM/DataQueryTest.php +++ b/tests/php/ORM/DataQueryTest.php @@ -24,6 +24,8 @@ class DataQueryTest extends SapphireTest DataQueryTest\ObjectE::class, DataQueryTest\ObjectF::class, DataQueryTest\ObjectG::class, + DataQueryTest\ObjectH::class, + DataQueryTest\ObjectI::class, SQLSelectTest\TestObject::class, SQLSelectTest\TestBase::class, SQLSelectTest\TestChild::class, @@ -405,4 +407,25 @@ class DataQueryTest extends SapphireTest $this->assertEquals('Last', $second['Title']); $this->assertEmpty(array_shift($arrayResult)); } + + /** + * Tests that sorting against multiple relationships is working + */ + public function testMultipleRelationSort() + { + $query = new DataQuery(DataQueryTest\ObjectH::class); + $query->applyRelation('ManyTestEs'); + $query->applyRelation('ManyTestIs'); + $query->sort([ + '"manytestes_DataQueryTest_E"."SortOrder"', + '"manytestis_DataQueryTest_I"."SortOrder"', + '"SortOrder"', + ]); + + $titles = $query->column('Name'); + + $this->assertEquals('First', $titles[0]); + $this->assertEquals('Second', $titles[1]); + $this->assertEquals('Last', $titles[2]); + } } diff --git a/tests/php/ORM/DataQueryTest.yml b/tests/php/ORM/DataQueryTest.yml index cb514c76a..917175cc2 100644 --- a/tests/php/ORM/DataQueryTest.yml +++ b/tests/php/ORM/DataQueryTest.yml @@ -8,3 +8,31 @@ SilverStripe\ORM\Tests\DataQueryTest\ObjectE: query3: Title: 'Second' SortOrder: 2 + +SilverStripe\ORM\Tests\DataQueryTest\ObjectI: + query1: + Title: 'First' + SortOrder: 1 + query2: + Title: 'Second' + SortOrder: 2 + query3: + Title: 'Last' + SortOrder: 3 + +SilverStripe\ORM\Tests\DataQueryTest\ObjectH: + query1: + Name: 'First' + SortOrder: 3 + ManyTestEs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectE.query2 + ManyTestIs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectI.query2 + query2: + Name: 'Last' + SortOrder: 1 + ManyTestEs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectE.query1 + ManyTestIs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectI.query1 + query3: + Name: 'Second' + SortOrder: 2 + ManyTestEs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectE.query2 + ManyTestIs: =>SilverStripe\ORM\Tests\DataQueryTest\ObjectI.query3 diff --git a/tests/php/ORM/DataQueryTest/ObjectH.php b/tests/php/ORM/DataQueryTest/ObjectH.php new file mode 100644 index 000000000..1e28d80f7 --- /dev/null +++ b/tests/php/ORM/DataQueryTest/ObjectH.php @@ -0,0 +1,21 @@ + 'Varchar', + 'SortOrder' => 'Int', + ); + + private static $many_many = array( + 'ManyTestEs' => ObjectE::class, + 'ManyTestIs' => ObjectI::class, + ); +} diff --git a/tests/php/ORM/DataQueryTest/ObjectI.php b/tests/php/ORM/DataQueryTest/ObjectI.php new file mode 100644 index 000000000..03f535667 --- /dev/null +++ b/tests/php/ORM/DataQueryTest/ObjectI.php @@ -0,0 +1,16 @@ + 'Varchar', + 'SortOrder' => 'Int', + ); +} From 3d989a6eae979f2671889376179dfdc7085658ac Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Fri, 9 Aug 2019 14:51:56 +1200 Subject: [PATCH 3/3] FIX Use content generated by DataColumns component for print and csv export --- src/Forms/GridField/GridFieldExportButton.php | 7 +++++++ src/Forms/GridField/GridFieldPrintButton.php | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Forms/GridField/GridFieldExportButton.php b/src/Forms/GridField/GridFieldExportButton.php index 4c4d41eac..eb8698b72 100644 --- a/src/Forms/GridField/GridFieldExportButton.php +++ b/src/Forms/GridField/GridFieldExportButton.php @@ -214,6 +214,9 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP } } + /** @var GridFieldDataColumns|null $gridFieldColumnsComponent */ + $gridFieldColumnsComponent = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class); + /** @var DataObject $item */ foreach ($items->limit(null) as $item) { if (!$item->hasMethod('canView') || $item->canView()) { @@ -228,6 +231,10 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP } $value = $columnHeader($relObj); + } elseif ($gridFieldColumnsComponent) { + $value = strip_tags( + $gridFieldColumnsComponent->getColumnContent($gridField, $item, $columnSource) + ); } else { $value = $gridField->getDataFieldValue($item, $columnSource); diff --git a/src/Forms/GridField/GridFieldPrintButton.php b/src/Forms/GridField/GridFieldPrintButton.php index aba827282..547997899 100644 --- a/src/Forms/GridField/GridFieldPrintButton.php +++ b/src/Forms/GridField/GridFieldPrintButton.php @@ -156,7 +156,7 @@ class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionPr } /** @var GridFieldDataColumns $dataCols */ - $dataCols = $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns'); + $dataCols = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class); if ($dataCols) { return $dataCols->getDisplayFields($gridField); } @@ -223,12 +223,17 @@ class GridFieldPrintButton implements GridField_HTMLProvider, GridField_ActionPr $items = $gridField->getManipulatedList(); $itemRows = new ArrayList(); + /** @var GridFieldDataColumns $gridFieldColumnsComponent */ + $gridFieldColumnsComponent = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class); + /** @var DataObject $item */ foreach ($items->limit(null) as $item) { $itemRow = new ArrayList(); foreach ($printColumns as $field => $label) { - $value = $gridField->getDataFieldValue($item, $field); + $value = $gridFieldColumnsComponent + ? strip_tags($gridFieldColumnsComponent->getColumnContent($gridField, $item, $field)) + : $gridField->getDataFieldValue($item, $field); $itemRow->push(new ArrayData(array( "CellString" => $value,