Jump to content

modulo prestashop 1.7 salvare dati nel database


AlexTed

Recommended Posts

ciao ragazzi ho creato un modulo (creato è una parolona forse) ma funziona e fa il suo scopo quindi ne sono contento.

Cosa fa questo modulo:

non fa altro che inserire del testo, immagine prima del carrello niente di più

cosa vorrei ottenere:

vorrei che le informazioni che inserisco nel campo text vengano salvate nel database riesco a creare le tabelle ma non so come fare per salvare 

vi posto il mio codice se volete lo potete pure utilizzare, modificare come meglio credete ma per favore aiutatemi a salvare i dati nel database

grazie

<?php

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

class Cartmess extends Module
{
    protected $config_form = false;

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

        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Cart Message');
        $this->description = $this->l('Visualizza Messaggio nel Carrello Cliente');

        $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) {
            Configuration::updateValue('CARTMESS_CART_TEXT_' . $lang['id_lang'], '', true);
        }
        include(dirname(__FILE__).'/sql/install.php');

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

    public function uninstall()
    {

        $languages = Language::getLanguages(false);

        foreach ($languages as $lang) {
            Configuration::deleteByName('CARTMESS_CART_TEXT_' . $lang['id_lang']);
        }
        include(dirname(__FILE__).'/sql/uninstall.php');

        return parent::uninstall();
    }

    public function getContent()
    {
        if (((bool)Tools::isSubmit('submitCartmessModule')) == 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 = 'submitCartmessModule';
        $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' => 'textarea',
                        'desc' => $this->l('Questo messaggio verrà visualizzato sopra il riepilogo del carrello.'),
                        'name' => 'CARTMESS_CART_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['CARTMESS_CART_TEXT'][$lang['id_lang']] = Configuration::get('CARTMESS_CART_TEXT_' . $lang['id_lang']);

        }
        return $values;

    }

    protected function postProcess()
    {
        $languages = Language::getLanguages(false);
        foreach ($languages as $lang) {
            Configuration::updateValue('CARTMESS_CART_TEXT_' . $lang['id_lang'], Tools::getValue('CARTMESS_CART_TEXT_' . $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 hookDisplayShoppingCart()
    {

        $some_string = Configuration::get('CARTMESS_CART_TEXT_' . $this->context->language->id);

        $this->context->smarty->assign([
            'module_dir' => $this->_path,
            'cart_message' => $some_string
        ]);

        return $this->display(__FILE__, 'message.tpl');

    }

    public function hookDisplayCheckoutSummaryTop()
    {

        $some_string = Configuration::get('CARTMESS_CART_TEXT_' . $this->context->language->id);

        $this->context->smarty->assign([
            'module_dir' => $this->_path,
            'cart_message' => $some_string
        ]);

        return $this->display(__FILE__, 'message.tpl');

    }
}

questo invece crea le tabelle

<?php

$sql = array();

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

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

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...