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

DMSDocumentAdmin::modifyGridField()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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