Where define new global variable in Prestashop - variables

The file defines.inc.php contains multiple globals variables but if I want to define new variable which file is the best ?
If I update Prestashop the file defines.inc.php is reset and I loose my global variable.
Maybe in settings.inc.php but this file is not versioned.

You can create a file config/defines_custom.inc.php next to config/defines.inc.php. At startup Prestashop checks if this file exists. If it exists then it is included before the default one.
You can find the related code in config/config.inc.php :
$currentDir = dirname(__FILE__);
/* Custom defines made by users */
if (is_file($currentDir.'/defines_custom.inc.php')) {
include_once($currentDir.'/defines_custom.inc.php');
}
require_once($currentDir.'/defines.inc.php');
This way you can for example set mode dev on without touching the default file:
define('_PS_MODE_DEV_', true);
And in the default file, this define will not occur:
if (!defined('_PS_MODE_DEV_')) {
define('_PS_MODE_DEV_', false);
}

I suggest to create your own module (maybe a 'dummy' module :)), and declare there your global variables.
For example create a module called 'mymodule', the main file mymodule.php should be:
// Here you can define your global vars
define('MY_CUSTOM_VAR', 100);
class MyModule extends MyModule
{
public function __construct()
{
// See documentation
}
public function install(){ return parent::install(); }
}
So you can update your PrestaShop version without problems losing your global vars ;)

Related

Prestashop beforeRequest Middleware

I am trying to build a module for Prestashop 1.6 that would redirect the user if the targeted URL is present in a database.
What I'm going to do is the following:
public function checkRedirection ($url) {
$line = Db::getInstance()->executeS('SELECT * FROM ps_custom_redirection WHERE url = ' . pSQL($url));
if (!sizeof($line)) {
return null;
}
header('Location: ' . $line[0]['destination']);
http_response_code($line[0]['http_code']);
exit();
}
Now, I could run this function when the displayTop hook is fired. But I would rather launch this function at the beginning of the request's process.
Does Prestashop provide such a hook? If not, can I create one? Where should I write the code to fire it?
The fist hook executed is actionDispatcher – you can use it if you want.
You'll find this hook executed in /classes/Dispatcher.php. Search for the code Hook::exec('actionDispatcher', $params_hook_action_dispatcher);.
If you want to add this hook to your module, you need to use its name in the main module file like this:
public function install() {
return parent::install()
&& $this->registerHook('actionDispatcher');
}
public function hookActionDispatcher($params) {
// your code
Tools::redirect($url);
}
In Prestashop Tools::redirect($url); is used if redirecting.

What is module's "themes" folder used for?

PS documentation says that when developing a module you can create the /themes/[theme_name]/modules subfolder and that it is used for:
"Sub-folder for overriding .tpl files and languages files, if necessary."
and that it:
"enables you to handle the module's template files in various ways, depending on the current theme.",
but i don't really understand its practical usage. What would be a use case of it?
Thanks
When you develop a Prestashop website you should never change core files. This mean that you can only create new modules in the /modules/ folder but not alter existing one. Because if you update a module you altered, all your changes will be gone.
Each time Prestashop needs to load a module template file it will first look in the current theme /themes/your_theme/modules/the_module/path_to_tpl.tpl if an override of this template exists. If not it will load the template from the /modules directory.
This recommandation is also valid for .css and .js files.
The documentation you mentioned in the comment below is wrong and should be updated. You can't put a themes folder inside a module.
Here is the _isTemplateOverloadedStatic() method from Module class called everytime we need a module template:
/*
** Template management (display, overload, cache)
*/
protected static function _isTemplateOverloadedStatic($module_name, $template)
{
if (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/'.$template)) {
return _PS_THEME_DIR_.'modules/'.$module_name.'/'.$template;
} elseif (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/hook/'.$template)) {
return _PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/hook/'.$template;
} elseif (Tools::file_exists_cache(_PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/front/'.$template)) {
return _PS_THEME_DIR_.'modules/'.$module_name.'/views/templates/front/'.$template;
} elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/views/templates/hook/'.$template)) {
return false;
} elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/views/templates/front/'.$template)) {
return false;
} elseif (Tools::file_exists_cache(_PS_MODULE_DIR_.$module_name.'/'.$template)) {
return false;
}
return null;
}
As you can see in this code, Prestashop will never look into a themes folder inside your module when loading a template.

addJS function not working for admin in prestashop

