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\View; |
17
|
|
|
|
18
|
|
|
use Core\Cms; |
19
|
|
|
use Cake\Core\App; |
20
|
|
|
use Cake\View\View; |
21
|
|
|
use JBZoo\Utils\FS; |
22
|
|
|
use JBZoo\Utils\Arr; |
23
|
|
|
use Core\Core\Plugin; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class AppView |
27
|
|
|
* |
28
|
|
|
* @package Core\View |
29
|
|
|
* @property \Core\View\Helper\JsHelper $Js |
30
|
|
|
* @property \Core\View\Helper\UrlHelper $Url |
31
|
|
|
* @property \Core\View\Helper\NavHelper $Nav |
32
|
|
|
* @property \Core\View\Helper\LessHelper $Less |
33
|
|
|
* @property \Core\View\Helper\FormHelper $Form |
34
|
|
|
* @property \Core\View\Helper\HtmlHelper $Html |
35
|
|
|
* @property \Core\View\Helper\FilterHelper $Filter |
36
|
|
|
* @property \Core\View\Helper\AssetsHelper $Assets |
37
|
|
|
* @property \Core\View\Helper\DocumentHelper $Document |
38
|
|
|
*/ |
39
|
|
|
class AppView extends View |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
const VIEW_FORM = 'form'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Controller form actions. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $_formActions = ['edit', 'add']; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get view file path. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @return null|string |
56
|
|
|
*/ |
57
|
|
|
public function getViewFile($name) |
58
|
|
|
{ |
59
|
|
|
list($plugin, $name) = $this->pluginSplit($name); |
60
|
|
|
$return = null; |
61
|
|
|
foreach ($this->_paths($plugin) as $path) { |
62
|
|
|
$viewFile = FS::clean($path . $name . $this->_ext); |
63
|
|
|
if (FS::isFile($viewFile)) { |
64
|
|
|
$return = $this->_checkFilePath($viewFile, $path); |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initialization hook method. |
74
|
|
|
* |
75
|
|
|
* Properties like $helpers etc. cannot be initialized statically in your custom |
76
|
|
|
* view class as they are overwritten by values from controller in constructor. |
77
|
|
|
* So this method allows you to manipulate them as required after view instance |
78
|
|
|
* is constructed. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
* |
82
|
|
|
* @throws \JBZoo\Utils\Exception |
83
|
|
|
*/ |
84
|
|
|
public function initialize() |
85
|
|
|
{ |
86
|
|
|
parent::initialize(); |
87
|
|
|
|
88
|
|
|
$pluginEvent = Plugin::getData('Core', 'View.initialize'); |
89
|
|
|
if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.initialize')) { |
90
|
|
|
call_user_func_array($pluginEvent->find(0), [$this]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Render layout partial. |
96
|
|
|
* |
97
|
|
|
* @param string $name |
98
|
|
|
* @param array $data |
99
|
|
|
* @return null|string |
100
|
|
|
*/ |
101
|
|
|
public function partial($name, array $data = []) |
102
|
|
|
{ |
103
|
|
|
$file = $this->_getLayoutPartialPath($name); |
104
|
|
|
|
105
|
|
|
if (FS::isFile($file)) { |
|
|
|
|
106
|
|
|
return $this->_render($file, $data); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Renders view for given template file and layout. |
114
|
|
|
* |
115
|
|
|
* @param null|string $view |
116
|
|
|
* @param null|string $layout |
117
|
|
|
* @return null|string |
118
|
|
|
*/ |
119
|
|
|
public function render($view = null, $layout = null) |
120
|
|
|
{ |
121
|
|
|
$view = $this->_getFormView($view); |
122
|
|
|
return parent::render($view, $layout); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Find form view by request. |
127
|
|
|
* |
128
|
|
|
* @return string|null |
129
|
|
|
*/ |
130
|
|
|
protected function _findViewByRequest() |
131
|
|
|
{ |
132
|
|
|
$paths = App::path('Template', $this->plugin); |
133
|
|
|
|
134
|
|
|
$action = (string) $this->request->getParam('action'); |
135
|
|
|
$controller = $this->request->getParam('controller'); |
136
|
|
|
$viewFile = $action . $this->_ext; |
137
|
|
|
$viewSubPath = $this->_getSubPaths($controller); |
138
|
|
|
|
139
|
|
|
foreach ($paths as $path) { |
140
|
|
|
foreach ($viewSubPath as $subPath) { |
141
|
|
|
$full = $path . $subPath . DS . $viewFile; |
142
|
|
|
if (FS::isFile($full)) { |
143
|
|
|
return $action; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$formView = $path . $subPath . DS . self::VIEW_FORM . $this->_ext; |
147
|
|
|
if (FS::isFile($formView)) { |
148
|
|
|
return self::VIEW_FORM; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get current form view. |
158
|
|
|
* |
159
|
|
|
* @param null|string $view |
160
|
|
|
* @return null |
161
|
|
|
*/ |
162
|
|
|
protected function _getFormView($view = null) |
163
|
|
|
{ |
164
|
|
|
if (empty($view) && Arr::in($this->request->getParam('action'), $this->_formActions)) { |
165
|
|
|
$view = $this->_findViewByRequest(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $view; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Finds an partial filename, returns false on failure. |
173
|
|
|
* |
174
|
|
|
* @param string $name |
175
|
|
|
* @return bool|string |
176
|
|
|
*/ |
177
|
|
|
protected function _getLayoutPartialPath($name) |
178
|
|
|
{ |
179
|
|
|
list($plugin, $name) = $this->pluginSplit($name); |
180
|
|
|
|
181
|
|
|
$paths = $this->_paths($plugin); |
182
|
|
|
$layoutPaths = $this->_getSubPaths('Layout' . DS . 'Partial'); |
183
|
|
|
|
184
|
|
|
foreach ($paths as $path) { |
185
|
|
|
foreach ($layoutPaths as $layoutPath) { |
186
|
|
|
$partial = $path . $layoutPath . DS . $name . $this->_ext; |
187
|
|
|
if (FS::isFile($partial)) { |
188
|
|
|
return $partial; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.