Stefanxx Max
Stefanxx Max

Reputation: 25

Example module how to save data in the database

I cloned a module of prestashop 1.7 and everything works perfectly without any problem. What I would like to do (please) is save the data in the prestashop database itself but I can't do it I hope I have explained myself better

code

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class Stagenti extends Module
{
    protected $config_form = false;

    public function __construct()
    {
        $this->name = 'stagenti';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Lab';
        $this->need_instance = 0;

        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Lab Agenti');
        $this->description = $this->l('Gestionale per agenti di commercio');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    public function install()
    {
        if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL);
        $languages = Language::getLanguages(false);

        foreach ($languages as $lang) {
            //$values[] = Tools::getValue('SOMETEXT_TEXT_'.$lang['id_lang']);
            Configuration::updateValue('STAGENTI_TEXT_' . $lang['id_lang'], '', true);
            Configuration::updateValue('STAGENTI_TEXT2_' . $lang['id_lang'], '', true);
        }
        include(dirname(__FILE__).'/sql/install.php');

        return parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('backOfficeHeader') &&
            $this->registerHook('displayBanner');
    }

    public function uninstall()
    {

        $languages = Language::getLanguages(false);

        foreach ($languages as $lang) {
            //$values[] = Tools::getValue('SOMETEXT_TEXT_'.$lang['id_lang']);
            Configuration::deleteByName('STAGENTI_TEXT_' . $lang['id_lang']);
            Configuration::deleteByName('STAGENTI_TEXT2_' . $lang['id_lang']);
        }
        include(dirname(__FILE__).'/sql/uninstall.php');

        return parent::uninstall();
    }

    public function getContent()
    {
        if (((bool)Tools::isSubmit('submitStagentiModule')) == true) {
            $this->postProcess();
        }

        $this->context->smarty->assign('module_dir', $this->_path);

        $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

        return $output . $this->renderForm();
    }

    protected function renderForm()
    {
        $helper = new HelperForm();

        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $helper->module = $this;
        $helper->default_form_language = $this->context->language->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitStagentiModule';
        $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(
            'fields_value' => $this->getConfigFormValues(),
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );

        return $helper->generateForm(array($this->getConfigForm()));
    }

    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Impostazioni'),
                    'icon' => 'icon-cogs',
                ),
                'input' => array(
                  array(
                      'col' => 9,
                      'type' => 'text',
                      'desc' => $this->l('inserisci lo slogan che desideri far visualizzare'),
                      'name' => 'STAGENTI_TEXT2',
                      'label' => $this->l('Messaggio Slogan'),
                      'autoload_rte' => true,
                      'lang' => true
                  ),
                    array(
                        'col' => 9,
                        'type' => 'textarea',
                        'desc' => $this->l('se inserisci una img le misure devono essere 1140 x 270 px'),
                        'name' => 'STAGENTI_TEXT',
                        'label' => $this->l('Messaggio riepilogo'),
                        'autoload_rte' => true,
                        'lang' => true
                    ),

                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }
    protected function getConfigFormValues()
    {
        $languages = Language::getLanguages(false);
        $values = array();
        foreach ($languages as $lang) {
            $values['STAGENTI_TEXT'][$lang['id_lang']] = Configuration::get('STAGENTI_TEXT_' . $lang['id_lang']);
            $values['STAGENTI_TEXT2'][$lang['id_lang']] = Configuration::get('STAGENTI_TEXT2_' . $lang['id_lang']);
        }
        return $values;
    }
    protected function postProcess()
    {
        $languages = Language::getLanguages(false);
        foreach ($languages as $lang) {
            Configuration::updateValue('STAGENTI_TEXT_' . $lang['id_lang'], Tools::getValue('STAGENTI_TEXT_' . $lang['id_lang']), true);
            Configuration::updateValue('STAGENTI_TEXT2_' . $lang['id_lang'], Tools::getValue('STAGENTI_TEXT2_' . $lang['id_lang']), true);
        }
    }
    public function hookBackOfficeHeader()
    {
        if (Tools::getValue('module_name') == $this->name) {
            $this->context->controller->addJS($this->_path . 'views/js/back.js');
            $this->context->controller->addCSS($this->_path . 'views/css/back.css');
        }
    }
    public function hookHeader()
    {
        $this->context->controller->addJS($this->_path . 'views/js/front.js');
        $this->context->controller->addCSS($this->_path . 'views/css/front.css');
    }
    public function hookDisplayBanner()
    {
        $some_string = Configuration::get('STAGENTI_TEXT_' . $this->context->language->id);
        $this->context->smarty->assign([
            'module_dir' => $this->_path,
            'stagenti_message' => $some_string
        ]);
$some_string = Configuration::get('STAGENTI_TEXT2_' . $this->context->language->id);
        $this->context->smarty->assign([
            'module_dir' => $this->_path,
            'stagenti_message5' => $some_string
        ]);
        return $this->display(__FILE__, 'message.tpl');
    }
}

database

<?php

$sql = array();

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . '` (
    `id_stagenti` int(11) NOT NULL AUTO_INCREMENT,
    `stagenti_text` text NOT NULL,
    `stagenti_text2` text NOT NULL,
    PRIMARY KEY  (`id_infomess`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

foreach ($sql as $query) {
    if (Db::getInstance()->execute($query) == false) {
        return false;
    }
}

Upvotes: 0

Views: 1018

Answers (1)

Boryss96
Boryss96

Reputation: 186

Db::getInstance()->insert('your_table', array(
    'stagenti_text' => pSQL($value1),
    'stagenti_text2' => pSQL($value2),
));

Upvotes: 1

Related Questions