|
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\Controller\Component; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\ORM\Table; |
|
19
|
|
|
use JBZoo\Data\Data; |
|
20
|
|
|
use JBZoo\Data\JSON; |
|
21
|
|
|
use JBZoo\Utils\Arr; |
|
22
|
|
|
use JBZoo\Utils\Str; |
|
23
|
|
|
use Cake\Utility\Hash; |
|
24
|
|
|
use Cake\Utility\Inflector; |
|
25
|
|
|
use Core\Event\EventManager; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class ProcessComponent |
|
29
|
|
|
* |
|
30
|
|
|
* @package Core\Controller\Component |
|
31
|
|
|
* @property FlashComponent $Flash |
|
32
|
|
|
*/ |
|
33
|
|
|
class ProcessComponent extends AppComponent |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
const PRIMARY_KEY = 'id'; |
|
37
|
|
|
const EVENT_NAME_BEFORE = 'Before'; |
|
38
|
|
|
const EVENT_NAME_AFTER = 'After'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Other Components this component uses. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
public $components = [ |
|
46
|
|
|
'Core.Flash' |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get actual request vars for process. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param string $primaryKey |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getRequestVars($name, $primaryKey = self::PRIMARY_KEY) |
|
57
|
|
|
{ |
|
58
|
|
|
$name = Str::low(Inflector::singularize($name)); |
|
59
|
|
|
$requestIds = (array) $this->_request->getData($name); |
|
60
|
|
|
$action = $this->_request->getData('action'); |
|
61
|
|
|
$ids = $this->_getIds($requestIds, $primaryKey); |
|
62
|
|
|
|
|
63
|
|
|
return [$action, $ids]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Constructor hook method. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $config |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function initialize(array $config) |
|
73
|
|
|
{ |
|
74
|
|
|
parent::initialize($config); |
|
75
|
|
|
|
|
76
|
|
|
$_config = [ |
|
77
|
|
|
'context' => __d('core', 'record'), |
|
78
|
|
|
'redirect' => [ |
|
79
|
|
|
'action' => 'index', |
|
80
|
|
|
'prefix' => $this->_request->getParam('prefix'), |
|
81
|
|
|
'plugin' => $this->_request->getParam('plugin'), |
|
82
|
|
|
'controller' => $this->_request->getParam('controller'), |
|
83
|
|
|
], |
|
84
|
|
|
'messages' => [ |
|
85
|
|
|
'no_action' => __d('core', 'Action not found.'), |
|
86
|
|
|
'no_choose' => __d('core', 'Please choose only one item.'), |
|
87
|
|
|
] |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
$config = Hash::merge($_config, $config); |
|
91
|
|
|
$this->setConfig($config); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Make process. |
|
96
|
|
|
* |
|
97
|
|
|
* @param Table $table |
|
98
|
|
|
* @param string $action |
|
99
|
|
|
* @param array $ids |
|
100
|
|
|
* @param array $options |
|
101
|
|
|
* @return \Cake\Http\Response|null |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \Aura\Intl\Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
public function make(Table $table, $action, array $ids = [], array $options = []) |
|
106
|
|
|
{ |
|
107
|
|
|
$count = count($ids); |
|
108
|
|
|
$options = $this->_getOptions($options, $count); |
|
109
|
|
|
$redirectUrl = $options['redirect']; |
|
110
|
|
|
$messages = new JSON($options['messages']); |
|
111
|
|
|
|
|
112
|
|
|
$event = EventManager::trigger($this->_getEventName($action), $this->_controller, ['ids' => $ids]); |
|
113
|
|
|
$ids = (array) $event->getData('ids'); |
|
114
|
|
|
$count = count($ids); |
|
115
|
|
|
|
|
116
|
|
|
if (!$action) { |
|
117
|
|
|
$this->Flash->error($messages->get('no_action')); |
|
118
|
|
|
return $this->_controller->redirect($redirectUrl); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ($count <= 0) { |
|
122
|
|
|
$this->Flash->error($messages->get('no_choose')); |
|
123
|
|
|
return $this->_controller->redirect($redirectUrl); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->_loadBehavior($table); |
|
127
|
|
|
if ($table->process($action, $ids)) { |
|
|
|
|
|
|
128
|
|
|
return $this->_process($action, $messages, $redirectUrl, $ids); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->Flash->error(__d('core', 'An error has occurred. Please try again.')); |
|
132
|
|
|
return $this->_controller->redirect($redirectUrl); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Setup default action messages. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int $count |
|
139
|
|
|
* @return array |
|
140
|
|
|
* |
|
141
|
|
|
* @throws \Aura\Intl\Exception |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function _getDefaultMessages($count) |
|
144
|
|
|
{ |
|
145
|
|
|
$context = $this->_configRead('context'); |
|
146
|
|
|
$contextPlural = Inflector::pluralize($context); |
|
147
|
|
|
$countString = sprintf('<strong>%s</strong>', $count); |
|
148
|
|
|
return [ |
|
149
|
|
|
'delete' => __dn( |
|
150
|
|
|
'core', |
|
151
|
|
|
'One ' . $context . ' success removed', |
|
152
|
|
|
'{0} ' . $contextPlural . ' success removed', |
|
153
|
|
|
$count, |
|
154
|
|
|
$countString |
|
155
|
|
|
), |
|
156
|
|
|
'publish' => __dn( |
|
157
|
|
|
'core', |
|
158
|
|
|
'One ' . $context . ' success publish', |
|
159
|
|
|
'{0} ' . $contextPlural . ' success published', |
|
160
|
|
|
$count, |
|
161
|
|
|
$countString |
|
162
|
|
|
), |
|
163
|
|
|
'unpublish' => __dn( |
|
164
|
|
|
'core', |
|
165
|
|
|
'One ' . $context . ' success unpublish', |
|
166
|
|
|
'{0} ' . $contextPlural . ' success unpublished', |
|
167
|
|
|
$count, |
|
168
|
|
|
$countString |
|
169
|
|
|
), |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get event name by data. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $action |
|
177
|
|
|
* @param string $event |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function _getEventName($action, $event = self::EVENT_NAME_BEFORE) |
|
181
|
|
|
{ |
|
182
|
|
|
$details = []; |
|
183
|
|
|
if ($prefix = $this->_request->getParam('prefix')) { |
|
184
|
|
|
$details[] = ucfirst($prefix); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
$details = Hash::merge($details, [ |
|
188
|
|
|
'Controller', |
|
189
|
|
|
$this->_controller->getName(), |
|
190
|
|
|
$event . Inflector::camelize($action), |
|
191
|
|
|
'Process', |
|
192
|
|
|
]); |
|
193
|
|
|
|
|
194
|
|
|
return implode('.', $details); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get ids by request. |
|
199
|
|
|
* |
|
200
|
|
|
* @param array $ids |
|
201
|
|
|
* @param string $primaryKey |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function _getIds(array $ids, $primaryKey = self::PRIMARY_KEY) |
|
205
|
|
|
{ |
|
206
|
|
|
$return = []; |
|
207
|
|
|
foreach ($ids as $id => $value) { |
|
208
|
|
|
if (is_array($value) && is_int($id) && (int) $value[$primaryKey] === 1) { |
|
209
|
|
|
$return[$id] = $id; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $return; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Create and merge actual process options. |
|
218
|
|
|
* |
|
219
|
|
|
* @param array $options |
|
220
|
|
|
* @param int|string $count |
|
221
|
|
|
* @return array |
|
222
|
|
|
* |
|
223
|
|
|
* @throws \Aura\Intl\Exception |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function _getOptions(array $options, $count) |
|
226
|
|
|
{ |
|
227
|
|
|
$options = Hash::merge($this->_config, $options); |
|
228
|
|
|
return Hash::merge(['messages' => $this->_getDefaultMessages($count)], $options); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Load process behavior. |
|
233
|
|
|
* |
|
234
|
|
|
* @param Table $table |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function _loadBehavior(Table $table) |
|
237
|
|
|
{ |
|
238
|
|
|
$behaviors = $table->behaviors(); |
|
239
|
|
|
if (!Arr::in('Process', $behaviors->loaded())) { |
|
240
|
|
|
$behaviors->load('Core.Process'); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Controller process. |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $action |
|
248
|
|
|
* @param Data $messages |
|
249
|
|
|
* @param array $redirect |
|
250
|
|
|
* @param array $ids |
|
251
|
|
|
* @return \Cake\Http\Response|null |
|
252
|
|
|
* |
|
253
|
|
|
* @throws \Aura\Intl\Exception |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function _process($action, Data $messages, array $redirect, array $ids) |
|
256
|
|
|
{ |
|
257
|
|
|
$count = count($ids); |
|
258
|
|
|
$defaultMsg = __dn( |
|
259
|
|
|
'core', |
|
260
|
|
|
'One record success processed', |
|
261
|
|
|
'{0} records processed', |
|
262
|
|
|
$count, |
|
263
|
|
|
sprintf('<strong>%s</strong>', $count) |
|
264
|
|
|
); |
|
265
|
|
|
|
|
266
|
|
|
EventManager::trigger($this->_getEventName($action, self::EVENT_NAME_AFTER), $this->_controller, [ |
|
267
|
|
|
'ids' => $ids |
|
268
|
|
|
]); |
|
269
|
|
|
|
|
270
|
|
|
$this->Flash->success($messages->get($action, $defaultMsg)); |
|
271
|
|
|
return $this->_controller->redirect($redirect); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.