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