Merge pull request #111 from sachajudd/feature/modeladmin

NEW add DMSDocumentAdmin and new "add document" GridField component
This commit is contained in:
Franco Springveldt 2017-05-04 13:27:28 +12:00 committed by GitHub
commit 38501542ee
7 changed files with 197 additions and 15 deletions

View File

@ -0,0 +1,32 @@
<?php
class DMSDocumentAdmin extends ModelAdmin
{
private static $managed_models = array(
'DMSDocument'
);
private static $url_segment = 'documents';
private static $menu_title = 'Documents';
/**
* Remove the default "add" button and replace it with a customised version for DMS
*
* @return CMSForm
*/
public function getEditForm($id = null, $fields = null)
{
/** @var CMSForm $form */
$form = parent::getEditForm($id, $fields);
// See parent class
$gridFieldName = $this->sanitiseClassName($this->modelClass);
$gridFieldConfig = $form->Fields()->fieldByName($gridFieldName)->getConfig();
$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
$gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton');
return $form;
}
}

View File

@ -0,0 +1,68 @@
<?php
class DMSGridFieldAddNewButton extends GridFieldAddNewButton implements GridField_HTMLProvider
{
/**
* The page ID that the document should be attached to. Used in the GridField for Documents in a Page.
*
* @var int
*/
protected $pageId;
/**
* Overriding the parent method to change the template that the DMS add button will be rendered with
*
* @param GridField $gridField
* @return array
*/
public function getHTMLFragments($gridField)
{
$singleton = singleton($gridField->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;
}
}

View File

@ -120,23 +120,13 @@ class DMSSiteTreeExtension extends DataExtension
);
$gridField->addExtraClass('documents');
$uploadBtn = new LiteralField(
'UploadButton',
sprintf(
'<a class="ss-ui-button ss-ui-action-constructive cms-panel-link" data-pjax-target="Content"'
. ' data-icon="add" href="%s">%s</a>',
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);
}
/**

View File

@ -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

View File

@ -0,0 +1,4 @@
<%-- Customised to change the button's link to the DMSDocumentAddController instead of the default --%>
<a href="$NewLink" class="action action-detail ss-ui-action-constructive ss-ui-button ui-button ui-widget ui-state-default ui-corner-all new new-link" data-icon="add">
$ButtonName
</a>

View File

@ -0,0 +1,34 @@
<?php
class DMSDocumentAdminTest extends FunctionalTest
{
/**
* Check that the default "add new" button is gone, and replaced with our customised version of it
*/
public function testGridFieldHasCustomisedAddNewButton()
{
$modelAdmin = new DMSDocumentAdmin;
// SS < 3.3 doesn't have a response setter, this initialises it
$modelAdmin->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'
);
}
}

View File

@ -0,0 +1,47 @@
<?php
class DMSGridFieldAddNewButtonTest extends SapphireTest
{
protected static $fixture_file = 'dmstest.yml';
/**
* @var DMSGridFieldAddNewButton
*/
protected $button;
/**
* @var GridField
*/
protected $gridField;
public function setUp()
{
parent::setUp();
$fakeList = DMSDocument::get();
$this->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);
}
}