From 1c1b1d2aeb07a5282289e32ff42e3f6e9ba58e19 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 9 May 2017 10:13:13 +1200 Subject: [PATCH] FIX Don't show GridField until set has been saved. Add more tests, fix typos in docs. --- code/cms/DMSGridFieldDetailForm.php | 2 +- code/model/DMSDocumentSet.php | 58 ++++++++++++++++++++++------- docs/en/changelogs/2.0.0.md | 2 +- lang/en.yml | 1 + tests/DMSDocumentSetTest.php | 51 +++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 16 deletions(-) diff --git a/code/cms/DMSGridFieldDetailForm.php b/code/cms/DMSGridFieldDetailForm.php index 9fdf8b4..94c8ede 100644 --- a/code/cms/DMSGridFieldDetailForm.php +++ b/code/cms/DMSGridFieldDetailForm.php @@ -13,7 +13,7 @@ class DMSGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest //add a data attribute specifying how many pages this document is referenced on if ($record = $this->record) { - $numberOfPageRelations = $record->getRelatedPages()->Count(); + $numberOfPageRelations = $record->getRelatedPages()->count(); $relations = new ShortCodeRelationFinder(); $numberOfInlineRelations = $relations->findPageCount($record->ID); diff --git a/code/model/DMSDocumentSet.php b/code/model/DMSDocumentSet.php index 84ce674..8e88967 100644 --- a/code/model/DMSDocumentSet.php +++ b/code/model/DMSDocumentSet.php @@ -47,14 +47,25 @@ class DMSDocumentSet extends DataObject */ public function getCMSFields() { - $this->beforeUpdateCMSFields(function (FieldList $fields) { - // Javascript to customize the grid field for the DMS document (overriding entwine - // in FRAMEWORK_DIR.'/javascript/GridField.js' - Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js'); - Requirements::css(DMS_DIR . '/css/DMSMainCMS.css'); + // PHP 5.3 only + $self = $this; - // Javascript for the link editor pop-up in TinyMCE - Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js'); + $this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { + // Don't put the GridField for documents in until the set has been created + if (!$self->isInDB()) { + $fields->addFieldToTab( + 'Root.Main', + LiteralField::create( + 'GridFieldNotice', + '

' . _t( + 'DMSDocumentSet.GRIDFIELD_NOTICE', + 'Managing documents will be available once you have created this document set.' + ) . '

