bulkaction.php
17.2 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
/**
* $Id$
*
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1.2 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.knowledgetree.com/KPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* All copies of the Covered Code must include on each user interface screen:
* (i) the "Powered by KnowledgeTree" logo and
* (ii) the KnowledgeTree copyright notice
* in the same form as they appear in the distribution. See the License for
* requirements.
*
* The Original Code is: KnowledgeTree Open Source
*
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2007 The Jam Warehouse Software (Pty) Ltd;
* All Rights Reserved.
* Contributor( s): ______________________________________
*
*/
require_once(KT_LIB_DIR . '/actions/actionregistry.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflowutil.inc.php');
require_once(KT_LIB_DIR . '/dispatcher.inc.php');
require_once(KT_LIB_DIR . '/browse/browseutil.inc.php');
require_once(KT_LIB_DIR . '/actions/entitylist.php');
require_once(KT_LIB_DIR . '/foldermanagement/folderutil.inc.php');
require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
require_once(KT_LIB_DIR . '/widgets/forms.inc.php');
require_once(KT_LIB_DIR . "/util/sanitize.inc");
class KTBulkAction extends KTStandardDispatcher {
var $sName;
var $sDescription;
var $_sDisablePermission;
var $bAllowInAdminMode = false;
var $sHelpPage = 'ktcore/browse.html';
var $sSection = 'view_details';
var $_bMutator = false;
var $_bMutationAllowedByAdmin = true;
var $sIconClass;
// not 'sShowPermission' - mass actions are always shown
// this is used to check against individual entities
var $_sPermission = 'ktcore.permissions.read';
function KTBulkAction($oUser = null, $oPlugin = null) {
$this->oEntityList = null;
$this->oActiveEntityList = null;
$this->oUser =& $oUser;
$this->oPlugin =& $oPlugin;
$this->aBreadcrumbs = array(
array('action' => 'browse', 'name' => _kt('Browse')),
);
$this->persistParams('fEntityListCode');
parent::KTStandardDispatcher();
}
function setEntityList(&$oEntityList) {
$this->oEntityList =& $oEntityList;
}
function setUser(&$oUser) {
$this->oUser =& $oUser;
}
function _show() {
return true;
}
function getURL() {
$oKTConfig =& KTConfig::getSingleton();
$sExt = '.php';
if (KTUtil::arrayGet($_SERVER, 'kt_no_extensions')) {
$sExt = '';
}
if ($oKTConfig->get('KnowledgeTree/pathInfoSupport')) {
return sprintf('%s/action%s/%s', $GLOBALS['KTRootUrl'], $sExt, $this->sName);
} else {
return sprintf('%s/action%s?kt_path_info=%s', $GLOBALS['KTRootUrl'], $sExt, $this->sName);
}
}
function getInfo() {
if ($this->_show() === false) {
return null;
}
$url = $this->getURL();
$aInfo = array(
'description' => $this->sDescription,
'name' => $this->getDisplayName(),
'ns' => $this->sName,
'url' => $url,
'icon_class' => $this->sIconClass,
);
$aInfo = $this->customiseInfo($aInfo);
return $aInfo;
}
function getName() {
return sanitizeForSQLtoHTML($this->sName);
}
function getDisplayName() {
return sanitizeForSQLtoHTML($this->sDisplayName);
}
function getDescription() {
return sanitizeForSQLtoHTML($this->sDescription);
}
function customiseInfo($aInfo) {
return $aInfo;
}
// helper function
function _getNames($aIds, $sEntity) {
if(count($aIds)) {
$aNames = array();
$aFunc = array($sEntity, 'get');
foreach($aIds as $id) {
$oE =& call_user_func($aFunc, $id);
$aNames[] = $oE->getName();
}
return $aNames;
} else {
return array();
}
}
// doesn't actually do checks, as they have to be performed per-entity
function check() {
// not necessarily coming from a folder...
$iFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', 1);
$this->oFolder = Folder::get($iFolderId);
//$this->oFolder =& $this->oValidator->validateFolder($_REQUEST['fFolderId']);
$aOptions = array(
'final' => false,
'documentaction' => 'viewDocument',
'folderaction' => 'browse',
);
$this->aBreadcrumbs = array(array('name'=>_kt('Bulk Actions')),
array('name'=>$this->getDisplayName()));
return true;
}
// check the entire entity list. this needn't be overrided at any point
function check_entities() {
$aFailed = array('documents' => array(), 'folders' => array());
$aSucceeded = array('documents' => array(), 'folders' => array());
if(!$this->oEntityList) {
return true;
}
foreach($this->oEntityList->getDocumentIds() as $iId) {
$oDocument =& Document::get($iId);
if(PEAR::isError($oDocument)) {
$aFailed['documents'][] = array($iId, _kt('No such document'));
} else {
$res = $this->check_entity($oDocument);
// all these checks allow a return from check_entity of:
// 1. a PEAR error, indicating failure, with the message in the error
// 2. false, for unknown error
// 3. true, to pass
if(PEAR::isError($res)) {
$aFailed['documents'][] = array($oDocument->getName(), $res->getMessage());
} else if($res === false) {
$aFailed['documents'][] = array($oDocument->getName(), _kt('Failed (unknown reason)'));
} else {
$aSucceeded['documents'][] = $oDocument->getId();
}
}
}
foreach($this->oEntityList->getFolderIds() as $iId) {
$oFolder =& Folder::get($iId);
if(PEAR::isError($oFolder)) {
$aFailed['folders'][] = array($iId, _kt('No such folder'));
} else {
$res = $this->check_entity($oFolder);
if(PEAR::isError($res)) {
$aFailed['folders'][] = array($oFolder->getName(), $res->getMessage());
} else if($res === false) {
$aFailed['folders'][] = array($oFolder->getName(), _kt('Failed (unknown reason)'));
} else {
$aSucceeded['folders'][] = $oFolder->getId();
}
}
}
$this->oActiveEntityList = new KTEntityList($aSucceeded['documents'], $aSucceeded['folders']);
$this->aFailed = $aFailed;
return count($aSucceeded['documents']) + count($aSucceeded['folders']);
}
// iterate over all entites to act on them
function perform_action_on_list() {
$this->aActionResults = array('folders'=>array(), 'documents'=>array());
foreach($this->oActiveEntityList->getDocumentIds() as $iId) {
$oDocument =& Document::get($iId);
if(!PEAR::isError($oDocument)) {
$sName = $oDocument->getName();
} else {
$sName = _kt('Error fetching document name');
}
$res = $this->perform_action($oDocument);
if(PEAR::isError($res)) {
$this->aActionResults['documents'][] = array($sName, $res->getMessage());
} else {
$this->aActionResults['documents'][] = array($sName, _kt('Success'));
}
}
foreach($this->oActiveEntityList->getFolderIds() as $iId) {
$oFolder =& Folder::get($iId);
if(!PEAR::isError($oFolder)) {
$sName = $oFolder->getName();
} else {
$sName = _kt('Error fetching folder name');
}
$res = $this->perform_action($oFolder);
if(PEAR::isError($res)) {
$this->aActionResults['folders'][] = array($sName, $res->getMessage());
} else {
$this->aActionResults['folders'][] = array($sName, _kt('Success'));
}
}
}
// list persistance
// fetch existing lists
function get_lists() {
$this->oEntityList = KTEntityList::retrieveList(KTUtil::arrayGet($_REQUEST, 'fListCode', null));
$this->oActiveEntityList = KTEntityList::retrieveList(KTUtil::arrayGet($_REQUEST, 'fActiveListCode', null));
if(PEAR::isError($this->oActiveEntityList)) {
$this->oActiveEntityList = null;
}
}
// persist
function store_lists() {
$this->persistParams(array('fListCode', 'fActiveListCode', 'fFolderId', 'fReturnData', 'fReturnAction'));
}
// forms
// form to list the entites after checking each one
function form_listing() {
$sListCode = $this->oEntityList->getCode();
$sActiveListCode = $this->oActiveEntityList->getCode();
$oForm = new KTForm;
$oForm->setOptions(array(
'identifier' => 'ktcore.actions.bulk.listing.form',
'submit_label' => _kt('Continue'),
'targeturl' => $this->getURL(),
'action' => 'collectinfo',
'fail_action' => 'main',
'noframe' => true,
'extraargs' => array('fListCode' => $sListCode,
'fActiveListCode' => $sActiveListCode,
'fFolderId' => $this->oFolder->getId(),
'fReturnAction' => KTUtil::arrayGet($_REQUEST, 'fReturnAction'),
'fReturnData' => KTUtil::arrayGet($_REQUEST, 'fReturnData'),
),
'context' => $this,
));
return $oForm;
}
// form to show on action completion, and list results
function form_complete() {
$sReturnAction = KTUtil::arrayGet($_REQUEST, 'fReturnAction');
$sReturnData = KTUtil::arrayGet($_REQUEST, 'fReturnData');
$sAction = 'main';
if($sReturnAction == 'browse') {
if ($sReturnData == '')
{
$sReturnData = KTUtil::arrayGet($_REQUEST, 'fFolderId');
}
$sTargetUrl = KTBrowseUtil::getUrlForFolder(Folder::get($sReturnData));
} else if($sReturnAction == 'simpleSearch') {
$sTargetUrl = KTBrowseUtil::getSimpleSearchBaseUrl();
$extraargs = array('fSearchableText'=>$sReturnData);
} else if($sReturnAction == 'booleanSearch') {
$sTargetUrl = KTBrowseUtil::getBooleanSearchBaseUrl();
$sAction = 'performSearch';
$extraargs = array('boolean_search_id'=>$sReturnData);
}
$oForm = new KTForm;
$oForm->setOptions(array(
'identifier' => 'ktcore.actions.bulk.complete.form',
'submit_label' => _kt('Return'),
'targeturl' => $sTargetUrl,
'context' => $this,
'action' => $sAction,
'extraargs' => $extraargs,
'noframe' => true,
));
return $oForm;
}
// main entry point - checks the entity list and displays lists
function do_main() {
// get entities (using the checkboxes atm)
$aFolders = KTUtil::arrayGet($_REQUEST, 'selection_f' , array());
$aDocuments = KTUtil::arrayGet($_REQUEST, 'selection_d' , array());
$this->oEntityList = new KTEntityList($aDocuments, $aFolders);
// gives us $this->aFailed
$iActiveCount = $this->check_entities();
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate('ktcore/bulk_action_listing');
$this->store_lists();
return $oTemplate->render(array('context' => $this,
'form' => $this->form_listing(),
'failed' => $this->aFailed,
'active' => $this->oActiveEntityList,
'activecount' => $iActiveCount,
'failedform' => $this->form_complete(),
'folders' => $this->_getNames($this->oActiveEntityList->getFolderIds(), 'Folder'),
'documents' => $this->_getNames($this->oActiveEntityList->getDocumentIds(), 'Document')));
}
// override to add a screen to get a reason, or whatever
// remember to pass to perform action next, and call the super's method
function do_collectinfo() {
$this->store_lists();
return $this->do_performaction();
}
// perform the action itself, and list results (completion action)
function do_performaction() {
$this->get_lists();
$this->aPersistParams = array();
$this->perform_action_on_list();
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate('ktcore/bulk_action_complete');
return $oTemplate->render(array('context' => $this,
'list' => $this->aActionResults,
'form' => $this->form_complete()));
}
// main overrides
// override to do the actual action, on an individual entity
function perform_action($oEntity) {
return PEAR::raiseError(_kt('Action component not implemented'));
}
// check an individual entity - this should be overrided with additional
// checks required for the specific action, but should always call its
// parent implementation
function check_entity($oEntity) {
$oPermission =& KTPermission::getByName($this->_sPermission);
if(PEAR::isError($oPermission)) {
return true;
}
// basic document checks
// TODO: check if this is appropriate
// should probably store the 'equivalent' action (ie. document.delete)
// and check that, rather than add a new list of actions to the workflow
// section
if(is_a($oEntity, 'Document')) {
if(!KTWorkflowUtil::actionEnabledForDocument($oEntity, $this->sName)) {
return PEAR::raiseError(_kt('Action is disabled by workflow'));
}
$status = $oEntity->getStatusID();
if($status==DELETED||$status==ARCHIVED) {
return PEAR::raiseError(_kt('Document is archived or deleted'));
}
}
// admin check
if($this->bAllowInAdminMode) {
if(KTBrowseUtil::inAdminMode($this->oUser, null)) {
return true;
}
}
if(!KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oPermission, $oEntity)) {
return PEAR::raiseError(_kt('You do not have the required permissions'));
}
return true;
}
}
class KTBulkDocumentAction extends KTBulkAction {
function check_entity($oEntity) {
if(!is_a($oEntity, 'Document')) {
return false;
}
return parent::check_entity($oEntity);
}
}
class KTBulkFolderAction extends KTBulkAction {
function check_entity($oEntity) {
if(!is_a($oEntity, 'Folder')) {
return false;
}
return parent::check_entity($oEntity);
}
}
// util class for bulk actions
class KTBulkActionUtil {
function getBulkActionInfo($slot = 'bulkaction') {
$oRegistry =& KTActionRegistry::getSingleton();
return $oRegistry->getActions($slot);
}
function getAllBulkActions($slot = 'bulkaction') {
$aObjects = array();
foreach (KTBulkActionUtil::getBulkActionInfo($slot) as $aAction) {
list($sClassName, $sPath, $sName, $sPlugin) = $aAction;
$oRegistry =& KTPluginRegistry::getSingleton();
$oPlugin =& $oRegistry->getPlugin($sPlugin);
if (!empty($sPath)) {
require_once($sPath);
}
$aObjects[] =& new $sClassName(null, null, $oPlugin);
}
return $aObjects;
}
function getBulkActionsByNames($aNames, $slot = 'bulkaction', $oUser = null) {
$aObjects = array();
foreach (KTBulkActionUtil::getBulkActionInfo($slot) as $aAction) {
list($sClassName, $sPath, $sName, $sPlugin) = $aAction;
$oRegistry =& KTPluginRegistry::getSingleton();
$oPlugin =& $oRegistry->getPlugin($sPlugin);
if (!in_array($sName, $aNames)) {
continue;
}
if (!empty($sPath)) {
require_once($sPath);
}
$aObjects[] =& new $sClassName(null, $oUser, $oPlugin);
}
return $aObjects;
}
}
?>