Jump to content

Problème hook module réassurance


Recommended Posts

Bonjour,
En plein travail du thème pour améliorer la réassurance, j'aimerais déplacer le module réassurance (footer) vers le haut de la boutique (cf capture d'écran voulu). Or, ce hook n'est pas prévu.
Ma question : comment m'y prendre ?
J'ai fait des recherches et il faut modifier le fichier PHP du module :

<?php
/**
 * Block Reinsurance Custom Prestashop module
 *
 * @author    Prestaddons <[email protected]>
 * @copyright 2014 Prestaddons
 * @license
 * @link      http://www.prestaddons.fr
 */

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

include_once _PS_MODULE_DIR_.'blockreinsurancecustom/reinsurancecustomClass.php';

class BlockReinsuranceCustom extends Module
{
    /** @var string html Output */
    protected $html = '';

    /** @var array post_errors Errors on forms */
    protected $post_errors = array();

    /** @var bool $is_version15 Prestashop is under 1.5 version */
    public $is_version15;

    /** @var bool $is_version16 Prestashop is under 1.6 version */
    public $is_version16;

    public function __construct()
    {
        $this->name = 'blockreinsurancecustom';
        $this->tab = 'front_office_features';
        $this->version = '1.1.1';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.7');
        $this->bootstrap = true;
        $this->module_key = '';

        parent::__construct();

        $this->displayName = $this->l('Reassurance block Custom');
        $this->description = $this->l('Adds an information block aimed at offering helpful information to reassure customers that your store is trustworthy');
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall the Block Reinsurance Custom module?');
        $this->author = $this->l('Prestaddons');
        $this->contact = '[email protected]';
        $this->is_version15 = $this->checkPSVersion('1.5.0.0');
        $this->is_version16 = $this->checkPSVersion();
    }

    public function install()
    {
        if (!parent::install() || !$this->installDB() || !$this->registerHook('header') || !$this->installFixtures()) {
            return false;
        }

        if ($this->is_version16) {
            // Disable on mobiles and tablets
            if (!$this->registerHook('home') || !$this->disableDevice(Context::DEVICE_TABLET | Context::DEVICE_MOBILE)) {
                return false;
            }
        } else {
            if (!$this->registerHook('footer')) {
                return false;
            }
        }

        return true;
    }

    private function installDB()
    {
        $return = true;
        $return &= Db::getInstance()->execute('
			CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'reinsurance_custom` (
				`id_reinsurance_custom` INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `id_shop` int(11) unsigned NOT NULL,
				`icon_name` VARCHAR(100) NOT NULL,
				`new_window` BOOL NOT NULL DEFAULT 0,
				PRIMARY KEY (`id_reinsurance_custom`)
			) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');

        $return &= Db::getInstance()->execute('
			CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'reinsurance_custom_lang` (
				`id_reinsurance_custom` INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `id_lang` int(10) unsigned NOT NULL,
                `id_shop` int(11) unsigned NOT NULL,
                `title` VARCHAR(300) NOT NULL,
				`text` VARCHAR(300) NOT NULL,
				`link` VARCHAR(255) NOT NULL,
				PRIMARY KEY (`id_reinsurance_custom`, `id_lang`)
			) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');
        
        $return &= Db::getInstance()->execute('
			CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'reinsurance_custom_shop` (
				`id_reinsurance_custom` INT UNSIGNED NOT NULL AUTO_INCREMENT,
				`id_shop` int(11) unsigned NOT NULL,
				PRIMARY KEY (`id_reinsurance_custom`, `id_shop`)
			) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');

        return $return;
    }

