Files
interblue.pl/modules/sposoby_dostawy/sposoby_dostawy.php
2024-10-25 14:16:28 +02:00

196 lines
6.5 KiB
PHP

<?php
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class sposoby_dostawy extends Module implements WidgetInterface
{
private $templateFile;
public function __construct()
{
$this->name = 'sposoby_dostawy';
$this->version = '1.0';
$this->author = 'Dominik Wójcicki';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Sposoby dostawy', array(), 'Modules.sposoby_dostawy.Admin');
$this->description = $this->trans('Moduł wyświetlający wybrane sposoby dostawy na karcie produktu.', array(), 'Modules.sposoby_dostawy.Admin');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->templateFile = 'module:sposoby_dostawy/sposoby_dostawy.tpl';
}
public function install()
{
return (parent::install() &&
$this->registerHook('displayHome') &&
$this->registerHook('actionObjectLanguageAddAfter') &&
$this->installFixtures() &&
$this->disableDevice(Context::DEVICE_MOBILE));
}
protected function installFixture($id_lang, $image = null)
{
$values['ID_SPOSOBU_DOSTAWY'][(int)$id_lang] = '';
Configuration::updateValue('ID_SPOSOBU_DOSTAWY', $values['ID_SPOSOBU_DOSTAWY']);
}
public function uninstall()
{
Configuration::deleteByName('ID_SPOSOBU_DOSTAWY');
return parent::uninstall();
}
public function postProcess()
{
if (Tools::isSubmit('submitStoreConf')) {
$languages = Language::getLanguages(false);
$values = array();
$update_images_values = false;
foreach ($languages as $lang) {
$values['ID_SPOSOBU_DOSTAWY'][$lang['id_lang']] = Tools::getValue('ID_SPOSOBU_DOSTAWY_'.$lang['id_lang']);
}
Configuration::updateValue('ID_SPOSOBU_DOSTAWY', $values['ID_SPOSOBU_DOSTAWY']);
$this->_clearCache($this->templateFile);
return $this->displayConfirmation($this->trans('The settings have been updated.', array(), 'Admin.Notifications.Success'));
}
return '';
}
public function getContent()
{
return $this->postProcess().$this->renderForm();
}
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->trans('Settings', array(), 'Admin.Global'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'lang' => true,
'label' => $this->trans('Id sposobów dostawy', array(), 'Modules.sposoby_dostawy.Admin'),
'name' => 'ID_SPOSOBU_DOSTAWY',
'desc' => $this->trans('Podaj numer Id sposobu dostawy. Możesz podać kilka Id rozdzielając je przecinkiem.', array(), 'Modules.sposoby_dostawy.Admin')
)
),
'submit' => array(
'title' => $this->trans('Save', array(), 'Admin.Actions')
)
),
);
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->default_form_language = $lang->id;
$helper->module = $this;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitStoreConf';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'uri' => $this->getPathUri(),
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
public function getConfigFieldsValues()
{
$languages = Language::getLanguages(false);
$fields = array();
foreach ($languages as $lang) {
$fields['ID_SPOSOBU_DOSTAWY'][$lang['id_lang']] = Tools::getValue('ID_SPOSOBU_DOSTAWY_'.$lang['id_lang'], Configuration::get('ID_SPOSOBU_DOSTAWY', $lang['id_lang']));
}
return $fields;
}
public function renderWidget($hookName, array $params)
{
if (!$this->isCached($this->templateFile, $this->getCacheId('sposoby_dostawy'))) {
$this->smarty->assign($this->getWidgetVariables($hookName, $params));
}
return $this->fetch($this->templateFile, $this->getCacheId('sposoby_dostawy'));
}
public function hookdisplayProductTab($params)
{
return $this->display(__FILE__, 'sposoby_dostawy_product_tab_name.tpl');
}
public function getWidgetVariables($hookName, array $params) {
$id_sposobu_dostawy = Configuration::get('ID_SPOSOBU_DOSTAWY', $this->context->language->id);
$sql = "SELECT DISTINCT
ps_carrier.*,
ps_carrier_lang.delay,
ps_delivery.price,
ps_carrier_tax_rules_group_shop.id_tax_rules_group,
ps_tax_rules_group.id_tax_rules_group,
ps_tax_rules_group.name AS TaxName,
ps_tax_rule.id_tax_rules_group,
ps_tax_rule.id_tax,
ps_tax.id_tax,
ps_tax.rate
FROM
ps_carrier,
ps_carrier_lang,
ps_delivery,
ps_carrier_tax_rules_group_shop,
ps_tax_rules_group,
ps_tax_rule,
ps_tax
WHERE
ps_carrier.active = 1 AND
ps_carrier.deleted = 0 AND
ps_carrier.id_carrier IN (". $id_sposobu_dostawy .") AND
ps_carrier_lang.id_carrier = ps_carrier.id_carrier AND
ps_delivery.id_carrier = ps_carrier.id_carrier AND
ps_carrier_tax_rules_group_shop.id_carrier = ps_carrier.id_carrier AND
ps_delivery.id_carrier = ps_carrier_lang.id_carrier AND
ps_carrier_tax_rules_group_shop.id_tax_rules_group = ps_tax_rules_group.id_tax_rules_group AND
ps_tax_rules_group.id_tax_rules_group = ps_tax_rule.id_tax_rules_group AND
ps_tax_rule.id_tax = ps_tax.id_tax ORDER BY position ASC";
$results = Db::getInstance()->ExecuteS($sql);
$sposoby_dostawy = $results;
$this->smarty->assign('sposoby_dostawy', $sposoby_dostawy);
}
}