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 Core\ORM\Table; |
19
|
|
|
use JBZoo\Utils\Arr; |
20
|
|
|
use JBZoo\Utils\Str; |
21
|
|
|
use Cake\Utility\Hash; |
22
|
|
|
use Cake\Http\ServerRequest; |
23
|
|
|
use Cake\Controller\Component; |
24
|
|
|
use Cake\Controller\Controller; |
25
|
|
|
use Cake\Network\Exception\BadRequestException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class AppComponent |
29
|
|
|
* |
30
|
|
|
* @package Core\Controller\Component |
31
|
|
|
*/ |
32
|
|
|
class AppComponent extends Component |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Controller object. |
37
|
|
|
* |
38
|
|
|
* @var Controller |
39
|
|
|
*/ |
40
|
|
|
protected $_controller; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Hold controller request (Server request). |
44
|
|
|
* |
45
|
|
|
* @var ServerRequest |
46
|
|
|
*/ |
47
|
|
|
protected $_request; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor hook method. |
51
|
|
|
* |
52
|
|
|
* @param array $config |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function initialize(array $config) |
56
|
|
|
{ |
57
|
|
|
$this->_controller = $this->_registry->getController(); |
58
|
|
|
$this->_request = $this->_controller->request; |
59
|
|
|
|
60
|
|
|
parent::initialize($config); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Redirect by request data. |
65
|
|
|
* |
66
|
|
|
* @param array $options |
67
|
|
|
* @return \Cake\Http\Response|null |
68
|
|
|
*/ |
69
|
|
|
public function redirect(array $options = []) |
70
|
|
|
{ |
71
|
|
|
$plugin = $this->_request->getParam('plugin'); |
72
|
|
|
$controller = $this->_request->getParam('controller'); |
73
|
|
|
|
74
|
|
|
$_options = [ |
75
|
|
|
'apply' => [], |
76
|
|
|
'savenew' => [ |
77
|
|
|
'plugin' => $plugin, |
78
|
|
|
'controller' => $controller, |
79
|
|
|
'action' => 'add' |
80
|
|
|
], |
81
|
|
|
'save' => [ |
82
|
|
|
'plugin' => $plugin, |
83
|
|
|
'controller' => $controller, |
84
|
|
|
'action' => 'index', |
85
|
|
|
] |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
$options = Hash::merge($_options, $options); |
89
|
|
|
|
90
|
|
|
$url = $options['save']; |
91
|
|
|
if ($rAction = $this->_request->getData('action')) { |
92
|
|
|
list(, $action) = pluginSplit($rAction); |
|
|
|
|
93
|
|
|
$action = Str::low($action); |
94
|
|
|
if (Arr::key($action, $options)) { |
95
|
|
|
$url = $options[$action]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->_controller->redirect($url); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Toggle table field value. |
104
|
|
|
* |
105
|
|
|
* @param Table $table |
106
|
|
|
* @param int $id |
107
|
|
|
* @param string|int $value |
108
|
|
|
* @param string $field |
109
|
|
|
*/ |
110
|
|
|
public function toggleField(Table $table, $id, $value, $field = 'status') |
111
|
|
|
{ |
112
|
|
|
$this->checkIsAjax(); |
113
|
|
|
$this->_checkToggleData($id, $value); |
114
|
|
|
|
115
|
|
|
$this->_controller->viewBuilder() |
116
|
|
|
->setLayout('ajax') |
117
|
|
|
->setTemplate('toggle') |
118
|
|
|
->setTemplatePath('Common'); |
119
|
|
|
|
120
|
|
|
$entity = $table->get($id); |
121
|
|
|
$entity->set($field, !(int) $value); |
122
|
|
|
|
123
|
|
|
if ($result = $table->save($entity)) { |
124
|
|
|
$this->_controller->set('entity', $result); |
125
|
|
|
$this->_controller->render('toggle'); |
126
|
|
|
} else { |
127
|
|
|
throw new \RuntimeException(__d('core', 'Failed toggling field {0} to {1}', $field, $entity->get($field))); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Check is ajax request. |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function checkIsAjax() |
137
|
|
|
{ |
138
|
|
|
if (!$this->_request->is('ajax')) { |
139
|
|
|
throw new \RuntimeException(__d('core', 'Bad request')); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check toggle data. |
145
|
|
|
* |
146
|
|
|
* @param int $id |
147
|
|
|
* @param string|int $value |
148
|
|
|
*/ |
149
|
|
|
protected function _checkToggleData($id, $value) |
150
|
|
|
{ |
151
|
|
|
if (empty($id) || $value === null) { |
152
|
|
|
throw new BadRequestException(__d('core', 'Invalid content')); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.