1
|
|
|
<?php |
2
|
|
|
namespace samsoncms\cms; |
3
|
|
|
|
4
|
|
|
use samson\core\CompressableExternalModule; |
5
|
|
|
use samsonphp\event\Event; |
6
|
|
|
use samsonphp\router\Module; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* SamsonCMS external compressible application for integrating |
10
|
|
|
* @author Vitaly Iegorov <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Application extends CompressableExternalModule |
13
|
|
|
{ |
14
|
|
|
const EVENT_IS_CMS = 'samsonsms.is.cms'; |
15
|
|
|
|
16
|
|
|
/** @var string Module identifier */ |
17
|
|
|
public $id = 'cms'; |
18
|
|
|
|
19
|
|
|
/** @var bool Flag that currently we are woring in SamsonCMS */ |
20
|
|
|
protected $isCMS = false; |
21
|
|
|
|
22
|
|
|
public function init(array $params = array()) |
23
|
|
|
{ |
24
|
|
|
//trace('cmsInit'); |
25
|
|
|
// Old applications main page rendering |
26
|
|
|
Event::subscribe('template.main.rendered', array($this, 'oldMainRenderer')); |
27
|
|
|
// Old applications menu rendering |
28
|
|
|
Event::subscribe('template.menu.rendered', array($this, 'oldMenuRenderer')); |
29
|
|
|
|
30
|
|
|
Event::subscribe('samson.url.build', array($this, 'buildUrl')); |
31
|
|
|
|
32
|
|
|
Event::subscribe('samson.url.args.created', array($this, 'parseUrl')); |
33
|
|
|
|
34
|
|
|
Event::subscribe(Module::EVENT_ROUTE_FOUND, array($this, 'activeModuleHandler')); |
35
|
|
|
|
36
|
|
|
Event::subscribe('samsonphp.router.create.module.routes', array($this, 'updateCMSPrefix')); |
37
|
|
|
|
38
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
|
|
|
|
39
|
|
|
$moduleList = $this->system->module_stack; |
|
|
|
|
40
|
|
|
foreach ($this->system->module_stack as $id => $module) { |
|
|
|
|
41
|
|
|
if ( ! (isset($module->composerParameters['composerName']) && |
42
|
|
|
isset($this->composerParameters['required']) && |
|
|
|
|
43
|
|
|
in_array($module->composerParameters['composerName'], $this->composerParameters['required'])) |
|
|
|
|
44
|
|
|
) { |
45
|
|
|
if ($id != 'core') { |
46
|
|
|
unset($moduleList[$id]); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Generate resources for new module |
52
|
|
|
$this->system->module('resourcer')->generateResources($moduleList, $this->path() . 'app/view/index.php'); |
|
|
|
|
53
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
|
|
|
|
54
|
|
|
|
55
|
|
|
// Call parent initialization |
56
|
|
|
return parent::init($params); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function isCMS() |
60
|
|
|
{ |
61
|
|
|
return $this->isCMS; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function activeModuleHandler($module) |
65
|
|
|
{ |
66
|
|
|
// Define if routed module is related to SamsonCMS |
67
|
|
|
if($this->isCMS = $this->ifModuleRelated($module)){ |
68
|
|
|
// TODO: This should be removed - Reparse url |
69
|
|
|
url()->parse(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
// Switch template to SamsonCMS |
72
|
|
|
$this->system->template($this->path() . 'app/view/index.php', true); |
|
|
|
|
73
|
|
|
|
74
|
|
|
Event::fire(self::EVENT_IS_CMS, array(&$this)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Callback for adding SamsonCMS related modules prefix to routes. |
80
|
|
|
* |
81
|
|
|
* @param $module |
82
|
|
|
* @param $prefix |
83
|
|
|
*/ |
84
|
|
|
public function updateCMSPrefix($module, &$prefix) |
85
|
|
|
{ |
86
|
|
|
if (($module->id != $this->id) && $this->ifModuleRelated($module)) { |
87
|
|
|
$prefix = '/' . $this->id . $prefix; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function buildUrl(&$urlObj, &$httpHost, &$urlParams) |
92
|
|
|
{ |
93
|
|
|
if ($this->isCMS) { |
94
|
|
|
array_unshift($urlParams, $this->id); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function parseUrl(&$urlObj, &$urlArgs) |
99
|
|
|
{ |
100
|
|
|
if ($this->isCMS) { |
101
|
|
|
array_shift($urlArgs); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if passed module is related to SamsonCMS. |
107
|
|
|
* Also method stores data to flag variable. |
108
|
|
|
* |
109
|
|
|
* @param $module |
110
|
|
|
* |
111
|
|
|
* @return bool True if module related to SamsonCMS |
112
|
|
|
*/ |
113
|
|
|
public function ifModuleRelated($module) |
114
|
|
|
{ |
115
|
|
|
// Analyze if module class or one of its parents has samsoncms\ namespace pattern |
116
|
|
|
return sizeof(preg_grep('/samsoncms\\\\/i', array_merge(array(get_class($module)), class_parents($module)))); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function __base() |
120
|
|
|
{ |
121
|
|
|
$templateModule = $this->system->module('template'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// Switch system to SamsonCMS template module |
124
|
|
|
$this->system->active($templateModule); |
|
|
|
|
125
|
|
|
|
126
|
|
|
// Call template handler |
127
|
|
|
$templateModule->__handler(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function oldMainRenderer(&$html) |
131
|
|
|
{ |
132
|
|
|
// Render application main page block |
133
|
|
|
foreach (\samsoncms\Application::loaded() as $app) { |
|
|
|
|
134
|
|
|
// Show only visible apps |
135
|
|
|
if ($app->hide == false && $app->findView('sub_menu')) { |
136
|
|
|
$html .= $app->main(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @deprecated All application should draw menu block via events |
143
|
|
|
*/ |
144
|
|
|
public function oldMenuRenderer(&$html, &$subMenu) |
145
|
|
|
{ |
146
|
|
|
// Iterate loaded samson\cms\application |
147
|
|
|
foreach (\samsoncms\Application::loaded() as $app) { |
|
|
|
|
148
|
|
|
// Show only visible apps |
149
|
|
|
if ($app->hide == false) { |
150
|
|
|
// Render application menu item |
151
|
|
|
$html .= m('template') |
152
|
|
|
->view('menu/item') |
153
|
|
|
->active(url()->module == $app->id() ? 'active' : '') |
154
|
|
|
->app($app) |
155
|
|
|
->icon($app->icon) |
156
|
|
|
->name(isset($app->name{0}) ? $app->name : '') |
157
|
|
|
->output(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
$subMenu = ''; |
161
|
|
|
// Find current SamsonCMS application |
162
|
|
|
if (\samsoncms\Application::find(url()->module, $app/*@var $app App*/)) { |
163
|
|
|
// If module has sub_menu view - render it |
164
|
|
|
if ($app->findView('sub_menu')) { |
165
|
|
|
$subMenu .= $app->view('sub_menu')->output(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @deprecated |
172
|
|
|
* @return string Page title |
173
|
|
|
*/ |
174
|
|
|
public function oldGetTitle() |
175
|
|
|
{ |
176
|
|
|
$local = m('local'); |
177
|
|
|
$current = m(); |
178
|
|
|
|
179
|
|
|
return isset($current['title']) ? $current['title'] : |
180
|
|
|
(isset($local['title']) ? $local['title'] : ''); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.