I am trying to add javascript file in prestashop admin using backOfficeHeader hook using a module but nothing happened. My code is given below.
public function install()
{
if (!parent::install()
|| !$this->registerHook('backOfficeHeader'))
return false;
return parent::install() &&
$this->registerHook('backOfficeHeader');
}
public function hookBackOfficeHeader() {
$this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'js/hs_custom.js');
}
If you are using PS 1.5 or 1.6 you should use hook "actionAdminControllerSetMedia".
Your module installer should check which prestashop version is used and then register the needed hook.
if (version_compare(substr(_PS_VERSION_, 0, 3), '1.5', '<'))
$this->registerHook('BackOfficeHeader');
else
$this->registerHook('actionAdminControllerSetMedia');
Then you need to addJS on each hook in its version format:
PS>=1.5
public function hookActionAdminControllerSetMedia($params) {
$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
}
PS<=1.4
public function hookBackOfficeHeader($params) {
Tools::addJS($this->_path.'views/js/hs_custom.js');
}
did u try to check addJS path? I think nothing more can be possible if other JS files working.
Try to use $this->_path.
$this->context->controller->addJS($this->_path.'views/js/hs_custom.js');
1) Output path and check if it is valid.
2) Reload page and check network. Page load your script or not?
3) Remember to reset module if u change something with hooks.
4) Check module hooks.
You did several mistakes.
This is the invalid access to the property: $this->module->name. Must be $this->name. I.e., the correct code to generate a path to JavaScript file is:
_MODULE_DIR_ . $this->name . '/js/hs_custom.js'
Or like this (shorted):
$this->_path . 'js/hs_custom.js'
You are also did the double installation of the module and of the hook.
You can use the hook BackOfficeHeader, but the hook ActionAdminControllerSetMedia is preferred.
So, the correct example to add a JS and a CSS files for a back-office (i.e. for AdminController) via a module class is:
public function hookActionAdminControllerSetMedia($params)
{
// Adds your's CSS file from a module's directory
$this->context->controller->addCSS($this->_path . 'views/css/example.css');
// Adds your's JavaScript file from a module's directory
$this->context->controller->addJS($this->_path . 'views/js/example.js');
}
Here is the detailed information, how to register JavaScript in a back-office (in admin pages).
I also met this problem, there is no error and warning, all grammar is right. But cannot find my js File.
I found the reason finally. In my case there is nothing in JS file and system passes this file which has no content always.
For me "this->_path" dosn't work. My solution is to use $_SERVER['DOCUMENT_ROOT']
public function hookActionAdminControllerSetMedia($params)
{
// add necessary javascript to products back office
if($this->context->controller->controller_name == 'AdminProducts' && Tools::getValue('id_product'))
{
$this->context->controller->addJS($_SERVER['DOCUMENT_ROOT']."/modules/apl/views/js/jquery.ui.touch-punch.min.js");
}
}

Prestashop: How to include a block in a module?

I have a module and I want to display the "blocknewproducts" inside of it. What is the best option?
Thanks!
Ok, we lack some more info but i will share a way to do it in Prestashop 1.6.x:
Your module :
Given you have a custom hook named displayMyModule in your module install method :
public function install() {
if (!parent::install() || !$this->registerHook('displayMyModule')) {
return false;
} else {
return true;
}
}
Now, where you want to display the content of this hook is up to you. For example if it's on a category page, in category.tpl you add
{hook::exec('displayMyModule')}
Block new Products
Now to display the blocknewproducts we will create an override of this core module and register the displayMyModule hook :
in /override/modules/blocknewproducts/blocknewproducts.php :
if (!defined('_PS_VERSION_'))
exit;
class BlockNewProductsOverride extends BlockNewProducts
{
public function install()
{
$success = (parent::install()
&& $this->registerHook('displayMyModule'));
return $success;
}
public function hookDisplayMyModule($params)
{
return $this->hookRightColumn($params);
}
}
In hookDisplayMyModule we simply return the hookRightColumn method to not rewrite code.
Don't forget to go into back-office/modules and reinitialize blocknewproducts module
And that's about it... but keep in mind that you could also just override the blocknewproducts module to register hooks that already exist like shown above.
TLDR: Maybe you don't need a module if you just want to show block new products elsewhere.

module creation in suiteCRM

I am using SuiteCRM ( Sugar CRM 6.x community edition ) & want to create a custom login page and after successful login I want to redirect based on user type
tried to create some modules but there is no clear documentation except few of useful links, below are my queries :
Can we create custom modules without using module builder, if yes then what would be steps?
Do we need to write module in /module folder or /custom/module folder or on both place?
any link is also appreciated.
You can create a custom Login Page by modfying "modules/Users/login.tpl"
Custom Modules can be created through Modulebuilder or manually.
When creating modules manually it's important to use the right names.
The easiest way is a Plural name for the folder, table and Module and a singular name for the class.
Manual steps:
You need a Folder in modules/ named like you module (i.e. CAccounts)
In this folder you need a file named like the class (i.e CAccount.php) with something like that as content:
require_once('data/SugarBean.php');
require_once('include/utils.php');
class CAccount extends SugarBean{
var $table_name = 'caccounts';
var $object_name = 'CAccount';
var $module_dir = 'CAccounts';
var $new_schema = true;
var $name;
var $created_by;
var $id;
var $deleted;
var $date_entered;
var $date_modified;
var $modified_user_id;
var $modified_by_name;
function CAccount (){
parent::SugarBean();
}
function get_summary_text(){
return $this->name;
}
function bean_implements($interface)
{
switch($interface)
{
case 'ACL':return true;
}
return false;
}
}
In this folder you need a vardefs.php file:
$dictionary['CAccount'] = array(
'table'=>'caccounts',
'audited'=>false,
'fields'=>array (
//Your fielddefs here
)
);
require_once('include/SugarObjects/VardefManager.php');
VardefManager::createVardef('CAccounts','CAccount', array('basic'));
For the language and metadata folders look at any other module.
Next is a file at "custom/Extension/application/Ext/Include/CAccounts.include.php"
$moduleList[] = 'CAccounts';
$beanList['CAccounts'] = 'CAccount';
$beanFiles['CAccount'] = 'modules/CAccounts/CAccount.php';
A language file for the module name must be in "custom/Extension/application/Ext/Language/"
$app_list_strings['moduleList']['CAccounts'] = 'Custom Accounts';
To display the Module in your tabs you need to use "rebuild and repair" and then the "Display Modules and Subpanels" option in the admin menu.
For a custom module you don't need the "custom/" folder structure. Files there will be used by sugar if provided, but often there's no need for that in a custom module.
Guides about the Module Framework can be found on the sugarcrm support site:
http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_6.5/03_Module_Framework