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

DMSDocumentAdmin::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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 Document Set on the actual page
62
                            if ($page = SiteTree::get()->byID($item->PageID)) {
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
                                $link = CMSPageEditController::create()->Link('EditForm');
64
                                return sprintf(
65
                                    "<a class='dms-doc-sets-link'" .
66
                                            "href='%s/%s/field/Document Sets/item/%s/edit'>$value</a>",
67
                                    $link,
68
                                    $item->PageID,
69
                                    $item->ID
70
                                );
71
                            }
72
                        }
73
                    )
74
                );
75
        }
76
77
        return $form;
78
    }
79
}
80