CMSBatchAction.php
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* A class representing back actions.
* See CMSMain.BatchActions.js on how to add custom javascript
* functionality.
*
* <code>
* CMSMain::register_batch_action('publishitems', new CMSBatchAction('doPublish',
* _t('CMSBatchActions.PUBLISHED_PAGES', 'published %d pages')));
* </code>
*
* @package framework
* @subpackage admin
*/
abstract class CMSBatchAction extends Object {
protected $managedClass = 'SiteTree';
/**
* The the text to show in the dropdown for this action
*/
abstract public function getActionTitle();
/**
* Run this action for the given set of pages.
* Return a set of status-updated JavaScript to return to the CMS.
*/
abstract public function run(SS_List $objs);
/**
* Helper method for responding to a back action request
* @param $successMessage string - The message to return as a notification.
* Can have up to two %d's in it. The first will be replaced by the number of successful
* changes, the second by the number of failures
* @param $status array - A status array like batchactions builds. Should be
* key => value pairs, the key can be any string: "error" indicates errors, anything
* else indicates a type of success. The value is an array. We don't care what's in it,
* we just use count($value) to find the number of items that succeeded or failed
*/
public function response($successMessage, $status) {
$count = 0;
$errors = 0;
foreach($status as $k => $v) {
if ($k == 'errors') $errors = count($v);
else $count += count($v);
}
$response = Controller::curr()->getResponse();
if($response) {
$response->setStatusCode(
200,
sprintf($successMessage, $count, $errors)
);
}
return Convert::raw2json($status);
}
/**
* Helper method for processing batch actions.
* Returns a set of status-updating JavaScript to return to the CMS.
*
* @param $objs The SS_List of objects to perform this batch action
* on.
* @param $helperMethod The method to call on each of those objects.
* @return JSON encoded map in the following format:
* {
* 'modified': {
* 3: {'TreeTitle': 'Page3'},
* 5: {'TreeTitle': 'Page5'}
* },
* 'deleted': {
* // all deleted pages
* }
* }
*/
public function batchaction(SS_List $objs, $helperMethod, $successMessage, $arguments = array()) {
$status = array('modified' => array(), 'error' => array());
foreach($objs as $obj) {
// Perform the action
if (!call_user_func_array(array($obj, $helperMethod), $arguments)) {
$status['error'][$obj->ID] = '';
}
// Now make sure the tree title is appropriately updated
$publishedRecord = DataObject::get_by_id($this->managedClass, $obj->ID);
if ($publishedRecord) {
$status['modified'][$publishedRecord->ID] = array(
'TreeTitle' => $publishedRecord->TreeTitle,
);
}
$obj->destroy();
unset($obj);
}
return $this->response($successMessage, $status);
}
/**
* Helper method for applicablePages() methods. Acts as a skeleton implementation.
*
* @param $ids The IDs passed to applicablePages
* @param $methodName The canXXX() method to call on each page to check if the action is applicable
* @param $checkStagePages Set to true if you want to check stage pages
* @param $checkLivePages Set to true if you want to check live pages (e.g, for deleted-from-draft)
*/
public function applicablePagesHelper($ids, $methodName, $checkStagePages = true, $checkLivePages = true) {
if(!is_array($ids)) user_error("Bad \$ids passed to applicablePagesHelper()", E_USER_WARNING);
if(!is_string($methodName)) user_error("Bad \$methodName passed to applicablePagesHelper()", E_USER_WARNING);
$applicableIDs = array();
$SQL_ids = implode(', ', array_filter($ids, 'is_numeric'));
$draftPages = DataObject::get(
$this->managedClass,
sprintf(
"\"%s\".\"ID\" IN (%s)",
ClassInfo::baseDataClass($this->managedClass),
$SQL_ids
)
);
$onlyOnLive = array_fill_keys($ids, true);
if($checkStagePages) {
foreach($draftPages as $obj) {
unset($onlyOnLive[$obj->ID]);
if($obj->$methodName()) $applicableIDs[] = $obj->ID;
}
}
$managed_class = $this->managedClass;
if($managed_class::has_extension('Versioned')) {
// Get the pages that only exist on live (deleted from stage)
if($checkLivePages && $onlyOnLive) {
$SQL_ids = implode(', ', array_keys($onlyOnLive));
$livePages = Versioned::get_by_stage(
$this->managedClass, "Live",
sprintf(
"\"%s\".\"ID\" IN (%s)",
ClassInfo::baseDataClass($this->managedClass),
$SQL_ids
)
);
if($livePages) foreach($livePages as $obj) {
if($obj->$methodName()) $applicableIDs[] = $obj->ID;
}
}
}
return $applicableIDs;
}
// if your batchaction has parameters, return a FieldList here
public function getParameterFields() {
return false;
}
/**
* If you wish to restrict the batch action to some users, overload this function.
*/
public function canView() {
return true;
}
}