Completed
Push — master ( 31b3ff...06692e )
by Robbie
03:48 queued 01:52
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
     * 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
        $gridFieldConfig->removeComponentsByType('GridFieldEditButton');
48
        $gridFieldConfig->addComponent(new DMSGridFieldEditButton(), 'GridFieldDeleteAction');
49
50
        if ($this->modelClass === 'DMSDocument') {
51
            $gridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
52
            $gridFieldConfig->addComponent(
53
                new DMSGridFieldAddNewButton('buttons-before-left'),
54
                'GridFieldExportButton'
55
            );
56
        } elseif ($this->modelClass === 'DMSDocumentSet') {
57
            $dataColumns = $gridFieldConfig->getComponentByType('GridFieldDataColumns');
58
            $fields = $dataColumns->getDisplayFields($gridField);
59
            $fields = array('Title' => 'Title', 'Page.Title' => 'Page') + $fields;
60
            $dataColumns->setDisplayFields($fields)
61
                ->setFieldFormatting(
62
                    array(
63
                        'Page.Title' => function ($value, $item) {
64
                            // Link a page click directly to the Document Set on the actual page
65
                            if ($page = SiteTree::get()->byID($item->PageID)) {
66
                                return sprintf(
67
                                    "<a class='dms-doc-sets-link' href='%s/#Root_DocumentSets%s'>$value</a>",
68
                                    $page->CMSEditLink(),
69
                                    $page->DocumentSets()->count()
70
                                );
71
                            }
72
                        }
73
                    )
74
                );
75
        }
76
77
        return $form;
78
    }
79
}
80