Files
interblue.pl/modules/empikmarketplace/controllers/admin/AdminEmpikOrdersController.php
2025-05-30 09:08:26 +02:00

284 lines
11 KiB
PHP

<?php
use Empik\Marketplace\Adapter\ConfigurationAdapter;
use Empik\Marketplace\Adapter\LinkAdapter;
use Empik\Marketplace\API\EmpikClient;
use Empik\Marketplace\DataProvider\CarriersDataProvider;
use Empik\Marketplace\Factory\EmpikClientFactory;
use Empik\Marketplace\Manager\CarrierMapManager;
use Empik\Marketplace\Validator\Validator;
class AdminEmpikOrdersController extends ModuleAdminController
{
/** @var EmpikMarketplace */
public $module;
/** @var EmpikClientFactory */
private $empikClientFactory;
/** @var CarrierMapManager */
protected $carrierMapManager;
/** @var LinkAdapter */
protected $link;
public function __construct()
{
parent::__construct();
$this->bootstrap = true;
$this->empikClientFactory = $this->module->getService('empik.marketplace.factory.empikClientFactory');
$this->carrierMapManager = $this->module->getService('empik.marketplace.manager.carrierMapManager');
$this->link = $this->module->getService('empik.marketplace.adapter.link');
}
public function init()
{
if (Tools::getIsset('submitOrderForm') || Tools::getIsset('submitOrderFormShipping')) {
$this->handleForm();
}
parent::init();
}
public function initContent()
{
$this->display = 'edit';
parent::initContent();
}
public function handleForm()
{
$result = true;
if (Tools::getIsset('submitOrderForm')) {
$importOrders = Tools::getValue('import_orders');
$autoAcceptOrders = Tools::getValue('auto_accept_orders');
$newOrderState = Tools::getValue('new_order_state');
$shippedOrderState = Tools::getValue('shipped_order_state');
if (!Validator::isBool($importOrders)) {
$this->errors[] = $this->l('Invalid Import orders field');
}
if (!Validator::isBool($autoAcceptOrders)) {
$this->errors[] = $this->l('Invalid Auto accept orders field');
}
if (!Validator::isInt($newOrderState)) {
$this->errors[] = $this->l('Invalid New order state field');
}
if (!Validator::isInt($shippedOrderState)) {
$this->errors[] = $this->l('Invalid Shipped order state field');
}
if (empty($this->errors)) {
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_IMPORT_ORDERS, (int)$importOrders);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_AUTO_ACCEPT_ORDERS, (int)$autoAcceptOrders);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_NEW_ORDER_STATE, (int)$newOrderState);
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_SHIPPED_ORDER_STATE, (int)$shippedOrderState);
}
}
if (Tools::getIsset('submitOrderFormShipping')) {
$result &= $this->carrierMapManager->store(Tools::getValue('carrier', []));
}
if ($result && empty($this->errors)) {
Tools::redirectAdmin(
$this->link->getAdminLink($this->controller_name, ['conf' => 4])
);
}
$this->errors[] = $this->l('Error saving form.');
}
public function renderForm()
{
if (!$this->module->getAuthStatus()) {
$this->errors[] = $this->l('Before starting, set up the connection');
return false;
}
return
$this->renderOrderForm().
$this->renderShippingMappingForm();
}
public function renderOrderForm()
{
$language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$orderStates = OrderState::getOrderStates($language->id);
$formFields = [
'form' => [
'legend' => [
'title' => $this->l('Orders'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'switch',
'label' => $this->l('Import orders from Empik Marketplace'),
'hint' => $this->l('Import orders from Empik Marketplace'),
'name' => 'import_orders',
'is_bool' => true,
'values' => [
[
'id' => 'import_orders_on',
'value' => 1,
'label' => $this->l('Yes'),
],
[
'id' => 'import_orders_off',
'value' => 0,
'label' => $this->l('No'),
],
],
],
[
'type' => 'switch',
'label' => $this->l('Auto accept orders in Empik Marketplace'),
'hint' => $this->l('Auto accept orders in Empik Marketplace'),
'name' => 'auto_accept_orders',
'is_bool' => true,
'values' => [
[
'id' => 'auto_accept_orders_on',
'value' => 1,
'label' => $this->l('Yes'),
],
[
'id' => 'auto_accept_orders_off',
'value' => 0,
'label' => $this->l('No'),
],
],
],
[
'type' => 'select',
'label' => $this->l('New order state'),
'hint' => $this->l('New order state'),
'name' => 'new_order_state',
'options' => [
'query' => $orderStates,
'id' => 'id_order_state',
'name' => 'name',
],
'class' => 'fixed-width-xxl',
],
[
'type' => 'select',
'label' => $this->l('Shipped order state'),
'hint' => $this->l('Shipped order state'),
'name' => 'shipped_order_state',
'options' => [
'query' => $orderStates,
'id' => 'id_order_state',
'name' => 'name',
],
'class' => 'fixed-width-xxl',
],
],
'buttons' => [],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->default_form_language = $language->id;
$helper->submit_action = 'submitOrderForm';
$helper->currentIndex = $this->link->getAdminLink($this->controller_name);
$helper->token = Tools::getAdminTokenLite('AdminEmpikOrders');
$helper->tpl_vars = [
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
$helper->tpl_vars['fields_value']['import_orders'] = Tools::getValue('import_orders', (int)Configuration::get(ConfigurationAdapter::CONF_IMPORT_ORDERS));
$helper->tpl_vars['fields_value']['auto_accept_orders'] = Tools::getValue('auto_accept_orders', (int)Configuration::get(ConfigurationAdapter::CONF_AUTO_ACCEPT_ORDERS));
$helper->tpl_vars['fields_value']['new_order_state'] = Tools::getValue('new_order_state', (int)Configuration::get(ConfigurationAdapter::CONF_NEW_ORDER_STATE));
$helper->tpl_vars['fields_value']['shipped_order_state'] = Tools::getValue('shipped_order_state', (int)Configuration::get(ConfigurationAdapter::CONF_SHIPPED_ORDER_STATE));
return $helper->generateForm([$formFields]);
}
public function renderShippingMappingForm()
{
$language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
/** @var EmpikClient $client */
$client = $this->empikClientFactory->createClient();
try {
$responseShippingTypes = $client->getShippingTypes();
$responseCarriers = $client->getCarriers();
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
return false;
}
$shopCarriers = (new CarriersDataProvider())->getCarriers();
$this->context->smarty->assign(
[
'empik_carriers' => $responseCarriers['carriers'],
'empik_shipping_types' => $responseShippingTypes['shipping_types'],
'shop_carriers' => $shopCarriers,
'map' => $this->carrierMapManager->loadFromConfig(),
]
);
$formFields = [
'form' => [
'legend' => [
'title' => $this->l('Shipping'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'html',
'label' => $this->l('Shipping mapping'),
'name' => 'html_data',
'html_content' => $this->context->smarty->fetch(
$this->module->getLocalPath().'/views/templates/admin/_partials/shipping_table.tpl'
),
],
],
'buttons' => [],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->default_form_language = $language->id;
$helper->submit_action = 'submitOrderFormShipping';
$helper->currentIndex = $this->link->getAdminLink($this->controller_name);
$helper->token = Tools::getAdminTokenLite('AdminEmpikOrders');
$helper->tpl_vars = [
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([$formFields]);
}
protected function l($string, $class = null, $addslashes = false, $htmlentities = true)
{
return Translate::getModuleTranslation($this->module, $string, get_class($this), null, $addslashes);
}
}