/
MassActionComponent.php
509 lines (435 loc) · 16 KB
/
MassActionComponent.php
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
<?php
/**
* Mass action component
*
* This handles all the different form actions, especialy delete / copy /
* toggle where you need to manipulate many records at a time.
*
* @copyright Copyright (c) 2010 Carl Sutton ( dogmatic69 )
* @link http://www.infinitas-cms.org
* @package Infinitas.Libs.Controller.Component
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.7a
*
* @author Carl Sutton ( dogmatic69 )
* @author dakota
*/
App::uses('InfinitasComponent', 'Libs.Controller/Component');
class MassActionComponent extends InfinitasComponent {
/**
* check if cancel has been clicked to allow things such as locking to act
*
* @param Controller $Controller
*/
public function initialize(Controller $Controller) {
parent::initialize($Controller);
if ($this->getAction(false) == 'cancel') {
$Controller->Event->trigger(
'editCanceled',
!empty($Controller->request->data[$Controller->modelClass]['id']) ? $Controller->request->data[$Controller->modelClass]['id'] : null
);
$Controller->redirect($Controller->getPageRedirectVar());
}
}
/**
* Method to handle mass actions (Such as mass deletions, toggles, etc.)
*
* @return void
*/
public function actionAdminMass() {
$massAction = $this->getAction();
if (isset($this->Controller->modelClass)) {
$modelName = $this->Controller->modelClass;
}
if (!empty($this->Controller->request->data['Confirm']['model'])) {
$modelName = $this->Controller->request->data['Confirm']['model'];
}
$ids = $this->getIds(
$massAction,
!empty($this->Controller->request->data[$modelName]) ? $this->Controller->request->data[$modelName] : array()
);
$massActionMethod = '__massAction' . ucfirst($massAction);
if (method_exists($this->Controller, $massActionMethod)) {
return $this->Controller->{$massActionMethod}($ids);
} else if (method_exists($this, $massAction)) {
return $this->{$massAction}($ids);
}
return $this->generic($massAction, $ids);
}
/**
* Get submitted ids.
*
* Checks the form data and returns an array of all the ids found for that
* model.
*
* @param string $massAction the action to preform
* @param array $data the form data
*
* @return array
*/
public function getIds($massAction, $data) {
if (in_array($massAction, array('add', 'filter', 'install'))) {
return null;
}
$ids = array_values(array_filter(Hash::extract($data, '{n}.massCheckBox')));
if (empty($ids)) {
$this->Controller->notice(__d('libs', 'Nothing was selected, please select something and try again.'), array(
'level' => 'warning',
'redirect' => ''
));
}
return $ids;
}
/**
* Get the action to preform.
*
* Gets the action that was selected from the form.
*
* @param array $form the data from the form submition $this->request->params['form']
*
* @return string
*/
public function getAction($redirect = true) {
if (!empty($this->Controller->request->data['action'])) {
return $this->Controller->request->data['action'];
}
if ($redirect) {
$this->Controller->notice(
__d('libs', 'I dont know what to do.'),
array(
'level' => 'error',
'redirect' => true
)
);
}
}
/**
* Filter records.
*
* This will auto filter out the InfinitasComponent::massActionCheckBox() fields
*
* Checks the data posted from the form and redirects to the url with the params
* for the filter component to catch.
*/
public function filter($null = null) {
$data = array();
foreach ($this->Controller->data[$this->Controller->modelClass] as $k => $field ) {
if (is_int($k) || $k == 'all' || $k == 'massCheckBox') {
continue;
}
if (empty($field) && $field !== 0) {
continue;
}
$data[$this->Controller->modelClass.'.'.$k] = $field;
}
foreach ($this->Controller->{$this->Controller->modelClass}->belongsTo as $model => $options) {
if (empty($this->Controller->data[$model])) {
continue;
}
foreach ((array)$this->Controller->data[$model] as $k => $field) {
if ((empty($field) && $field !== 0) || is_int($k) || $k == 'all' || $k == 'massCheckBox') {
continue;
}
$data[$model . '.' . $k] = $field;
}
}
$this->Controller->redirect(array(
'plugin' => $this->Controller->request->params['plugin'],
'controller' => $this->Controller->request->params['controller'],
'action' => 'index'
) + $this->Controller->request->params['named'] + $data);
}
/**
* Delete records.
*
* Take the array of ids and checks that the deletion was confirmed. if it is
* they will be sent for delete processing (could be soft|hard delete).
*
* If there was no javascript confirmation a page is displayed with the confirmation
*/
public function delete($ids) {
$delete = (isset($this->Controller->data['Confirm']['confirmed']) && $this->Controller->data['Confirm']['confirmed']) ||
(isset($this->Controller->{$this->Controller->modelClass}->noConfirm));
if ($delete) {
if (method_exists($this->Controller, '__handleDeletes')) {
$this->Controller->__handleDeletes($ids);
} else {
$this->__handleDeletes($ids);
}
}
$rows = $this->Controller->{$this->Controller->modelClass}->find('list', array(
'conditions' => array(
$this->Controller->modelClass . '.id' => $ids
)
));
$this->Controller->set('model', $this->Controller->modelClass);
$this->Controller->set(compact('rows'));
$pluginOverload = App::pluginPath($this->Controller->plugin) . 'View' . DS . 'Global' . DS . 'delete.ctp';
if (is_file($pluginOverload)) {
$this->Controller->render($this->Controller->plugin . '.Global/delete');
return;
}
$this->Controller->render('Libs.Global/delete');
$this->Controller->saveRedirectMarker();
}
/**
* Handle delete requests.
*
* Takes the ids and if the model is using the soft delete behavior it will
* stick them in the trash (set a delete flag on the record) or it will do a hard
* delete.
*
* @param array $ids the ids to delete.
*/
public function __handleDeletes($ids) {
$this->Controller->{$this->Controller->modelClass}->transaction();
$deleted = true;
foreach ($ids as $id) {
$deleted = $deleted && $this->Controller->{$this->Controller->modelClass}->delete($id);
}
if ($deleted) {
$this->Controller->{$this->Controller->modelClass}->transaction(true);
$this->Controller->notice('deleted');
}
$this->Controller->{$this->Controller->modelClass}->transaction(false);
$this->Controller->notice('not_deleted');
}
/**
* toggle records.
*
* Takes the array of ids that are passed in and toggles them. If they are active
* they will be inactive and inactive records will be active.
*
* @param array $ids array of ids.
* @param string $message optional string you want the notice to display
*/
public function toggle($ids, $message = null) {
if (!$this->Controller->{$this->Controller->modelClass}->hasField('active')) {
throw new Exception(sprintf('The model "%s" does not have an active field', $this->Controller->modelClass));
}
if (empty($ids)) {
return false;
}
$conditions = array($this->Controller->modelClass . '.id' => $ids);
$newValues = array(
$this->Controller->modelClass . '.active' => '1 - `' . $this->Controller->modelClass . '`.`active`'
);
if ($this->Controller->{$this->Controller->modelClass}->hasField('modified')) {
$newValues[$this->Controller->modelClass . '.modified'] = '\'' . date('Y-m-d H:m:s') . '\'';
}
// unbind things for the update. dont need all the models for this.
$this->Controller->{$this->Controller->modelClass}->unbindModel(array(
'belongsTo' => array_keys($this->Controller->{$this->Controller->modelClass}->belongsTo),
'hasOne' => array_keys($this->Controller->{$this->Controller->modelClass}->hasOne)
));
if ($this->Controller->{$this->Controller->modelClass}->updateAll($newValues, $conditions)) {
$this->Controller->{$this->Controller->modelClass}->afterSave(false);
if (!empty($message)) {
$this->Controller->notice($message, array(
'redirect' => true
));
}
$this->Controller->notice(__d('libs', 'The %s were toggled', $this->Controller->prettyModelName), array(
'redirect' => true
));
}
$this->Controller->notice(__d('libs', 'The %s could not be toggled', $this->Controller->prettyModelName), array(
'level' => 'error',
'redirect' => true
));
}
/**
* Copy a record.
*
* Takes a record id and reads it from the database. Then unset some data
* that is not needed like id and created times and saves the new record.
*
* @todo open add page that is filled out.
*
* @param array $ids array of ids.
*/
public function copy($ids) {
$copyText = sprintf(' - %s (%s)', __d('libs', 'copy'), date('Y-m-d'));
$saves = 0;
if ($this->Controller->{$this->Controller->modelClass}->Behaviors->attached('Contentable')) {
$this->Controller->notice(__d('content', 'Copy is not currently supported for this data'), array(
'redirect' => '',
'level' => 'warning'
));
}
foreach ($ids as $id) {
$record = $this->Controller->{$this->Controller->modelClass}->read(null, $id);
unset($record[$this->Controller->modelClass]['id']);
$check = $record[$this->Controller->modelClass][$this->Controller->{$this->Controller->modelClass}->displayField] != $this->Controller->{$this->Controller->modelClass}->primaryKey;
if ($check) {
$record[$this->Controller->modelClass][$this->Controller->{$this->Controller->modelClass}->displayField] =
$record[$this->Controller->modelClass][$this->Controller->{$this->Controller->modelClass}->displayField] . $copyText;
}
unset(
$record[$this->Controller->modelClass]['created'],
$record[$this->Controller->modelClass]['modified'],
$record[$this->Controller->modelClass]['lft'],
$record[$this->Controller->modelClass]['rght'],
$record[$this->Controller->modelClass]['ordering'],
$record[$this->Controller->modelClass]['order_id'],
$record[$this->Controller->modelClass]['views']
);
$record[$this->Controller->modelClass]['active'] = 0;
foreach ($record[$this->Controller->modelClass] as $field => $value) {
$schema = $this->Controller->{$this->Controller->modelClass}->schema($field);
if (!empty($schema['key']) && $schema['key'] == 'unique') {
$record[$this->Controller->modelClass][$field] .= ' - ' . time();
}
if (strstr($field, '_count')) {
unset($record[$this->Controller->modelClass][$field]);
}
}
$this->Controller->{$this->Controller->modelClass}->create();
if ($this->Controller->{$this->Controller->modelClass}->save($record, array('validate' => false))) {
$saves++;
}
}
if ($saves) {
$this->Controller->notice(__d('libs', '%s copies of %s were made', $saves, $this->Controller->prettyModelName), array(
'redirect' => true
));
}
$this->Controller->notice(__d('libs', 'No copies of %s could be made', $this->Controller->prettyModelName), array(
'level' => 'error',
'redirect' => true
));
}
/**
* Move records
*
* find out relations like belongsTo and habtm and send the ids to a view
* so you can easily move many items
*
* @param array $ids array of ids.
*/
public function move($ids) {
if (isset($this->Controller->data['Move']['confirmed']) && $this->Controller->data['Move']['confirmed']) {
if (method_exists($this->Controller, '__handleMove')) {
$this->Controller->__handleMove($ids);
} else {
$this->__handleMove($ids);
}
}
$rows = $this->Controller->{$this->Controller->modelClass}->find('all', array(
'conditions' => array($this->Controller->modelClass.'.id' => $ids),
'contain' => false
));
$relations['belongsTo'] = array();
if (isset($this->Controller->{$this->Controller->modelClass}->belongsTo)) {
$relations['belongsTo'] = $this->Controller->{$this->Controller->modelClass}->belongsTo;
foreach ($relations['belongsTo'] as $alias => $belongsTo) {
switch($alias) {
case 'Locker':
break;
case 'Parent Post':
case 'Parent':
$_Model = ClassRegistry::init($this->Controller->plugin . '.' . $this->Controller->modelClass);
if (in_array('Tree', $_Model->Behaviors->_attached)) {
$_Model->order = array();
$this->Controller->set(strtolower(Inflector::pluralize($alias)), $_Model->generateTreeList());
} else{
$this->Controller->set(strtolower(Inflector::pluralize($alias)), $_Model->find('list'));
}
break;
default:
$_Model = ClassRegistry::init($this->Controller->plugin . '.' . $alias);
$_Model->order = array();
$this->Controller->set(strtolower(Inflector::pluralize($alias)), $_Model->find('list'));
break;
}
}
}
$relations['hasAndBelongsToMany'] = array();
if (isset($this->Controller->{$this->Controller->modelClass}->hasAndBelongsToMany)) {
$relations['hasAndBelongsToMany'] = $this->Controller->{$this->Controller->modelClass}->hasAndBelongsToMany;
foreach ($relations['hasAndBelongsToMany'] as $alias => $belongsTo) {
$_Model = ClassRegistry::init($this->Controller->plugin . '.' . $alias);
$this->Controller->set(strtolower($alias), $_Model->find('list'));
}
}
if (array_filter(Set::flatten($relations)) == array()) {
$this->Controller->notice(__d($this->Controller->request->params['plugin'], 'There is nothing to move for these records'), array(
'redirect' => true
));
}
$modelSetup['displayField'] = $this->Controller->{$this->Controller->modelClass}->displayField;
$modelSetup['primaryKey'] = $this->Controller->{$this->Controller->modelClass}->primaryKey;
$this->Controller->set(compact('rows', 'model', 'modelSetup', 'relations'));
$this->Controller->saveRedirectMarker();
$pluginOverload = App::pluginPath($this->Controller->plugin) . 'View' . DS . 'Global' . DS . 'move.ctp';
if (is_file($pluginOverload)) {
$this->Controller->render($this->Controller->plugin . '.Global/move');
return;
}
$this->Controller->render('Libs.Global/move');
}
/**
* Handle move requests.
*
* get the id's passed and assign the new relations so the records are
* moved.
*
* @param array $ids the ids to delete.
*/
private function __handleMove($ids) {
$movedTo = $this->Controller->data['Move'];
unset($movedTo['model'], $movedTo['confirmed']);
$result = true;
foreach ($ids as $id) {
$row = array('id' => $id);
$_data = array_merge(array_filter($movedTo), $row);
$_mn = $this->Controller->modelClass;
foreach ($_data as $key => $value) {
if (is_array($value)) {
$save[$key][$key] = $value;
} else {
$save[$_mn][$key] = $value;
}
}
$data = $this->Controller->{$this->Controller->modelClass}->read(null, $id);
unset($data[$this->Controller->modelClass]['lft']);
unset($data[$this->Controller->modelClass]['rght']);
$save[$this->Controller->modelClass] = array_merge($data[$this->Controller->modelClass], $save[$this->Controller->modelClass]);
// @todo this is messing up trees, need to see why the lft and rght is not updating.
$result = $result && $this->Controller->{$this->Controller->modelClass}->saveAll($save);
unset($save);
}
if (in_array('Tree', $this->Controller->{$this->Controller->modelClass}->Behaviors->attached())) {
//$this->Controller->{$this->Controller->modelClass}->recover('parent');
}
if ($result == true) {
$params = array(
'message' => __d('libs', 'The %s have been moved', $this->Controller->prettyModelName)
);
} else {
$params = array(
'level' => 'warning',
'message' => __d('libs', 'Some of the %s could not be moved', $this->Controller->prettyModelName)
);
}
$params['redirect'] = '';
$this->Controller->notice($params['message'], $params);
}
/**
* Generic action.
*
* This method handles the actions like add and edit. If there is no ids or
* there is no id in the array it will redirect to the action without passing
* an id.
*
* @param string $action the action to redirect to.
* @param int $id the id of the record that is selected.
*/
public function generic($action = 'add', $ids = null) {
$url = array('action' => $action);
if (!empty($ids)) {
$url = array_merge($url, $ids);
}
$this->Controller->redirect(array_merge($url, $this->Controller->request->params['named'], (array)$this->Controller->request->params['pass']));
}
}