Jump to content

[PS 1.7] - Module not save in sql


herwaldi
 Share

Recommended Posts

Hi,

why my module don't save data to sql? :(

Field is visible on sql..

image.thumb.png.8536ad6dbc9cb2a217bba69ad85be216.png

My code module:

<?php
/*
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2016 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

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

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Validation;

class Ps_Customfields extends Module implements WidgetInterface
{
    private $templateFile;

    const BLOCK_CUSTOM = [
        'toptext',
    ];

    public function __construct()
    {
        $this->name = 'ps_customfields';
        $this->author = 'Intermedia';
        $this->version = '1.0.0';

        $this->bootstrap = true;
        parent::__construct();

        $this->displayName = $this->trans('Custom fields', array(), 'Modules.Customfields.Admin');
        $this->description = $this->trans('Custom fields for theme.', array(), 'Modules.Customfields.Admin');

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

        $this->templateFile = 'module:ps_customfields/ps_customfields.tpl';
    }

    public function install()
    {
        return (parent::install() &&
            Configuration::updateValue('BLOCKCUSTOM_TOPTEXT', '') &&
            $this->registerHook('displayFooter'));
    }

    public function uninstall()
    {
        return (Configuration::deleteByName('BLOCKCUSTOM_TOPTEXT') &&
            parent::uninstall());
    }

    public function getContent()
    {
        if (Tools::isSubmit('submitModule')) {
            $this->updateFields();
            $this->_clearCache('*');

            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&tab_module='.$this->tab.'&conf=4&module_name='.$this->name);
        }

        return $this->renderForm();
    }

    public function _clearCache($template, $cache_id = null, $compile_id = null)
    {
        parent::_clearCache($this->templateFile);
    }

    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',
                        'label' => $this->trans('Top Text', array(), 'Modules.Customfields.Admin'),
                        'name' => 'blockcustom_toptext',
                    ),
                ),
                'submit' => array(
                    'title' => $this->trans('Save', array(), 'Admin.Global'),
                )
            ),
        );

        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table =  $this->table;
        $helper->submit_action = 'submitModule';
        $helper->tpl_vars = array(
            'fields_value' => $this->getConfigFieldsValues(),
        );
        return $helper->generateForm(array($fields_form));
    }

    public function getConfigFieldsValues()
    {
        $result = [];
        foreach (static::BLOCK_CUSTOM as $custom) {
            $result['blockcustom_' . $custom] = Configuration::get('BLOCKCUSTOM_' . strtoupper($custom));
        }
        return $result;
    }

    public function renderWidget($hookName = null, array $configuration = [])
    {
        if (!$this->isCached($this->templateFile, $this->getCacheId('ps_customfields'))) {
            $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
        }

        return $this->fetch($this->templateFile, $this->getCacheId('ps_customfields'));
    }

    public function getWidgetVariables($hookName = null, array $configuration = [])
    {
        $block_custom = array();

        if ($sf_toptext = Configuration::get('BLOCKCUSTOM_TOPTEXT')) {
            $block_custom['toptext'] = array(
                'label' => $this->trans('Top Text', array(), 'Modules.Customfields.Shop'),
                'class' => 'toptext',
                'url' => $sf_toptext,
            );
        }

        return array(
            'social_links' => $block_custom,
        );
    }

    protected function updateFields()
    {
        $validator = Validation::createValidator();
        $constraints = [new Url()];

        foreach (static::BLOCK_CUSTOM as $custom) {
            $value = Tools::getValue('blockcustom_' . $custom, '');
            $violations = $validator->validate($value, $constraints);
            if (0 === count($violations)) {
                Configuration::updateValue('BLOCKCUSTOM_' . strtoupper($custom), $value);
            }
        }
    }
}

image.thumb.png.155191cf82a0facd09a6d00257984fba.png

image.png.e9e7a8e0b5c97d85ce8443debf134dee.png

image.png.0fc0b8e2f127f7698265ed18fe6e4a1a.png

Edited by herwaldi (see edit history)

Share this post


Link to post
Share on other sites

From your getContent method you have to call the postProcess() method where you add your logic for writing to DB.

Prestashop specifically says that "POST requests will be managed from the method postProcess()." 

  • Like 1

Share this post


Link to post
Share on other sites

On 4/21/2021 at 9:21 AM, Rhobur said:

From your getContent method you have to call the postProcess() method where you add your logic for writing to DB.

Prestashop specifically says that "POST requests will be managed from the method postProcess()." 

ok bro, add code - its work :)

if (Tools::isSubmit('submitModule')) {

            foreach (static::CUSTOM_FIELDS as $custom) {
                $value = Tools::getValue('blockcustom_' . $custom, '');
                Configuration::updateValue('BLOCKCUSTOM_' . strtoupper($custom), $value);
            }
            $this->_clearCache('*');

            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&tab_module='.$this->tab.'&conf=4&module_name='.$this->name);
        }

but on saving, it doesn't add me <strong> in input :(

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More