Last Updated: February 25, 2016
·
2.275K
· alan

Basic template for a 1.5 prestashop module

This template will help you create a basic prestashop 1.5 module you just have to change :

modulename
tab
name
author
description
tablename
hook
name

</>
class modulename extends Module
{
// contains back Office html, $this->html.= '<tags>foo</tags>' to display anything...
private $
html;

//contains back Office errors to display, $this->errors.= 'warning, something's wrong<br/>' to display anything...
private $errors;

//contains back Office good message, $this->messages.= 'Congrats ! Your settings have been saved<br/>' to display anything...
private $messages;

//use id to fill forms after posted them
private $postForms;

function __construct() {
    $this->name = 'module_name';
    $this->tab = 'tab_name';
    $this->version = 1;
    $this->author = 'author';

    parent::__construct();

    $this->displayName = $this->l('module_name');
    $this->description = $this->l('description');
    $this->confirmUninstall = $this->l('Are you sure you want to delete your configuration ?');

}

function install() {
    Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'table_name`;');

    if (!Db::getInstance()->Execute('
        CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'table_name` (
        `id` int(10) unsigned NOT NULL auto_increment,
        `name` varchar(255) NOT NULL,
        `description` text NOT NULL,
        PRIMARY KEY (`id`))
        ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8')

        || !Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ludi_idols` (`id`, `name`, `description`) VALUES (1, "John Doe", "Da best customer ever")')
    ) return false;

    if (!parent::install() || !$this->registerHook('hook_name'))
        return false;
    return true;
}


function uninstall() {
    if (!parent::uninstall() ||  !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'table_name`;'))
        return false;
    return true;
}

public function getContent() {
    if (Tools::isSubmit('submit') )
        $this->_postDatas();

    $this->showConfig();

    return $this->_html;
}

//This function will display the admin module configuration pannel
private function showConfig(){
    //used to get module infos from sql
    $infos = $this->getInfos();

    $this->_html .= '
        <link rel="stylesheet" media="all" href="'.$this->_path.'style/style.css" type="text/css" />
        <h2>'.$this->l('module_name').'</h2>
        <form action=""  id="sentences" method="post">
            <fieldset>
                <legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> module_name</legend>
                <p>Some stuffs to display</p>
                <input type="submit" name="submit" value="'.$this->l('Submit').'"
            </fieldset>
        </form>';
}


//This function manage all $_POST Vars selected in BO and will display errors
private function _postDatas(){
    if(isset($_POST['submit'])){
        //What you'll do with $_POST vars
    }

    if ($this->errors != '') $this->_html .= $this->displayError($this->errors);
    elseif ($this->messages != '') $this->_html .= $this->displayConfirmation($this->messages);
    else $this->_html .= $this->displayConfirmation( $this->l('Configuration updated').'<br />');
}


//This function will catch all BO values
private function getInfos(){
    $sql = 'SELECT id,name,description FROM '. _DB_PREFIX_ .'table_name ORDER BY id';
    if($result = Db::getInstance()->ExecuteS($sql))
        return($result);
}



//This is where you'll get and sort FO vars
private function displayFront(){
    return $this->l('hello world !');
}


//the hook, to display FO
public function hookHome() {
    global $smarty;

    $smarty->assign(Array(
        'foo' => $this->display_front()
    ));

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

}
</>

1 Response
Add your response

Thanks for sharing !

over 1 year ago ·