Prestashop: Disable contact form - prestashop

I would like to disable the contact form in my prestashop installation but there is no plugin to do so. Any suggestions how to do that?

Depends what you mean by disabling contact form but here are few possibilities.
Modifying core contact controller (not recommended since you will lose custom code when updating Prestashop)
Open file controllers/front/ContactController.php and add this code inside the ContactControllerCode class.
public function init()
{
Tools::redirect('pagenotfound'); // redirect contact page to 404 page
}
Overriding contact controller
Create a new file ContactController.php and place it in folder overrides/controllers/front/ and add the following code
class ContactController extends ContactControllerCore {
public function init()
{
Tools::redirect('pagenotfound'); // redirect contact page to 404 page
}
}
Create a small module
Create a new directory contactpagedisabler in folder modules and inside create a file contactpagedisabler.php and put this code in
class ContactPageDisabler extends Module
{
public function __construct()
{
$this->name = 'contactpagedisabler';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'whatever';
parent::__construct();
$this->displayName = $this->l('Contact page disabler');
$this->description = $this->l('Disables contact page.');
}
public function install()
{
return parent::install() && $this->registerHook('actionDispatcher');
}
// hook runs just after controller has been instantiated
public function hookActionDispatcher($params)
{
if ($params['controller_type'] === 1 && $params['controller_class'] === 'ContactController') {
Tools::redirect('pagenotfound'); // redirect contact page to 404 page
}
}
}
And then install this module from backoffice.
2nd option is simplest and it doesn't interfere with core files.
3rd option is probably overkill for such a small thing however it doesn't require overriding and if you or store manager ever needs the contact page back he can just disable the module from backoffice.
The module could also be expanded/modified with configuration page where you could for example get a list of all pages in store and let user decide which ones to enable/disable etc.
Update April 2018
Forget first two options and use third. Always use a module (if possible) when modifying your shop.

If You want to block just contact form but You want to display contact page You can put in override\controllers\front\ContactController.php:
<?php
class ContactController extends ContactControllerCore
{
public function postProcess()
{
if (Tools::isSubmit('submitMessage'))
{die('Form disabled');}
else
parent::postProcess();
//return null;
}
}
This will disable ability to send mails.
Then You can cut contact form from theme: /themes/YOUR-THEME/contact-form.tpl
to not display contact form at all
After this You have to delete file /cache/class_index.php to refresh classes in prestashop.

Barto's solution can also be achieved without an override.
Create another module contactformdisabler
class ContactFormDisabler extends Module
{
public function __construct()
{
$this->name = 'contactformdisabler';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'whatever';
parent::__construct();
$this->displayName = $this->l('Contact form disabler');
$this->description = $this->l('Disables contact form submission.');
}
public function install()
{
return parent::install() && $this->registerHook('actionDispatcher');
}
public function hookActionDispatcher($params)
{
if ($params['controller_type'] === 1
&& $params['controller_class'] === 'ContactController'
&& Tools::isSubmit('submitMessage')) {
die('Contact form submission disabled');
}
}
}

Related

How to load a template file from my admin controller in custom module in prestashop

