Merge pull request #141 from creative-commoners/pulls/2.0/doc-sets-modeladmin

NEW Add ModelAdmin interface for managing DMSDocumentSets
This commit is contained in:
Franco Springveldt 2017-05-31 10:34:32 +12:00 committed by GitHub
commit 886fd217f2
3 changed files with 97 additions and 8 deletions

View File

@ -3,7 +3,8 @@
class DMSDocumentAdmin extends ModelAdmin
{
private static $managed_models = array(
'DMSDocument'
'DMSDocument',
'DMSDocumentSet'
);
private static $url_segment = 'documents';
@ -21,13 +22,34 @@ class DMSDocumentAdmin extends ModelAdmin
{
/** @var CMSForm $form */
$form = parent::getEditForm($id, $fields);
$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
return $this->modifyGridField($form, $gridField);
}
// See parent class
$gridFieldName = $this->sanitiseClassName($this->modelClass);
/**
* If the GridField is for DMSDocument then add a custom "add" button. If it's for DMSDocumentSet then
* update the display fields to include some extra columns that are only for this ModelAdmin, so cannot
* be added directly to the model's display fields.
*
* @param CMSForm $form
* @param GridField $gridField
* @return CMSForm
*/
protected function modifyGridField(CMSForm $form, GridField $gridField)
{
$gridFieldConfig = $gridField->getConfig();
$gridFieldConfig = $form->Fields()->fieldByName($gridFieldName)->getConfig();
$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
$gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton');
if ($this->modelClass === 'DMSDocument') {
$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
$gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton');
} elseif ($this->modelClass === 'DMSDocumentSet') {
$gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
$dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns');
$fields = $dataColumns->getDisplayFields($gridField);
$fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields;
$dataColumns->setDisplayFields($fields);
}
return $form;
}

View File

@ -2,14 +2,21 @@
class DMSDocumentAdminTest extends FunctionalTest
{
protected static $fixture_file = 'DMSDocumentAdminTest.yml';
public function setUp()
{
parent::setUp();
$this->logInWithPermission('ADMIN');
}
/**
* 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();
@ -31,4 +38,32 @@ class DMSDocumentAdminTest extends FunctionalTest
'Model admin for documents contains customised DMS add new button'
);
}
/**
* Quick check to ensure that the ModelAdmin endpoint is working
*/
public function testModelAdminEndpointWorks()
{
$this->assertEquals(200, $this->get('admin/documents')->getStatusCode());
}
/**
* Check that the document sets GridField has had its "add new" button removed
*/
public function testDocumentSetsGridFieldHasNoAddButton()
{
$result = (string) $this->get('admin/documents/DMSDocumentSet')->getBody();
$this->assertNotContains('Add Document Set', $result);
}
/**
* Check that the document sets GridField has a data column for the parent page title. Here we check for the
* Page title existing in the DOM, since "Page" is guaranteed to exist somewhere else.
*/
public function testDocumentSetsGridFieldHasParentPageColumn()
{
$result = (string) $this->get('admin/documents/DMSDocumentSet')->getBody();
$this->assertContains('Home Test Page', $result);
$this->assertContains('About Us Test Page', $result);
}
}

View File

@ -0,0 +1,32 @@
Page:
home:
Title: Home Test Page
URLSegment: home
about_us:
Title: About Us Test Page
URLSegment: About Us
DMSDocumentSet:
home_default:
Title: Home Default
Page: =>Page.home
home_alt:
Title: Home Alternative
Page: =>Page.home
about_default:
Title: About Default
Page: =>Page.about_us
DMSDocument:
h_d_1:
Title: Home Default 1
Filename: home-default-1
Sets: =>DMSDocumentSet.home_default
h_a_2:
Title: Home Default 2
Filename: home-default-2
Sets: =>DMSDocumentSet.home_alt
a_d_1:
Title: About Default 1
Filename: about-default-1
Sets: =>DMSDocumentSet.about_default