1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* View module |
5
|
|
|
* |
6
|
|
|
* Rendering pages, contents and widgets |
7
|
|
|
* |
8
|
|
|
* @author Alexey Krupskiy <[email protected]> |
9
|
|
|
* @link http://inji.ru/ |
10
|
|
|
* @copyright 2015 Alexey Krupskiy |
11
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
12
|
|
|
*/ |
13
|
|
|
class View extends \Module { |
14
|
|
|
|
15
|
|
|
public $title = 'No title'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var View\Template |
19
|
|
|
*/ |
20
|
|
|
public $template = null; |
21
|
|
|
public $libAssets = ['css' => [], 'js' => []]; |
22
|
|
|
public $dynAssets = ['css' => [], 'js' => []]; |
23
|
|
|
public $dynMetas = []; |
24
|
|
|
public $viewedContent = ''; |
25
|
|
|
public $contentData = []; |
26
|
|
|
public $templatesPath = ''; |
27
|
|
|
public $loadedCss = []; |
28
|
|
|
public $loadedJs = []; |
29
|
|
|
|
30
|
|
|
public function init() { |
31
|
|
|
if (!empty($this->app->config['site']['name'])) { |
32
|
|
|
$this->title = $this->app->config['site']['name']; |
33
|
|
|
} |
34
|
|
|
$this->resolveTemplate(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function resolveTemplate() { |
38
|
|
|
$templateName = 'default'; |
39
|
|
|
if (!empty($this->config[$this->app->type]['current'])) { |
40
|
|
|
$templateName = $this->config[$this->app->type]['current']; |
41
|
|
|
if (!empty($this->config[$this->app->type]['installed'][$templateName]['location'])) { |
42
|
|
|
$this->templatesPath = App::$primary->path . "/templates"; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
if (!$this->templatesPath) { |
46
|
|
|
$this->templatesPath = $this->app->path . "/templates"; |
47
|
|
|
} |
48
|
|
|
$this->template = \View\Template::get($templateName, $this->app, $this->templatesPath); |
49
|
|
|
if (!$this->template) { |
50
|
|
|
$this->template = new \View\Template([ |
51
|
|
|
'name' => 'default', |
52
|
|
|
'path' => $this->templatesPath . '/default', |
53
|
|
|
'app' => $this->app |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function page($params = []) { |
59
|
|
|
$this->paramsParse($params); |
60
|
|
|
if (file_exists($this->template->pagePath)) { |
61
|
|
|
$source = file_get_contents($this->template->pagePath); |
62
|
|
|
if (strpos($source, 'BODYEND') === false) { |
63
|
|
|
$source = str_replace('</body>', '{BODYEND}</body>', $source); |
64
|
|
|
} |
65
|
|
|
$this->parseSource($source); |
66
|
|
|
} else { |
67
|
|
|
$this->content(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function paramsParse($params) { |
72
|
|
|
// set template |
73
|
|
|
if (!empty($params['template']) && $params['template'] != 'current') { |
74
|
|
|
$this->template = \View\Template::get($params['template']); |
75
|
|
|
} |
76
|
|
|
//set page |
77
|
|
|
if (!empty($params['page']) && $params['page'] != 'current') { |
78
|
|
|
$this->template->setPage($params['page']); |
79
|
|
|
} |
80
|
|
|
//set module |
81
|
|
|
if (!empty($params['module'])) { |
82
|
|
|
$this->template->setModule($params['module']); |
83
|
|
|
} |
84
|
|
|
//set content |
85
|
|
|
if (!empty($params['content'])) { |
86
|
|
|
$this->template->setContent($params['content']); |
87
|
|
|
} elseif (!$this->template->contentPath) { |
88
|
|
|
$this->template->setContent(); |
89
|
|
|
} |
90
|
|
|
//set data |
91
|
|
|
if (!empty($params['data'])) { |
92
|
|
|
$this->contentData = array_merge($this->contentData, $params['data']); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function content($params = []) { |
97
|
|
|
|
98
|
|
|
$this->paramsParse($params); |
99
|
|
|
|
100
|
|
|
if (empty($this->template->config['noSysMsgAutoShow'])) { |
101
|
|
|
Msg::show(); |
102
|
|
|
} |
103
|
|
|
if (!file_exists($this->template->contentPath)) { |
104
|
|
|
echo 'Content not found'; |
105
|
|
|
} else { |
106
|
|
|
extract($this->contentData); |
107
|
|
|
include $this->template->contentPath; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function parentContent($contentName = '') { |
112
|
|
|
if (!$contentName) { |
113
|
|
|
$contentName = $this->template->content; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$paths = $this->template->getContentPaths($contentName); |
117
|
|
|
|
118
|
|
|
$data = []; |
119
|
|
|
$exist = false; |
120
|
|
|
foreach ($paths as $type => $path) { |
121
|
|
|
if (substr($path, 0, strrpos($path, '/')) == substr($this->template->contentPath, 0, strrpos($this->template->contentPath, '/'))) { |
122
|
|
|
$exist = true; |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
if (file_exists($path) && $exist) { |
126
|
|
|
$data['contentPath'] = $path; |
127
|
|
|
$data['content'] = $contentName; |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
if (!$data) { |
132
|
|
|
echo 'Content not found'; |
133
|
|
|
} else { |
134
|
|
|
extract($this->contentData); |
135
|
|
|
include $data['contentPath']; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function parseRaw($source) { |
140
|
|
|
if (!$source) { |
141
|
|
|
return []; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
preg_match_all("|{([^}]+)}|", $source, $result); |
145
|
|
|
return $result[1]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function parseSource($source) { |
149
|
|
|
$tags = $this->parseRaw($source); |
150
|
|
|
foreach ($tags as $rawTag) { |
151
|
|
|
$tag = explode(':', $rawTag); |
152
|
|
|
switch ($tag[0]) { |
153
|
|
|
case 'CONTENT': |
154
|
|
|
$source = $this->cutTag($source, $rawTag); |
155
|
|
|
$this->content(); |
156
|
|
|
break; |
157
|
|
|
case 'TITLE': |
158
|
|
|
$source = $this->cutTag($source, $rawTag); |
159
|
|
|
echo $this->title; |
160
|
|
|
break; |
161
|
|
|
case 'WIDGET': |
162
|
|
|
$source = $this->cutTag($source, $rawTag); |
163
|
|
|
$params = array_slice($tag, 2); |
164
|
|
|
$this->widget($tag[1], ['params' => $params], ':' . implode(':', $params)); |
165
|
|
|
break; |
166
|
|
|
case 'HEAD': |
167
|
|
|
$source = $this->cutTag($source, $rawTag); |
168
|
|
|
$this->head(); |
169
|
|
|
break; |
170
|
|
|
case 'PAGE': |
171
|
|
|
$source = $this->cutTag($source, $rawTag); |
172
|
|
|
$this->page(['page' => $tag[1]]); |
173
|
|
|
break; |
174
|
|
|
case 'BODYEND': |
175
|
|
|
$source = $this->cutTag($source, $rawTag); |
176
|
|
|
$this->bodyEnd(); |
177
|
|
|
break; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
echo $source; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function cutTag($source, $rawTag) { |
184
|
|
|
$pos = strpos($source, $rawTag) - 1; |
185
|
|
|
echo substr($source, 0, $pos); |
186
|
|
|
return substr($source, ($pos + strlen($rawTag) + 2)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function getHref($type, $params) { |
190
|
|
|
$href = ''; |
191
|
|
|
if (is_string($params)) { |
192
|
|
|
$href = $params; |
193
|
|
|
} elseif (empty($params['template']) && !empty($params['file'])) { |
194
|
|
|
$href = ($this->app->type != 'app' ? '/' . $this->app->name : '') . $params['file']; |
195
|
|
|
} elseif (!empty($params['template']) && !empty($params['file'])) { |
196
|
|
|
$href = $this->app->templatesPath . "/{$this->template->name}/{$type}/{$params['file']}"; |
197
|
|
|
} |
198
|
|
|
return $href; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function checkNeedLibs() { |
202
|
|
|
if (!empty($this->template->config['libs'])) { |
203
|
|
|
foreach ($this->template->config['libs'] as $libName => $libOptions) { |
204
|
|
|
if (!is_array($libOptions)) { |
205
|
|
|
$libName = $libOptions; |
206
|
|
|
$libOptions = []; |
207
|
|
|
} |
208
|
|
|
$this->app->libs->loadLib($libName, $libOptions); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
View Code Duplication |
foreach ($this->dynAssets['js'] as $asset) { |
|
|
|
|
212
|
|
|
if (is_array($asset) && !empty($asset['libs'])) { |
213
|
|
|
foreach ($asset['libs'] as $libName) { |
214
|
|
|
$this->app->libs->loadLib($libName); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
View Code Duplication |
foreach ($this->libAssets['js'] as $asset) { |
|
|
|
|
219
|
|
|
if (is_array($asset) && !empty($asset['libs'])) { |
220
|
|
|
foreach ($asset['libs'] as $libName) { |
221
|
|
|
$this->app->libs->loadLib($libName); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function head() { |
228
|
|
|
|
229
|
|
|
echo "<title>{$this->title}</title>\n"; |
230
|
|
|
if (!empty(\App::$primary->config['site']['favicon']) && file_exists(\App::$primary->path . '/' . \App::$primary->config['site']['favicon'])) { |
231
|
|
|
echo " <link rel='shortcut icon' href='" . \App::$primary->config['site']['favicon'] . "' />"; |
232
|
|
|
} elseif (!empty($this->template->config['favicon']) && file_exists($this->template->path . "/{$this->template->config['favicon']}")) { |
233
|
|
|
echo " <link rel='shortcut icon' href='/templates/{$this->template->name}/{$this->template->config['favicon']}' />"; |
234
|
|
|
} elseif (!empty($this->template->config['favicon']) && file_exists($this->app->path . "/static/images/{$this->template->config['favicon']}")) { |
235
|
|
|
echo " <link rel='shortcut icon' href='/static/images/{$this->template->config['favicon']}' />"; |
236
|
|
|
} elseif (file_exists($this->app->path . '/static/images/favicon.ico')) { |
237
|
|
|
echo " <link rel='shortcut icon' href='/static/images/favicon.ico' />"; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
foreach ($this->getMetaTags() as $meta) { |
241
|
|
|
echo "\n " . Html::el('meta', $meta, '', null); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (!empty(Inji::$config['assets']['js'])) { |
245
|
|
|
foreach (Inji::$config['assets']['js'] as $js) { |
246
|
|
|
$this->customAsset('js', $js); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->checkNeedLibs(); |
251
|
|
|
$this->parseCss(); |
252
|
|
|
echo "\n <script src='" . Statics::file(($this->app->type != 'app' ? '/' . $this->app->name : '') . "/static/system/js/Inji.js") . "'></script>"; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function parseCss() { |
256
|
|
|
$css = $this->getCss(); |
257
|
|
|
$urls = []; |
258
|
|
|
$timeStr = ''; |
259
|
|
|
$cssAll = ''; |
260
|
|
|
$exclude = ['^http:', '^https:', '^//']; |
261
|
|
|
foreach ($css as $href) { |
262
|
|
|
if (!empty($this->loadedCss[$href])) { |
263
|
|
|
continue; |
264
|
|
|
} |
265
|
|
|
foreach ($exclude as $item) { |
266
|
|
|
if (preg_match("!{$item}!", $href)) { |
267
|
|
|
echo "\n <link href='{$href}' rel='stylesheet' type='text/css' />"; |
268
|
|
|
continue; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
$path = $this->app->staticLoader->parsePath($href); |
272
|
|
|
if (file_exists($path)) { |
273
|
|
|
$this->loadedCss[$href] = $href; |
274
|
|
|
$urls[$href] = $path; |
275
|
|
|
$timeStr .= filemtime($path); |
276
|
|
|
} else { |
277
|
|
|
echo "\n <link href='{$href}' rel='stylesheet' type='text/css' />"; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
$timeMd5 = md5($timeStr); |
281
|
|
|
$cacheDir = Cache::getDir('static'); |
282
|
|
|
if (!file_exists($cacheDir . '/all' . $timeMd5 . '.css')) { |
283
|
|
|
foreach ($urls as $primaryUrl => $url) { |
284
|
|
|
$source = file_get_contents($url); |
285
|
|
|
$rootPath = substr($primaryUrl, 0, strrpos($primaryUrl, '/')); |
286
|
|
|
$levelUpPath = substr($rootPath, 0, strrpos($rootPath, '/')); |
287
|
|
|
$source = preg_replace('!url\((\'?"?)[\.]{2}!isU', 'url($1' . $levelUpPath, $source); |
288
|
|
|
$source = preg_replace('!url\((\'?"?)[\.]{1}!isU', 'url($1' . $rootPath, $source); |
289
|
|
|
$source = preg_replace('#url\(([\'"]){1}(?!http|https|/|data\:)([^/])#isU', 'url($1' . $rootPath . '/$2', $source); |
290
|
|
|
$source = preg_replace('#url\((?!http|https|/|data\:|\'|")([^/])#isU', 'url(' . $rootPath . '/$1$2', $source); |
291
|
|
|
$cssAll .= $source . "\n"; |
292
|
|
|
} |
293
|
|
|
file_put_contents($cacheDir . '/all' . $timeMd5 . '.css', $cssAll); |
294
|
|
|
} |
295
|
|
|
echo "\n <link href='/{$cacheDir}/all{$timeMd5}.css' rel='stylesheet' type='text/css' />"; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
View Code Duplication |
public function getCss() { |
299
|
|
|
$css = []; |
300
|
|
|
if (!empty($this->libAssets['css'])) { |
301
|
|
|
$this->ResolveCssHref($this->libAssets['css'], 'libs', $css); |
302
|
|
|
} |
303
|
|
|
if (!empty($this->template->config['css'])) { |
304
|
|
|
$this->ResolveCssHref($this->template->config['css'], 'template', $css); |
305
|
|
|
} |
306
|
|
|
if (!empty($this->dynAssets['css'])) { |
307
|
|
|
$this->ResolveCssHref($this->dynAssets['css'], 'custom', $css); |
308
|
|
|
} |
309
|
|
|
return $css; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function ResolveCssHref($cssArray, $type = 'custom', &$hrefs) { |
313
|
|
|
switch ($type) { |
314
|
|
|
case 'libs': |
315
|
|
|
foreach ($cssArray as $css) { |
316
|
|
|
if (is_array($css)) { |
317
|
|
|
$this->ResolveCssHref($css, $type, $hrefs); |
318
|
|
|
continue; |
319
|
|
|
} |
320
|
|
|
$hrefs[$css] = $css; |
321
|
|
|
} |
322
|
|
|
break; |
323
|
|
View Code Duplication |
case 'template': |
|
|
|
|
324
|
|
|
foreach ($cssArray as $css) { |
325
|
|
|
if (is_array($css)) { |
326
|
|
|
$this->ResolveCssHref($css, $type, $hrefs); |
327
|
|
|
continue; |
328
|
|
|
} |
329
|
|
|
if (strpos($css, '://') !== false) { |
330
|
|
|
$href = $css; |
331
|
|
|
} else { |
332
|
|
|
$href = $this->app->templatesPath . "/{$this->template->name}/css/{$css}"; |
333
|
|
|
} |
334
|
|
|
$hrefs[$href] = $href; |
335
|
|
|
} |
336
|
|
|
break; |
337
|
|
View Code Duplication |
case 'custom': |
|
|
|
|
338
|
|
|
foreach ($cssArray as $css) { |
339
|
|
|
if (is_array($css)) { |
340
|
|
|
$this->ResolveCssHref($css, $type, $hrefs); |
341
|
|
|
continue; |
342
|
|
|
} |
343
|
|
|
if (strpos($css, '//') !== false) { |
344
|
|
|
$href = $css; |
345
|
|
|
} else { |
346
|
|
|
$href = ($this->app->type != 'app' ? '/' . $this->app->name : '') . $css; |
347
|
|
|
} |
348
|
|
|
$hrefs[$href] = $href; |
349
|
|
|
} |
350
|
|
|
break; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public function getMetaTags() { |
355
|
|
|
$metas = []; |
356
|
|
|
|
357
|
|
View Code Duplication |
if (!empty($this->app->config['site']['keywords'])) { |
|
|
|
|
358
|
|
|
$metas['metaName:keywords'] = ['name' => 'keywords', 'content' => $this->app->config['site']['keywords']]; |
359
|
|
|
} |
360
|
|
View Code Duplication |
if (!empty($this->app->config['site']['description'])) { |
|
|
|
|
361
|
|
|
$metas['metaName:description'] = ['name' => 'description', 'content' => $this->app->config['site']['description']]; |
362
|
|
|
} |
363
|
|
|
if (!empty($this->app->config['site']['metatags'])) { |
364
|
|
|
foreach ($this->app->config['site']['metatags'] as $meta) { |
365
|
|
|
if (!empty($meta['name'])) { |
366
|
|
|
$metas['metaName:' . $meta['name']] = $meta; |
367
|
|
|
} elseif (!empty($meta['property'])) { |
368
|
|
|
$metas['metaProperty:' . $meta['property']] = $meta; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
if ($this->dynMetas) { |
373
|
|
|
$metas = array_merge($metas, $this->dynMetas); |
374
|
|
|
} |
375
|
|
|
return $metas; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function addMetaTag($meta) { |
379
|
|
|
if (!empty($meta['name'])) { |
380
|
|
|
$this->dynMetas['metaName:' . $meta['name']] = $meta; |
381
|
|
|
} elseif (!empty($meta['property'])) { |
382
|
|
|
$this->dynMetas['metaProperty:' . $meta['property']] = $meta; |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function bodyEnd() { |
387
|
|
|
$this->checkNeedLibs(); |
388
|
|
|
$this->parseCss(); |
389
|
|
|
$scripts = $this->getScripts(); |
390
|
|
|
$onLoadModules = []; |
391
|
|
|
$scriptAll = ''; |
392
|
|
|
$urls = []; |
393
|
|
|
$nativeUrl = []; |
394
|
|
|
$timeStr = ''; |
395
|
|
|
$noParsedScripts = []; |
396
|
|
|
foreach ($scripts as $script) { |
397
|
|
|
if (is_string($script)) { |
398
|
|
|
if (!empty($urls[$script])) { |
399
|
|
|
continue; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
$path = $this->app->staticLoader->parsePath($script); |
403
|
|
|
if (file_exists($path)) { |
404
|
|
|
$nativeUrl[$script] = $script; |
405
|
|
|
$urls[$script] = $path; |
406
|
|
|
$timeStr .= filemtime($path); |
407
|
|
|
} else { |
408
|
|
|
$noParsedScripts[$script] = $script; |
409
|
|
|
} |
410
|
|
|
} elseif (!empty($script['file'])) { |
411
|
|
|
if (!empty($urls[$script['file']])) { |
412
|
|
|
continue; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
$path = $this->app->staticLoader->parsePath($script['file']); |
416
|
|
|
if (file_exists($path)) { |
417
|
|
|
$nativeUrl[$script['file']] = $script['file']; |
418
|
|
|
$urls[$script['file']] = $path; |
419
|
|
|
if (!empty($script['name'])) { |
420
|
|
|
$onLoadModules[$script['name']] = $script['name']; |
421
|
|
|
} |
422
|
|
|
$timeStr .= filemtime($path); |
423
|
|
|
} else { |
424
|
|
|
$noParsedScripts[$script] = $script; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$timeMd5 = md5($timeStr); |
430
|
|
|
$cacheDir = Cache::getDir('static'); |
431
|
|
|
if (!file_exists($cacheDir . '/all' . $timeMd5 . '.js')) { |
432
|
|
|
foreach ($urls as $url) { |
433
|
|
|
$scriptAll .= ";\n" . file_get_contents($url); |
434
|
|
|
} |
435
|
|
|
file_put_contents($cacheDir . '/all' . $timeMd5 . '.js', $scriptAll); |
436
|
|
|
} |
437
|
|
|
$options = [ |
438
|
|
|
'scripts' => array_values($noParsedScripts), |
439
|
|
|
'compresedScripts' => $nativeUrl, |
440
|
|
|
'styles' => [], |
441
|
|
|
'appRoot' => $this->app->type == 'app' ? '/' : '/' . $this->app->name . '/', |
442
|
|
|
'onLoadModules' => $onLoadModules |
443
|
|
|
]; |
444
|
|
|
$options['scripts'][] = '/' . $cacheDir . '/all' . $timeMd5 . '.js'; |
445
|
|
|
$this->widget('View\bodyEnd', compact('options')); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
View Code Duplication |
public function getScripts() { |
449
|
|
|
$scripts = []; |
450
|
|
|
if (!empty($this->libAssets['js'])) { |
451
|
|
|
$this->genScriptArray($this->libAssets['js'], 'libs', $scripts); |
452
|
|
|
} |
453
|
|
|
if (!empty($this->dynAssets['js'])) { |
454
|
|
|
$this->genScriptArray($this->dynAssets['js'], 'custom', $scripts); |
455
|
|
|
} |
456
|
|
|
if (!empty($this->template->config['js'])) { |
457
|
|
|
$this->genScriptArray($this->template->config['js'], 'template', $scripts); |
458
|
|
|
} |
459
|
|
|
return $scripts; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
public function genScriptArray($jsArray, $type = 'custom', &$resultArray) { |
463
|
|
|
switch ($type) { |
464
|
|
|
case 'libs': |
465
|
|
|
foreach ($jsArray as $js) { |
466
|
|
|
if (is_array($js)) { |
467
|
|
|
$this->genScriptArray($js, $type, $resultArray); |
468
|
|
|
continue; |
469
|
|
|
} |
470
|
|
|
if (strpos($js, '//') !== false) { |
471
|
|
|
$href = $js; |
472
|
|
|
} else { |
473
|
|
|
$href = $this->getHref('js', $js); |
474
|
|
|
} |
475
|
|
|
if (!$href) { |
476
|
|
|
continue; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
$resultArray[] = $href; |
480
|
|
|
} |
481
|
|
|
break; |
482
|
|
View Code Duplication |
case 'template': |
|
|
|
|
483
|
|
|
foreach ($jsArray as $js) { |
484
|
|
|
if (is_array($js)) { |
485
|
|
|
$this->genScriptArray($js, $type, $resultArray); |
486
|
|
|
continue; |
487
|
|
|
} |
488
|
|
|
if (strpos($js, '//') !== false) { |
489
|
|
|
$href = $js; |
490
|
|
|
} else { |
491
|
|
|
$href = $this->app->templatesPath . "/{$this->template->name}/js/{$js}"; |
492
|
|
|
} |
493
|
|
|
$resultArray[] = $href; |
494
|
|
|
} |
495
|
|
|
break; |
496
|
|
|
case 'custom': |
497
|
|
|
foreach ($jsArray as $js) { |
498
|
|
|
if (is_array($js)) { |
499
|
|
|
if (!empty($js[0]) && is_array($js[0])) { |
500
|
|
|
$this->genScriptArray($js, $type, $resultArray); |
501
|
|
|
continue; |
502
|
|
|
} |
503
|
|
|
$asset = $js; |
504
|
|
|
} else { |
505
|
|
|
$asset = []; |
506
|
|
|
} |
507
|
|
|
$asset['file'] = $this->getHref('js', $js); |
508
|
|
|
if (!$asset['file']) { |
509
|
|
|
continue; |
510
|
|
|
} |
511
|
|
|
$resultArray[] = $asset; |
512
|
|
|
} |
513
|
|
|
break; |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
public function customAsset($type, $asset, $lib = false) { |
518
|
|
|
if (!$lib) { |
519
|
|
|
$this->dynAssets[$type][] = $asset; |
520
|
|
|
} else { |
521
|
|
|
if (empty($this->libAssets[$type][$lib]) || !in_array($asset, $this->libAssets[$type][$lib])) { |
522
|
|
|
$this->libAssets[$type][$lib][] = $asset; |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
public function setTitle($title, $add = true) { |
528
|
|
|
if ($add && !empty($this->app->config['site']['name'])) { |
529
|
|
|
if ($title) { |
530
|
|
|
$this->title = $title . ' - ' . $this->app->config['site']['name']; |
531
|
|
|
} else { |
532
|
|
|
$this->title = $this->app->config['site']['name']; |
533
|
|
|
} |
534
|
|
|
} else { |
535
|
|
|
$this->title = $title; |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
public function widget($_widgetName, $_params = [], $lineParams = null) { |
540
|
|
|
$_paths = $this->getWidgetPaths($_widgetName); |
541
|
|
|
$find = false; |
542
|
|
|
foreach ($_paths as $_path) { |
543
|
|
|
if (file_exists($_path)) { |
544
|
|
|
$find = true; |
545
|
|
|
break; |
546
|
|
|
} |
547
|
|
|
} |
548
|
|
|
if ($lineParams === null) { |
549
|
|
|
$lineParams = ''; |
550
|
|
|
if ($_params) { |
551
|
|
|
$paramArray = false; |
552
|
|
|
foreach ($_params as $param) { |
553
|
|
|
if (is_array($param) || is_object($param)) { |
554
|
|
|
$paramArray = true; |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
if (!$paramArray) { |
558
|
|
|
$lineParams = ':' . implode(':', $_params); |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
echo "<!--start:{WIDGET:{$_widgetName}{$lineParams}}-->\n"; |
563
|
|
|
if ($find) { |
564
|
|
|
if ($_params && is_array($_params)) { |
565
|
|
|
extract($_params); |
566
|
|
|
} |
567
|
|
|
include $_path; |
568
|
|
|
} |
569
|
|
|
echo "<!--end:{WIDGET:{$_widgetName}{$lineParams}}-->\n"; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
public function getWidgetPaths($widgetName) { |
573
|
|
|
$paths = []; |
574
|
|
|
if (strpos($widgetName, '\\')) { |
575
|
|
|
$widgetName = explode('\\', $widgetName); |
576
|
|
|
|
577
|
|
|
$paths['templatePath_widgetDir'] = $this->templatesPath . '/' . $this->template->name . '/widgets/' . $widgetName[0] . '/' . $widgetName[1] . '/' . $widgetName[1] . '.php'; |
578
|
|
|
$paths['templatePath'] = $this->templatesPath . '/' . $this->template->name . '/widgets/' . $widgetName[0] . '/' . $widgetName[1] . '.php'; |
579
|
|
|
App::$cur->$widgetName[0]; |
580
|
|
|
$modulePaths = Module::getModulePaths(ucfirst($widgetName[0])); |
581
|
|
|
foreach ($modulePaths as $pathName => $path) { |
582
|
|
|
$paths[$pathName . '_widgetDir'] = $path . '/widgets/' . $widgetName[1] . '/' . $widgetName[1] . '.php'; |
583
|
|
|
$paths[$pathName] = $path . '/widgets/' . $widgetName[1] . '.php'; |
584
|
|
|
} |
585
|
|
|
return $paths; |
586
|
|
|
} else { |
587
|
|
|
$paths['templatePath_widgetDir'] = $this->templatesPath . '/' . $this->template->name . '/widgets/' . $widgetName . '/' . $widgetName . '.php'; |
588
|
|
|
$paths['templatePath'] = $this->templatesPath . '/' . $this->template->name . '/widgets/' . $widgetName . '.php'; |
589
|
|
|
|
590
|
|
|
$paths['curAppPath_widgetDir'] = $this->app->path . '/widgets/' . $widgetName . '/' . $widgetName . '.php'; |
591
|
|
|
$paths['curAppPath'] = $this->app->path . '/widgets/' . $widgetName . '.php'; |
592
|
|
|
|
593
|
|
|
$paths['systemPath_widgetDir'] = INJI_SYSTEM_DIR . '/widgets/' . $widgetName . '/' . $widgetName . '.php'; |
594
|
|
|
$paths['systemPath'] = INJI_SYSTEM_DIR . '/widgets/' . $widgetName . '.php'; |
595
|
|
|
} |
596
|
|
|
return $paths; |
597
|
|
|
} |
598
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.