Skip to content

Commit e993082

Browse files
committed
Merge branch 'develop'
2 parents 7a8edb8 + 5841aaf commit e993082

File tree

6 files changed

+170
-30
lines changed

6 files changed

+170
-30
lines changed

binshopsrest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct()
2323
{
2424
$this->name = 'binshopsrest';
2525
$this->tab = 'others';
26-
$this->version = '2.4.7';
26+
$this->version = '2.5.0';
2727
$this->author = 'Binshops';
2828
$this->need_instance = 0;
2929

@@ -60,7 +60,9 @@ public function install()
6060

6161
return parent::install() &&
6262
$this->registerHook('header') &&
63-
$this->registerHook('backOfficeHeader') && $this->registerHook('moduleRoutes');
63+
$this->registerHook('backOfficeHeader') &&
64+
$this->registerHook('moduleRoutes') &&
65+
$this->registerHook('actionDispatcherBefore');
6466
}
6567

6668
public function uninstall()
@@ -215,6 +217,52 @@ public function hookHeader()
215217
$this->context->controller->addCSS($this->_path . '/views/css/front.css');
216218
}
217219

220+
public function hookactionDispatcherBefore($controller)
221+
{
222+
if ($controller['controller_type'] === 1 && preg_match("#/rest/#", $_SERVER['REQUEST_URI'], $k)){
223+
preg_match('`rest/(.*)`', $_SERVER['REQUEST_URI'], $m);
224+
$s = explode('/', $m[0]);
225+
$_GET['fc'] = 'module';
226+
$_GET['module'] = $s[1];
227+
$_GET['controller'] = $s[2];
228+
$controller_name = $s[2];
229+
230+
$module_name = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
231+
232+
$module = Module::getInstanceByName($module_name);
233+
$controller_class = 'PageNotFoundController';
234+
235+
if (Validate::isLoadedObject($module) && $module->active) {
236+
$controllers = Dispatcher::getControllers(_PS_MODULE_DIR_ . "$module_name/controllers/front/");
237+
if (isset($controllers[strtolower($controller_name)])) {
238+
include_once _PS_MODULE_DIR_ . "$module_name/controllers/front/{$controller_name}.php";
239+
if (file_exists(
240+
_PS_OVERRIDE_DIR_ . "modules/$module_name/controllers/front/{$controller_name}.php"
241+
)) {
242+
include_once _PS_OVERRIDE_DIR_ . "modules/$module_name/controllers/front/{$controller_name}.php";
243+
$controller_class = $module_name . $controller_name . 'ModuleFrontControllerOverride';
244+
} else {
245+
$controller_class = $module_name . $controller_name . 'ModuleFrontController';
246+
}
247+
}
248+
}
249+
250+
if (!isset($controller) || !$module){
251+
header('Content-Type: ' . "application/json");
252+
253+
echo json_encode([
254+
'success' => true,
255+
'message' => 'This endpoint is not defined.',
256+
'code' => 410
257+
]);
258+
}
259+
260+
$controller = Controller::getController($controller_class);
261+
262+
$controller->restRun();
263+
}
264+
}
265+
218266
public function hookModuleRoutes()
219267
{
220268
return APIRoutes::getRoutes();

classes/RESTTrait.php

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,77 @@
22

33
trait RESTTrait
44
{
5-
protected function processGetRequest(){
6-
$this->ajaxRender(json_encode([
5+
public function restRun(){
6+
header('Content-Type: ' . "application/json");
7+
if (Tools::getValue('iso_currency')){
8+
$_GET['id_currency'] = (string)Currency::getIdByIsoCode(Tools::getValue('currency'));
9+
$_GET['SubmitCurrency'] = "1";
10+
}
11+
12+
parent::init();
13+
14+
$response = [
715
'success' => true,
8-
'message' => $this->trans('GET not supported on this path', [], 'Modules.Binshopsrest.Admin')
9-
]));
16+
'code' => 210,
17+
'psdata' => null,
18+
'message' => 'empty'
19+
];
20+
21+
switch ($_SERVER['REQUEST_METHOD']) {
22+
case 'GET':
23+
$response = $this->processGetRequest();
24+
break;
25+
case 'POST':
26+
$response = $this->processPostRequest();
27+
break;
28+
case 'PATCH':
29+
case 'PUT':
30+
$response = $this->processPutRequest();
31+
break;
32+
case 'DELETE':
33+
$response = $this->processDeleteRequest();
34+
break;
35+
default:
36+
// throw some error or whatever
37+
}
38+
39+
$this->ajaxRender(json_encode($response));
1040
die;
1141
}
1242

43+
protected function processGetRequest(){
44+
return [
45+
'success' => true,
46+
'code' => 310,
47+
'psdata' => null,
48+
'message' => $this->trans('GET not supported on this path', [], 'Modules.Binshopsrest.Admin'),
49+
];
50+
}
51+
1352
protected function processPostRequest(){
14-
$this->ajaxRender(json_encode([
53+
return [
1554
'success' => true,
16-
'message' => $this->trans('POST not supported on this path', [], 'Modules.Binshopsrest.Admin')
17-
]));
18-
die;
55+
'code' => 310,
56+
'psdata' => null,
57+
'message' => $this->trans('POST not supported on this path', [], 'Modules.Binshopsrest.Admin'),
58+
];
1959
}
2060

2161
protected function processPutRequest(){
22-
$this->ajaxRender(json_encode([
62+
return [
2363
'success' => true,
24-
'message' => $this->trans('PUT not supported on this path', [], 'Modules.Binshopsrest.Admin')
25-
]));
26-
die;
64+
'code' => 310,
65+
'psdata' => null,
66+
'message' => $this->trans('PUT not supported on this path', [], 'Modules.Binshopsrest.Admin'),
67+
];
2768
}
2869

2970
protected function processDeleteRequest(){
30-
$this->ajaxRender(json_encode([
71+
return [
3172
'success' => true,
32-
'message' => $this->trans('DELETE not supported on this path', [], 'Modules.Binshopsrest.Admin')
33-
]));
34-
die;
73+
'code' => 310,
74+
'psdata' => null,
75+
'message' => $this->trans('DELETE not supported on this path', [], 'Modules.Binshopsrest.Admin'),
76+
];
3577
}
3678
}

controllers/AbstractCartRESTController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,33 @@ public function init()
1616

1717
parent::init();
1818

19+
$response = [
20+
'success' => true,
21+
'code' => 210,
22+
'psdata' => null,
23+
'message' => 'empty'
24+
];
25+
1926
switch ($_SERVER['REQUEST_METHOD']) {
2027
case 'GET':
21-
$this->processGetRequest();
28+
$response = $this->processGetRequest();
2229
break;
2330
case 'POST':
24-
$this->processPostRequest();
31+
$response = $this->processPostRequest();
2532
break;
2633
case 'PATCH':
2734
case 'PUT':
28-
$this->processPutRequest();
35+
$response = $this->processPutRequest();
2936
break;
3037
case 'DELETE':
31-
$this->processDeleteRequest();
38+
$response = $this->processDeleteRequest();
3239
break;
3340
default:
3441
// throw some error or whatever
3542
}
43+
44+
$this->ajaxRender(json_encode($response));
45+
die;
3646
}
3747

3848
protected function checkCartProductsMinimalQuantities()

controllers/AbstractProductListingRESTController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,33 @@ public function init()
3737

3838
parent::init();
3939

40+
$response = [
41+
'success' => true,
42+
'code' => 210,
43+
'psdata' => null,
44+
'message' => 'empty'
45+
];
46+
4047
switch ($_SERVER['REQUEST_METHOD']) {
4148
case 'GET':
42-
$this->processGetRequest();
49+
$response = $this->processGetRequest();
4350
break;
4451
case 'POST':
45-
$this->processPostRequest();
52+
$response = $this->processPostRequest();
4653
break;
4754
case 'PATCH':
4855
case 'PUT':
49-
$this->processPutRequest();
56+
$response = $this->processPutRequest();
5057
break;
5158
case 'DELETE':
52-
$this->processDeleteRequest();
59+
$response = $this->processDeleteRequest();
5360
break;
5461
default:
5562
// throw some error or whatever
5663
}
64+
65+
$this->ajaxRender(json_encode($response));
66+
die;
5767
}
5868

5969
protected function getImage($object, $id_image)

controllers/AbstractRESTController.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,33 @@ public function init()
3030

3131
parent::init();
3232

33+
$response = [
34+
'success' => true,
35+
'code' => 210,
36+
'psdata' => null,
37+
'message' => 'empty'
38+
];
39+
3340
switch ($_SERVER['REQUEST_METHOD']) {
3441
case 'GET':
35-
$this->processGetRequest();
42+
$response = $this->processGetRequest();
3643
break;
3744
case 'POST':
38-
$this->processPostRequest();
45+
$response = $this->processPostRequest();
3946
break;
4047
case 'PATCH':
4148
case 'PUT':
42-
$this->processPutRequest();
49+
$response = $this->processPutRequest();
4350
break;
4451
case 'DELETE':
45-
$this->processDeleteRequest();
52+
$response = $this->processDeleteRequest();
4653
break;
4754
default:
4855
// throw some error or whatever
4956
}
57+
58+
$this->ajaxRender(json_encode($response));
59+
die;
5060
}
5161

5262
public function formatPrice($price)

upgrade/upgrade-2.5.0.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* BINSHOPS REST API
4+
*
5+
* @author BINSHOPS | Best In Shops
6+
* @copyright BINSHOPS | Best In Shops
7+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
8+
* Best In Shops eCommerce Solutions Inc.
9+
*
10+
*/
11+
12+
if (!defined('_PS_VERSION_')) {
13+
exit;
14+
}
15+
16+
function upgrade_module_2_5_0($module)
17+
{
18+
$module->registerHook('actionDispatcherBefore');
19+
return true;
20+
}

0 commit comments

Comments
 (0)