Completed
Pull Request — master (#146)
by Franco
02:26
created

DMSDocumentAdmin::modifyGridField()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
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
    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
        if ($this->modelClass === 'DMSDocument') {
48
            $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
49
            $gridFieldConfig->addComponent(
50
                new DMSGridFieldAddNewButton('buttons-before-left'),
51
                'GridFieldExportButton'
52
            );
53
        } elseif ($this->modelClass === 'DMSDocumentSet') {
54
            $dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns');
55
            $fields = $dataColumns->getDisplayFields($gridField);
56
            $fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields;
57
            $dataColumns->setDisplayFields($fields)
58
                ->setFieldFormatting(
59
                    array(
60
                        'Page.Title' => function ($value, $item) {
61
                            // Link a page click directly to the page click to the DocumentSets tab
62
                            if ($page = SiteTree::get()->byID($item->PageID)) {
63
                                $link = CMSMain::create()->LinkPageEdit($item->PageID);
64
                                return sprintf(
65
                                    "<a class='dms-doc-sets-link' href='%s#Root_DocumentSets%s'>$value</a>",
66
                                    $link,
67
                                    $page->DocumentSets()->count()
68
                                );
69
                            }
70
                        }
71
                    )
72
                );
73
        }
74
75
        return $form;
76
    }
77
}
78