1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the RewriteUrl module for Thelia. */ |
4
|
|
|
/* */ |
5
|
|
|
/* Copyright (c) OpenStudio */ |
6
|
|
|
/* email : [email protected] */ |
7
|
|
|
/* web : http://www.thelia.net */ |
8
|
|
|
/* */ |
9
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
10
|
|
|
/* file that was distributed with this source code. */ |
11
|
|
|
/*************************************************************************************/ |
12
|
|
|
|
13
|
|
|
namespace RewriteUrl\Controller\Admin; |
14
|
|
|
|
15
|
|
|
use Propel\Runtime\ActiveQuery\Criteria; |
16
|
|
|
use RewriteUrl\Model\RewritingUrlOverride; |
17
|
|
|
use RewriteUrl\Event\RewriteUrlEvent; |
18
|
|
|
use RewriteUrl\Event\RewriteUrlEvents; |
19
|
|
|
use RewriteUrl\Form\AddUrlForm; |
20
|
|
|
use RewriteUrl\Form\ReassignForm; |
21
|
|
|
use RewriteUrl\Form\SetDefaultForm; |
22
|
|
|
use RewriteUrl\RewriteUrl; |
23
|
|
|
use Thelia\Controller\Admin\BaseAdminController; |
24
|
|
|
use Thelia\Core\Security\AccessManager; |
25
|
|
|
use Thelia\Core\Security\Resource\AdminResources; |
26
|
|
|
use Thelia\Form\Exception\FormValidationException; |
27
|
|
|
use Thelia\Model\BrandI18nQuery; |
28
|
|
|
use Thelia\Model\CategoryI18nQuery; |
29
|
|
|
use Thelia\Model\ContentI18nQuery; |
30
|
|
|
use Thelia\Model\FolderI18nQuery; |
31
|
|
|
use Thelia\Model\ProductI18nQuery; |
32
|
|
|
use Thelia\Model\ProductQuery; |
33
|
|
|
use Thelia\Model\RewritingUrl; |
34
|
|
|
use Thelia\Model\RewritingUrlQuery; |
35
|
|
|
use Thelia\Tools\URL; |
36
|
|
|
use Thelia\Log\Tlog; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class RewriteUrlAdminController |
40
|
|
|
* @package RewriteUrl\Controller\Admin |
41
|
|
|
* @author Vincent Lopes <[email protected]> |
42
|
|
|
* @author Gilles Bourgeat <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class RewriteUrlAdminController extends BaseAdminController |
45
|
|
|
{ |
46
|
|
|
/** @var array */ |
47
|
|
|
private $correspondence = array( |
48
|
|
|
'brand' => 'brand', |
49
|
|
|
'category' => 'categories', |
50
|
|
|
'content' => 'content', |
51
|
|
|
'folder' => 'folders', |
52
|
|
|
'product' => 'products' |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function deleteAction() |
59
|
|
|
{ |
60
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::DELETE)) { |
61
|
|
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$id_url = $this->getRequest()->request->get('id_url'); |
65
|
|
|
$rewritingUrl = RewritingUrlQuery::create()->findOneById($id_url); |
66
|
|
|
|
67
|
|
|
if ($rewritingUrl !== null) { |
68
|
|
|
$event = new RewriteUrlEvent($rewritingUrl); |
69
|
|
|
$this->getDispatcher()->dispatch(RewriteUrlEvents::REWRITEURL_DELETE, $event); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (method_exists($this, 'generateSuccessRedirect')) { |
73
|
|
|
//for 2.1 |
74
|
|
|
return $this->generateRedirectFromRoute( |
75
|
|
|
'admin.'.$this->correspondence[$rewritingUrl->getView()].'.update', |
76
|
|
|
[ |
77
|
|
|
$rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId(), |
78
|
|
|
'current_tab' => 'modules' |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
$rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId() |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
} else { |
85
|
|
|
//for 2.0 |
86
|
|
|
$this->redirectToRoute( |
87
|
|
|
'admin.'.$this->correspondence[$rewritingUrl->getView()].'.update', |
88
|
|
|
[ |
89
|
|
|
$rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId(), |
90
|
|
|
'current_tab' => 'modules' |
91
|
|
|
], |
92
|
|
|
[ |
93
|
|
|
$rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId() |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function addAction() |
103
|
|
|
{ |
104
|
|
|
$message = null; |
105
|
|
|
$exception = null; |
106
|
|
|
|
107
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::CREATE)) { |
108
|
|
|
return $response; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$addForm = new AddUrlForm($this->getRequest()); |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$form = $this->validateForm($addForm); |
115
|
|
|
$data = $form->getData($form); |
116
|
|
|
|
117
|
|
|
$findExist = RewritingUrlQuery::create()->findOneByUrl(($data['url'])); |
118
|
|
|
|
119
|
|
|
if ($findExist !== null && in_array($findExist->getView(), RewriteUrl::getUnknownSources()) && !empty($findExist->getView())) { |
120
|
|
|
throw new \Exception("Url already exist"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$rewriting = $findExist !== null ? $findExist : new RewritingUrlOverride(); |
124
|
|
|
$rewriting->setUrl($data['url']) |
125
|
|
|
->setView($data['view']) |
126
|
|
|
->setViewId($data['view-id']) |
127
|
|
|
->setViewLocale($data['locale']); |
128
|
|
|
|
129
|
|
|
$rewriteDefault = RewritingUrlQuery::create() |
130
|
|
|
->filterByView($rewriting->getView()) |
131
|
|
|
->filterByViewId($rewriting->getViewId()) |
132
|
|
|
->filterByViewLocale($rewriting->getViewLocale()) |
133
|
|
|
->findOneByRedirected(null); |
134
|
|
|
|
135
|
|
|
if ($data['default'] == 1) { |
136
|
|
|
$rewriting->setRedirected(null); |
137
|
|
|
} else { |
138
|
|
|
if ($rewriteDefault !== null) { |
139
|
|
|
$rewriting->setRedirected($rewriteDefault->getId()); |
140
|
|
|
} else { |
141
|
|
|
$rewriting->setRedirected(null); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->getDispatcher()->dispatch( |
146
|
|
|
RewriteUrlEvents::REWRITEURL_ADD, |
147
|
|
|
new RewriteUrlEvent($rewriting) |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
if (method_exists($this, 'generateSuccessRedirect')) { |
151
|
|
|
//for 2.1 |
152
|
|
|
return $this->generateSuccessRedirect($addForm); |
153
|
|
|
} else { |
154
|
|
|
//for 2.0 |
155
|
|
|
$this->redirectSuccess($addForm); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} catch (FormValidationException $exception) { |
159
|
|
|
$message = $this->createStandardFormValidationErrorMessage($exception); |
160
|
|
|
} catch (\Exception $exception) { |
161
|
|
|
$message = $exception->getMessage(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($message !== null && $exception !== null) { |
165
|
|
|
Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $exception->getMessage())); |
166
|
|
|
|
167
|
|
|
$addForm->setErrorMessage($message); |
168
|
|
|
|
169
|
|
|
$this->getParserContext() |
170
|
|
|
->addForm($addForm) |
171
|
|
|
->setGeneralError($message) |
172
|
|
|
; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (method_exists($this, 'generateSuccessRedirect')) { |
176
|
|
|
//for 2.1 |
177
|
|
|
return $this->generateSuccessRedirect($addForm); |
178
|
|
|
} else { |
179
|
|
|
//for 2.0 |
180
|
|
|
$this->redirectSuccess($addForm); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
public function setDefaultAction() |
188
|
|
|
{ |
189
|
|
|
$message = false; |
190
|
|
|
|
191
|
|
|
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'RewriteUrl', AccessManager::UPDATE)) { |
192
|
|
|
return $response; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$setDefaultForm = new SetDefaultForm($this->getRequest()); |
196
|
|
|
|
197
|
|
|
try { |
198
|
|
|
$form = $this->validateForm($setDefaultForm); |
199
|
|
|
$data = $form->getData($form); |
200
|
|
|
|
201
|
|
|
$rewritingUrl = RewritingUrlQuery::create()->findOneById($data['rewrite-id']); |
202
|
|
|
$newEvent = new RewriteUrlEvent($rewritingUrl); |
203
|
|
|
$this->getDispatcher()->dispatch(RewriteUrlEvents::REWRITEURL_SET_DEFAULT, $newEvent); |
204
|
|
|
} catch (FormValidationException $e) { |
205
|
|
|
$message = $this->createStandardFormValidationErrorMessage($e); |
206
|
|
|
} catch (\Exception $e) { |
207
|
|
|
$message = $e->getMessage(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($message !== false) { |
211
|
|
|
$setDefaultForm->setErrorMessage($message); |
212
|
|
|
|
213
|
|
|
$this->getParserContext() |
214
|
|
|
->addForm($setDefaultForm) |
215
|
|
|
->setGeneralError($message) |
216
|
|
|
; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (method_exists($this, 'generateSuccessRedirect')) { |
220
|
|
|
//for 2.1 |
221
|
|
|
return $this->generateSuccessRedirect($setDefaultForm); |
222
|
|
|
} else { |
223
|
|
|
//for 2.0 |
224
|
|
|
$this->redirectSuccess($setDefaultForm); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
public function reassignAction() |
232
|
|
|
{ |
233
|
|
|
$message = false; |
234
|
|
|
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'RewriteUrl', AccessManager::UPDATE)) { |
235
|
|
|
return $response; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$reassignForm = new ReassignForm($this->getRequest()); |
239
|
|
|
|
240
|
|
|
try { |
241
|
|
|
$form = $this->validateForm($reassignForm); |
242
|
|
|
$data = $form->getData($form); |
243
|
|
|
|
244
|
|
|
$all = $data['all']; |
245
|
|
|
$newRewrite = explode('::', $data['select-reassign']); |
246
|
|
|
$rewriteId = $data['rewrite-id']; |
247
|
|
|
$newView = $newRewrite[1]; |
248
|
|
|
$newViewId = $newRewrite[0]; |
249
|
|
|
|
250
|
|
|
if ($all === 1) { |
251
|
|
|
self::allReassign($rewriteId, $newView, $newViewId); |
252
|
|
|
} else { |
253
|
|
|
self::simpleReassign($rewriteId, $newView, $newViewId); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if (method_exists($this, 'generateSuccessRedirect')) { |
257
|
|
|
//for 2.1 |
258
|
|
|
return $this->generateSuccessRedirect($reassignForm); |
259
|
|
|
} else { |
260
|
|
|
//for 2.0 |
261
|
|
|
$this->redirectSuccess($reassignForm); |
262
|
|
|
} |
263
|
|
|
} catch (FormValidationException $e) { |
264
|
|
|
$message = $this->createStandardFormValidationErrorMessage($e); |
265
|
|
|
} catch (\Exception $e) { |
266
|
|
|
$message = $e->getMessage(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if ($message !== false) { |
270
|
|
|
$reassignForm->setErrorMessage($message); |
271
|
|
|
|
272
|
|
|
$this->getParserContext() |
273
|
|
|
->addForm($reassignForm) |
274
|
|
|
->setGeneralError($message) |
275
|
|
|
; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param int $rewriteId |
281
|
|
|
* @param string $newView |
282
|
|
|
* @param int $newViewId |
283
|
|
|
*/ |
284
|
|
|
protected function allReassign($rewriteId, $newView, $newViewId) |
285
|
|
|
{ |
286
|
|
|
$origin = RewritingUrlQuery::create()->findOneById($rewriteId); |
287
|
|
|
|
288
|
|
|
$rewrites = RewritingUrlQuery::create() |
289
|
|
|
->filterByView($origin->getView()) |
290
|
|
|
->filterByViewId($origin->getViewId()) |
291
|
|
|
->find(); |
292
|
|
|
|
293
|
|
|
/** @var RewritingUrl $rewrite */ |
294
|
|
|
foreach ($rewrites as $rewrite) { |
295
|
|
|
$destination = RewritingUrlQuery::create() |
296
|
|
|
->filterByView($newView) |
297
|
|
|
->filterByViewId($newViewId) |
298
|
|
|
->filterByViewLocale($rewrite->getViewLocale()) |
299
|
|
|
->filterByRedirected(null) |
300
|
|
|
->findOne(); |
301
|
|
|
|
302
|
|
|
$rewrite |
303
|
|
|
->setView($newView) |
304
|
|
|
->setViewId($newViewId) |
305
|
|
|
->setRedirected(($destination === null) ? null : $destination->getId()); |
306
|
|
|
|
307
|
|
|
$this->getDispatcher()->dispatch( |
308
|
|
|
RewriteUrlEvents::REWRITEURL_UPDATE, |
309
|
|
|
new RewriteUrlEvent($rewrite) |
310
|
|
|
); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param int $rewriteId |
316
|
|
|
* @param string $newView |
317
|
|
|
* @param int $newViewId |
318
|
|
|
*/ |
319
|
|
|
protected function simpleReassign($rewriteId, $newView, $newViewId) |
320
|
|
|
{ |
321
|
|
|
$rewrite = RewritingUrlQuery::create()->findOneById($rewriteId); |
322
|
|
|
|
323
|
|
|
// add new default url |
324
|
|
|
if (null !== $newDefault = RewritingUrlQuery::create()->findOneByRedirected($rewrite->getId())) { |
325
|
|
|
$this->getDispatcher()->dispatch( |
326
|
|
|
RewriteUrlEvents::REWRITEURL_UPDATE, |
327
|
|
|
new RewriteUrlEvent( |
328
|
|
|
$newDefault->setRedirected(null) |
329
|
|
|
) |
330
|
|
|
); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
//Update urls who redirected to updated URL |
334
|
|
|
if (null !== $isRedirection = RewritingUrlQuery::create()->findByRedirected($rewrite->getId())) { |
335
|
|
|
/** @var \Thelia\Model\RewritingUrl $redirected */ |
336
|
|
|
foreach ($isRedirection as $redirected) { |
337
|
|
|
$this->getDispatcher()->dispatch( |
338
|
|
|
RewriteUrlEvents::REWRITEURL_UPDATE, |
339
|
|
|
new RewriteUrlEvent( |
340
|
|
|
$redirected->setRedirected( |
341
|
|
|
($newDefault !== null) ? $newDefault->getId() : $rewrite->getRedirected() |
342
|
|
|
) |
343
|
|
|
) |
344
|
|
|
); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$rewrite->setView($newView) |
349
|
|
|
->setViewId($newViewId); |
350
|
|
|
|
351
|
|
|
//Check if default url already exist for the view with the locale |
352
|
|
|
$rewriteDefault = RewritingUrlQuery::create() |
353
|
|
|
->filterByView($newView) |
354
|
|
|
->filterByViewId($newViewId) |
355
|
|
|
->filterByViewLocale($rewrite->getViewLocale()) |
356
|
|
|
->findOneByRedirected(null); |
357
|
|
|
|
358
|
|
|
if ($rewriteDefault !== null) { |
359
|
|
|
$rewrite->setRedirected($rewriteDefault->getId()); |
360
|
|
|
} else { |
361
|
|
|
$rewrite->setRedirected(null); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
$event = new RewriteUrlEvent($rewrite); |
365
|
|
|
$this->getDispatcher()->dispatch(RewriteUrlEvents::REWRITEURL_UPDATE, $event); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return mixed|\Thelia\Core\HttpFoundation\Response |
370
|
|
|
*/ |
371
|
|
|
public function existAction() |
372
|
|
|
{ |
373
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::VIEW)) { |
374
|
|
|
return $response; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$search = $this->getRequest()->query->get('q'); |
378
|
|
|
|
379
|
|
|
$rewritingUrl = RewritingUrlQuery::create() |
380
|
|
|
->filterByView(RewriteUrl::getUnknownSources(), Criteria::NOT_IN) |
381
|
|
|
->findOneByUrl($search); |
382
|
|
|
|
383
|
|
|
if ($rewritingUrl !== null) { |
384
|
|
|
if (!in_array($rewritingUrl->getView(), $this->correspondence)) { |
385
|
|
|
return $this->jsonResponse(json_encode(false)); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$route = $this->getRoute( |
389
|
|
|
"admin.".$this->correspondence[$rewritingUrl->getView()].".update", |
390
|
|
|
[$rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId()] |
391
|
|
|
); |
392
|
|
|
$url = URL::getInstance()->absoluteUrl( |
393
|
|
|
$route, |
394
|
|
|
[$rewritingUrl->getView().'_id'=>$rewritingUrl->getViewId()] |
395
|
|
|
); |
396
|
|
|
|
397
|
|
|
$rewritingUrlArray = ["reassignUrl" => $url]; |
398
|
|
|
|
399
|
|
|
return $this->jsonResponse(json_encode($rewritingUrlArray)); |
400
|
|
|
} else { |
401
|
|
|
return $this->jsonResponse(json_encode(false)); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @return mixed|\Thelia\Core\HttpFoundation\Response |
407
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
408
|
|
|
*/ |
409
|
|
|
public function searchAction() |
410
|
|
|
{ |
411
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'RewriteUrl', AccessManager::VIEW)) { |
412
|
|
|
return $response; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
$search = '%'.$this->getRequest()->query->get('q').'%'; |
416
|
|
|
|
417
|
|
|
$resultArray = array(); |
418
|
|
|
|
419
|
|
|
$categoriesI18n = CategoryI18nQuery::create()->filterByTitle($search)->limit(10); |
420
|
|
|
$contentsI18n = ContentI18nQuery::create()->filterByTitle($search)->limit(10); |
421
|
|
|
$foldersI18n = FolderI18nQuery::create()->filterByTitle($search)->limit(10); |
422
|
|
|
$brandsI18n = BrandI18nQuery::create()->filterByTitle($search)->limit(10); |
423
|
|
|
|
424
|
|
|
$productsI18n = ProductI18nQuery::create()->filterByTitle($search)->limit(10); |
425
|
|
|
$productsRef = ProductQuery::create()->filterByRef($search)->limit(10); |
426
|
|
|
|
427
|
|
|
/** @var \Thelia\Model\CategoryI18n $categoryI18n */ |
428
|
|
|
foreach ($categoriesI18n as $categoryI18n) { |
429
|
|
|
$category = $categoryI18n->getCategory(); |
430
|
|
|
$resultArray['category'][$category->getId()] = $categoryI18n->getTitle(); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** @var \Thelia\Model\ContentI18n $contentI18n */ |
434
|
|
|
foreach ($contentsI18n as $contentI18n) { |
435
|
|
|
$content = $contentI18n->getContent(); |
436
|
|
|
$resultArray['content'][$content->getId()] = $contentI18n->getTitle(); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** @var \Thelia\Model\FolderI18n $folderI18n */ |
440
|
|
|
foreach ($foldersI18n as $folderI18n) { |
441
|
|
|
$folder = $folderI18n->getFolder(); |
442
|
|
|
$resultArray['folder'][$folder->getId()] = $folderI18n->getTitle(); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** @var \Thelia\Model\BrandI18n $brandI18n */ |
446
|
|
|
foreach ($brandsI18n as $brandI18n) { |
447
|
|
|
$brand = $brandI18n->getBrand(); |
448
|
|
|
$resultArray['brand'][$brand->getId()] = $brandI18n->getTitle(); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** @var \Thelia\Model\ProductI18n $productI18n */ |
452
|
|
|
foreach ($productsI18n as $productI18n) { |
453
|
|
|
$product = $productI18n->getProduct(); |
454
|
|
|
$resultArray['product'][$product->getId()] = $productI18n->getTitle(); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** @var \Thelia\Model\Product $product */ |
458
|
|
|
foreach ($productsRef as $product) { |
459
|
|
|
$productI18n = ProductI18nQuery::create()->filterByProduct($product)->findOne(); |
460
|
|
|
$resultArray['product'][$product->getId()] = $productI18n->getTitle(); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
return $this->jsonResponse(json_encode($resultArray)); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|