' + ), + 'Title' + ); + return; + } // Document listing $gridFieldConfig = GridFieldConfig::create() @@ -65,9 +76,9 @@ class DMSDocumentSet extends DataObject // new GridFieldOrderableRows('DocumentSort'), new GridFieldDataColumns(), new GridFieldEditButton(), - new DMSGridFieldDeleteAction(), //special delete dialog to handle custom behaviour of unlinking and deleting + // Special delete dialog to handle custom behaviour of unlinking and deleting + new DMSGridFieldDeleteAction(), new GridFieldDetailForm() - //GridFieldLevelup::create($folder->ID)->setLinkSpec('admin/assets/show/%d') ); if (class_exists('GridFieldPaginatorWithShowAll')) { @@ -79,7 +90,7 @@ class DMSDocumentSet extends DataObject if (class_exists('GridFieldSortableRows')) { $sortableComponent = new GridFieldSortableRows('DocumentSort'); - //setUsePagenation method removed from newer version of SortableGridField. + // setUsePagenation method removed from newer version of SortableGridField. if (method_exists($sortableComponent, 'setUsePagination')) { $sortableComponent->setUsePagination(false)->setForceRedraw(true); } @@ -90,21 +101,21 @@ class DMSDocumentSet extends DataObject singleton('DMSDocument'); $gridFieldConfig->getComponentByType('GridFieldDataColumns') ->setDisplayFields(Config::inst()->get('DMSDocument', 'display_fields')) - ->setFieldCasting(array('LastChanged'=>"Datetime->Ago")) + ->setFieldCasting(array('LastChanged' => 'Datetime->Ago')) ->setFieldFormatting( array( 'FilenameWithoutID' => '$FilenameWithoutID' ) ); - //override delete functionality with this class + // Override delete functionality with this class $gridFieldConfig->getComponentByType('GridFieldDetailForm') ->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); $gridField = GridField::create( 'Documents', false, - $this->Documents(),//->Sort('DocumentSort'), + $self->Documents(), //->Sort('DocumentSort'), $gridFieldConfig ); $gridField->addExtraClass('documents'); @@ -113,11 +124,30 @@ class DMSDocumentSet extends DataObject $addNewButton = new DMSGridFieldAddNewButton, 'GridFieldExportButton' ); - $addNewButton->setDocumentSetId($this->ID); + $addNewButton->setDocumentSetId($self->ID); $fields->removeByName('Documents'); $fields->addFieldToTab('Root.Main', $gridField); }); + $this->addRequirements(); return parent::getCMSFields(); } + + /** + * Add required CSS and Javascript requirements for managing documents + * + * @return $this + */ + protected function addRequirements() + { + // Javascript to customize the grid field for the DMS document (overriding entwine + // in FRAMEWORK_DIR.'/javascript/GridField.js' + Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js'); + Requirements::css(DMS_DIR . '/css/DMSMainCMS.css'); + + // Javascript for the link editor pop-up in TinyMCE + Requirements::javascript(DMS_DIR . '/javascript/DocumentHtmlEditorFieldToolbar.js'); + + return $this; + } } diff --git a/docs/en/changelogs/2.0.0.md b/docs/en/changelogs/2.0.0.md index 762499c..b983468 100644 --- a/docs/en/changelogs/2.0.0.md +++ b/docs/en/changelogs/2.0.0.md @@ -5,7 +5,7 @@ Documents now belong to "sets", which are attached to Pages. A Page can have many Document Sets, and a Set has a many_many relationship with Documents. -When upgrading from 1.x to 2.x you will need to migrate the relationships from your Pages to Documents so support +When upgrading from 1.x to 2.x you will need to migrate the relationships from your Pages to Documents to support having a Document Set intermediary (@todo Add a build task for this). ## API changes diff --git a/lang/en.yml b/lang/en.yml index 9e4d9f1..18d5e8e 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -21,6 +21,7 @@ en: DMSDocumentSet: ADDDOCUMENTBUTTON: Add Document ADDDOCUMENTSBUTTON: Add Documents + GRIDFIELD_NOTICE: Managing documents will be available once you have created this document set. PLURALNAME: Document Sets SINGULARNAME: Document Set DMSTag: diff --git a/tests/DMSDocumentSetTest.php b/tests/DMSDocumentSetTest.php index 53f6521..7be996e 100644 --- a/tests/DMSDocumentSetTest.php +++ b/tests/DMSDocumentSetTest.php @@ -18,6 +18,28 @@ class DMSDocumentSetTest extends SapphireTest $this->assertSame('Extended', $documents->first()->Filename); } + /** + * Test that the GridField for documents isn't shown until you've saved the set + */ + public function testGridFieldShowsWhenSetIsSaved() + { + $set = DMSDocumentSet::create(); + + // Not in database yet + $fields = $set->getCMSFields(); + $this->assertNull($fields->fieldByName('Root.Main.Documents')); + $gridFieldNotice = $fields->fieldByName('Root.Main.GridFieldNotice'); + $this->assertNotNull($gridFieldNotice); + $this->assertContains('Managing documents will be available', $gridFieldNotice->getContent()); + + // In the database + $set->Title = 'Testing'; + $set->write(); + $fields = $set->getCMSFields(); + $this->assertNotNull($fields->fieldByName('Root.Main.Documents')); + $this->assertNull($fields->fieldByName('Root.Main.GridFieldNotice')); + } + public function testRelations() { $s1 = $this->objFromFixture('SiteTree', 's1'); @@ -32,4 +54,33 @@ class DMSDocumentSetTest extends SapphireTest $this->assertCount(2, $s1->getDocumentSets(), 'Page 1 has 2 document sets'); $this->assertEquals(array($ds1->ID, $ds2->ID), $s1->getDocumentSets()->column('ID')); } + + /** + * Test that various components exist in the GridField config. See {@link DMSDocumentSet::getCMSFields} for context. + */ + public function testDocumentGridFieldConfig() + { + $set = $this->objFromFixture('DMSDocumentSet', 'ds1'); + $fields = $set->getCMSFields(); + $gridField = $fields->fieldByName('Root.Main.Documents'); + $this->assertTrue((bool) $gridField->hasClass('documents')); + + /** @var GridFieldConfig $config */ + $config = $gridField->getConfig(); + + $this->assertNotNull($config->getComponentByType('DMSGridFieldDeleteAction')); + $this->assertNotNull($addNew = $config->getComponentByType('DMSGridFieldAddNewButton')); + $this->assertSame($set->ID, $addNew->getDocumentSetId()); + + if (class_exists('GridFieldPaginatorWithShowAll')) { + $this->assertNotNull($config->getComponentByType('GridFieldPaginatorWithShowAll')); + } else { + $paginator = $config->getComponentByType('GridFieldPaginator'); + $this->assertNotNull($paginator); + $this->assertSame(15, $paginator->getItemsPerPage()); + } + + $sortableAssertion = class_exists('GridFieldSortableRows') ? 'assertNotNull' : 'assertNull'; + $this->$sortableAssertion($config->getComponentByType('GridFieldSortableRows')); + } }