php 我的模块未出现在已安装的挂接中

eqqqjvef  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(344)

我已经创建了一个模块,为每个产品添加额外的文本,可以从后台编辑,并将其文本保存在数据库中,以便在更新时保持保存状态,等等。
问题是,说的形式没有出现在任何已安装的挂钩,任何建议?谢谢!
验证码:

<?php

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

class MyModuleText extends Module
{
    protected $config_form = false;

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

        /**
         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
         */
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('My_Module_Text');
        $this->description = $this->l('Crea un texto editable desde el backoffice para cualquier producto de forma independiente');

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

    /**
     * Don't forget to create update methods if needed:
     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
     */
    public function install()
    {
        include(dirname(__FILE__).'/sql/install.php');

       // Register hooks
       $this->registerHook('displayProductTabContent');
       $this->registerHook('actionProductUpdate');
       $this->registerHook('displayProductAdditionalInfo');
       $this->registerHook('header');

       return parent::install()
           && Configuration::updateValue('MYMODULETEXT_MODULE_NAME', 'producttextmodule');
    }

    public function uninstall()
    {
        include(dirname(__FILE__).'/sql/uninstall.php');

        // Unregister hooks
        $this->unregisterHook('actionProductUpdate');
        $this->unregisterHook('displayProductAdditionalInfo');
        $this->unregisterHook('header');

        return parent::uninstall()
            && Configuration::deleteByName('MYMODULETEXT_MODULE_NAME');
    }

   
    public function hookDisplayProductTabContent($params)
    {
        $productText = $this->getProductText((int)$params['product']['id_product']);

        $this->smarty->assign(array(
            'productText' => $productText,
        ));

        return $this->context->smarty->fetch($this->local_path.'../producttabcontent.tpl');
    }

    

    public function hookActionProductUpdate($params)
    {
        // Save custom text for product
        if (Tools::isSubmit('submitProductTextModule')) {
            $id_product = (int)$params['id_product'];
            $text = pSQL(Tools::getValue('mymoduletext'));
            
            if (isset($text)) {
                $sql = 'REPLACE INTO `'._DB_PREFIX_.'mymoduletext` (`id_product`, `text`) 
                VALUES ('.$id_product.', "'.$text.'")';
    
                Db::getInstance()->execute($sql);
            }
        }
    }

    public function hookDisplayProductAdditionalInfo($params)
{
    // Get custom text for product
    $id_product = (int)$params['product']['id_product'];
    $sql = 'SELECT `text` FROM `'._DB_PREFIX_.'mymoduletext` WHERE `id_product` = '.(int)$id_product;
    $text = Db::getInstance()->getValue($sql);
    

    if ($text === false) {
        return '';
    }

    // Display custom text in product information block
    $this->context->smarty->assign(array(
        'mymoduletext' => $text,
    ));

    return $this->context->smarty->fetch($this->local_path.'../producttextmodule.tpl');
}

        
        public function hookHeader()
        {
            // Include CSS and JS files
            $this->context->controller->addCSS($this->_path.'views/css/producttextmodule.css');
            $this->context->controller->addJS($this->_path.'views/js/producttextmodule.js');
        }
        
        public function getContent()
        {
            // Handle form submission
            $output = '';
            if (Tools::isSubmit('submitProductTextModule')) {
                $my_module_name = strval(Tools::getValue('MYMODULETEXT_MODULE_NAME'));
                if (!$my_module_name
                    || empty($my_module_name)
                    || !Validate::isGenericName($my_module_name)
                ) {
                    $output .= $this->displayError($this->l('Invalid Configuration value'));
                } else {
                    Configuration::updateValue('MYMODULETEXT_MODULE_NAME', $my_module_name);
                    $output .= $this->displayConfirmation($this->l('Settings updated'));
                }
            }
        
            // Display form
            return $output.$this->displayForm();
        }
        
        public function displayForm()
        {
            // Get default language
            $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
        
            // Init Fields form array
            $fields_form[0]['form'] = array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                ),
                'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Configuration value'),
                        'name' => 'MYMODULETEXT_MODULE_NAME_MODULE_NAME',
                        'size' => 20,
                        'required' => true
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                    'class' => 'btn btn-default pull-right'
                )
            );
        
            $helper = new HelperForm();
        
            // Module, token and currentIndex
            $helper->module = $this;
            $helper->name_controller = $this->name;
            $helper->token = Tools::getAdminTokenLite('AdminModules');
            $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
        
            
            // Language
        
        $languages = Language::getLanguages(false);
        $helper->default_form_language = $default_lang;
        $helper->allow_employee_form_lang = $default_lang;
        $helper->languages = $languages;

            // Title and toolbar
    $helper->title = $this->displayName;
    $helper->show_toolbar = true;       
    $helper->toolbar_scroll = true;      
    $helper->submit_action = 'submitProductTextModule';
    $helper->toolbar_btn = array(
        'save' =>
            array(
                'desc' => $this->l('Save'),
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
            ),
        'back' => array(
            'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Back to list')
        )
    );

    // Load current value
    $helper->fields_value['MYMODULETEXT_MODULE_NAME'] = Configuration::get('MYMODULETEXT_MODULE_NAME');

    return $helper->generateForm($fields_form);
}
}

?>

<div class="product-text-module">
    <form action="{$link->getAdminLink('AdminProducts')}&id_product={$product.id_product}&updateproduct" method="post">
        <textarea name="mymoduletext" id="mymoduletext">{$productText}</textarea>
        <button type="submit" name="submitProductTextModule" class="btn btn-default">{l s='Save'}</button>
    </form>
</div>

{literal}
<style>

.product-text-module {
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 5px;
    margin-bottom: 20px;
}
</style>
{/literal}



<div class="product-text-module">
    <p>{$mymoduletext}</p>
</div>

{literal}
<style>

.product-text-module {
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 5px;
    margin-bottom: 20px;
}

</style>
{/literal}
All sugestions accepted
svmlkihl

svmlkihl1#

如果您的模块安装正确的挂钩,尝试这个:

$this->fetch('module:producttextmodule/producttextmodule.tpl');

相关问题