Created
March 31, 2017 14:41
-
-
Save anegoda1995/b58072d9c6d41c52c334a194008485f1 to your computer and use it in GitHub Desktop.
[[[PrestaShop]]] create module for PrestaShop CMS (complete code of main file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
This is main file example of new empty module for PrestaShop 1.7 (yeah, 1.7 not 1.6); | |
There you need to change name of module, name of class, and 'ANY_VALUE' for those what you need; | |
This code you can copy to your main file and that is! Work! | |
Addition information you can find at: [[[http://doc.prestashop.com/display/PS16/Creating+a+first+module]]] where I taught it | |
*/ | |
if (!defined('_PS_VERSION_')) | |
exit; | |
class NameOfYourModule extends Module { | |
public function __construct() { | |
$this->name = 'NameOfYourModule'; | |
$this->tab = 'front_office_features'; //for example 'back_office' | |
$this->version = '0.1'; | |
$this->author = 'Andrew Negoda'; | |
$this->need_instance = 0; | |
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); | |
$this->bootstrap = true; | |
parent::__construct(); | |
$this->displayName = $this->l('You can out there name of module'); | |
$this->description = $this->l('You can ot there small descriptionof your module for user in back_office (admin panel)'); | |
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); | |
if (!Configuration::get('ANY_VALUE')) | |
$this->warning = $this->l('No config value'); | |
} | |
public function install() { | |
if (Shop::isFeatureActive()) | |
Shop::setContext(Shop::CONTEXT_ALL); | |
if (!parent::install() || | |
!$this->registerHook('top') || | |
!$this->registerHook('header') || | |
!Configuration::updateValue('ANY_VALUE', '1234567abc') | |
) | |
return false; | |
return true; | |
} | |
public function uninstall() { | |
if (!parent::uninstall() || | |
!Configuration::deleteByName('ANY_VALUE') | |
) | |
return false; | |
return true; | |
} | |
public function getContent() { | |
$output = null; | |
if (Tools::isSubmit('submit' . $this->name)) { | |
$my_module_name = strval(Tools::getValue('ANY_VALUE')); | |
if (!$my_module_name || empty($my_module_name) || !Validate::isGenericName($my_module_name)) | |
$output .= $this->displayError($this->l('Invalid value')); | |
else { | |
Configuration::updateValue('ANY_VALUE', $my_module_name); | |
$output .= $this->displayConfirmation($this->l('Value is 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' => array( | |
array( | |
'type' => 'html', | |
'label' => $this->l('File limit size (bytes):'), | |
'name' => 'ANY_VALUE', | |
'required' => true, | |
'html_content' => '<input style="width: 100px" type="number" name="ANY_VALUE" value="' . Configuration::get('ANY_VALUE') . '">' | |
) | |
), | |
'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 | |
$helper->fields_value['ANY_VALUE'] = Configuration::get('ANY_VALUE'); | |
return $helper->generateForm($fields_form); | |
} | |
public function hookDisplayTop($params) { | |
$this->context->smarty->assign( | |
array( | |
'some_value_from_this_module' => Configuration::get('ANY_VALUE') | |
) | |
); | |
return $this->display(__FILE__, 'views/templates/hooks/NameOfYourModule.tpl'); | |
} | |
public function hookDisplayRightColumn($params) { | |
return $this->hookDisplayTop($params); | |
} | |
public function hookDisplayHeader() { | |
$this->context->controller->addCSS($this->_path . 'css/NameOfYourModule.css', 'all'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment