GridFieldMergeAction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 153
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A augmentColumns() 0 8 2
A getColumnsHandled() 0 4 1
A getColumnContent() 0 31 3
A getColumnAttributes() 0 4 1
A getColumnMetadata() 0 4 1
A getActions() 0 4 1
A handleAction() 0 24 3
1
<?php
2
3
namespace SilverStripe\Blog\Admin;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\Injector\Injectable;
7
use Silverstripe\Forms\DropdownField;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridField_ActionProvider;
10
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
11
use SilverStripe\ORM\SS_List;
12
13
class GridFieldMergeAction implements GridField_ColumnProvider, GridField_ActionProvider
14
{
15
    use Injectable;
16
17
    /**
18
     * List of records to show in the MergeAction column.
19
     *
20
     * @var array|SS_List
21
     */
22
    protected $records;
23
24
    /**
25
     * Type of parent DataObject (i.e BlogTag, BlogCategory).
26
     *
27
     * @var string
28
     */
29
    protected $parentType;
30
31
    /**
32
     * Relationship method to reference parent (i.e BlogTags).
33
     *
34
     * @var string
35
     */
36
    protected $parentMethod;
37
38
    /**
39
     * Relationship method to reference child (i.e BlogPosts).
40
     *
41
     * @var string
42
     */
43
    protected $childMethod;
44
45
    /**
46
     * @param array|SS_List $records
47
     * @param string $parentType
48
     * @param string $parentMethod
49
     * @param string $childMethod
50
     */
51
    public function __construct($records, $parentType, $parentMethod, $childMethod)
52
    {
53
        $this->records = $records;
54
        $this->parentType = $parentType;
55
        $this->parentMethod = $parentMethod;
56
        $this->childMethod = $childMethod;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function augmentColumns($gridField, &$columns)
63
    {
64
        if (!in_array('MergeAction', $columns)) {
65
            $columns[] = 'MergeAction';
66
        }
67
68
        return $columns;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getColumnsHandled($gridField)
75
    {
76
        return ['MergeAction'];
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getColumnContent($gridField, $record, $columnName)
83
    {
84
        if ($columnName === 'MergeAction' && $record->{$this->childMethod}()->Count() > 0) {
85
            $dropdown = DropdownField::create('Target', 'Target', $this->records->exclude('ID', $record->ID)->map());
86
            $dropdown->setAttribute('id', 'Target_'.$record->ID);
87
            $prefix = strtolower($this->parentMethod . '-' . $this->childMethod);
88
89
            $action = GridFieldFormAction::create(
90
                $gridField,
91
                'MergeAction' . $record->ID,
92
                'Move',
93
                'merge',
94
                [
95
                    'record' => $record->ID,
96
                    'target' => $prefix . '-target-record-' . $record->ID,
97
                ]
98
            );
99
100
            $action->setExtraAttributes([
101
                'data-target' => $prefix . '-target-record-' . $record->ID
102
            ]);
103
104
            $action->addExtraClass('btn btn-primary btn-sm blog-merge-action');
105
            $MovePostsTo = _t(__CLASS__ . '.MovePostsTo', 'Move posts to');
106
            $MergeActionReveal = '<a title="' . $MovePostsTo . '" class="MergeActionReveal">' . $MovePostsTo . '</a>';
107
108
            return $dropdown->Field() . $action->Field() . $MergeActionReveal;
109
        }
110
111
        return null;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getColumnAttributes($gridField, $record, $columnName)
118
    {
119
        return ['class' => 'MergeAction'];
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getColumnMetadata($gridField, $columnName)
126
    {
127
        return ['title' => _t(__CLASS__ . '.MovePostsTo', 'Move posts to')];
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getActions($gridField)
134
    {
135
        return ['merge'];
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
142
    {
143
        if ($actionName === 'merge') {
144
            $controller = Controller::curr();
145
146
            $request = $controller->getRequest();
147
148
            $target = $request->requestVar($arguments["target"]);
149
150
            $parentType = $this->parentType;
151
152
            $fromParent = $parentType::get()->byId($arguments['record']);
153
            $toParent = $parentType::get()->byId($target);
154
155
            $posts = $fromParent->{$this->childMethod}();
156
157
            foreach ($posts as $post) {
158
                $relationship = $post->{$this->parentMethod}();
159
160
                $relationship->remove($fromParent);
161
                $relationship->add($toParent);
162
            }
163
        }
164
    }
165
}
166