1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use CMSFactory\Events; |
4
|
|
|
use CMSFactory\Exception; |
|
|
|
|
5
|
|
|
use Propel\Runtime\Collection\ObjectCollection as PropelObjectCollection; |
6
|
|
|
|
7
|
|
|
(defined('BASEPATH')) OR exit('No direct script access allowed'); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Image CMS |
11
|
|
|
* |
12
|
|
|
* Класс редиректа удаленных товаров |
13
|
|
|
* @property Cms_base $cms_base |
14
|
|
|
*/ |
15
|
|
|
class Trash extends MY_Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string $is_create |
20
|
|
|
*/ |
21
|
|
|
private $trash_is_create_type; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Construct. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
|
28
|
|
|
parent::__construct(); |
29
|
|
|
$lang = new MY_Lang(); |
30
|
|
|
$lang->load('trash'); |
31
|
|
|
$this->load->module('core'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Index method. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function index() { |
40
|
|
|
|
41
|
|
|
$this->core->error_404(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* AdminAutoload method. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public static function adminAutoload() { |
50
|
|
|
|
51
|
|
|
parent::adminAutoload(); |
52
|
|
|
Events::create()->onShopProductDelete()->setListener('addProductWhenDelete'); |
53
|
|
|
Events::create()->onShopProductCreate()->setListener('delProductWhenCreate'); |
54
|
|
|
Events::create()->onShopProductAjaxChangeActive()->setListener('addProductWhenAjaxChangeActive'); |
55
|
|
|
Events::create()->onShopCategoryDelete()->setListener('addProductsWhenCatDelete'); |
56
|
|
|
Events::create()->onShopCategoryEdit()->setListener('updateCatUrl'); |
57
|
|
|
Events::create()->onShopProductUpdate()->setListener('addProductWhenAjaxChangeActive'); |
58
|
|
|
Events::create()->onShopProductCreate()->setListener('addProductWhenAjaxChangeActive'); |
59
|
|
|
Events::create()->on('ShopAdminProducts:fastProdCreate')->setListener('addProductWhenAjaxChangeActive'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Autoload method. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function autoload() { |
68
|
|
|
|
69
|
|
|
$url = ltrim(str_replace('/' . MY_Controller::getCurrentLocale() . '/', '', $this->input->server('REQUEST_URI')), '/'); //locale fix |
70
|
|
|
$row = $this->db |
71
|
|
|
->where('trash_url', $url) |
72
|
|
|
->or_where('trash_url', $this->uri->uri_string()) |
73
|
|
|
->get('trash')->row(); |
74
|
|
|
|
75
|
|
|
if ($row != null) { |
76
|
|
|
/** Подключает модули, в авто лоаде они идут после*/ |
77
|
|
|
$this->load->helper('menu/menu'); |
78
|
|
|
$this->load->helper('xbanners/xbanners'); |
79
|
|
|
|
80
|
|
|
($row->trash_redirect_type != '404') OR $this->core->error_404(); |
81
|
|
|
|
82
|
|
|
$this->setTrashIsCreateType($row->trash_redirect_type); |
83
|
|
|
redirect($this->formRedirectUrl($row->trash_redirect), 'location', $row->trash_type); |
84
|
|
|
} else { |
85
|
|
|
$url = $this->uri->getBaseUrl() . $this->input->server('REQUEST_URI'); |
86
|
|
|
if ($url != $this->formRedirectUrl($url)) { |
87
|
|
|
redirect($this->formRedirectUrl($url), 'location', 301); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Form URL redirect to |
94
|
|
|
* @param $url - url string |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function formRedirectUrl($url) { |
98
|
|
|
|
99
|
|
|
$siteSettings = $this->cms_base->get_settings(); |
100
|
|
|
|
101
|
|
|
if ($this->getTrashIsCreateType() && $this->getTrashIsCreateType() != 'url') { |
102
|
|
|
|
103
|
|
|
$site_length = mb_strlen($this->input->server('REQUEST_SCHEME') . '://' .$this->input->server('SERVER_NAME')); |
104
|
|
|
|
105
|
|
|
$url = site_url(substr($url, $site_length)); |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
switch ($siteSettings['www_redirect']) { |
110
|
|
|
case 'from_www': |
111
|
|
|
return str_replace('://www.', '://', $url); |
112
|
|
|
case 'to_www': |
113
|
|
|
$url = str_replace('://www.', '://', $url); |
114
|
|
|
return str_replace('://', '://www.', $url); |
115
|
|
|
default: |
116
|
|
|
return $url; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* @param string $trash_url |
124
|
|
|
* @param string $redirect_url |
125
|
|
|
* @param int $type |
126
|
|
|
* @throws Exception |
127
|
|
|
*/ |
128
|
|
|
public function create_redirect($trash_url, $redirect_url, $type = 301) { |
129
|
|
|
|
130
|
|
|
if (!isset($trash_url)) { |
131
|
|
|
throw new Exception(lang('Old URL is not specified', 'trash')); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!isset($redirect_url)) { |
135
|
|
|
throw new Exception(lang('New URL is not specified', 'trash')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$array = [ |
139
|
|
|
'trash_url' => ltrim($trash_url, '/'), |
140
|
|
|
'trash_redirect_type' => 'url', |
141
|
|
|
'trash_type' => in_array($type, [301, 302]) ? $type : 301, |
142
|
|
|
'trash_redirect' => '/' . str_replace(['http://', 'https://'], '', $redirect_url), |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$this->db->insert('trash', $array); |
146
|
|
|
|
147
|
|
|
if ($this->db->_error_message()) { |
148
|
|
|
throw new Exception($this->db->_error_message()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param array $arg |
154
|
|
|
*/ |
155
|
|
|
public static function delProductWhenCreate($arg) { |
156
|
|
|
|
157
|
|
|
/** @var SProducts $model */ |
158
|
|
|
$model = $arg['model']; |
159
|
|
|
$ci = &get_instance(); |
160
|
|
|
$ci->db->where('trash_url', $model->getRouteUrl())->delete('trash'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $arg |
165
|
|
|
*/ |
166
|
|
|
public static function addProductWhenAjaxChangeActive($arg) { |
167
|
|
|
|
168
|
|
|
/* @var $model SProducts */ |
169
|
|
|
$models = $arg['model']; |
170
|
|
|
|
171
|
|
|
/* @var $ci MY_Controller */ |
172
|
|
|
$ci = &get_instance(); |
173
|
|
|
|
174
|
|
|
if (!$models instanceof PropelObjectCollection) { |
175
|
|
|
$model = $models; |
176
|
|
|
$models = new PropelObjectCollection(); |
177
|
|
|
$models->append($model); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
foreach ($models as $model) { |
181
|
|
|
if (!$model) { |
182
|
|
|
continue; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$ci->db->where('trash_url', $model->getRouteUrl())->delete('trash'); |
186
|
|
|
if (!$model->getActive()) { |
187
|
|
|
$array = [ |
188
|
|
|
'trash_id' => $model->getCategoryId(), |
189
|
|
|
'trash_url' => $model->getRouteUrl(), |
190
|
|
|
'trash_redirect_type' => 'category', |
191
|
|
|
'trash_type' => '302', |
192
|
|
|
'trash_redirect' => site_url($model->getMainCategory()->getRouteUrl()), |
193
|
|
|
]; |
194
|
|
|
$ci->db->insert('trash', $array); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $arg |
201
|
|
|
*/ |
202
|
|
|
public static function addProductWhenDelete($arg) { |
203
|
|
|
|
204
|
|
|
$models = $arg['model']; |
205
|
|
|
$ci = &get_instance(); |
206
|
|
|
/** @var SProducts $model */ |
207
|
|
|
foreach ($models as $model) { |
208
|
|
|
$array = [ |
209
|
|
|
'trash_id' => $model->getCategoryId(), |
210
|
|
|
'trash_url' => $model->getRouteUrl(), |
211
|
|
|
'trash_redirect_type' => 'category', |
212
|
|
|
'trash_type' => '301', |
213
|
|
|
'trash_redirect' => site_url($model->getMainCategory()->getRouteUrl()), |
214
|
|
|
]; |
215
|
|
|
$ci->db->insert('trash', $array); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function _install() { |
220
|
|
|
|
221
|
|
|
$this->load->dbforge(); |
222
|
|
|
($this->dx_auth->is_admin()) OR exit; |
223
|
|
|
$fields = [ |
224
|
|
|
'id' => [ |
225
|
|
|
'type' => 'INT', |
226
|
|
|
'auto_increment' => true, |
227
|
|
|
], |
228
|
|
|
'trash_id' => [ |
229
|
|
|
'type' => 'VARCHAR', |
230
|
|
|
'constraint' => '255', |
231
|
|
|
'null' => true, |
232
|
|
|
], |
233
|
|
|
'trash_url' => [ |
234
|
|
|
'type' => 'VARCHAR', |
235
|
|
|
'constraint' => '255', |
236
|
|
|
'null' => true, |
237
|
|
|
], |
238
|
|
|
'trash_redirect_type' => [ |
239
|
|
|
'type' => 'VARCHAR', |
240
|
|
|
'constraint' => '20', |
241
|
|
|
'null' => true, |
242
|
|
|
], |
243
|
|
|
'trash_redirect' => [ |
244
|
|
|
'type' => 'VARCHAR', |
245
|
|
|
'constraint' => '255', |
246
|
|
|
'null' => true, |
247
|
|
|
], |
248
|
|
|
'trash_type' => [ |
249
|
|
|
'type' => 'VARCHAR', |
250
|
|
|
'constraint' => '3', |
251
|
|
|
'null' => true, |
252
|
|
|
], |
253
|
|
|
]; |
254
|
|
|
|
255
|
|
|
$this->dbforge->add_field($fields); |
256
|
|
|
$this->dbforge->add_key('id', true); |
257
|
|
|
$this->dbforge->create_table('trash'); |
258
|
|
|
|
259
|
|
|
$this->db->where('name', 'trash'); |
260
|
|
|
$this->db->update('components', ['enabled' => 0, 'autoload' => 1]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function _deinstall() { |
264
|
|
|
|
265
|
|
|
$this->load->dbforge(); |
266
|
|
|
($this->dx_auth->is_admin()) OR exit; |
267
|
|
|
$this->dbforge->drop_table('trash'); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function getTrashIsCreateType() { |
274
|
|
|
|
275
|
|
|
return $this->trash_is_create_type; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string $trash_is_create_type |
280
|
|
|
*/ |
281
|
|
|
public function setTrashIsCreateType($trash_is_create_type) { |
282
|
|
|
|
283
|
|
|
$this->trash_is_create_type = $trash_is_create_type; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/* End of file trash.php */ |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: