|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Admin\GridFieldFormAction; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
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
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @package blog |
|
14
|
|
|
*/ |
|
15
|
|
|
class GridFieldMergeAction implements GridField_ColumnProvider, GridField_ActionProvider |
|
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 = array(), $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 array('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 = new GridFieldFormAction( |
|
90
|
|
|
$gridField, |
|
91
|
|
|
'MergeAction' . $record->ID, |
|
92
|
|
|
'Move', |
|
93
|
|
|
'merge', |
|
94
|
|
|
array( |
|
95
|
|
|
'record' => $record->ID, |
|
96
|
|
|
'target' => $prefix . '-target-record-' . $record->ID, |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$action->setExtraAttributes(array( |
|
101
|
|
|
'data-target' => $prefix . '-target-record-' . $record->ID |
|
102
|
|
|
)); |
|
103
|
|
|
|
|
104
|
|
|
return $dropdown->Field() . $action->Field() . '<a title="Move posts to" class="MergeActionReveal">move posts to</a>'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getColumnAttributes($gridField, $record, $columnName) |
|
114
|
|
|
{ |
|
115
|
|
|
return array('class' => 'MergeAction'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getColumnMetadata($gridField, $columnName) |
|
122
|
|
|
{ |
|
123
|
|
|
return array('title' => 'Move posts to'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getActions($gridField) |
|
130
|
|
|
{ |
|
131
|
|
|
return array('merge'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($actionName === 'merge') { |
|
140
|
|
|
$controller = Controller::curr(); |
|
141
|
|
|
|
|
142
|
|
|
$request = $controller->getRequest(); |
|
143
|
|
|
|
|
144
|
|
|
$target = $request->requestVar($arguments["target"]); |
|
145
|
|
|
|
|
146
|
|
|
$parentType = $this->parentType; |
|
147
|
|
|
|
|
148
|
|
|
$fromParent = $parentType::get()->byId($arguments['record']); |
|
149
|
|
|
$toParent = $parentType::get()->byId($target); |
|
150
|
|
|
|
|
151
|
|
|
$posts = $fromParent->{$this->childMethod}(); |
|
152
|
|
|
|
|
153
|
|
|
foreach ($posts as $post) { |
|
154
|
|
|
$relationship = $post->{$this->parentMethod}(); |
|
155
|
|
|
|
|
156
|
|
|
$relationship->remove($fromParent); |
|
157
|
|
|
$relationship->add($toParent); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|