I created file mymodule.php for Prestashop 1.4
<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
class MyModule extends Module
{
function __construct()
{
$this->name = "mymodule";
$this->version = "1.0";
$this->author = "Tomtop";
$this->tab = "front_office_features";
$this->_postErrors = array();
parent::__construct();
$this->displayName = $this->l("My Module Name");
$this->description = $this->l("This is my module description.");
}
protected function setConfig($key,$value)
{
return Configuration::updateValue($this->name.$key,$value,true);
}
protected function getConfig($value)
{
return Configuration::get($this->name.$value);
}
protected function deleteConfig($value)
{
return Configuration::deleteByName($this->name.$value);
}
function install()
{
if (!parent::install()
OR !$this->registerHook('home')
OR !$this->registerHook('footer')
)
return false;
return true;
}
public function uninstall()
{
parent::uninstall();
return true;
}
public function hookHome($params)
{
}
public function hookfooter($params)
{
}
private function _postProcess()
{
$this->_html .= '<div class="conf confirm">'.$this->l("Updated")."</div>";
}
public function getContent()
{
$this->_html .= "<h2>".$this->displayName."</h2>";
if (Tools::isSubmit("submit"))
{
$this->_postProcess();
}
$this->_displayForm();
return $this->_html;
}
private function _displayForm()
{
$this->_html .= '<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
<fieldset>
<legend><img src="../modules/scroller/logo.gif" alt="" class="middle" />'.$this->l('Settings').'</legend>
<br />
<center><input type="submit" name="submit" value="'.$this->l('Upgrade').'" class="button" /></center>
</fieldset>
</form>';
}
}
Where in above code should be added mymodule.tpl template, which contain main html code, i.e.
return $this->display(__FILE__, 'mymodule.tpl');
Also, in which place in mymodule.php to add js and css files that are linked in head tag like this:
public function hookHeader()
{
Tools::addJS($this->_path.'js/myjscript1.js');
Tools::addJS($this->_path.'js/myjscript2.js');
Tools::addCSS($this->_path.'css/mymodule.css', 'all');
}
And where should be added global $smarty;
if it required in above code?
The hookHeader
should be declared in the install
method.
You should reference global $smarty
at the beginning of each class method that needs to use $smarty
.
That means that in the methods where you'll be using $this->display()
to render a .tpl you should also add the global $smarty
to be able to use the $smarty->assign()
method.
Anything returned by the functions hookHome() and hookFooter() will be displayed when the hook points are rendered. Ignoring smarty to begin with you can just do something like:
public function hookHome($params)
{
return "<h2>Wow, my module displays something</h2>";
}
You may of course want to use a template file with your module -- although you don't have to. If you are going to use a smarty template then you can declare a global instance within the hook functions.
public function hookHome($params)
{
global $smarty;
...
...
As noted in Mihai's answer you will also have to modify the install function should you want to insert the css and js using the "header" hook:
function install()
{
if (!parent::install()
OR !$this->registerHook('home')
OR !$this->registerHook('footer')
OR !$this->registerHook('header')
)
return false;
return true;
}