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

code/cms/DMSDocumentAdmin.php (2 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
4
{
5
    private static $managed_models = array(
6
        'DMSDocument',
7
        'DMSDocumentSet'
8
    );
9
10
    private static $url_segment = 'documents';
11
12
    private static $menu_title = 'Documents';
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
     * 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->removeComponentsByType('GridFieldEditButton');
50
            $gridFieldConfig->addComponent(
51
                new DMSGridFieldAddNewButton('buttons-before-left'),
52
                'GridFieldExportButton'
53
            );
54
            $gridFieldConfig->addComponent(
55
                new DMSGridFieldEditButton(),
56
                'GridFieldDeleteAction'
57
            );
58
        } elseif ($this->modelClass === 'DMSDocumentSet') {
59
            $dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns');
60
            $fields = $dataColumns->getDisplayFields($gridField);
61
            $fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields;
62
            $dataColumns->setDisplayFields($fields)
63
                ->setFieldFormatting(
64
                    array(
65
                        'Page.Title' => function ($value, $item) {
66
                            // Link a page click directly to the Document Set on the actual page
67
                            if ($page = SiteTree::get()->byID($item->PageID)) {
68
                                return sprintf(
69
                                    "<a class='dms-doc-sets-link' href='%s/#Root_DocumentSets%s'>$value</a>",
70
                                    $page->CMSEditLink(),
71
                                    $page->DocumentSets()->count()
72
                                );
73
                            }
74
                        }
75
                    )
76
                );
77
        }
78
79
        return $form;
80
    }
81
}
82