DMSDocumentAdmin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A getEditForm() 0 7 1
A modifyGridField() 0 36 4
1
<?php
2
3
class DMSDocumentAdmin extends ModelAdmin
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...
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...
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...
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...
15
16
    public function init()
17
    {
18
        parent::init();
19
        Requirements::javascript(DMS_DIR . '/javascript/DMSGridField.js');
20
    }
21
    /**
22
     * Remove the default "add" button and replace it with a customised version for DMS
23
     *
24
     * @return CMSForm
25
     */
26
    public function getEditForm($id = null, $fields = null)
27
    {
28
        /** @var CMSForm $form */
29
        $form = parent::getEditForm($id, $fields);
30
        $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
31
        return $this->modifyGridField($form, $gridField);
32
    }
33
34
    /**
35
     * If the GridField is for DMSDocument then add a custom "add" button. If it's for DMSDocumentSet then
36
     * update the display fields to include some extra columns that are only for this ModelAdmin, so cannot
37
     * be added directly to the model's display fields.
38
     *
39
     * @param  CMSForm   $form
40
     * @param  GridField $gridField
41
     * @return CMSForm
42
     */
43
    protected function modifyGridField(CMSForm $form, GridField $gridField)
44
    {
45
        $gridFieldConfig = $gridField->getConfig();
46
47
        $gridFieldConfig->removeComponentsByType('GridFieldEditButton');
48
        $gridFieldConfig->addComponent(new DMSGridFieldEditButton(), 'GridFieldDeleteAction');
49
50
        if ($this->modelClass === 'DMSDocument') {
51
            $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
52
            $gridFieldConfig->addComponent(
53
                new DMSGridFieldAddNewButton('buttons-before-left'),
54
                'GridFieldExportButton'
55
            );
56
        } elseif ($this->modelClass === 'DMSDocumentSet') {
57
            $dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns');
58
            $fields = $dataColumns->getDisplayFields($gridField);
59
            $fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields;
60
            $dataColumns->setDisplayFields($fields)
61
                ->setFieldFormatting(
62
                    array(
63
                        'Page.Title' => function ($value, $item) {
64
                            // Link a page click directly to the Document Set on the actual page
65
                            if ($page = SiteTree::get()->byID($item->PageID)) {
66
                                return sprintf(
67
                                    "<a class='dms-doc-sets-link' href='%s/#Root_DocumentSets%s'>$value</a>",
68
                                    $page->CMSEditLink(),
69
                                    $page->DocumentSets()->count()
70
                                );
71
                            }
72
                        }
73
                    )
74
                );
75
        }
76
77
        return $form;
78
    }
79
}
80