diff --git a/code/cms/DMSDocumentAdmin.php b/code/cms/DMSDocumentAdmin.php new file mode 100644 index 0000000..998bfef --- /dev/null +++ b/code/cms/DMSDocumentAdmin.php @@ -0,0 +1,32 @@ +sanitiseClassName($this->modelClass); + + $gridFieldConfig = $form->Fields()->fieldByName($gridFieldName)->getConfig(); + $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton'); + $gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton'); + + return $form; + } +} diff --git a/code/cms/DMSGridFieldAddNewButton.php b/code/cms/DMSGridFieldAddNewButton.php new file mode 100644 index 0000000..132d077 --- /dev/null +++ b/code/cms/DMSGridFieldAddNewButton.php @@ -0,0 +1,68 @@ +getModelClass()); + + if (!$singleton->canCreate()) { + return array(); + } + + if (!$this->buttonName) { + // provide a default button name, can be changed by calling {@link setButtonName()} on this component + $objectName = $singleton->i18n_singular_name(); + $this->buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName)); + } + + $link = singleton('DMSDocumentAddController')->Link(); + if ($this->getPageId()) { + $link = Controller::join_links($link, '?ID=' . $this->getPageId()); + } + + $data = new ArrayData(array( + 'NewLink' => $link, + 'ButtonName' => $this->buttonName, + )); + + return array( + $this->targetFragment => $data->renderWith('DMSGridFieldAddNewButton'), + ); + } + + /** + * Set the page ID that this document should be attached to + * + * @param int $id + * @return $this + */ + public function setPageId($id) + { + $this->pageId = $id; + return $this; + } + + /** + * Get the page ID that this document should be attached to + * + * @return int + */ + public function getPageId() + { + return $this->pageId; + } +} diff --git a/code/extensions/DMSSiteTreeExtension.php b/code/extensions/DMSSiteTreeExtension.php index 9e7e653..1444746 100644 --- a/code/extensions/DMSSiteTreeExtension.php +++ b/code/extensions/DMSSiteTreeExtension.php @@ -120,23 +120,13 @@ class DMSSiteTreeExtension extends DataExtension ); $gridField->addExtraClass('documents'); - $uploadBtn = new LiteralField( - 'UploadButton', - sprintf( - '%s', - Controller::join_links(singleton('DMSDocumentAddController')->Link(), '?ID=' . $this->owner->ID), - "Add Documents" - ) + $gridFieldConfig->addComponent( + $addNewButton = new DMSGridFieldAddNewButton, + 'GridFieldExportButton' ); + $addNewButton->setPageId($this->owner->ID); - $fields->addFieldsToTab( - 'Root.Documents (' . $this->owner->Documents()->Count() . ')', - array( - $uploadBtn, - $gridField - ) - ); + $fields->addFieldToTab('Root.Documents (' . $this->owner->Documents()->Count() . ')', $gridField); } /** diff --git a/code/model/DMSDocument.php b/code/model/DMSDocument.php index 48b57f8..95961ba 100644 --- a/code/model/DMSDocument.php +++ b/code/model/DMSDocument.php @@ -54,6 +54,13 @@ class DMSDocument extends DataObject implements DMSDocumentInterface 'LastChanged' ); + private static $summary_fields = array( + 'Filename' => 'Filename', + 'Title' => 'Title', + 'ViewCount' => 'ViewCount', + 'getPages.count' => 'Page Use' + ); + /** * @var string download|open * @config diff --git a/templates/Includes/DMSGridFieldAddNewButton.ss b/templates/Includes/DMSGridFieldAddNewButton.ss new file mode 100644 index 0000000..4268a0e --- /dev/null +++ b/templates/Includes/DMSGridFieldAddNewButton.ss @@ -0,0 +1,4 @@ +<%-- Customised to change the button's link to the DMSDocumentAddController instead of the default --%> + + $ButtonName + diff --git a/tests/DMSDocumentAdminTest.php b/tests/DMSDocumentAdminTest.php new file mode 100644 index 0000000..d1c6c7e --- /dev/null +++ b/tests/DMSDocumentAdminTest.php @@ -0,0 +1,34 @@ +handleRequest(new SS_HTTPRequest('GET', '/'), DataModel::inst()); + $modelAdmin->init(); + + $form = $modelAdmin->getEditForm(); + $gridFieldConfig = $form->Fields()->first()->getConfig(); + + // Our button is an instance of the original, so is returned when asking for the original + $addNewButtons = $gridFieldConfig->getComponentsByType('GridFieldAddNewButton'); + foreach ($addNewButtons as $key => $addNewButton) { + if ($addNewButton instanceof DMSGridFieldAddNewButton) { + // Remove our version for testing's sake + $addNewButtons->remove($addNewButton); + } + } + + $this->assertCount(0, $addNewButtons, 'Original add new button is removed'); + $this->assertInstanceOf( + 'DMSGridFieldAddNewButton', + $gridFieldConfig->getComponentByType('DMSGridFieldAddNewButton'), + 'Model admin for documents contains customised DMS add new button' + ); + } +} diff --git a/tests/DMSGridFieldAddNewButtonTest.php b/tests/DMSGridFieldAddNewButtonTest.php new file mode 100644 index 0000000..39edd51 --- /dev/null +++ b/tests/DMSGridFieldAddNewButtonTest.php @@ -0,0 +1,47 @@ +gridField = GridField::create('TestGridField', false, $fakeList); + $this->button = new DMSGridFieldAddNewButton; + } + + /** + * Test that when no page ID is present then it is not added to the URL for "add document" + */ + public function testNoPageIdInAddUrlWhenNotProvided() + { + $fragments = $this->button->getHTMLFragments($this->gridField); + $result = array_pop($fragments)->getValue(); + $this->assertNotContains('?ID', $result); + } + + /** + * Test that when a page ID is provided, it is added onto the "add document" link + */ + public function testPageIdAddedToLinkWhenProvided() + { + $this->button->setPageId(123); + + $fragments = $this->button->getHTMLFragments($this->gridField); + $result = array_pop($fragments)->getValue(); + $this->assertContains('?ID=123', $result); + } +}