    private function installFixtures()
    {
        $id_lang_en = Language::getIdByIso('en');
        $id_lang_fr = Language::getIdByIso('fr');

        $return = true;
        $tab_texts = array(
            array(
                'title' => array($id_lang_en => 'Money back guarantee', $id_lang_fr => 'Satisfait ou remboursé'),
                'text' => array($id_lang_en => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', $id_lang_fr => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'),
                'icon_name' => 'f118'
            ),
            array(
                'title' => array($id_lang_en => 'In-store exchange', $id_lang_fr => 'Echange en magasin'),
                'text' => array($id_lang_en => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', $id_lang_fr => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'),
                'icon_name' => 'f0e2'
            ),
            array(
                'title' => array($id_lang_en => 'Payment upon shipment', $id_lang_fr => 'Paiement à la livraison'),
                'text' => array($id_lang_en => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', $id_lang_fr => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'),
                'icon_name' => 'f09d'
            ),
            array(
                'title' => array($id_lang_en => 'Free Shipping', $id_lang_fr => 'Livraison gratuite'),
                'text' => array($id_lang_en => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', $id_lang_fr => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'),
                'icon_name' => 'f0d1'
            ),
            array(
                'title' => array($id_lang_en => '100% secure payment', $id_lang_fr => 'Paiement 100% sécurisé'),
                'text' => array($id_lang_en => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', $id_lang_fr => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'),
                'icon_name' => 'f13e'
            )
        );

        foreach ($tab_texts as $tab) {
            $reinsurance = new ReinsuranceCustomClass();
            foreach (Language::getLanguages(false) as $lang) {
                $reinsurance->title[$lang['id_lang']] = $tab['title'][$lang['id_lang']];
                $reinsurance->text[$lang['id_lang']] = $tab['text'][$lang['id_lang']];
            }

            $reinsurance->icon_name = $tab['icon_name'];
            $reinsurance->id_shop = $this->context->shop->id;
            $return &= $reinsurance->save();
        }
        return $return;
    }

    public function uninstall()
    {
        return $this->uninstallDB() && parent::uninstall();
    }

