1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RewriteUrl\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use RewriteUrl\Model\RewriteurlRule; |
6
|
|
|
use RewriteUrl\Model\RewriteurlRuleParam; |
7
|
|
|
use RewriteUrl\Model\RewriteurlRuleQuery; |
8
|
|
|
use RewriteUrl\RewriteUrl; |
9
|
|
|
use Thelia\Controller\Admin\BaseAdminController; |
10
|
|
|
use Thelia\Core\Security\AccessManager; |
11
|
|
|
use Thelia\Core\Security\Resource\AdminResources; |
12
|
|
|
use Thelia\Core\Translation\Translator; |
13
|
|
|
use Thelia\Model\ConfigQuery; |
14
|
|
|
|
15
|
|
|
class ModuleConfigController extends BaseAdminController |
16
|
|
|
{ |
17
|
|
|
public function viewConfigAction($params = array()) |
18
|
|
|
{ |
19
|
|
|
if (null !== $response = $this->checkAuth(array(AdminResources::MODULE), 'GoogleShoppingXml', AccessManager::VIEW)) { |
20
|
|
|
return $response; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$isRewritingEnabled = ConfigQuery::isRewritingEnable(); |
24
|
|
|
$rewritingRules = RewriteurlRuleQuery::create()->orderByPosition()->find(); |
25
|
|
|
|
26
|
|
|
return $this->render( |
27
|
|
|
"RewriteUrl/module-configuration", |
28
|
|
|
[ |
29
|
|
|
"isRewritingEnabled" => $isRewritingEnabled, |
30
|
|
|
"rewritingRules" => $rewritingRules, |
31
|
|
|
] |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setRewritingEnableAction() |
36
|
|
|
{ |
37
|
|
|
$request = $this->getRequest()->request; |
38
|
|
|
$isRewritingEnable = $request->get("rewriting_enable", null); |
39
|
|
|
|
40
|
|
|
if ($isRewritingEnable !== null) { |
41
|
|
|
ConfigQuery::write("rewriting_enable", $isRewritingEnable ? 1 : 0); |
42
|
|
|
return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
43
|
|
|
} else { |
44
|
|
|
return $this->jsonResponse(Translator::getInstance()->trans( |
45
|
|
|
"Unable to change the configuration variable.", |
46
|
|
|
[], |
47
|
|
|
RewriteUrl::MODULE_DOMAIN |
48
|
|
|
), 500); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function addRuleAction() |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
$request = $this->getRequest()->request; |
56
|
|
|
|
57
|
|
|
$rule = new RewriteurlRule(); |
58
|
|
|
|
59
|
|
|
$this->fillRuleObjectFields($rule, $request); |
60
|
|
|
} catch (\Exception $ex) { |
61
|
|
|
return $this->jsonResponse($ex->getMessage(), 500); |
62
|
|
|
} |
63
|
|
|
return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
public function updateRuleAction() |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
$request = $this->getRequest()->request; |
71
|
|
|
|
72
|
|
|
$rule = RewriteurlRuleQuery::create()->findOneById($request->get("id")); |
73
|
|
|
if ($rule == null) { |
74
|
|
|
throw new \Exception(Translator::getInstance()->trans( |
75
|
|
|
"Unable to find rule to update.", |
76
|
|
|
[], |
77
|
|
|
RewriteUrl::MODULE_DOMAIN |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->fillRuleObjectFields($rule, $request); |
82
|
|
|
} catch (\Exception $ex) { |
83
|
|
|
return $this->jsonResponse($ex->getMessage(), 500); |
84
|
|
|
} |
85
|
|
|
return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
public function removeRuleAction() |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$request = $this->getRequest()->request; |
93
|
|
|
|
94
|
|
|
$rule = RewriteurlRuleQuery::create()->findOneById($request->get("id")); |
95
|
|
|
if ($rule == null) { |
96
|
|
|
throw new \Exception(Translator::getInstance()->trans( |
97
|
|
|
"Unable to find rule to remove.", |
98
|
|
|
[], |
99
|
|
|
RewriteUrl::MODULE_DOMAIN |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$rule->delete(); |
104
|
|
|
} catch (\Exception $ex) { |
105
|
|
|
return $this->jsonResponse($ex->getMessage(), 500); |
106
|
|
|
} |
107
|
|
|
return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
public function moveRulePositionAction() |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$request = $this->getRequest()->request; |
115
|
|
|
|
116
|
|
|
$rule = RewriteurlRuleQuery::create()->findOneById($request->get("id")); |
117
|
|
|
if ($rule == null) { |
118
|
|
|
throw new \Exception(Translator::getInstance()->trans( |
119
|
|
|
"Unable to find rule to change position.", |
120
|
|
|
[], |
121
|
|
|
RewriteUrl::MODULE_DOMAIN |
122
|
|
|
)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$type = $request->get("type", null); |
126
|
|
|
if ($type == "up") { |
127
|
|
|
$rule->movePositionUp(); |
128
|
|
|
} elseif ($type == "down") { |
129
|
|
|
$rule->movePositionDown(); |
130
|
|
|
} elseif ($type == "absolute") { |
131
|
|
|
$position = $request->get("position", null); |
132
|
|
|
if (!empty($position)) { |
133
|
|
|
$rule->changeAbsolutePosition($position); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} catch (\Exception $ex) { |
137
|
|
|
return $this->jsonResponse($ex->getMessage(), 500); |
138
|
|
|
} |
139
|
|
|
return $this->jsonResponse(json_encode(["state" => "Success"]), 200); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
|
|
protected function fillRuleObjectFields(RewriteurlRule $rule, $request) |
145
|
|
|
{ |
146
|
|
|
$ruleType = $request->get("ruleType", null); |
147
|
|
|
if ($ruleType !== "regex" && $ruleType !== "params") { |
148
|
|
|
throw new \Exception(Translator::getInstance()->trans("Unknown rule type.", [], RewriteUrl::MODULE_DOMAIN)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$regexValue = $request->get("value", null); |
152
|
|
|
if ($ruleType == "regex" && empty($regexValue)) { |
153
|
|
|
throw new \Exception(Translator::getInstance()->trans("Regex value cannot be empty.", [], RewriteUrl::MODULE_DOMAIN)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$redirectUrl = $request->get("redirectUrl", null); |
157
|
|
|
if (empty($redirectUrl)) { |
158
|
|
|
throw new \Exception(Translator::getInstance()->trans("Redirect url cannot be empty.", [], RewriteUrl::MODULE_DOMAIN)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$paramRuleArray = array(); |
162
|
|
|
if ($ruleType == "params") { |
163
|
|
|
$paramRuleArray = $request->get("paramRules", null); |
164
|
|
|
if (empty($paramRuleArray)) { |
165
|
|
|
throw new \Exception(Translator::getInstance()->trans("At least one GET parameter is required.", [], RewriteUrl::MODULE_DOMAIN)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
$rule->setRuleType($ruleType); |
172
|
|
|
$rule->setValue($regexValue); |
173
|
|
|
$rule->setOnly404($request->get("only404", 1)); |
174
|
|
|
$rule->setRedirectUrl($redirectUrl); |
175
|
|
|
if (empty($rule->getPosition())) { |
176
|
|
|
$rule->setPosition($rule->getNextPosition()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
$rule->deleteAllRelatedParam(); |
181
|
|
|
|
182
|
|
|
$rule->save(); |
183
|
|
|
|
184
|
|
|
if ($ruleType == "params") { |
185
|
|
|
foreach ($paramRuleArray as $paramRule) { |
186
|
|
|
if (!array_key_exists("paramName", $paramRule) || empty($paramRule["paramName"])) { |
187
|
|
|
throw new \Exception(Translator::getInstance()->trans( |
188
|
|
|
"Param name is empty.", |
189
|
|
|
[], |
190
|
|
|
RewriteUrl::MODULE_DOMAIN |
191
|
|
|
)); |
192
|
|
|
} |
193
|
|
|
if (!array_key_exists("condition", $paramRule) || empty($paramRule["condition"])) { |
194
|
|
|
throw new \Exception(Translator::getInstance()->trans( |
195
|
|
|
"Param condition is empty.", |
196
|
|
|
[], |
197
|
|
|
RewriteUrl::MODULE_DOMAIN |
198
|
|
|
)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$paramRuleObject = new RewriteurlRuleParam(); |
202
|
|
|
$paramRuleObject->setParamName($paramRule["paramName"]); |
203
|
|
|
$paramRuleObject->setParamCondition($paramRule["condition"]); |
204
|
|
|
$paramRuleObject->setParamValue($paramRule["paramValue"]); |
205
|
|
|
$paramRuleObject->setIdRule($rule->getId()); |
206
|
|
|
$paramRuleObject->save(); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|