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

code/cms/DMSDocumentAdmin.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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