-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathAbstractBulkProcessingForm.class.php
More file actions
258 lines (213 loc) · 8.48 KB
/
AbstractBulkProcessingForm.class.php
File metadata and controls
258 lines (213 loc) · 8.48 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace wcf\acp\form;
use wcf\data\object\type\ObjectType;
use wcf\data\object\type\ObjectTypeCache;
use wcf\form\AbstractForm;
use wcf\system\bulk\processing\AbstractBulkProcessingAction;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\UserInputException;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
/**
* Abstract implementation of a form for bulk processing objects of a certain type.
*
* @author Matthias Schmidt
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
abstract class AbstractBulkProcessingForm extends AbstractForm
{
/**
* object action object type types
* @var ObjectType[]
*/
public $actions = [];
/**
* number of objects affected by bulk processing
* @var int
*/
public $affectedObjectCount = 0;
/**
* object condition object type types
* @var ObjectType[][]
*/
public $conditions = [];
/**
* list with bulk processed objects
* @var \wcf\data\DatabaseObjectList
* @phpstan-ignore missingType.generics
*/
public $objectList;
/**
* bulk processable object type
* @var ObjectType
*/
public $objectType;
/**
* name of the bulk processable object type
* @var string
*/
public $objectTypeName = '';
/**
* @inheritDoc
*/
public $templateName = 'bulkProcessing';
#[\Override]
public function assignVariables()
{
parent::assignVariables();
$classParts = \explode('\\', static::class);
WCF::getTPL()->assign([
'actions' => $this->actions,
'affectedObjectCount' => $this->affectedObjectCount,
'controller' => \str_replace('Form', '', \array_pop($classParts)),
'controllerApplication' => $classParts[0],
'conditions' => $this->conditions,
'objectType' => $this->objectType,
]);
}
#[\Override]
public function readData()
{
// read bulk processable object type
$this->objectType = ObjectTypeCache::getInstance()->getObjectTypeByName(
'com.woltlab.wcf.bulkProcessableObject',
$this->objectTypeName
);
if ($this->objectType === null) {
throw new \LogicException("Unknown bulk processable object type '" . $this->objectTypeName . "'");
}
// read conditions
if (ObjectTypeCache::getInstance()->getDefinitionByName($this->objectType->getProcessor()->getConditionObjectTypeDefinition()) === null) {
throw new \LogicException("Unknown condition object type definition '" . $this->objectType->getProcessor()->getConditionObjectTypeDefinition() . "'");
}
$conditionObjectTypes = ObjectTypeCache::getInstance()->getObjectTypes($this->objectType->getProcessor()->getConditionObjectTypeDefinition());
if (empty($conditionObjectTypes)) {
throw new IllegalLinkException();
}
foreach ($conditionObjectTypes as $objectType) {
if ($objectType->conditiongroup) {
if (!isset($this->conditions[$objectType->conditiongroup])) {
$this->conditions[$objectType->conditiongroup] = [];
}
$this->conditions[$objectType->conditiongroup][$objectType->objectTypeID] = $objectType;
} else {
$this->conditions[''][$objectType->objectTypeID] = $objectType;
}
}
// read actions
if (ObjectTypeCache::getInstance()->getDefinitionByName($this->objectType->getProcessor()->getActionObjectTypeDefinition()) === null) {
throw new \LogicException("Unknown action object type definition '" . $this->objectType->getProcessor()->getActionObjectTypeDefinition() . "'");
}
$actions = ObjectTypeCache::getInstance()->getObjectTypes($this->objectType->getProcessor()->getActionObjectTypeDefinition());
foreach ($actions as $objectType) {
if (isset($this->actions[$objectType->action])) {
throw new \LogicException("Duplicate action with name '" . $objectType->action . "'");
}
if ($objectType->validateOptions() && $objectType->validatePermissions()) {
$this->actions[$objectType->action] = $objectType;
}
}
if (empty($this->actions)) {
throw new IllegalLinkException();
}
parent::readData();
if (empty($_POST)) {
if (isset($_REQUEST['success']) && isset($_REQUEST['count'])) {
$this->affectedObjectCount = \intval($_REQUEST['count']);
WCF::getTPL()->assign('success', true);
}
}
}
#[\Override]
public function readFormParameters()
{
parent::readFormParameters();
foreach ($this->conditions as $groupedObjectTypes) {
foreach ($groupedObjectTypes as $objectType) {
$objectType->getProcessor()->readFormParameters();
}
}
if (isset($this->actions[$this->action])) {
$this->actions[$this->action]->getProcessor()->readFormParameters();
}
}
#[\Override]
public function save()
{
$action = $this->actions[$this->action]->getProcessor();
\assert($action instanceof AbstractBulkProcessingAction);
$this->objectList = $action->getObjectList();
parent::save();
// read objects
foreach ($this->conditions as $groupedObjectTypes) {
foreach ($groupedObjectTypes as $objectType) {
$data = $objectType->getProcessor()->getData();
if ($data !== null) {
$objectType->getProcessor()->addObjectListCondition($this->objectList, $data);
}
}
}
if ($action->canRunInWorker()) {
$this->objectList->readObjectIDs();
$this->affectedObjectCount = \count($this->objectList->getObjectIDs());
// save config in session
$bulkProcessingData = WCF::getSession()->getVar('bulkProcessingData');
if ($bulkProcessingData === null) {
$bulkProcessingData = [];
}
$bulkProcessingID = \count($bulkProcessingData) + 1;
$bulkProcessingData[$bulkProcessingID] = [
'objectTypeID' => $action->objectTypeID,
'additionalParameters' => $action->getAdditionalParameters(),
'objectIDs' => $this->objectList->getObjectIDs(),
'form' => LinkHandler::getInstance()->getControllerLink(\get_called_class(), [
'isACP' => true,
'success' => true,
'count' => $this->affectedObjectCount,
]),
];
WCF::getSession()->register('bulkProcessingData', $bulkProcessingData);
$this->saved();
WCF::getTPL()->assign('bulkProcessingID', $bulkProcessingID);
} else {
// Set a limit to avoid the 'Prepared statement contains too many placeholders' error
$this->objectList->sqlLimit = 65000;
$this->objectList->readObjects();
// execute action
if (\count($this->objectList)) {
$this->actions[$this->action]->getProcessor()->executeAction($this->objectList);
}
$this->affectedObjectCount = \count($this->objectList);
$this->saved();
// reset fields
$this->actions[$this->action]->getProcessor()->reset();
foreach ($this->conditions as $groupedObjectTypes) {
foreach ($groupedObjectTypes as $objectType) {
$objectType->getProcessor()->reset();
}
}
$this->action = '';
WCF::getTPL()->assign('success', true);
}
}
#[\Override]
public function validate()
{
parent::validate();
// validate action
if (empty($this->action)) {
throw new UserInputException('action');
}
if (!isset($this->actions[$this->action])) {
throw new UserInputException('action', 'noValidSelection');
}
$this->actions[$this->action]->getProcessor()->validate();
// validate conditions
foreach ($this->conditions as $groupedObjectTypes) {
foreach ($groupedObjectTypes as $objectType) {
$objectType->getProcessor()->validate();
}
}
}
}