|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core\ORM\Behavior; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\ORM\Behavior; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class ProcessBehavior |
|
22
|
|
|
* |
|
23
|
|
|
* @package Core\ORM\Behavior |
|
24
|
|
|
*/ |
|
25
|
|
|
class ProcessBehavior extends Behavior |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Default config. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $_defaultConfig = [ |
|
33
|
|
|
'field' => 'status', |
|
34
|
|
|
'actions' => [ |
|
35
|
|
|
'delete' => 'processDelete', |
|
36
|
|
|
'publish' => 'processPublish', |
|
37
|
|
|
'unpublish' => 'processUnPublish' |
|
38
|
|
|
], |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Process table method. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* @param array $ids |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public function process($name, array $ids = []) |
|
49
|
|
|
{ |
|
50
|
|
|
$allowActions = $this->config('actions'); |
|
51
|
|
|
if (!isset($allowActions[$name])) { |
|
52
|
|
|
throw new \InvalidArgumentException(__d('core', 'Invalid action to perform')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$action = $allowActions[$name]; |
|
56
|
|
|
if ($action === false) { |
|
57
|
|
|
throw new \InvalidArgumentException(__d('core', 'Action "{0}" is disabled', $name)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (in_array($action, get_class_methods($this->_table))) { |
|
61
|
|
|
return $this->_table->{$action}($ids); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->{$action}($ids); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Process delete method. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $ids |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function processDelete(array $ids) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->_table->deleteAll([ |
|
76
|
|
|
$this->_table->primaryKey() . ' IN (' . implode(',', $ids) . ')' |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Process publish method. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $ids |
|
84
|
|
|
* @return int |
|
85
|
|
|
*/ |
|
86
|
|
|
public function processPublish(array $ids) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->_toggleField($ids, STATUS_PUBLISH); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Process un publish method. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $ids |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
public function processUnPublish(array $ids) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->_toggleField($ids, STATUS_UN_PUBLISH); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Toggle table field. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $ids |
|
106
|
|
|
* @param int $value |
|
107
|
|
|
* @return int |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function _toggleField(array $ids, $value = STATUS_UN_PUBLISH) |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->_table->updateAll([ |
|
112
|
|
|
$this->_configRead('field') => $value, |
|
113
|
|
|
], [ |
|
114
|
|
|
$this->_table->primaryKey() . ' IN (' . implode(',', $ids) . ')' |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|