How to load a template file from my admin controller in custom module in prestashop 1.6
if (!defined('_PS_VERSION_')) exit;
class QueryAllTrxController extends ModuleAdminController
{
public $module;
public function __construct()
{
parent::__construct();
}
public function initContent()
{
parent::initContent();
$this->setTemplate('display.tpl');
//$this->setTemplate(_PS_THEME_DIR_.'mypage.tpl');
}
}
I had the same problem and it took forever to figure out.
I ended up spotting the solution in this video : https://www.youtube.com/watch?v=CdnJpLqqvcM
Any this is how I got it to work :
1 - Create the controller in ModuleName/controllers/AdminMyControllerNameController.php
class AdminMyControllerNameController extends ModuleAdminController
{
public function __construct()
{
$this->display = 'view';
$this->meta_title = $this->l('metatitle');
$this->toolbar_title = $this->l('tollbartitle');
parent::__construct();
}
public function initContent()
{
$this->show_toolbar = true;
$this->display = 'view';
$this->meta_title = $this->l('META TITLE');
parent::initContent();
$this->setTemplate('templatename.tpl');
}
public function initToolBarTitle()
{
$this->toolbar_title = $this->l('TOOLBAR TITLE??');
}
public function initToolBar()
{
return true;
}
}
2 - Create the template file in ModuleName/views/admin/my_controller_name/template.tpl
You have to create a directory in the views/admin folder using the name of your controller written in snake case.
Anyway I hope this will help.
WORKING CODE HERE
Background:
You want to add a custom admin page with a custom module controller. But you cannot customize template because you're stuck with this error message:
Fatal error: Uncaught --> Smarty: Unable to load template file '/var/www/html/admin-dev/themes/default/template/catalog/index.tpl' <-- thrown in /var/www/html/tools/smarty/sysplugins/smarty_internal_templatebase.php on line 129
Your current source code is:
class AdminYourModuleNameProductsController extends ModuleAdminController {
public function initContent() {
parent::initContent();
// enable these lines if you're stuck with damn stupid blank page with just 500 error
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', '1');
// error_reporting(E_ALL);
$this->setTemplate('products/index.tpl');
}
}
And you don't know what to do because PrestaShop dev doc is the worst document in the history of ecommerce platform developer document and moreover its forum is full of chitchats and junks.
Solution
Place index.tpl at
{%PRESTA_ROOT%}/modules/{%YOUR MODULE DIR%}/views/templates/admin/{% snake case version of controller %}/products/index.tpl
For example, if your module name is yourmodulename and the controller name is AdminYourModuleNameProductsController (as in the example), the correct path is:
{%PRESTA_ROOT%}/modules/yourmodulename/views/templates/admin/your_module_name_products/products/index.tpl
If the error still persists:
Check this line:
{%PRESTA_ROOT%}/classes/controller/ModuleAdminController.php
public function createTemplate($tpl_name)
{
if (file_exists(_PS_THEME_DIR_.'modules/'.$this->module->name.'/views/templates/admin/'.$tpl_name) && $this->viewAccess()) {
// echo the following line and exit
return $this->context->smarty->createTemplate(_PS_THEME_DIR_.'modules/'.$this->module->name.'/views/templates/admin/'.$tpl_name, $this->context->smarty);
} elseif (file_exists($this->getTemplatePath().$this->override_folder.$tpl_name) && $this->viewAccess()) {
// echo the following line and exit
return $this->context->smarty->createTemplate($this->getTemplatePath().$this->override_folder.$tpl_name, $this->context->smarty);
}
// the error occurs because php get reach to the following line:
return parent::createTemplate($tpl_name);
}
Do as I commented and you can get the correct file path. Make sure the file exists in the path.
My PrestaShop version is 1.6.1.24
The code $this->setTemplate('display.tpl'); is loading a template file modules/your-custom-module/views/templates/admin/display.tpl or modules/your-custom-module/display.tpl.
The classname must be named that way: AdminQueryAllTrxController
you can put display.tpl in :
modules\module_name\views\templates\admin\classe_name(QueryAllTrx)
and use :$this->setTemplate('display.tpl'); in your AdminQueryAllTrxController
First of all add controller to your module:
modules\module_name\controllers\admin\SomeNameController.php
and extend it by ModuleAdminController, you need at least two methods for it to work properly __construct and initContent
put the following code to the later method:
$this->content .= $this->context->smarty->fetch($this->pathToTpl);
$this->context->smarty->assign(array(
'content' => $this->content,
));
You could replace $this->pathToTpl with any path which is pointed to your tpl file, I'm prefer to create the path dynamically. You can see a simple example here:
class SomeNameController extends ModuleAdminController{
var $pathToTpl;
public function __construct()
{
$this->bootstrap = true;
$this->context = Context::getContext();
$this->pathToTpl = _PS_MODULE_DIR_ .
$this->module->name . // put the name of module
'/views/templates/admin' .
'/' .
'templateName.tpl';
parent::__construct();
}
public function initContent()
{
parent::initContent();
$this->content .= $this->context->smarty->fetch($this->pathToTpl);
$this->context->smarty->assign(array(
'content' => $this->content,
));
}
}
finally you need to place templateName.tpl in the path you wanted to be:
modules\module_name\views\templates\admin\templateName.tpl

Prestashop 1.6 - Back Office - Smarty - Creating a module that makes a simple blank page?

I am trying to add some functionality to my shop and have spent the past two days trying to come to grasp with how smarty actually works within prestashop or rather in general.
I have so far made a module that can install, on install it creates a tab on the left menu, I can click on the tab and it will load the controller but this is where i get stuck... I can't figure out how to display custom content within that state.
What i would like is very simple, just a paragraph of text and a button. When clicking the button i will do a few things and record a few things then show the results as a simple report.
So for starters... I'd like to create the page with the paragraph and button.
So i have created a folder in the module diretory called priceupdate
Inside of this there is:
/priceupdate.php
<?php
if (!defined('_PS_VERSION_'))
exit;
class PriceUpdate extends Module
{
public function __construct()
{
$this->name = 'priceupdate';
$this->tab = 'quick_bulk_update';
$this->version = '0.8';
$this->author = 'Me';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Pricing Update');
$this->description = $this->l('Adds functionality relating to maintaining product my prices.');
$this->confirmUninstall = $this->l('Are you sure you would like to uninstall?');
}
public function install()
{
if (!parent::install()
|| !$this->installModuleTab('AdminPricingUpdate', array(1=>'Pricing Update'), 0))
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall()
|| !$this->uninstallModuleTab('AdminPricingUpdate', array(1=>'Pricing Update'), 0))
return false;
return true;
}
private function installModuleTab($tabClass, $tabName, $idTabParent)
{
$tab = new Tab();
$tab->name = $tabName;
$tab->class_name = $tabClass;
$tab->module = $this->name;
$tab->id_parent = $idTabParent;
if(!$tab->save())
return false;
return true;
}
private function uninstallModuleTab($tabClass)
{
$idTab = Tab::getIdFromClassName($tabClass);
if($idTab != 0)
{
$tab = new Tab($idTab);
$tab->delete();
return true;
}
return false;
}
}
?>
And
/controllers/admin/AdminPricingUpdateController.php
<?php
class AdminPricingUpdateController extends AdminController
{
public function __construct()
{
$this->lang = (!isset($this->context->cookie) || !is_object($this->context->cookie)) ? intval(Configuration::get('PS_LANG_DEFAULT')) : intval($this->context->cookie->id_lang);
parent::__construct();
}
public function display(){
parent::display();
}
public function renderList() {
return $this->context->smarty->fetch(dirname(__FILE__).'/content.tpl');
}
}
?>
This works however where I am stuck relates to the content.tpl part. What goes inside of this the content.tpl file in order to get it to make a blank page of content within the content area of the admin section?
I've looked through the manual and spent countless hours on forums looking though questions, tried to figure it out by breaking down other modules but i've found it too complex to really understand what is what.
If anyone could help me to understand this or point me to a source of info on this specific subject then it would be greatly appreciated, thanks!
Check that answer
If you need "a blank page of content within the content area of the admin section" you need to make the content.tpl blank.
Note in my example that you do not have to set the name of the template if it's called "content.tpl".

how to use SimpleSAMLphp in yii framework?

I have two project in yii framework and I want to use both project using SimpleSAMLphp with SSO. The condition, I need is if I login from the first project, i want access to the second project.
Thank you in advance.
First you load the SAML library by temporarily disabling the Yii autoloader. This is just to let you use the SAML classes and methods:
<?php
class YiiSAML extends CComponent {
private $_yiiSAML = null;
static private function pre() {
require_once (Yii::app()->params['simpleSAML'] . '/lib/_autoload.php');
// temporary disable Yii autoloader
spl_autoload_unregister(array(
'YiiBase',
'autoload'
));
}
static private function post() {
// enable Yii autoloader
spl_autoload_register(array(
'YiiBase',
'autoload'
));
}
public function __construct() {
self::pre();
//We select our authentication source:
$this->_yiiSAML = new SimpleSAML_Auth_Simple(Yii::app()->params['authSource']);
self::post();
}
static public function loggedOut($param, $stage) {
self::pre();
$state = SimpleSAML_Auth_State::loadState($param, $stage);
self::post();
if (isset($state['saml:sp:LogoutStatus'])) {
$ls = $state['saml:sp:LogoutStatus']; /* Only for SAML SP */
} else return true;
return $ls['Code'] === 'urn:oasis:names:tc:SAML:2.0:status:Success' && !isset($ls['SubCode']);
}
public function __call($method, $args) {
$params = (is_array($args) and !empty($args)) ? $args[0] : $args;
if (method_exists($this->_yiiSAML, $method)) return $this->_yiiSAML->$method($params);
else throw new YiiSAMLException(Yii::t('app', 'The method {method} does not exist in the SAML class', array(
'{method}' => $method
)));
}
}
class YiiSAMLException extends CException {
}
Then you define a filter extending the CFilter Yii class:
<?php
Yii::import('lib.YiiSAML');
class SAMLControl extends CFilter {
protected function preFilter($filterChain) {
$msg = Yii::t('yii', 'You are not authorized to perform this action.');
$saml = new YiiSAML();
if (Yii::app()->user->isGuest) {
Yii::app()->user->loginRequired();
return false;
} else {
$saml_attributes = $saml->getAttributes();
if (!$saml->isAuthenticated() or Yii::app()->user->id != $saml_attributes['User.id'][0]) {
Yii::app()->user->logout();
Yii::app()->user->loginRequired();
return false;
}
return true;
}
}
}
And finally, in the controllers you are interested to restrict, you override the filters() method:
public function filters() {
return array(
array(
'lib.SAMLControl'
) , // perform access control for CRUD operations
...
);
}
Hope it helps.
It can be done simply using "vendors" directory.
Download PHP Library from https://simplesamlphp.org/
Implement it in Yii Framework as a vendor library. (http://www.yiiframework.com/doc/guide/1.1/en/extension.integration)
Good Luck :)
I came across an Yii Extension for SimpleSAMLphp in github
https://github.com/asasmoyo/yii-simplesamlphp
You can load the simplesamlphp as a vendor library and then specify the autoload file in the extension.
Apart from the extension you can copy all the necessary configs and metadatas into the application and configure SimpleSAML Configuration to load the configurations from your directory, so you can keep the vendor package untouched for future updates.

Prestashop module installs but does not work as expected

I have a prestashop plugin that adds an extra tab to the product page:
<?php
// Disable direct addressing to the script:
if (!defined('_PS_VERSION_'))
exit;
//Create module class:
class producttab extends Module {
//Class constructor that contains its configuration:
public function __construct()
{
$this->name = "producttab"; //Module name
$this->tab = "front_office_features"; //Tab with the module in Prestashop back-office modules list
$this->version = "1.0"; // Module version
$this->author = "BelVG"; // Module author
parent::__construct();
$this->displayName = $this->l("Product Tab"); // Module title
$this->description = $this->l("Module creates a new tab on the frontend product page "); // Module description
}
//Module installation-method:
public function install()
{
return (parent::install()
AND $this->registerHook('productTab') //Register productTab hook that will display the tab button
AND $this->registerHook('productTabContent') //Register productTabContent hook that will display the tab content
);
}
//Module deinstallation-method:
public function uninstall()
{
return (parent::uninstall()
AND $this->unregisterHook('productTab')
AND $this->unregisterHook('productTabContent')); // Delete all hooks, registered by the module
}
//Method will be called while performing the "ProductTab" hook (tab buttons generation):
public function hookProductTab($params)
{
global $smarty;
//Call the template containing the HTML-code ?? our button
return $this->display(__FILE__ , 'tpl/productTab.tpl');
}
public function hookProductTabContent($params)
{
global $smarty;
//Transfer the new tab content into template via smatry
//( it is optional as far as the content can be assigned directly in the template)
$result = Db::getInstance()->executeS('SELECT * FROM ps_cms_lang WHERE id_cms =14');
$smarty->assign('content', $result);
// Call the template containing the HTML-code of our new tab content:
return $this->display(__FILE__ , 'tpl/productTabContent.tpl');
}
}
?>
The module works as expected. I'm trying to adapt the same code to add another tab. For some reason the new module installs but the tab does not appear. Is there something I'm missing?
<?php
// Disable direct addressing to the script:
if (!defined('_PS_VERSION_'))
exit;
//Create module class:
class jewellerytab extends Module {
//Class constructor that contains its configuration:
public function __construct()
{
$this->name = "jewellerytab"; //Module name
$this->tab = "front_office_features"; //Tab with the module in Prestashop back-office modules list
$this->version = "1.0"; // Module version
$this->author = "Mike Rifgin"; // Module author
parent::__construct();
$this->displayName = $this->l("jewellery Tab"); // Module title
$this->description = $this->l("Module creates a new tab on the frontend jewellery page "); // Module description
}
//Module installation-method:
public function install()
{
return (parent::install()
AND $this->registerHook('jewelleryTab') //Register jewelleryTab hook that will display the tab button
AND $this->registerHook('jewelleryTabContent') //Register jewelleryTabContent hook that will display the tab content
);
}
//Module deinstallation-method:
public function uninstall()
{
return (parent::uninstall()
AND $this->unregisterHook('jewelleryTab')
AND $this->unregisterHook('jewelleryTabContent')); // Delete all hooks, registered by the module
}
//Method will be called while performing the "jewelleryTab" hook (tab buttons generation):
public function hookjewelleryTab($params)
{
global $smarty;
//Call the template containing the HTML-code ?? our button
return $this->display(__FILE__ , 'tpl/jewelleryTab.tpl');
}
public function hookjewelleryTabContent($params)
{
global $smarty;
//Transfer the new tab content into template via smatry
//( it is optional as far as the content can be assigned directly in the template)
$result = Db::getInstance()->executeS('SELECT * FROM ps_cms_lang WHERE id_cms =14');
$smarty->assign('content', $result);
// Call the template containing the HTML-code of our new tab content:
return $this->display(__FILE__ , 'tpl/jewelleryTabContent.tpl');
}
}
?>
There are no such hooks as jewelleryTab and jewelleryTabContent, you need to work with productTab and productTabContent (which is part of Prestashop core) Here you can find list of hooks for prestahop 1.5 http://doc.prestashop.com/display/PS15/Hooks+in+PrestaShop+1.5 and some basic info on what hooks in prestashop are http://doc.prestashop.com/display/PS14/Understanding+and+using+hooks
You can also try to reach Denis, author of this extension from BelVG
And after this article Denis developed flexible extension to add extra product tabs
jewelleryTab and jewelleryTabContent are custom hook.
You need to add these hook to your .tpl template file:
{hook h='jewelleryTab'}
{hook h='jewelleryTabContent'}
We can only use predefined hooks.
You can use the same hooks as instead of jewelleryTab and jewelleryTabContent.
Hopefully the hooks can be reused.
public function install()
{
return (parent::install()
AND $this->registerHook('productTab') //Register productTab hook that will display the tab button
AND $this->registerHook('productTabContent') //Register productTabContent hook that will display the tab content
);
}

Can not find model class in module application

I'm new in Zend, i had defined in my application.ini some lines to use multiple db.
resources.multidb.sitgm.adapter = "pdo_pgsql"
resources.multidb.sitgm.host = "localhost"
resources.multidb.sitgm.username = "postgres"
resources.multidb.sitgm.password = "pass"
resources.multidb.sitgm.dbname = "mydb"
resources.multidb.sitgm.isDefaultTableAdapter = true
In my APPLICATION Bootstrap i have a function:
public function _initDbRegistry()
{
$this->_application->bootstrap('multidb');
$multidb = $this->_application->getPluginResource('multidb');
Zend_Registry::set('db_sitgm', $multidb->getDb('sitgm'));
}
But when i had migrated to module squema, i have a default module, i added another DEFAULT Bootstrap.
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initDbRegistry()
{
//Do i must add something here to access application DB conf like app bootstrap????
}
}
In this point How i can call the application config beacuse i am getting an error in my default model class which can not find it.
class Default_Model_Base {
protected $db;
public $sql="";
function __construct() {
$this->db = Zend_Registry::get("db_sitgm"); //HERE I GOT THE ERROR
$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
}
}
Thanks in advance
You don't have to define the _initDbRegistry in your module bootstrap as well. You can leave it in your application Bootstrap.php