|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * BINSHOPS |
| 4 | + * |
| 5 | + * @author BINSHOPS |
| 6 | + * @copyright BINSHOPS |
| 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 | +require_once dirname(__FILE__) . '/../AbstractRESTController.php'; |
| 13 | + |
| 14 | +class BinshopsrestEmailsubscriptionModuleFrontController extends AbstractRESTController |
| 15 | +{ |
| 16 | + |
| 17 | + protected function processGetRequest() |
| 18 | + { |
| 19 | + $conditions = Configuration::get('NW_CONDITIONS', $this->context->language->id); |
| 20 | + |
| 21 | + $this->ajaxRender(json_encode([ |
| 22 | + 'success' => true, |
| 23 | + 'code' => 200, |
| 24 | + 'psdata' => $conditions, |
| 25 | + 'message' => 'success' |
| 26 | + ])); |
| 27 | + die; |
| 28 | + } |
| 29 | + |
| 30 | + protected function processPostRequest() |
| 31 | + { |
| 32 | + $ps_emailsubscription = Module::getInstanceByName('ps_emailsubscription'); |
| 33 | + $email = Tools::getValue('email', ''); |
| 34 | + |
| 35 | + if ($ps_emailsubscription->isNewsletterRegistered($email) === 1 || $ps_emailsubscription->isNewsletterRegistered($email) === 2){ |
| 36 | + $this->ajaxRender(json_encode([ |
| 37 | + 'success' => false, |
| 38 | + 'code' => 301, |
| 39 | + 'message' => 'This email is already registered' |
| 40 | + ])); |
| 41 | + die; |
| 42 | + } |
| 43 | + |
| 44 | + if ($this->registerGuest($email)) { |
| 45 | + $this->ajaxRender(json_encode([ |
| 46 | + 'success' => true, |
| 47 | + 'code' => 200, |
| 48 | + 'message' => 'success' |
| 49 | + ])); |
| 50 | + die; |
| 51 | + |
| 52 | + } elseif ($ps_emailsubscription->valid) { |
| 53 | + $this->ajaxRender(json_encode([ |
| 54 | + 'success' => false, |
| 55 | + 'code' => 300, |
| 56 | + 'message' => 'failure' |
| 57 | + ])); |
| 58 | + die; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected function processPutRequest() |
| 63 | + { |
| 64 | + $this->ajaxRender(json_encode([ |
| 65 | + 'success' => true, |
| 66 | + 'message' => 'put not supported on this path' |
| 67 | + ])); |
| 68 | + die; |
| 69 | + } |
| 70 | + |
| 71 | + protected function processDeleteRequest() |
| 72 | + { |
| 73 | + $this->ajaxRender(json_encode([ |
| 74 | + 'success' => true, |
| 75 | + 'message' => 'delete not supported on this path' |
| 76 | + ])); |
| 77 | + die; |
| 78 | + } |
| 79 | + |
| 80 | + protected function registerGuest($email, $active = true) |
| 81 | + { |
| 82 | + $sql = 'INSERT INTO ' . _DB_PREFIX_ . 'emailsubscription (id_shop, id_shop_group, email, newsletter_date_add, ip_registration_newsletter, http_referer, active, id_lang) |
| 83 | + VALUES |
| 84 | + (' . $this->context->shop->id . ', |
| 85 | + ' . $this->context->shop->id_shop_group . ', |
| 86 | + \'' . pSQL($email) . '\', |
| 87 | + NOW(), |
| 88 | + \'' . pSQL(Tools::getRemoteAddr()) . '\', |
| 89 | + ( |
| 90 | + SELECT c.http_referer |
| 91 | + FROM ' . _DB_PREFIX_ . 'connections c |
| 92 | + WHERE c.id_guest = ' . (int) $this->context->customer->id . ' |
| 93 | + ORDER BY c.date_add DESC LIMIT 1 |
| 94 | + ), |
| 95 | + ' . (int) $active . ', |
| 96 | + ' . $this->context->language->id . ' |
| 97 | + )'; |
| 98 | + |
| 99 | + return Db::getInstance()->execute($sql); |
| 100 | + } |
| 101 | +} |
0 commit comments