I've got a syntax error on my foreach. I want to select from my backoffice a text file which write all eamil from site. I want to only get email from my database and put them into a text file write in my hard disk.
<?php
if (!defined('_PS_VERSION_'))
exit;
class SuperModule extends Module
{
public function __construct()
{
$this->name = 'supermodule';
$this->tab = 'administration';
$this->version = 1.0;
$this->author = 'Lelu Matthias';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('My super module');
$this->description = $this->l('This module is super !');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('MYMODULE_NAME'))
$this->warning = $this->l('No name provided.');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
return parent::install() &&
$this->registerHook('displayNav') &&
$this->registerHook('header') &&
Configuration::updateValue('MYMODULE_NAME', 'super module') &&
Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS', 'Ouvert de 9h a 19h') && Configuration::updateValue('MOD_SUPERMODULE_NEWSLETTER', '0');
}
public function uninstall()
{
return parent::uninstall();
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit_openinghours'))
{
$supermodule_openinghours = Tools::getValue('MOD_SUPERMODULE_OPENINGHOURS');
if (!$supermodule_openinghours || empty($supermodule_openinghours) || !
Validate::isGenericName($supermodule_openinghours))
$output .= $this->displayError( $this->l('Invalid Configuration value') );
else
{
Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS',
$supermodule_openinghours);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
if (Tools::isSubmit('submit_exportnewsletter'))
{
$id_option = Tools::getValue('MOD_SUPERMODULE_NEWSLETTER');
$output .=$id_option;
$txt="";
if($id_option ==1)
{
(foreach $newsletter as $row)
{
$text.=$row['email'];}
$table='newsletter';
}
else
{
(foreach $customer as $row)
{
$text.=$row['email'];
}
$table='customer';
}
$date = gmdate('dmY');
$file = fopen(dirname(__FILE__).'/export-newsletter-'.$table.'-'.
$date.'.txt', 'w');
fputs($file, $txt);
fclose($file);
}
return $output.$this->displayForm1().$this->displayForm2();
}
public function displayForm1()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_form[0]['form'] = $this->formulaire1();
$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;
$helper->toolbar_scroll = true;
$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['MOD_SUPERMODULE_OPENINGHOURS'] =
Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
return $helper->generateForm($fields_form);
}
protected function formulaire1()
{
return array(
'legend' => array(
'title' => $this->l('Opening hours'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Opening hours'),
'name' => 'MOD_SUPERMODULE_OPENINGHOURS',
'size' => 20,
'required' => true
),
),
'submit' => array(
'name' => 'submit_openinghours',
'title' => $this->l('Save')
)
);
}
protected function formulaire2()
{
$options = array(
array(
'id_option' => 0,
'name' => $this->l('Customer table')
),
array(
'id_option' => 1,
'name' => $this->l('Newsletter table')
),
);
return array(
'legend' => array(
'title' => $this->l('Newsletter export'),
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Choice a table'),
'name' => 'MOD_SUPERMODULE_NEWSLETTER',
'desc' => $this->l('Please choice a table.'),
'options' => array(
'query' => $options,
'id' => 'id_option',
'name' => 'name'
),
),
),
'submit' => array(
'name' => 'submit_exportnewsletter',
'title' => $this->l('Save')
)
);
}
public function displayForm2()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_form[0]['form'] = $this->formulaire2();
$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;
$helper->toolbar_scroll = true;
$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['MOD_SUPERMODULE_NEWSLETTER'] =
Configuration::get('MOD_SUPERMODULE_NEWSLETTER');
return $helper->generateForm($fields_form);
}
public function hookDisplayNav($params)
{
$ma_variable = Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
$this->context->smarty->assign(array(
'ma_variable' => $ma_variable
));
return $this->display(__FILE__, 'supermodulenav.tpl');
}
public function hookDisplayHeader()
{
$this->context->controller->addCSS($this->_path.'css/supermodule.css', 'all');
}
public function GetMailNewsLetter()
{
$sql='SELECT email FROM '._DB_PREFIX_.'newsletter';
$newsletter=Db::getInstance()->executeS($sql);
return $newsletter;
}
public function GetMailCustomer()
{
$sql='SELECT email FROM '._DB_PREFIX_.'customer';
$customer=Db::getInstance()->executeS($sql);
return $customer;
}
}
Try this
foreach ($newsletter as $row)
Check the Manual here
Hey only change your (foreach $newsletter as $row) to foreach($newsletter as $row)
something like this
<?php
if (!defined('_PS_VERSION_'))
exit;
class SuperModule extends Module
{
public function __construct()
{
$this->name = 'supermodule';
$this->tab = 'administration';
$this->version = 1.0;
$this->author = 'Lelu Matthias';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('My super module');
$this->description = $this->l('This module is super !');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('MYMODULE_NAME'))
$this->warning = $this->l('No name provided.');
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
return parent::install() &&
$this->registerHook('displayNav') &&
$this->registerHook('header') &&
Configuration::updateValue('MYMODULE_NAME', 'super module') &&
Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS', 'Ouvert de 9h a 19h') && Configuration::updateValue('MOD_SUPERMODULE_NEWSLETTER', '0');
}
public function uninstall()
{
return parent::uninstall();
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit_openinghours'))
{
$supermodule_openinghours = Tools::getValue('MOD_SUPERMODULE_OPENINGHOURS');
if (!$supermodule_openinghours || empty($supermodule_openinghours) || !
Validate::isGenericName($supermodule_openinghours))
$output .= $this->displayError( $this->l('Invalid Configuration value') );
else
{
Configuration::updateValue('MOD_SUPERMODULE_OPENINGHOURS',
$supermodule_openinghours);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
if (Tools::isSubmit('submit_exportnewsletter'))
{
$id_option = Tools::getValue('MOD_SUPERMODULE_NEWSLETTER');
$output .=$id_option;
$txt="";
if($id_option ==1)
{
foreach ($newsletter as $row)
{
$text.=$row['email'];
}
$table='newsletter';
}
else
{
foreach ($customer as $row)
{
$text.=$row['email'];
}
$table='customer';
}
$date = gmdate('dmY');
$file = fopen(dirname(__FILE__).'/export-newsletter-'.$table.'-'.
$date.'.txt', 'w');
fputs($file, $txt);
fclose($file);
}
return $output.$this->displayForm1().$this->displayForm2();
}
public function displayForm1()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_form[0]['form'] = $this->formulaire1();
$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;
$helper->toolbar_scroll = true;
$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['MOD_SUPERMODULE_OPENINGHOURS'] =
Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
return $helper->generateForm($fields_form);
}
protected function formulaire1()
{
return array(
'legend' => array(
'title' => $this->l('Opening hours'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Opening hours'),
'name' => 'MOD_SUPERMODULE_OPENINGHOURS',
'size' => 20,
'required' => true
),
),
'submit' => array(
'name' => 'submit_openinghours',
'title' => $this->l('Save')
)
);
}
protected function formulaire2()
{
$options = array(
array(
'id_option' => 0,
'name' => $this->l('Customer table')
),
array(
'id_option' => 1,
'name' => $this->l('Newsletter table')
),
);
return array(
'legend' => array(
'title' => $this->l('Newsletter export'),
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Choice a table'),
'name' => 'MOD_SUPERMODULE_NEWSLETTER',
'desc' => $this->l('Please choice a table.'),
'options' => array(
'query' => $options,
'id' => 'id_option',
'name' => 'name'
),
),
),
'submit' => array(
'name' => 'submit_exportnewsletter',
'title' => $this->l('Save')
)
);
}
public function displayForm2()
{
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$fields_form[0]['form'] = $this->formulaire2();
$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;
$helper->toolbar_scroll = true;
$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['MOD_SUPERMODULE_NEWSLETTER'] =
Configuration::get('MOD_SUPERMODULE_NEWSLETTER');
return $helper->generateForm($fields_form);
}
public function hookDisplayNav($params)
{
$ma_variable = Configuration::get('MOD_SUPERMODULE_OPENINGHOURS');
$this->context->smarty->assign(array(
'ma_variable' => $ma_variable
));
return $this->display(__FILE__, 'supermodulenav.tpl');
}
public function hookDisplayHeader()
{
$this->context->controller->addCSS($this->_path.'css/supermodule.css', 'all');
}
public function GetMailNewsLetter()
{
$sql='SELECT email FROM '._DB_PREFIX_.'newsletter';
$newsletter=Db::getInstance()->executeS($sql);
return $newsletter;
}
public function GetMailCustomer()
{
$sql='SELECT email FROM '._DB_PREFIX_.'customer';
$customer=Db::getInstance()->executeS($sql);
return $customer;
}
}
I think it help you
You are using wrong foreach syntax.follow the given below code.also check this and this
(foreach $newsletter as $row) // it is not a correct syntax
foreach ($newsletter as $row) // this is a correct syntax
(foreach $customer as $row) // it is not a correct syntax
foreach ($customer as $row) // this is a correct syntax
wrong syntax:
(foreach $newsletter as $row)
Correct Syntax
foreach($newsletter as $row)
The correct syntax of foreach is
foreach($newsletter as $row)
{
}
Not
(foreach $newsletter as $row)
Related
I cloned a module of prestashop 1.7 and everything works perfectly without any problem. What I would like to do (please) is save the data in the prestashop database itself but I can't do it I hope I have explained myself better
code
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Stagenti extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'stagenti';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'Lab';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Lab Agenti');
$this->description = $this->l('Gestionale per agenti di commercio');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
}
public function install()
{
if (Shop::isFeatureActive()) Shop::setContext(Shop::CONTEXT_ALL);
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
//$values[] = Tools::getValue('SOMETEXT_TEXT_'.$lang['id_lang']);
Configuration::updateValue('STAGENTI_TEXT_' . $lang['id_lang'], '', true);
Configuration::updateValue('STAGENTI_TEXT2_' . $lang['id_lang'], '', true);
}
include(dirname(__FILE__).'/sql/install.php');
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('displayBanner');
}
public function uninstall()
{
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
//$values[] = Tools::getValue('SOMETEXT_TEXT_'.$lang['id_lang']);
Configuration::deleteByName('STAGENTI_TEXT_' . $lang['id_lang']);
Configuration::deleteByName('STAGENTI_TEXT2_' . $lang['id_lang']);
}
include(dirname(__FILE__).'/sql/uninstall.php');
return parent::uninstall();
}
public function getContent()
{
if (((bool)Tools::isSubmit('submitStagentiModule')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');
return $output . $this->renderForm();
}
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitStagentiModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
. '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Impostazioni'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'col' => 9,
'type' => 'text',
'desc' => $this->l('inserisci lo slogan che desideri far visualizzare'),
'name' => 'STAGENTI_TEXT2',
'label' => $this->l('Messaggio Slogan'),
'autoload_rte' => true,
'lang' => true
),
array(
'col' => 9,
'type' => 'textarea',
'desc' => $this->l('se inserisci una img le misure devono essere 1140 x 270 px'),
'name' => 'STAGENTI_TEXT',
'label' => $this->l('Messaggio riepilogo'),
'autoload_rte' => true,
'lang' => true
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
protected function getConfigFormValues()
{
$languages = Language::getLanguages(false);
$values = array();
foreach ($languages as $lang) {
$values['STAGENTI_TEXT'][$lang['id_lang']] = Configuration::get('STAGENTI_TEXT_' . $lang['id_lang']);
$values['STAGENTI_TEXT2'][$lang['id_lang']] = Configuration::get('STAGENTI_TEXT2_' . $lang['id_lang']);
}
return $values;
}
protected function postProcess()
{
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
Configuration::updateValue('STAGENTI_TEXT_' . $lang['id_lang'], Tools::getValue('STAGENTI_TEXT_' . $lang['id_lang']), true);
Configuration::updateValue('STAGENTI_TEXT2_' . $lang['id_lang'], Tools::getValue('STAGENTI_TEXT2_' . $lang['id_lang']), true);
}
}
public function hookBackOfficeHeader()
{
if (Tools::getValue('module_name') == $this->name) {
$this->context->controller->addJS($this->_path . 'views/js/back.js');
$this->context->controller->addCSS($this->_path . 'views/css/back.css');
}
}
public function hookHeader()
{
$this->context->controller->addJS($this->_path . 'views/js/front.js');
$this->context->controller->addCSS($this->_path . 'views/css/front.css');
}
public function hookDisplayBanner()
{
$some_string = Configuration::get('STAGENTI_TEXT_' . $this->context->language->id);
$this->context->smarty->assign([
'module_dir' => $this->_path,
'stagenti_message' => $some_string
]);
$some_string = Configuration::get('STAGENTI_TEXT2_' . $this->context->language->id);
$this->context->smarty->assign([
'module_dir' => $this->_path,
'stagenti_message5' => $some_string
]);
return $this->display(__FILE__, 'message.tpl');
}
}
database
<?php
$sql = array();
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . '` (
`id_stagenti` int(11) NOT NULL AUTO_INCREMENT,
`stagenti_text` text NOT NULL,
`stagenti_text2` text NOT NULL,
PRIMARY KEY (`id_infomess`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
foreach ($sql as $query) {
if (Db::getInstance()->execute($query) == false) {
return false;
}
}
Db::getInstance()->insert('your_table', array(
'stagenti_text' => pSQL($value1),
'stagenti_text2' => pSQL($value2),
));
i'm trying to develop a PrestaShop module with controllers i placed, for example in:
/modules/mymodule/controllers/admin/myControlController.php
class MyControlController extends ModuleAdminController {
public function __construct() {
$this->module = 'mymodule';
$this->bootstrap = true;
$this->context = Context::getContext();
$token = Tools::getAdminTokenLite('AdminModules');
$currentIndex='index.php?controller=AdminModules&token='.$token.'&configure=mymodule&tab_module=administration&module_name=mymodule';
Tools::redirectAdmin($currentIndex);
parent::__construct();
}
public function showForm() {
die("hello");
}}
Controller works (construct method is called) if i call it form url
http://myshop.com/adminxxx/index.php?controller=MyControl&token=9faf638aa961468c8563ffb030b3c4a8
But i can't access methods of controller from main class of module:
ModuleAdminController::getController('MyControl')->showForm();
I received "Class not found" ever
Is that the correct method to access a control from outside?
Thanks!
If you want to show anything that concern a form you should use renderForm().
Your should try parent::showForm(); or $this->showForm();.
Here is an example of controller that can work :
require_once _PS_MODULE_DIR_.'modulename/models/ModuleNameLog.php';
require_once _PS_MODULE_DIR_.'modulename/modulename.php';
class AdminModuleNameLogController extends ModuleAdminController
{
protected $_defaultOrderBy = 'id_modulenamelog';
protected $_defaultOrderWay = 'DESC';
public function __construct()
{
$this->table = 'modulenamelog';
$this->className = 'ModuleNameLog';
$this->context = Context::getContext();
$this->lang = false;
$this->bootstrap = true;
$this->actions_available = array();
$this->actions = array();
$this->show_toolbar = false;
$this->toolbar_btn['new'] = array();
$this->tabAccess['add'] = '0';
$this->allow_export = true;
$this->requiredDatabase = true;
$this->page_header_toolbar_title = $this->l('Example Module Name logs');
$this->_select = 'SUM(a.quantity) as total_quantity';
$this->_group = ' GROUP BY a.id_product, a.id_product_attribute ';
$this->fields_list = array(
'id_product' => array(
'title' => $this->l('Product'),
'align' => 'center',
'callback' => 'getProductName',
),
'id_product_attribute' => array(
'title' => $this->l('Combination'),
'align' => 'center',
'callback' => 'getAttributeName',
),
'total_quantity' => array(
'title' => $this->l('Total Quantity'),
'align' => 'center',
),
);
$this->mod = new ModuleName();
$this->mod->cleanLogs();
$this->context = Context::getContext();
parent::__construct();
}
public function getProductName($id)
{
if (!empty($id)) {
$product = new Product($id, true, $this->context->cookie->id_lang);
return $product->name;
}
}
public function getAttributeName($id)
{
if (!empty($id)) {
$combination = new Combination($id);
$names = $combination->getAttributesName($this->context->cookie->id_lang);
$str = array();
if (!empty($names)) {
foreach ($names as $value) {
$str[] = $value['name'];
}
}
return implode(' - ', $str);
} else {
return '-';
}
}
public function postProcess()
{
if (Tools::isSubmit('purge_id')) {
// Do something here
$id = (int) Tools::getValue('purge_id');
Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModuleNameLog').'&conf=4');
}
parent::postProcess();
}
public function renderList()
{
$carts = Db::getInstance()->executeS('SELECT ct.*, cs.`firstname`, cs.`lastname` FROM '._DB_PREFIX_.'cart ct LEFT JOIN '._DB_PREFIX_.'customer cs ON ct.id_customer = cs.id_customer WHERE 1 ORDER BY id_cart DESC LIMIT 0,2000');
$tpl = $this->context->smarty->createTemplate(_PS_MODULE_DIR_.'modulename/views/templates/admin/preform.tpl');
$tpl->assign(array(
'carts' => $carts,
));
$html = $tpl->fetch();
return $html.parent::renderList();
}
public function renderForm()
{
if (!$this->loadObject(true)) {
return;
}
$obj = $this->loadObject(true);
if (isset($obj->id)) {
$this->display = 'edit';
} else {
$this->display = 'add';
}
$array_submit = array(
array(
'type' => 'select',
'label' => $this->l('Cart :'),
'name' => 'id_cart',
'options' => array(
'query' => Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'cart WHERE id_cart > 0 ORDER BY id_cart DESC LIMIT 0,500'),
'id' => 'id_cart',
'name' => 'id_cart',
),
),
array(
'type' => 'text',
'label' => $this->l('Quantity translation here'),
'hint' => $this->l('Description and translation here'),
'name' => 'quantity',
),
);
$this->fields_form[0]['form'] = array(
'tinymce' => false,
'legend' => array(
'title' => $this->l('Form title'),
),
'input' => $array_submit,
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default',
),
);
$this->multiple_fieldsets = true;
return parent::renderForm();
}
}
After installing prestashop custom module getting Fatal error: Call to undefined method when clicking the menu which is created by the module
Fatal error: Call to undefined method querydr::viewAccess() in D:\xampp\htdocs\raffleV1.3\oknr9hexztcseff5\functions.php on line 279
<?php
if (!defined('_PS_VERSION_')) exit;
class querydr extends Module
{
protected $config_form = false;
public $html;
public $tabName = 'renderForm';
public function __construct()
{
$this->name = 'querydr';
$this->tab = 'others';
$this->version = '1.0.0';
$this->author = 'xyz';
$this->need_instance = 1;
$this->module_key = 'f35950b303e7cbcda7fd8e56bb100d77121';
/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Query DR');
$this->description = $this->l('Allow your Back Office to Query the transactions ');
$this->confirmUninstall = $this->l('You want to Uninstall Query DR ?.');
$this->_tabsArray = array(
'Querydr' => 'Query DR',
);
if (!Configuration::get('QUERYDR'))
$this->warning = $this->l('No name provided');
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
include(dirname(__FILE__).'/sql/install.php');
return parent::install() &&
Configuration::updateValue('QUERYDR','Query Dr')
&& $this->_installTabs();
}
private function _installTabs()
{
$parentTab = new Tab();
foreach (Language::getLanguages() as $language) $parentTab->name[$language['id_lang']] = 'Query Dr';
$parentTab->class_name = 'Querydr';
$parentTab->module = $this->name;
$parentTab->id_parent = 0;
if (!$parentTab->save()) return false;
else {
$idTab = $parentTab->id;
//$idEn = Language::getIdByIso('en');
foreach ($this->_tabsArray as $tabKey => $name) {
$childTab = new Tab();
foreach (Language::getLanguages() as $language) $childTab->name[$language['id_lang']] = $name;
$childTab->class_name = $tabKey;
$childTab->module = $this->name;
$childTab->id_parent = $idTab;
if (!$childTab->save()) return false;
}
}
return true;
}
public function uninstall()
{
include(dirname(__FILE__).'/sql/uninstall.php');
Configuration::deleteByName('QUERYDR');
Configuration::deleteByName('QUERYDR_MERCHANT_ID');
Configuration::deleteByName('QUERYDR_SECRET_KEY');
$this->_uninstallTabs();
if (!parent::uninstall())
return false;
return true;
}
private function _uninstallTabs()
{
foreach ($this->_tabsArray as $tabKey => $name) {
$idTab = Tab::getIdFromClassName($tabKey);
if ($idTab != 0) {
$tab = new Tab($idTab);
$tab->delete();
}
}
$idTab = Tab::getIdFromClassName('QueryDr');
if ($idTab != 0) {
$tab = new Tab($idTab);
$tab->delete();
}
return true;
}
public function getContent()
{
if (Tools::isSubmit('submitQueryDrModule')) {
Configuration::updateValue('QUERYDR_MERCHANT_ID', Tools::getValue('QUERYDR_MERCHANT_ID'));
Configuration::updateValue('QUERYDR_SECRET_KEY', Tools::getValue('QUERYDR_SECRET_KEY'));
$this->html .= $this->displayConfirmation($this->l('Settings Updated'));
$this->tabName = 'renderForm';
}
if ($this->tabName) {
$this->html .= '<script>$(document).ready(function(){$("#'.$this->tabName.'").addClass("active");$(".'.$this->tabName.'").parents(".nav-tabs > li").addClass("active");});</script>';
} else {
$this->html .= '<script>$(document).ready(function(){$("#renderForm").addClass("active"); $(this).attr("href").indexOf("#renderForm").addClass("active");});</script>';
}
$this->html .= '<ul class="nav nav-tabs" role="tablist"><li ><a class="renderForm" href="#renderForm" role="tab" data-toggle="tab">SETTINGS</a></li></ul>';
$this->html .= '<div class="tab-content"><div class="tab-pane " id="renderForm">'.$this->renderForm_1_6().'</div></div>';
$this->html .='<style>div.flash.fail {color: #cd0a0a;background-color: #fef1ec;border: #cd0a0a 1px solid; padding: 0.5em;margin: 0 3px;margin-top: 22px;margin-bottom: 12px;text-align: center;border-radius: 2px 2px 2px 2px;}</style>';
return $this->html;
}
protected function renderForm_1_6()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'col' => 3,
'type' => 'text',
'desc' => $this->l('Your merchant ID ex: 201408191000001'),
'name' => 'QUERYDR_MERCHANT_ID',
'label' => $this->l('MERCHANT ID'),
), array(
'col' => 3,
'type' => 'text',
'desc' => $this->l('Your key provided by network international'),
'name' => 'QUERYDR_SECRET_KEY',
'label' => $this->l('SECRET KEY'),
)
),
'submit' => array(
'title' => $this->l('Save'),
),
)
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitQueryDrModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array(
$fields_form));
}
protected function getConfigFormValues()
{
return array(
'QUERYDR_MERCHANT_ID' => Configuration::get('QUERYDR_MERCHANT_ID'),
'QUERYDR_SECRET_KEY' => Configuration::get('QUERYDR_SECRET_KEY'),
);
}
}
My module link is here --
https://www.dropbox.com/s/juyhcu5zub8p9fi/querydr.zip?dl=0
I am not sure if it gonna help you but ... try to prefix your admin controller name with "Admin" like "AdminQuerydrController"and change it also in your tab installation
tab->class_name = 'AdminQuerydrController';
make sure to change also your controller php file name
"AdminQuerydrController.php"
I created mymodule following newest PrestaShop 1.6 documentation. Module is installed and uninstalled right, also css file is added right in header section. But there is no content is displayed in front office.
This is file /module/mymodule/mymodul.php
<?php
if (!defined('_PS_VERSION_'))
{exit;
}
class MyModule extends Module
{
public function __construct()
{
$this->name = 'mymodule';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'www12';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('My module');
$this->description = $this->l('Description of my module.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('MYMODULE_NAME'))
{$this->warning = $this->l('No name provided');}
}
public function install() {
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install() ||
!$this->registerHook('leftColumn') || !$this->registerHook('header') || !Configuration::updateValue('MYMODULE_NAME', 'my friend')
) {
return false;
} else {
return true;
}
}
public function uninstall() {
if (!parent::uninstall() ||
!Configuration::deleteByName('MYMODULE_NAME')
) {
return false;
} {
return true;
}
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit' . $this->name)) {
$my_module_name = strval(Tools::getValue('MYMODULE_NAME'));
if (!$my_module_name || empty($my_module_name) || !Validate::isGenericName($my_module_name)) {
$output .= $this->displayError($this->l('Invalid Configuration value'));
} else {
Configuration::updateValue('MYMODULE_NAME', $my_module_name);
$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' => array(
array(
'type' => 'text',
'label' => $this->l('Configuration value'),
'name' => 'MYMODULE_NAME',
'size' => 20,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$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['MYMODULE_NAME'] = Configuration::get('MYMODULE_NAME');
return $helper->generateForm($fields_form);
}
public function hookDisplayLeftColumn($params)
{
$this->context->smarty->assign(
array(
'my_module_name' => Configuration::get('MYMODULE_NAME'),
'my_module_link' => $this->context->link->getModuleLink('mymodule', 'display')
)
);
return $this->display(__FILE__, 'mymodule.tpl');
}
public function hookDisplayRightColumn($params)
{
return $this->hookDisplayLeftColumn($params);
}
public function hookDisplayHeader()
{
$this->context->controller->addCSS($this->_path.'css/mymodule.css', 'all');
}
}
and this is mymodule.tpl file located this same directory:
<!-- Block mymodule -->
<div id="mymodule_block_home" class="block">
<h4>Welcome!</h4>
<div class="block_content">
<p>Hello,
{if isset($my_module_name) && $my_module_name}
{$my_module_name}
{else}
World
{/if}
!
</p>
<ul>
<li>Click me!</li>
</ul>
</div>
</div>
<!-- /Block mymodule -->
I found similar topic but he forgoten important methodes. I did not :-(
If anybody has the same issue...The code is ok! The problem is that the tutorial says you will find the message on the home but it's not there because it doesn't have a left column.
You will find the message on the inner pages (any with a left column)
replace hookdisplayLeftColumn($params) by hookLeftColumn($params)
In your install () method :
$this->registerHook('leftColumn')
should be :
$this->registerHook('displayLeftColumn')
The name of the hook actually has "display" in it if you are referring to the hook that already exist in Prestashop(the one used in the front-office).
What you're actually doing here is creating a new hook named "leftColumn" that will get content from hook method named "hookLeftColumn". I think that's not what you want :)
I am using prestashop version 1.6 in localhost. I need to add our own video section in admin / back office to manage and display those videos in a separate page in front end. For example, about us and term and conditions pages like that.
I have developed my own module upto configuration section of the module. Now i need to add my own form fields like title, embedcode and submit button. I dont know how to add our own form fields with "helper class" of prestashop. Also i have read the documentation but could not able to get. Can you guide me please ? Thanks in advance and sorry for my english.
Here is my code:
videomodule.php:
<?php
if (!defined('_PS_VERSION_'))
exit;
class VideoModule extends Module
{
public function __construct()
{
$this->name = 'videomodule';
$this->tab = 'video';
$this->version = 1.0;
$this->author = 'Dinesh Kumar';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Video Module');
$this->description = $this->l('It is really an awesome experience');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
Configuration::updateValue('video', serialize(array(true, true, false)));
if (!Configuration::get('videomodule'))
$this->warning = $this->l('No name provided');
// echo Configuration::get('videoid');
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name))
{
$my_module_name = strval(Tools::getValue('videomodule'));
if (!$my_module_name
|| empty($my_module_name)
|| !Validate::isGenericName($my_module_name))
$output .= $this->displayError($this->l('Invalid Configuration value'));
else
{
Configuration::updateValue('videomodule', $my_module_name);
$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' => array(
array(
'type' => 'text',
'label' => $this->l('Configuration value'),
'name' => 'videomodule',
'size' => 20,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$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['videomodule'] = Configuration::get('videomodule');
return $helper->generateForm($fields_form);
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() ||
!$this->registerHook('leftColumn') ||
!$this->registerHook('header') ||
!Configuration::updateValue('videomodule', 'my friend')
)
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('videomodule')
)
return false;
return true;
}
public function hookLeftColumn( $params )
{
global $smarty;
return $this->display(__FILE__,'videomodule.tpl');
}
public function hookRightColumn($params)
{
return $this->hookLeftColumn($params);
}
}