1
|
|
|
<?php namespace core\src; |
2
|
|
|
|
3
|
|
|
use CI; |
4
|
|
|
use CMSFactory\Events; |
5
|
|
|
use core\models\Route; |
6
|
|
|
use core\src\Controller\CategoryController; |
7
|
|
|
use core\src\Controller\PageController; |
8
|
|
|
use core\src\Exception\PageNotFoundException; |
9
|
|
|
|
10
|
|
|
class FrontController |
11
|
|
|
{ |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var CI |
15
|
|
|
*/ |
16
|
|
|
private $ci; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var CoreModel |
20
|
|
|
*/ |
21
|
|
|
private $frontModel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Router |
25
|
|
|
*/ |
26
|
|
|
private $router; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* FrontController constructor. |
30
|
|
|
* @param CI $ci |
31
|
|
|
* @param CoreModel $frontModel |
32
|
|
|
* @param Router $router |
33
|
|
|
*/ |
34
|
|
|
public function __construct(CI $ci, CoreModel $frontModel, Router $router) { |
35
|
|
|
$this->ci = $ci; |
36
|
|
|
$this->frontModel = $frontModel; |
37
|
|
|
$this->router = $router; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $url |
42
|
|
|
* @throws PageNotFoundException |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function display($url) { |
45
|
|
|
|
46
|
|
|
$route = $this->router->findRoute($url); |
47
|
|
|
|
48
|
|
|
if (!$route) { |
49
|
|
|
throw new PageNotFoundException(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->ci->core->core_data['data_type'] = $route->getType(); |
53
|
|
|
$id = $route->getEntityId(); |
54
|
|
|
$this->ci->core->core_data['id'] = $id; |
55
|
|
|
|
56
|
|
|
CoreFactory::getConfiguration()->setCurrentEntityType($route->getType()); |
57
|
|
|
CoreFactory::getConfiguration()->setCurrentEntityId($id); |
58
|
|
|
|
59
|
|
|
switch ($route->getType()) { |
60
|
|
|
case Route::TYPE_MAIN: |
61
|
|
|
$this->main(); |
62
|
|
|
break; |
|
|
|
|
63
|
|
|
case Route::TYPE_PRODUCT: |
64
|
|
|
$this->product($id); |
65
|
|
|
break; |
|
|
|
|
66
|
|
|
case Route::TYPE_SHOP_CATEGORY: |
67
|
|
|
$this->shopCategory($id); |
68
|
|
|
break; |
|
|
|
|
69
|
|
|
case Route::TYPE_CATEGORY: |
70
|
|
|
$this->category($id); |
71
|
|
|
break; |
|
|
|
|
72
|
|
|
case Route::TYPE_PAGE: |
73
|
|
|
$this->page($id); |
74
|
|
|
break; |
|
|
|
|
75
|
|
|
case Route::TYPE_MODULE: |
76
|
|
|
$this->module($route->getUrl()); |
77
|
|
|
break; |
|
|
|
|
78
|
|
|
default: |
79
|
|
|
throw new PageNotFoundException(); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function main() { |
85
|
|
|
|
86
|
|
View Code Duplication |
if ($this->ci->input->get() || strstr($this->ci->input->server('REQUEST_URI'), '?')) { |
87
|
|
|
$this->ci->template->registerCanonical(site_url()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
Events::create()->registerEvent(NULL, 'Core:_mainPage')->runFactory(); |
91
|
|
|
|
92
|
|
|
$settings = CoreFactory::getConfiguration()->getSettings(); |
93
|
|
|
|
94
|
|
|
switch ($settings['main_type']) { |
95
|
|
|
case 'page': |
96
|
|
|
Events::create()->registerEvent(NULL, 'Core:_mainPage')->runFactory(); |
97
|
|
|
$this->page($settings['main_page_id']); |
98
|
|
|
break; |
|
|
|
|
99
|
|
|
case 'category': |
100
|
|
|
$this->category($settings['main_page_cat']); |
101
|
|
|
break; |
|
|
|
|
102
|
|
|
case 'module': |
103
|
|
|
$this->module($settings['main_page_module']); |
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param int $id |
110
|
|
|
*/ |
111
|
|
|
private function page($id) { |
112
|
|
|
|
113
|
|
|
(new PageController($this->ci, $this->frontModel))->index($id); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $id |
118
|
|
|
*/ |
119
|
|
|
private function category($id) { |
120
|
|
|
(new CategoryController($this->ci, $this->frontModel))->index($id); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param int $id |
125
|
|
|
*/ |
126
|
|
|
private function shopCategory($id) { |
127
|
|
|
$this->module('shop/category/index/' . $id); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param int $id |
132
|
|
|
*/ |
133
|
|
|
private function product($id) { |
134
|
|
|
|
135
|
|
View Code Duplication |
if ($this->ci->input->get() || strstr($this->ci->input->server('REQUEST_URI'), '?')) { |
136
|
|
|
$this->ci->template->registerCanonical(site_url($this->ci->uri->uri_string())); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->module('shop/product/index/' . $id); |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $url |
145
|
|
|
* @throws PageNotFoundException |
|
|
|
|
146
|
|
|
*/ |
147
|
|
|
private function module($url) { |
148
|
|
|
|
149
|
|
|
$moduleSegments = explode('/', $url); |
150
|
|
|
|
151
|
|
|
$moduleName = array_shift($moduleSegments); |
152
|
|
|
$moduleMethod = array_shift($moduleSegments); |
153
|
|
|
if ($moduleMethod == FALSE) { |
154
|
|
|
$moduleMethod = 'index'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$file = getModulePath($moduleName) . $moduleMethod . EXT; |
158
|
|
|
$this->core_data['module'] = $moduleName; |
|
|
|
|
159
|
|
|
|
160
|
|
|
if (file_exists($file)) { |
161
|
|
|
$moduleAction = array_shift($moduleSegments) ?: 'index'; |
162
|
|
|
$moduleFullPath = $moduleName . '/' . $moduleMethod; |
163
|
|
|
$module = $this->ci->load->module($moduleName . '/' . $moduleMethod); |
164
|
|
|
|
165
|
|
|
} else { |
166
|
|
|
$moduleAction = $moduleMethod; |
167
|
|
|
$moduleFullPath = $moduleName . '/' . $moduleName; |
168
|
|
|
$module = $this->ci->load->module($moduleName); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$args = $moduleSegments; |
172
|
|
|
if (method_exists($module, $moduleAction)) { |
173
|
|
|
echo \modules::run($moduleFullPath . '/' . $moduleAction, $args); |
174
|
|
|
} else { |
175
|
|
|
throw new PageNotFoundException(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |