Error when I try to install module in prestashop - module

Following the Developer's manual for prestashop I've this problem:
My code for my Test Module is:
if (!defined("_PS_VERSION_")){
exit;
}
class MyModule extends Module{
public function __construct(){
$this->name='Testing';
$this->tab='Modulo Prueba';
$this->version=1.0;
$this->author='Uniagro';
$this->need_instance=0;
parent::__construct();
$this->displayName = $this->l('Test');
$this->description = $this->l('Este es un modulo de prueba');
}
public function install(){
if (parent::install()==false){
return false;
}
return true;
}
public function uninstall(){
if (!parent::uninstall()){
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'mymodule');
}
parent::uninstall();
}
}
But, if I try to activate my module I always see this error:
You don't have permissions to update your Testing module.
I try prestashop in local and I've permissions to write in this folder.
I'm ussing Prestashop version 1.6.0.9

Your module name is set incorrectly:
$this->name='mymodule';
// This is internal module name in lowercase letters,
// must match the folder name too
// Class name must match too but it can be in camel case : MyModule

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.

Custom Sendinblue API available for CodeIgniter?

I am searching for a SendinBlue API for Codeigniter but not found yet.
In the case that the API does not exists yet, is that hard to code it by myself (I already know HTTP requests but I not have any idea about configs)
Or is that possible to use the PHP one ?
Thanks
I have a beautiful solution for this and it works fine for me and I hope it will work for you as well.
I am using API-v3.
What I did is:
Downloaded the API through composer on my local PC.
Created a folder named "sendinblue" on the server (where we have
assets folders) and upload the vendor folder from the downloaded API
inside this folder.
Created a library named "Sendinblue.php" and added all the necessary functions here. Now I can use this library like other libraries.
This is my library structure:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Sendinblue{
public $config;
public $apiInstance;
public function __construct(){
require_once('sendinblue/vendor/autoload.php');
$this->config = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', '');
$this->apiInstance = new SendinBlue\Client\Api\ContactsApi(
new GuzzleHttp\Client(),
$this->config
);
}
public function get_contact_info($data){
$identifier = $data['email'];
try {
return $result = $this->apiInstance->getContactInfo($identifier);
} catch (Exception $e) {
return 'Exception when calling ContactsApi->getContactInfo: '.$e->getMessage();
}
}
public function create_contact($data){
$createContact = new \SendinBlue\Client\Model\CreateContact();
$createContact['email'] = $data['email'];
$createContact['listIds'] = [2];
try {
return $result = $this->apiInstance->createContact($createContact);
} catch (Exception $e) {
return $e->getCode();
}
}

Where define new global variable in Prestashop

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 ;)

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.

Yii + ZendService/LiveDocx

I am trying to use the Zend Service Livedocx. I am using two models in my application - Submission and SubmissionFiles. I am saving the Submission data and the SubmissionFiles data with the Create action in the SubmissionController. I am trying to call the MailMerge class in that action but I am getting the error: Fatal error: Cannot redeclare class ZendService\LiveDocx\AbstractLiveDocx in C:\wamp\www\publications\src\web\protected\vendors\ZendService\LiveDocx\AbstractLiveDocx.php
SubmissionController
//some code
public function actionCreate()
{
$model=new Submission;
if(isset($_POST['Submission']))
{
$model->attributes=$_POST['Submission'];
if($model->save())
{
$modelFile = new SubmissionFiles;
if(isset($_POST['SubmissionFiles']))
{
Yii::import('application.vendors.zendservice.livedocx.*');
$phpLiveDocx = new MailMerge();
//somecode
protected/vendors/ZendService/LiveDocx/MailMerge.php
<?php
require_once 'ZendService\LiveDocx\AbstractLiveDocx.php';
class MailMerge extends AbstractLiveDocx
{ //somecode
protected/vendors/ZendService/LiveDocx/AbstractLiveDocx.php
<?php
namespace ZendService\LiveDocx;
use Traversable;
use Zend\Soap\Client as SoapClient;
use Zend\Stdlib\ArrayUtils;
abstract class AbstractLiveDocx
{ //some code
If I comment the require_once 'ZendService\LiveDocx\AbstractLiveDocx.php'; in MailMerge.php,
I get the error:
Fatal error: Class 'AbstractLiveDocx' not found in C:\wamp\www\publications\src\web\protected\vendors\ZendService\LiveDocx\MailMerge.php
What am I doing wrong?
I guess you are using composer to include Zend as dependancy. If so you must add the autoloader to your index.php file(or whatever you bootstrap with).
// change the following paths if necessary
require_once(dirname(__FILE__).'/../vendor/autoload.php');
After that replace the require_once line with:
use ZendService\LiveDocx\AbstractLiveDocx;
Hope that works :)