|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class GridFieldMergeAction implements GridField_ColumnProvider, GridField_ActionProvider |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Cache results of records queries. This is useful for blogs |
|
8
|
|
|
* with a lot of tags or categories. |
|
9
|
|
|
* |
|
10
|
|
|
* @var array |
|
11
|
|
|
* |
|
12
|
|
|
* @example |
|
13
|
|
|
* array(1) { |
|
14
|
|
|
* ["2d73dd8f18b8da3c8e5e75b6bee66d8d"]=> array(3) { |
|
15
|
|
|
* [2]=> string(5) "tag2" |
|
16
|
|
|
* [1]=> string(6) "tag1" |
|
17
|
|
|
* [3]=> string(4) "tag3" |
|
18
|
|
|
* } |
|
19
|
|
|
* } |
|
20
|
|
|
* The array key in the first level is made up of an md5 hash of the sql query, |
|
21
|
|
|
* then the second level is made if up ID => tag name. |
|
22
|
|
|
*/ |
|
23
|
|
|
protected static $cached_results = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* List of records to show in the MergeAction column. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array|SS_List |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $records; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Type of parent DataObject (i.e BlogTag, BlogCategory). |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $parentType; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Relationship method to reference parent (i.e BlogTags). |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $parentMethod; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Relationship method to reference child (i.e BlogPosts). |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $childMethod; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array|SS_List $records |
|
55
|
|
|
* @param string $parentType |
|
56
|
|
|
* @param string $parentMethod |
|
57
|
|
|
* @param string $childMethod |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct($records = array(), $parentType, $parentMethod, $childMethod) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->records = $records; |
|
62
|
|
|
$this->parentType = $parentType; |
|
63
|
|
|
$this->parentMethod = $parentMethod; |
|
64
|
|
|
$this->childMethod = $childMethod; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function augmentColumns($gridField, &$columns) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!in_array('MergeAction', $columns)) { |
|
73
|
|
|
$columns[] = 'MergeAction'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $columns; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getColumnsHandled($gridField) |
|
83
|
|
|
{ |
|
84
|
|
|
return array('MergeAction'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getColumnContent($gridField, $record, $columnName) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($columnName === 'MergeAction') { |
|
93
|
|
|
|
|
94
|
|
|
if(!$record->hasMethod($this->childMethod) || $record->{$this->childMethod}()->count() < 1) { |
|
95
|
|
|
return ''; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$children = $record->{$this->childMethod}(); |
|
99
|
|
|
$cacheKey = md5($children->dataQuery()->sql()); |
|
100
|
|
|
if(isset(self::$cached_results[$cacheKey])) { |
|
101
|
|
|
$data = self::$cached_results[$cacheKey]; |
|
102
|
|
|
} else { |
|
103
|
|
|
$data = $this->records->map()->toArray(); |
|
104
|
|
|
self::$cached_results[$cacheKey] = $data; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Unset the current record |
|
108
|
|
|
if(isset($data[$record->ID])) { |
|
109
|
|
|
unset($data[$record->ID]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if(count($data) > 0) { |
|
113
|
|
|
$dropdown = new DropdownField('Target', 'Target', $data); |
|
114
|
|
|
$dropdown->setAttribute('id', 'Target_'.$record->ID); |
|
115
|
|
|
$prefix = strtolower($this->parentMethod . '-' . $this->childMethod); |
|
116
|
|
|
|
|
117
|
|
|
$action = GridFieldFormAction::create( |
|
118
|
|
|
$gridField, |
|
119
|
|
|
'MergeAction' . $record->ID, |
|
120
|
|
|
'Move', |
|
121
|
|
|
'merge', |
|
122
|
|
|
array( |
|
123
|
|
|
'record' => $record->ID, |
|
124
|
|
|
'target' => $prefix . '-target-record-' . $record->ID, |
|
125
|
|
|
) |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$action->setExtraAttributes(array( |
|
129
|
|
|
'data-target' => $prefix . '-target-record-' . $record->ID |
|
130
|
|
|
)); |
|
131
|
|
|
|
|
132
|
|
|
return $dropdown->Field() . $action->Field() . '<a title="Move posts to" class="MergeActionReveal">move posts to</a>'; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getColumnAttributes($gridField, $record, $columnName) |
|
143
|
|
|
{ |
|
144
|
|
|
return array('class' => 'MergeAction'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getColumnMetadata($gridField, $columnName) |
|
151
|
|
|
{ |
|
152
|
|
|
return array('title' => 'Move posts to'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getActions($gridField) |
|
159
|
|
|
{ |
|
160
|
|
|
return array('merge'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
167
|
|
|
{ |
|
168
|
|
|
if ($actionName === 'merge') { |
|
169
|
|
|
$controller = Controller::curr(); |
|
170
|
|
|
|
|
171
|
|
|
$request = $controller->getRequest(); |
|
172
|
|
|
|
|
173
|
|
|
$target = $request->requestVar($arguments["target"]); |
|
174
|
|
|
|
|
175
|
|
|
$parentType = $this->parentType; |
|
176
|
|
|
|
|
177
|
|
|
$fromParent = $parentType::get()->byId($arguments['record']); |
|
178
|
|
|
$toParent = $parentType::get()->byId($target); |
|
179
|
|
|
|
|
180
|
|
|
$posts = $fromParent->{$this->childMethod}(); |
|
181
|
|
|
|
|
182
|
|
|
foreach ($posts as $post) { |
|
183
|
|
|
$relationship = $post->{$this->parentMethod}(); |
|
184
|
|
|
|
|
185
|
|
|
$relationship->remove($fromParent); |
|
186
|
|
|
$relationship->add($toParent); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
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.