    private function uninstallDB()
    {
        return Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'reinsurance_custom`') &&
                Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'reinsurance_custom_lang`') &&
                Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'reinsurance_custom_shop`');
    }

    /**
     * Méthode postProcess()
     *
     * Traitement des informations saisies dans le backoffice
     * Traitements divers, mise à jour la base de données, définition des messages d'erreur ou de confirmation...
     *
     * @return string HTML du résultat du traitement (message d'erreur ou de confirmation)
     */
    private function postProcess()
    {
        if (Tools::isSubmit('submit'.$this->name)) {
            if (Tools::getIsset('id_reinsurance_custom')) {
                $reinsurance = new ReinsuranceCustomClass((int)Tools::getValue('id_reinsurance_custom'));
            } else {
                $reinsurance = new ReinsuranceCustomClass();
            }

            $reinsurance->copyFromPost();
            $reinsurance->id_shop = $this->context->shop->id;

            if ($reinsurance->validateFields(false) && $reinsurance->validateFieldsLang(false)) {
                $reinsurance->save();
                $this->_clearCache('blockreinsurancecustom.tpl');
                $this->html .= $this->displayConfirmation($this->l('The block configuration has been updated.'));
            } else {
                $this->html .= $this->displayError($this->l('An error occurred while attempting to save.'));
            }
        }
    }

    /**
     * Méthode getContent()
     *
     * Gère l'administration du module dans le backoffice
     * Dispatch vers les différentes méthodes en fonctions des cas (affichage des formulaires, des erreurs, des confirmations, ...)
     *
     * @return string HTML de la partie backoffice du module
     */
    public function getContent()
    {
        if (!isset($this->_postErrors) || !count($this->_postErrors)) {
            $this->postProcess();
        } else {
            foreach ($this->_postErrors as $err) {
                $this->html .= $this->displayError($err);
            }
        }
        if (Tools::isSubmit('updateblockreinsurancecustom') || Tools::isSubmit('addblockreinsurancecustom')) {
            $this->html .= $this->renderForm();
        } elseif (Tools::isSubmit('deleteblockreinsurancecustom')) {
            $this->deleteBlock();
        } elseif (Tools::isSubmit('supportblockreinsurancecustom')) {
            $this->html .= $this->renderSupportForm();
        } else {
            $this->html .= $this->renderList();
        }

        return $this->html;
    }

    public function renderList()
    {
        $helper = $this->initList();
        return $helper->generateList($this->getListContent((int)$this->context->language->id, true), $this->fields_list);
    }

    protected function initList()
    {
        if ($this->is_version16) {
            $this->context->controller->addJS($this->_path.'views/js/admin.js');
        }

        $this->fields_list = array(
            'id_reinsurance_custom' => array(
                'title' => $this->l('ID'),
                'width' => 50,
                'type' => 'text',
                'search' => false,
                'orderby' => false
            ),
            'title' => array(
                'title' => $this->l('Title'),
                'width' => 140,
                'type' => 'text',
                'search' => false,
                'orderby' => false
            ),
            'text' => array(
                'title' => $this->l('Text'),
                'width' => 50,
                'type' => 'text',
                'search' => false,
                'orderby' => false
            ),
            'link' => array(
                'title' => $this->l('Link'),
                'width' => 140,
                'type' => 'text',
                'search' => false,
                'orderby' => false
            ),
            'new_window' => array(
                'title' => $this->l('New window'),
                'width' => 50,
                'active' => 'status',
                'align' => 'text-center',
                'type' => 'bool',
                'search' => false,
                'orderby' => false
            ),
        );

        if (Shop::isFeatureActive()) {
            $this->fields_list['id_shop'] = array('title' => $this->l('ID Shop'), 'align' => 'center', 'width' => 25, 'type' => 'int');
        }

        $helper = new HelperList();
        $helper->module = $this;
        $helper->shopLinkType = '';
        $helper->simple_header = false;
        $helper->identifier = 'id_reinsurance_custom';
        $helper->actions = array('edit', 'delete');
        $helper->show_toolbar = true;

        $helper->toolbar_btn['new'] = array(
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&add'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Add new')
        );

        $helper->toolbar_btn['help-new'] = array(
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&support'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Support')
        );

        $helper->icon = 'icon-info-circle';
        $helper->title = $this->displayName;
        $helper->table = $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
        $helper->listTotal = count($this->getListContent($this->context->cookie->id_lang));

        $helper->tpl_vars = array(
            'doc_link' => _MODULE_DIR_.$this->name.'/docs/readme_'.$this->context->language->iso_code.'.pdf',
            'add_link' => $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name.'&add'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'cancel_link' => $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'support_link' => $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name.'&support'.$this->name.'=1&token='.Tools::getAdminTokenLite('AdminModules'),
            //'support_link' => 'http://addons.prestashop.com/'.$this->context->language->iso_code.'/contact-community.php?id_product='.$this->addons_id,
            //'rating_link' => 'http://addons.prestashop.com/'.$this->context->language->iso_code.'/ratings.php'
        );

        return $helper;
    }

    protected function getListContent($id_lang, $strip_tags = false)
    {
        $list = Db::getInstance()->executeS(
            'SELECT r.`id_reinsurance_custom`, r.`id_shop`, r.`icon_name`, r.`new_window`, rl.`title`, rl.`text`, rl.`link`
            FROM `'._DB_PREFIX_.'reinsurance_custom` r
            LEFT JOIN `'._DB_PREFIX_.'reinsurance_custom_lang` rl ON (r.`id_reinsurance_custom` = rl.`id_reinsurance_custom`)
            WHERE `id_lang` = '.(int)$id_lang.' '.
            (Shop::isFeatureActive() ? 'AND r.`id_reinsurance_custom` IN (
            SELECT rs.`id_reinsurance_custom`
            FROM `'._DB_PREFIX_.'reinsurance_custom_shop` rs
            WHERE rs.`id_shop` IN ('.(int)$this->context->shop->id.'))' : '')
        );

        if ($strip_tags) {
            foreach ($list as &$block) {
                $block['text'] = strip_tags($block['text']);
            }
        }

        return $list;
    }

    public function renderForm()
    {
        // Get default Language
        $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

        $this->context->controller->addjQueryUI('ui.dialog');

        $this->context->controller->addJS(_PS_JS_DIR_.'tiny_mce/tiny_mce.js');
        $this->context->controller->addJS(_PS_JS_DIR_.'tinymce.inc.js');

        if (!$this->is_version16) {
            $this->context->controller->addCSS($this->_path.'views/css/font-awesome.css');
        }
        $this->context->controller->addCSS($this->_path.'views/css/admin.css');

        // Init Fields form array
        $fields_form = array();
        $fields_form[0]['form'] = array(
            'legend' => array(
                'title' => (Tools::getIsset('update'.$this->name))?$this->l('Edit a reinsurance block'):$this->l('Add a reinsurance block'),
                (!$this->is_version16) ? 'image' : 'icon' => (!$this->is_version16) ? _MODULE_DIR_.$this->name.'/views/img/settings_16x16.png' : 'icon-plus-sign-alt'
            ),
            'input' => array(
                array(
                    'type' => 'hidden',
                    'name' => 'id_reinsurance_custom',
                    'required' => true,
                    'size' => 50, //only 1.5
                    'class' => 'fixed-width-xl' //only 1.6
                ),
                array(
                    'type' => 'icon_select',
                    'label' => $this->l('Icon'),
                    'name' => 'icon_name',
                    'required' => true,
                    'size' => 50, //only 1.5
                    'class' => 'fixed-width-xl' //only 1.6
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Title'),
                    'name' => 'title',
                    'size' => 50, //only 1.5
                    'lang' => true,
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Text'),
                    'name' => 'text',
                    'autoload_rte' => true,
                    'lang' => true,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Link'),
                    'name' => 'link',
                    'prefix' => '<i class="icon-link"></i>',
                    'size' => 50, //only 1.5
                    'lang' => true,
                    'placeholder' => 'http://www.example.com'
                ),
                array(
                    'type' => ($this->is_version16) ? 'switch' : 'radio',
                    'label' => $this->l('New window'),
                    'name' => 'new_window',
                    'class' => 't', //only 1.5
                    'is_bool' => true,
                    'values' => array(
                        array(
                            'id' => 'new_window_on',
                            'value' => 1,
                            'label' => $this->l('Yes')
                        ),
                        array(
                            'id' => 'new_window_off',
                            'value' => 0,
                            'label' => $this->l('No')
                        )
                    ),
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'class' => ($this->is_version16) ? 'btn btn-default pull-right' : 'button',
                'name' => 'submit'.$this->name
            )
        );

        if (Shop::isFeatureActive()) {
            $this->fields_form['input'][] = array(
                'type' => 'shop',
                'label' => $this->l('Shop association'),
                'name' => 'checkBoxShopAsso',
            );
        }

        $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
        $helper->default_form_language = $default_lang;
        $helper->allow_employee_form_lang = $default_lang;

        // Title and toolbar
        $helper->title = $this->displayName;
        $helper->show_toolbar = true;  // false -> remove toolbar. Only 1.5
        $helper->toolbar_scroll = true;   // true -> Toolbar is always visible on the top of the screen. Only 1.5
        if (!$this->checkPSVersion('1.5.5.0')) {
            $helper->submit_action = 'submit'.$this->name;
        }

        if (!$this->is_version16) {
            // Only usefull on 1.5
            $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')
                )
            );
        }

        $helper->toolbar_btn['help-new'] = array(
            'desc' => $this->l('Support'),
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&support'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules')
        );

        $language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));

        $icon_list = array();
        // Boucle gérant l'affichage de la liste d'icônes
        for ($i = 61440; $i <= 61922; $i++) {
            $h = dechex($i);
            $last = Tools::substr($h, 3, 1);
            if ($last != 'f') {
                $icon_list[] = $h;
            }
        }

        // Needed for WYSIWYG
        $helper->tpl_vars = array(
            'base_url' => $this->context->shop->getBaseURL(),
            'language' => array(
                'id_lang' => $language->id,
                'iso_code' => $language->iso_code
            ),
            'fields_value' => $this->getAddFieldsValues(),
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
            'image_baseurl' => $this->_path.'images/',
            'admin_returns_url' => $this->context->link->getAdminLink('AdminReturn'),
            'ps_version16' => $this->is_version16,
            'icon_list' => $icon_list,
        );

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

    public function getAddFieldsValues()
    {
        $fields_value = array();
        $id_reinsurance_custom = (int)Tools::getValue('id_reinsurance_custom');
        $languages = Language::getLanguages(false);
        
        foreach ($languages as $lang) {
            if ($id_reinsurance_custom) {
                $reinsurance = new ReinsuranceCustomClass((int)$id_reinsurance_custom);
                $fields_value['id_reinsurance_custom'] = $id_reinsurance_custom;
                $fields_value['icon_name'] = $reinsurance->icon_name;
                $fields_value['new_window'] = $reinsurance->new_window;
                $fields_value['title'][(int)$lang['id_lang']] = $reinsurance->title[(int)$lang['id_lang']];
                $fields_value['text'][(int)$lang['id_lang']] = $reinsurance->text[(int)$lang['id_lang']];
                $fields_value['link'][(int)$lang['id_lang']] = $reinsurance->link[(int)$lang['id_lang']];
            } else {
                $fields_value['id_reinsurance_custom'] = '';
                $fields_value['icon_name'] = Tools::getValue('icon_name', '');
                $fields_value['new_window'] = Tools::getValue('new_window', '');
                $fields_value['title'][(int)$lang['id_lang']] = Tools::getValue('title_'.(int)$lang['id_lang'], '');
                $fields_value['text'][(int)$lang['id_lang']] = Tools::getValue('text_'.(int)$lang['id_lang'], '');
                $fields_value['link'][(int)$lang['id_lang']] = Tools::getValue('link_'.(int)$lang['id_lang'], '');
            }
        }

        return $fields_value;
    }

    public function renderSupportForm()
    {
        // Envoi des paramètres au template
        $this->context->smarty->assign(
            array(
                'path' => _MODULE_DIR_.$this->name.'/',
                'iso' => Language::getIsoById($this->context->cookie->id_lang),
                'display_name' => $this->displayName,
                'version' => $this->version,
                'author' => $this->author,
                'contact' => $this->contact,
                'back_link' => AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
                'ps_version16' => $this->is_version16
            )
        );

        return $this->display(__FILE__, 'views/templates/admin/support.tpl');
    }

    private function deleteBlock()
    {
        $reinsurance = new ReinsuranceCustomClass((int)Tools::getValue('id_reinsurance_custom'));
        $reinsurance->delete();
        $this->_clearCache('blockreinsurancecustom.tpl');
        Tools::redirectAdmin(AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'));
    }

    /**
     * Méthode checkPSVersion()
     *
     * Compare la version de Prestashop passée en paramètre avec la version courante
     *
     * @param string $version Version à comparer
     * @param string $compare Sens de la comparaison
     *
     * @return boolean True si la comparaison est vérifiée
     */
    private function checkPSVersion($version = '1.6.0.0', $compare = '>')
    {
        return version_compare(_PS_VERSION_, $version, $compare);
    }

    public function hookHeader()
    {
        if ($this->is_version16) {
            $this->context->controller->addCSS($this->_path.'views/css/styles.css', 'all');
        } else {
            $this->context->controller->addCSS($this->_path.'views/css/font-awesome.css', 'all');
            $this->context->controller->addCSS($this->_path.'views/css/styles15.css', 'all');
        }
    }

    public function hookFooter()
    {
        if ($this->checkPSVersion('1.5.6')) {
            if (!$this->isCached('blockreinsurancecustom.tpl', $this->getCacheId())) {
                $infos = $this->getListContent($this->context->language->id);
                $this->context->smarty->assign(array('infos' => $infos, 'nbblocks' => count($infos)));
            }
            return $this->display(__FILE__, 'blockreinsurancecustom.tpl', $this->getCacheId());
        } else {
            $infos = $this->getListContent($this->context->language->id);
            $this->context->smarty->assign(array('infos' => $infos, 'nbblocks' => count($infos)));
            return $this->display(__FILE__, 'blockreinsurancecustom.tpl');
        }
    }

    public function hookDisplayHome()
    {
        return $this->hookFooter();
    }

    public function hookDisplayContentFooter()
    {
        return $this->hookFooter();
    }
}

Faut-il juste ajouter ces lignes ?

    public function hookTop()
    {
        return $this->hookFooter();
    }

N'y connaissant rien en langage PHP, je préfère demander avant de faire une FATAL ERROR ;)

Merci d'avance pour l'aide.

David

prestashop endroit voulu pour module réassurance personnalisé.bmp

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...