(main): Add module

This commit is contained in:
2026-06-22 09:45:20 +02:00
parent e8b381b132
commit 763745ee1b
17 changed files with 620 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Websmid\GiftDropdown\Config;
class Config
{
const HOOKS = [
'actionGetExtraMailTemplateVars',
'actionValidateOrder',
'actionFrontControllerSetMedia',
'displayAdminOrderMainBottom',
'displayHeader',
'displayPDFInvoice',
'displayPDFDeliverySlip',
];
const TABS = [];
const CONFIG_KEY = 'WS_GIFT_OPTIONS';
const DEFAULT_OPTIONS = [
'Sterren',
'Bloemen',
'Strepen',
'Verrassing',
];
}
+90
View File
@@ -0,0 +1,90 @@
<?php
namespace Websmid\GiftDropdown\Module;
use Websmid\GiftDropdown\Config\Config;
use Configuration;
use Db;
use Media;
use Order;
use Tools;
trait AdminHooks
{
public function isUsingNewTranslationSystem()
{
return true;
}
public function getContent()
{
$confirmation = '';
if (Tools::isSubmit('submitWsGiftDropdown')) {
$raw = Tools::getValue('gift_options', '');
$lines = array_filter(array_map('trim', explode("\n", $raw)));
$options = array_values($lines);
Configuration::updateValue(Config::CONFIG_KEY, json_encode($options));
$confirmation = $this->displayConfirmation($this->trans('Settings saved.', [], 'Admin.Notifications.Success'));
}
$saved = Configuration::get(Config::CONFIG_KEY);
$options = $saved ? json_decode($saved, true) : Config::DEFAULT_OPTIONS;
$this->context->smarty->assign([
'gift_options' => implode("\n", $options),
'form_action' => $this->context->link->getAdminLink('AdminModules') . '&configure=' . $this->name,
'confirmation' => $confirmation,
]);
return $this->fetch('module:ws_giftdropdown/views/templates/admin/configuration.tpl');
}
public function hookActionFrontControllerSetMedia()
{
if (Tools::getValue('module') !== 'thecheckout' && $this->context->controller->php_self !== 'order') {
return;
}
$ajaxUrl = $this->context->link->getModuleLink($this->name, 'savepaper', [], true);
Media::addJsDef(['wsGiftDropdownAjaxUrl' => $ajaxUrl]);
$this->context->controller->registerStylesheet(
'ws-giftdropdown-css',
'modules/' . $this->name . '/views/css/giftwrap.css'
);
$this->context->controller->registerJavascript(
'ws-giftdropdown-js',
'modules/' . $this->name . '/views/js/giftwrap.js',
['position' => 'bottom', 'priority' => 200]
);
}
public function hookDisplayAdminOrderMainBottom(array $params)
{
$idOrder = (int) $params['id_order'];
$row = Db::getInstance()->getRow(
'SELECT `paper_choice` FROM `' . _DB_PREFIX_ . 'ws_gift_dropdown`
WHERE `id_order` = ' . (int) $idOrder
);
if (!$row) {
return '';
}
$order = new Order($idOrder);
if (!(bool) $order->gift) {
return '';
}
$this->context->smarty->assign([
'gift_paper_choice' => $row['paper_choice'],
]);
$this->context->controller->addJS($this->getPathUri() . 'views/js/admin_order.js');
return $this->fetch('module:ws_giftdropdown/views/templates/admin/order_detail.tpl');
}
}
+100
View File
@@ -0,0 +1,100 @@
<?php
namespace Websmid\GiftDropdown\Module;
use Websmid\GiftDropdown\Config\Config;
use Configuration;
use Db;
use Order;
use Tools;
use OrderInvoice;
trait DisplayHooks
{
public function hookDisplayHeader()
{
if (Tools::getValue('module') !== 'thecheckout' && $this->context->controller->php_self !== 'order') {
return;
}
$saved = Configuration::get(Config::CONFIG_KEY);
$options = $saved ? json_decode($saved, true) : Config::DEFAULT_OPTIONS;
$this->context->smarty->assign('gift_paper_options', $options);
return $this->display(__FILE__, 'views/templates/hook/your_template.tpl');
}
public function hookActionGetExtraMailTemplateVars(array $params): void
{
$paperChoice = $this->loadGiftPaperChoice((int) $params['template_vars']['{id_order}']);
if ($paperChoice === null) {
return;
}
$params['extra_template_vars']['{ws_gift_paper_choice}'] = $paperChoice;
}
public function hookActionValidateOrder(array $params)
{
/** @var Order $order */
$order = $params['order'];
$idCart = (int) $params['cart']->id;
if (!(bool) $order->gift) {
Db::getInstance()->execute(
'DELETE FROM `' . _DB_PREFIX_ . 'ws_gift_dropdown`
WHERE `id_cart` = ' . (int) $idCart . ' AND `id_order` = 0'
);
return;
}
Db::getInstance()->execute(
'UPDATE `' . _DB_PREFIX_ . 'ws_gift_dropdown`
SET `id_order` = ' . (int) $order->id . '
WHERE `id_cart` = ' . (int) $idCart
);
$paperChoice = Db::getInstance()->getValue(
'SELECT `paper_choice` FROM `' . _DB_PREFIX_ . 'ws_gift_dropdown`
WHERE `id_cart` = ' . (int) $idCart
);
if ($paperChoice) {
$note = 'Cadeaupapier: ' . $paperChoice;
$order->note = $order->note ? $order->note . "\n" . $note : $note;
$order->update();
}
}
public function hookDisplayPDFDeliverySlip(array $params)
{
return $this->hookDisplayPDFInvoice($params);
}
public function hookDisplayPDFInvoice(array $params)
{
/** @var OrderInvoice $orderInvoice */
$orderInvoice = $params['object'];
$idOrder = (int) $orderInvoice->id_order;
$paperChoice = Db::getInstance()->getValue(
'SELECT `paper_choice` FROM `' . _DB_PREFIX_ . 'ws_gift_dropdown`
WHERE `id_order` = ' . (int) $idOrder
);
$params['smarty']->assign('gift_paper_choice', $paperChoice ?: '');
return '';
}
private function loadGiftPaperChoice(int $idOrder): ?string
{
$value = Db::getInstance()->getValue(
'SELECT `paper_choice` FROM `' . _DB_PREFIX_ . 'ws_gift_dropdown`
WHERE `id_order` = ' . $idOrder
);
return $value !== false ? (string) $value : null;
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
namespace Websmid\GiftDropdown\Module;
use Websmid\GiftDropdown\Config\Config;
use Configuration;
use Db;
use Module;
trait Install
{
public function install()
{
if (!parent::install()) {
return false;
}
if (!$this->checkDependency()) {
return false;
}
if (!$this->createTables()) {
return false;
}
Configuration::updateValue(Config::CONFIG_KEY, json_encode(Config::DEFAULT_OPTIONS));
if (!$this->registerHooks()) {
return false;
}
return true;
}
private function checkDependency(): bool
{
$isInstalled = (bool) Db::getInstance()->getValue(
'SELECT `active` FROM `' . _DB_PREFIX_ . 'module` WHERE `name` = "thecheckout"'
);
if (!$isInstalled) {
$this->_errors[] = $this->trans(
'This module requires TheCheckout to be installed.',
[],
'Modules.Ws_GiftDropdown.Admin'
);
return false;
}
return true;
}
private function createTables()
{
$sql = '
CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'ws_gift_dropdown` (
`id_ws_gift_dropdown` INT(11) NOT NULL AUTO_INCREMENT,
`id_cart` INT(11) NOT NULL DEFAULT 0,
`id_order` INT(11) NOT NULL DEFAULT 0,
`paper_choice` VARCHAR(64) NOT NULL,
`date_add` DATETIME NOT NULL,
PRIMARY KEY (`id_ws_gift_dropdown`),
UNIQUE KEY `id_cart` (`id_cart`),
KEY `id_order` (`id_order`)
);
';
return Db::getInstance()->execute($sql);
}
public function registerHooks()
{
foreach (Config::HOOKS as $hook) {
if (!$this->registerHook($hook)) {
return false;
}
}
return true;
}
}