Passed
Push — master ( 0208b2...1155ca )
by Robbie
09:03
created

GridField_ActionMenu   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A augmentColumns() 0 4 2
A getColumnsHandled() 0 3 1
A getActions() 0 11 3
A handleAction() 0 10 4
A getColumnAttributes() 0 3 1
B getColumnContent() 0 27 4
A getColumnMetadata() 0 3 1
A getItems() 0 5 1
1
<?php
2
3
namespace SilverStripe\Forms\GridField;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\View\ArrayData;
7
use SilverStripe\View\SSViewer;
8
9
/**
10
 * Groups exiting actions in the Actions column in to a menu
11
 */
12
class GridField_ActionMenu implements GridField_ColumnProvider, GridField_ActionProvider
13
{
14
    public function augmentColumns($gridField, &$columns)
15
    {
16
        if (!in_array('Actions', $columns)) {
17
            $columns[] = 'Actions';
18
        }
19
    }
20
21
    public function getColumnsHandled($gridField)
22
    {
23
        return ['Actions'];
24
    }
25
26
    public function getColumnContent($gridField, $record, $columnName)
27
    {
28
        $items = $this->getItems($gridField);
29
30
        if (!$items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31
            return null;
32
        }
33
34
        $schema = array_map(function (GridField_ActionMenuItem $item) use ($gridField, $record, $columnName) {
35
            return [
36
                'type' => $item instanceof GridField_ActionMenuLink ? 'link' : 'submit',
37
                'title' => $item->getTitle($gridField, $record, $columnName),
38
                'url' => $item instanceof GridField_ActionMenuLink
39
                    ? $item->getUrl($gridField, $record, $columnName)
40
                    : null,
41
                'group' => $item->getGroup($gridField, $record, $columnName),
42
                'data' => $item->getExtraData($gridField, $record, $columnName),
43
            ];
44
        }, $items);
45
46
        $templateData = ArrayData::create([
47
            'Schema' => Convert::raw2json($schema),
48
        ]);
49
50
        $template = SSViewer::get_templates_by_class($this, '', static::class);
51
52
        return $templateData->renderWith($template);
53
    }
54
55
    public function getColumnAttributes($gridField, $record, $columnName)
56
    {
57
        return ['class' => 'grid-field__col-compact action-menu'];
58
    }
59
60
    public function getColumnMetadata($gridField, $columnName)
61
    {
62
        return ['title' => null];
63
    }
64
65
    public function getActions($gridField)
66
    {
67
        $actions = [];
68
69
        foreach ($this->getItems($gridField) as $item) {
70
            if ($item instanceof GridField_ActionProvider) {
71
                $actions = array_merge($actions, $item->getActions($gridField));
72
            }
73
        }
74
75
        return $actions;
76
    }
77
78
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
79
    {
80
        foreach ($this->getItems($gridField) as $item) {
81
            $actions = [];
82
            if ($item instanceof GridField_ActionProvider) {
83
                $actions = $item->getActions($gridField);
84
            }
85
86
            if (in_array($actionName, $actions)) {
87
                $item->handleAction($gridField, $actionName, $arguments, $data);
88
            }
89
        }
90
    }
91
92
    /**
93
     * Gets the list of items setup
94
     *
95
     * @return array
96
     */
97
    public function getItems($gridfield)
98
    {
99
        $items = $gridfield->config->getComponentsByType(GridField_ActionMenuItem::class)->items;
100
101
        return $items;
102
    }
103
}
104