Completed
Push — master ( e95182...886fd2 )
by Franco
08:09
created

DMSDocumentAdmin::modifyGridField()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
1
<?php
2
3
class DMSDocumentAdmin extends ModelAdmin
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $managed_models = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $managed_models is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
        'DMSDocument',
7
        'DMSDocumentSet'
8
    );
9
10
    private static $url_segment = 'documents';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $url_segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    private static $menu_title = 'Documents';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $menu_title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
14
    private static $menu_icon = 'dms/images/app_icons/drawer.png';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $menu_icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    /**
17
     * Remove the default "add" button and replace it with a customised version for DMS
18
     *
19
     * @return CMSForm
20
     */
21
    public function getEditForm($id = null, $fields = null)
22
    {
23
        /** @var CMSForm $form */
24
        $form = parent::getEditForm($id, $fields);
25
        $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
26
        return $this->modifyGridField($form, $gridField);
27
    }
28
29
    /**
30
     * If the GridField is for DMSDocument then add a custom "add" button. If it's for DMSDocumentSet then
31
     * update the display fields to include some extra columns that are only for this ModelAdmin, so cannot
32
     * be added directly to the model's display fields.
33
     *
34
     * @param  CMSForm   $form
35
     * @param  GridField $gridField
36
     * @return CMSForm
37
     */
38
    protected function modifyGridField(CMSForm $form, GridField $gridField)
39
    {
40
        $gridFieldConfig = $gridField->getConfig();
41
42
        if ($this->modelClass === 'DMSDocument') {
43
            $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
44
            $gridFieldConfig->addComponent(new DMSGridFieldAddNewButton('buttons-before-left'), 'GridFieldExportButton');
45
        } elseif ($this->modelClass === 'DMSDocumentSet') {
46
            $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
47
48
            $dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns');
49
            $fields = $dataColumns->getDisplayFields($gridField);
50
            $fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields;
51
            $dataColumns->setDisplayFields($fields);
52
        }
53
54
        return $form;
55
    }
56
}
57