Skip to content

Instantly share code, notes, and snippets.

@YavorK
Created December 6, 2016 14:22
Show Gist options
  • Save YavorK/ef04d240e8457e236abbcbd12b36e968 to your computer and use it in GitHub Desktop.
Save YavorK/ef04d240e8457e236abbcbd12b36e968 to your computer and use it in GitHub Desktop.
Example module for prestashop 1.6 with config page
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Lf_settings extends Module
{
private $settings = [
'lf_settings_support_email' => '[email protected]',
'lf_settings_support_phone_1' => '+33 333 33333333',
'lf_settings_support_phone_2' => '+32 333 33333333',
'lf_settings_support_phone_3' => '+34 333 33333333',
];
public function __construct()
{
$this->name = 'lf_settings'; // MUST be the same as module folder name
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Author Name';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('LF Settings');
$this->description = $this->l('Settings module');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('lf_settings'))
$this->warning = $this->l('No name provided');
}
/**
* Despite it's name this function actually saves the data from the POST request to the database
* @return string
*/
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit' . $this->name)) {
$lf_settings_support_email = strval(Tools::getValue('lf_settings_support_email'));
if (!$lf_settings_support_email
|| empty($lf_settings_support_email)
|| !Validate::isGenericName($lf_settings_support_email)
)
$output .= $this->displayError($this->l('Invalid Configuration value'));
else {
Configuration::updateValue('lf_settings', $lf_settings_support_email);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
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' => $this->getInputsForDisplayForm(),
'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
$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
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit' . $this->name;
$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
$this->addFieldsValuesToHelper($helper);
return $helper->generateForm($fields_form);
}
/**
* @param $helper
*/
private function addFieldsValuesToHelper($helper)
{
foreach ($this->settings as $name => $value) {
$helper->fields_value[$name] = Configuration::get($name);
}
}
public function getConfigFieldsValues()
{
$fields = [];
foreach ($this->settings as $name => $value) {
$fields[$name] = Tools::getValue($name, Configuration::get($name));
}
return $fields;
}
public function reset()
{
if (!$this->uninstall())
return false;
if (!$this->install())
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return $this->removeSettingsFromDB();
}
private function removeSettingsFromDB()
{
foreach ($this->settings as $name => $value) {
if (!Configuration::deleteByName($name)) {
return false;
}
}
return true;
}
public function install()
{
if (!parent::install()) {
return false;
}
$this->createSettingsInDB();
return true;
}
private function createSettingsInDB()
{
foreach ($this->settings as $name => $value) {
Configuration::updateValue($name, $value);
}
}
private function getInputsForDisplayForm()
{
return [
[
'type' => 'text',
'label' => $this->l('Support Email'),
'name' => 'lf_settings_support_email',
'size' => 100,
'required' => true
],
[
'type' => 'text',
'label' => $this->l('Support Phone 1'),
'name' => 'lf_settings_support_phone_1',
'size' => 100,
'required' => true
],
[
'type' => 'text',
'label' => $this->l('Support Phone 2'),
'name' => 'lf_settings_support_phone_2',
'size' => 100,
'required' => true
],
[
'type' => 'text',
'label' => $this->l('Support Phone 3'),
'name' => 'lf_settings_support_phone_3',
'size' => 100,
'required' => true
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment