diff --git a/controllers/front/withdraw.php b/controllers/front/withdraw.php index 7cec38f..645fe9c 100644 --- a/controllers/front/withdraw.php +++ b/controllers/front/withdraw.php @@ -26,12 +26,6 @@ class Simple_withdrawalbuttonWithdrawModuleFrontController extends ModuleFrontCo { parent::initContent(); - $this->registerStylesheet( - 'cyp-withdrawal', - 'modules/' . $this->module->name . '/views/css/withdrawal.css', - ['media' => 'all', 'priority' => 200] - ); - $this->context->smarty->assign([ 'errors_list' => $this->errorsList, 'form_data' => $this->formData, @@ -40,6 +34,7 @@ class Simple_withdrawalbuttonWithdrawModuleFrontController extends ModuleFrontCo 'success_data' => $this->successData, 'privacy_url' => (string) Configuration::get(Simple_withdrawalbutton::CONF_PRIVACY_URL), 'revocation_url' => (string) Configuration::get(Simple_withdrawalbutton::CONF_REVOCATION_URL), + 'withdrawal_deadline' => $this->computeDeadline(), ]); if ($this->currentView === 'confirm') { @@ -185,16 +180,37 @@ class Simple_withdrawalbuttonWithdrawModuleFrontController extends ModuleFrontCo $customerEmail = (string) $this->context->customer->email; } + $orderRef = $this->module->cleanText(Tools::getValue('ref'), 64); + return [ 'customer_name' => $customerName, 'customer_email' => $customerEmail, - 'order_reference' => '', + 'order_reference' => $orderRef, 'withdrawal_scope' => 'full', 'withdrawal_items_text' => '', 'message' => '', ]; } + private function computeDeadline() + { + $ref = isset($this->formData['order_reference']) ? $this->formData['order_reference'] : ''; + if ($ref === '') { + $ref = $this->module->cleanText(Tools::getValue('ref'), 64); + } + if ($ref === '') { + return ''; + } + + $orderInfo = $this->module->lookupOrderInfoByReference($ref); + if (!$orderInfo) { + return ''; + } + + $isoCode = Language::getIsoById((int) $this->context->language->id) ?: 'de'; + return (string) $this->module->computeWithdrawalDeadline($orderInfo['date_add'], $isoCode); + } + private function validateData(array $data) { $errors = []; diff --git a/simple_withdrawalbutton.php b/simple_withdrawalbutton.php index d47047f..90e71c8 100644 --- a/simple_withdrawalbutton.php +++ b/simple_withdrawalbutton.php @@ -50,6 +50,7 @@ class Simple_withdrawalbutton extends Module && $this->installTab() && $this->registerHook('displayFooter') && $this->registerHook('displayCustomerAccount') + && $this->registerHook('actionFrontControllerSetMedia') && Configuration::updateValue(self::CONF_SHOP_EMAIL, (string) Configuration::get('PS_SHOP_EMAIL')) && Configuration::updateValue(self::CONF_RATE_LIMIT, '5') && Configuration::updateValue(self::CONF_PRIVACY_URL, '') @@ -295,6 +296,20 @@ class Simple_withdrawalbutton extends Module return $this->display(__FILE__, 'views/templates/hook/customer_account.tpl'); } + public function hookActionFrontControllerSetMedia() + { + if ( + Tools::getValue('module') === $this->name + && Tools::getValue('controller') === 'withdraw' + ) { + $this->context->controller->registerStylesheet( + 'cyp-withdrawal', + 'modules/' . $this->name . '/views/css/withdrawal.css', + ['media' => 'all', 'priority' => 200] + ); + } + } + public function getWithdrawalLink() { return $this->context->link->getModuleLink($this->name, 'withdraw', [], true); @@ -342,6 +357,43 @@ class Simple_withdrawalbutton extends Module return $idOrder > 0 ? $idOrder : null; } + public function lookupOrderInfoByReference($orderReference) + { + $orderReference = $this->cleanText($orderReference, 64); + if ($orderReference === '') { + return null; + } + + $sql = 'SELECT `id_order`, `date_add`, `id_customer` + FROM `' . _DB_PREFIX_ . 'orders` + WHERE `reference` = "' . pSQL($orderReference) . '" + AND `id_shop` = ' . (int) $this->context->shop->id . ' + ORDER BY `id_order` DESC'; + + $row = Db::getInstance()->getRow($sql); + if (!$row) { + return null; + } + + return [ + 'id_order' => (int) $row['id_order'], + 'date_add' => $row['date_add'], + 'id_customer' => (int) $row['id_customer'], + ]; + } + + public function computeWithdrawalDeadline($orderDateAdd, $isoCode = 'de') + { + $timestamp = strtotime((string) $orderDateAdd); + if (!$timestamp) { + return null; + } + + $deadline = strtotime('+14 days', $timestamp); + + return ($isoCode === 'de') ? date('d.m.Y', $deadline) : date('d/m/Y', $deadline); + } + public function hashForPrivacy($value) { $value = trim((string) $value); diff --git a/upgrade/index.php b/upgrade/index.php new file mode 100644 index 0000000..30c3516 --- /dev/null +++ b/upgrade/index.php @@ -0,0 +1,8 @@ +registerHook('actionFrontControllerSetMedia'); +} diff --git a/views/templates/front/confirm.tpl b/views/templates/front/confirm.tpl index 8ac17fc..eedc841 100644 --- a/views/templates/front/confirm.tpl +++ b/views/templates/front/confirm.tpl @@ -70,6 +70,17 @@