How to print data to Panel Pane in Drupal 7? - php

I am trying to send data to a Panel Pane.
I have a simple .tpl file that for now just prints a message:
<?php print $message; ?>
However, I am having trouble sending the message data to this .tpl. The above print returns nothing.
On my .module file I have:
/**
* Implements hook_ctools_plugin_directory().
*/
function message_ctools_plugin_directory($owner, $plugin_type) {
if (($owner === 'ctools' && !empty($plugin_type)) || ($owner === 'panels' && $plugin_type === 'styles')) {
return "plugins/$plugin_type";
}
}
/**
* Implements hook_theme().
*/
function message_theme() {
return [
'message_base' => [
'template' => 'theme/message',
'variables' => [
'message' => '',
],
],
];
}
And on the panel .inc file I have:
function message_panel_render($subtype, $conf, $args, $contexts) {
$block = new stdClass();
$config = default_settings();
$block->content = [
'#theme' => 'message_base',
'message' => filter_xss($config['message']),
];
return $block;
}
When I dpm($block); within the above function I see all is correct. It's just nothing seems to reach the .tpl file.
Would anyone know what I've done wrong here?

Found it worked with a # at '#message' => filter_xss($config['message']),

Related

How can fixup dbforge problem in codeigniter -4

When I am trying to insert new column using codeigniter version 4 with dbforge.
I got an error in Production mode:
Whoops!We seem to have hit a snag. Please try again later...
public function addLanguage()
{
$language = preg_replace('/[^a-zA-Z0-9_]/', '', $this->request->getPost('language',FILTER_SANITIZE_STRING));
$language = strtolower($language);
if (!empty($language)) {
if (!$this->db->fieldExists($language, "language")) {
$this->dbforge->addColumn("language", [
$language => [
'type' => 'TEXT'
]
]);
$this->session->setFlashdata('message', 'Language added successfully');
return redirect()->route('backend/setting/language');
}
} else {
$this->session->setFlashdata('exception', display('please_try_again'));
}
return redirect()->route('backend/setting/language');
}
What am I doing wrong in this code? Any potential help would be greatly appreciated!
Development mode error image below:
You can use this code
public function addLanguage()
{
$dbforge = \Config\Database::forge();
$language = preg_replace('/[^a-zA-Z0-9_]/', '', $this->request->getPost('language',FILTER_SANITIZE_STRING));
$language = strtolower($language);
if (!empty($language)) {
if (!$this->db->fieldExists($language, "language")) {
$dbforge->addColumn("language", [
$language => [
'type' => 'TEXT'
]
]);
$this->session->setFlashdata('message', 'Language added successfully');
return redirect()->route('backend/setting/language');
}
} else {
$this->session->setFlashdata('exception', display('please_try_again'));
}
return redirect()->route('backend/setting/language');
}

create a custom controller back office on Custom module in prestashop

Firstly, I tried all the questions & answers related to this topic. Additionally and I tried related questions and try to solve it but no success. So please read my question thoroughly.
1) i want to create a custom controller on custom module in prestashop without tab.and get browser url Link.
2) how to create a controller url link with tocken on twig file.
i have successfully created module and installed in my PS.
i create a controller [Checkstatus.php]
file path module/mymodule/contollers/admin/Checkstatus.php
<?php
class CheckstatusController extends ModuleAdminController {
public function __construct()
{
$this->page_name = 'checkstatus'; // page_name and body id
echo "sfg";
parent::__construct();
}
public function init()
{
parenrt::init();
}
public function demoAction()
{
return $this->render('#Modules/your-module/templates/admin/demo.html.twig');
}
}
my Custom module
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class MyModule extends PaymentModule
{
public function __construct()
{
$this->name = 'MyCustomModule';
$this->tab = 'payments XYZ';
$this->version = '1.0';
$this->author = 'XYZ Technologies';
$this->bootstrap = true;
$this->displayName = 'XYZ';
$this->description = 'XYZ.';
$this->confirmUninstall = 'Are you sure you want to uninstall XYZ module?';
$this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);
$this->allow_countries = array('CH', 'LI', 'AT', 'DE');
$this->allow_currencies = array('CHF', 'EUR');
parent::__construct();
}
/**
* Install this module and register the following Hooks:
*
* #return bool
*/
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'MyCustomModule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(255) NOT NULL,
`MyCustomModule` int(255) DEFAULT NULL,
`lastcheck_date` date,
`add_date` date,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
');
return parent::install() && $this->registerHook('Statusbtnoncustomerview');
}
/**
* Uninstall this module and remove it from all hooks
*
* #return bool
*/
public function uninstall()
{
return parent::uninstall() && $this->uninstallDb() && $this->unregisterHook('Statusbtnoncustomerview');
}
public function uninstallDb()
{
return Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'MyCustomModule');
}
public function hookStatusbtnoncustomerview()
{
/**
* Verify if this module is enabled
*/
if (!$this->active) {
return;
}
return $this->fetch('module:MyCustomModule/views/templates/hook/personal_information.html.twig');
}
/**
* Returns a string containing the HTML necessary to
* generate a configuration screen on the admin
*
* #return string
*/
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name)) {
// get configuration fields value
$MyCustomModule_Account_Data = strval(Tools::getValue('MyCustomModule_Account_Data'));
$credit_Checkbox = strval(Tools::getValue('credit_Checkbox_1'));
$interval_Month = strval(Tools::getValue('Interval_Month'));
if (
!$MyCustomModule_Account_Data ||
empty($MyCustomModule_Account_Data) ||
!Validate::isGenericName($MyCustomModule_Account_Data)
) {
$output .= $this->displayError($this->l('Please Enter MyCustomModule Account Data.'));
} else{
// Update configuration fields value
Configuration::updateValue('MyCustomModule_Account_Data', $MyCustomModule_Account_Data);
Configuration::updateValue('credit_Checkbox_1', $credit_Checkbox);
Configuration::updateValue('Interval_Month', $interval_Month);
// Display message after successfully submit value
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output.$this->displayForm();
}
/**
* Display a form
*
* #param array $params
* #return form html using helper form
*/
public function displayForm()
{
// Get default language
$defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
$credit_Checkbox = [
[
'id'=>1,
'name'=>'',
'val' => 1
]
];
// Init Fields form array
$fieldsForm[0]['form'] = [
'legend' => [
'title' => $this->l('Configuration'),
],
'input' => [
[
'type' => 'text',
'label' => $this->l('MyCustomModule Account Data'),
'name' => 'MyCustomModule_Account_Data',
'required' => true
],
[
'type'=>'checkbox',
'label'=> $this->l('credit'),
'name'=>'credit_Checkbox',
'values'=>[
'query'=>$credit_Checkbox,
'id'=>'id',
'name'=>'name'
]
],
[
'type' => 'html',
'html_content' => '<input type="number" min="0" step="1" value="'.Configuration::get('Interval_Month').'" name="Interval_Month">',
'label' => $this->l('interval Month'),
'name' => 'Interval_Month',
'size' => 20
],
],
'submit' => [
'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 = $defaultLang;
$helper->allow_employee_form_lang = $defaultLang;
// 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 = [
'save' => [
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
],
'back' => [
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
]
];
// Load current value
$helper->fields_value['MyCustomModule_Account_Data'] = Configuration::get('MyCustomModule_Account_Data');
$helper->fields_value['credit_Checkbox_1'] = Configuration::get('credit_Checkbox_1');
$helper->fields_value['Interval_Month'] = Configuration::get('Interval_Month');
return $helper->generateForm($fieldsForm);
}
}
i trying this url : http://localhost/prestashop/admin482vzxnel/index.php?module=mymodule&controller=checkstatus
geting error :
Page not found
The controller checkstatus is missing or invalid.
Thanks
To include a Class in a module you can use php require. Once included in module main file you can create an instance of you class where you want in your module.
To assign variable that you can use on template there is $this->context->smarty->assig() function.
The code Below is just an example of how include and use a Class in a custom module Controller.
Even the edit in the hookStatusbtnoncustomerview() is an example of how retrieve the module link and the link on his method, so take the code below such as it is, only an example
require_once (dirname(__FILE__)).'contollers/admin/Checkstatus.php';
class MyModule extends PaymentModule
{
public function __construct()
{
$this->name = 'MyCustomModule';
$this->tab = 'payments XYZ';
$this->version = '1.0';
$this->author = 'XYZ Technologies';
$this->bootstrap = true;
$this->displayName = 'XYZ';
$this->description = 'XYZ.';
$this->confirmUninstall = 'Are you sure you want to uninstall XYZ module?';
$this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);
$this->allow_countries = array('CH', 'LI', 'AT', 'DE');
$this->allow_currencies = array('CHF', 'EUR');
$this->checkController = null;
parent::__construct();
}
private function _useCheckstatus(){
$this->checkController = new CheckstatusController();
}
public function MyMethod (){
$this->_useCheckstatus();
$this->checkController->demoAction();
}
public function hookStatusbtnoncustomerview()
{
/**
* Verify if this module is enabled
*/
if (!$this->active) {
return;
}
$this->context->smarty->assign(
array(
'my_module_name' => Configuration::get('MyCustomModule'),
'my_module_link' => $this->context->link->getModuleLink('MyCustomModule', 'mymethod'),
'token' => Tools::getToken(false)
)
);
return $this->fetch('module:MyCustomModule/views/templates/hook/personal_information.html.twig');
}
.........
it's not easy to investigate how prestashop works, so my tip is: look at the code from other modules, and take example from them.
Instead if you need to override a Core Prestashop Controller in your module you have to put the override file in /mymodule/override/controllers/admin/ModuleAdminController.php. installing/resetting the module will put this file within root/override/controllers/admin/ folder and the override becomes active. just remember to clear PS cache in "advanced settings->performance"

Prestashop tab creation, specific page for each tabs

Somehow, I have to create admin pages of my module. And this is how I am creating tabs
private function createTab()
{
$data = array(
'id_tab' => '',
'id_parent' => 0,
'class_name' => 'AdminSomeMenu',
'module' => $this->name,
'position' => 1, 'active' => 1
);
$res = Db::getInstance()->insert('tab', $data);
$id_tab = Db::getInstance()->Insert_ID();
$lang = (int)Configuration::get('PS_LANG_DEFAULT');
//Define tab multi language data
$data_lang = array(
'id_tab' => $id_tab,
'id_lang' => $lang,
'name' => $this->name
);
// Now insert the tab lang data
$res &= Db::getInstance()->insert('tab_lang', $data_lang);
$arrayTabs = array('TAB1','TAB2','TAB3');
foreach ($arrayTabs as $requiredTabs)
{
$tab = new Tab();
// Need a foreach for the language
$tab->name[$lang] = $this->l($requiredTabs);
$tab->class_name = 'Admin'.$requiredTabs;
$tab->id_parent = $id_tab;
$tab->module = $this->name;
$tab->add();
}
return true;
}
I hope I am going fine.
Once the tabs are created am trying linking with the following code.
<?php
class AdminMenuController extends ModuleAdminController
{
public function __construct()
{
$module = "mymodulename"
Tools::redirectAdmin('index.php?controller=AdminModules&configure='.$module.'&token='.Tools::getAdminTokenLite('AdminModules'));
}
}
This way the controller not found was gone. But I can create only the configure page through such link.
How should I go to achieve personalized page for each tabs.
Ah ! That was a missing parent::__construct(); in controllers causing the problem for not letting tabs behave the way they should.
<?php
class AdminTAB1Controller extends ModuleAdminController
{
public function __construct()
{
parent::__construct();
/* Tools::redirectAdmin('index.php?controller=AdminModules&configure='.$module.'&token='.Tools::getAdminTokenLite('AdminModules')); */
echo "Support page";
/* or further function can be called to load tpl files from views/templates/admin/ */
}
}
Now I have different pages for my tabs in both PS 1.6 and 1.7 !

Yii2 Dektrium User, make login required for all actions and all controllers

To make login necessary for all controllers and actions I did as said in Yii2 require all Controller and Action to login and added the below code to web.php
'as beforeRequest' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'allow' => true,
'actions' => ['login', 'forgot'],
],
[
'allow' => true,
'roles' => ['#'],
],
],
'denyCallback' => function () {
return Yii::$app->response->redirect(['user/login']);
},
],
but the problem is that ALL other actions like Forgot password are redirected to login page, I want to exclude user/forgot route from the login required condition. please help!
Thanks
I know I'm 3-years late, but it could be useful for other people searching for this answer :)
In config/web.php
$config => [
/* ... */
'as AccessBehavior' => [
'class' => 'app\components\AccessBehavior',
'allowedRoutes' => [
'/auth/register',
'/auth/forgot',
'/auth/resend',
],
'redirectUri' => '/auth/login',
],
/* ... */
Then create a "components" folder in your root project and create a "components\AccessBehavior.php" file with the following code:
<?php
namespace app\components;
use Yii;
use yii\base\Behavior;
use yii\console\Controller;
use yii\helpers\Url;
class AccessBehavior extends Behavior
{
protected $redirectUri;
protected $allowedRoutes = [];
protected $allowedUrls = [];
public function setRedirectUri($uri)
{
$this->redirectUri = $uri;
}
public function setAllowedRoutes(array $routes)
{
if (count($routes)) {
foreach ($routes as $route) {
$this->allowedUrls[] = Url::to($route);
}
}
$this->allowedRoutes = $routes;
}
public function init()
{
if (empty($this->redirectUri)) {
$this->redirectUri = Yii::$app->getUser()->loginUrl;
}
}
private function removeParams()
{
//enabled pretty url
if (strpos(Yii::$app->getRequest()->url, "?") === false)
{
$requestUrl = explode('/', Yii::$app->getRequest()->url);
$params = array_values(Yii::$app->getRequest()->queryParams);
$result = implode('/', array_diff($requestUrl, $params));
}
else
{//not enabled pretty url
$result = explode("?", \Yii::$app->getRequest()->url);
}
return $result;
}
public function events()
{
return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
}
public function beforeAction()
{
$requestUrl = $this->removeParams();
if (Yii::$app->user->isGuest)
{
if ($requestUrl !== Url::to($this->redirectUri) && !in_array($requestUrl, $this->allowedUrls))
{
Yii::$app->getResponse()->redirect($this->redirectUri)->send();
exit(0);
}
}
}
}
This code simply checks if user is logged and checks the route requested. If guest user is accessing to allowed routes (you can add allowed routes in the config) does nothing, else redirects user to the login page :)
In the code above, I set the dektrium prefix route as "auth". Of course in the allowed route, you have to set the route you actually use to make the user register, confirm, change password..
Haven't tested it but it should work.
'denyCallback'=>function() {
if($this->action->id == 'forgot')
return Yii::$app->response->redirect(['whatever/whatever']);
else
return Yii::$app->response->redirect(['user/login']);
},...

Zend Framework Custom filter in own libary directory

I'm trying to make my custom filter work...
I have the following code in my AuthController:
<?php
public function loginAction()
{
// Get db var
$db = $this->_getParam('db');
// Load loginform
$loginForm = new Application_Form_Auth_Login();
// Form posted?
if ($loginForm->isValid($_POST))
{
// Setup adapter
$adapter = new Zend_Auth_Adapter_DbTable(
$db,
'users',
'username',
'password'
);
// Set identity and credential
$adapter->setIdentity($loginForm->getValue('username'));
$adapter->setCredential($loginForm->getValue('password'));
// Setup Zend_Auth and try to authenticate the user
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
// If authentication succeed
if ($result->isValid())
{
$this->_helper->FlashMessenger('Inloggen geslaagd');
$this->_redirect('/');
return;
}
else
{
$this->_helper->FlashMessenger('Inloggen geslaagd');
}
}
$this->view->loginForm = $loginForm;
}
?>
The code of the form is:
<?php
class Application_Form_Auth_Login extends Zend_Form
{
/**
* Default_Form_Auth_Login::init()
*
* Form which authenticates guests
*
* #return void
*/
public function init()
{
$this->setMethod('post');
$this->addElement('text', 'username', array(
'label' => 'Gebruikersnaam:',
'required' => true,
'filters' => array('StringTrim'),
));
$this->addElement('password', 'password', array(
'label' => 'Wachtwoord:',
'required' => true,
'filters' => array('Pcw_Filter_Hash')
));
$this->addElement('hash', 'formToken', array(
'salt' => 'unique'
));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Inloggen',
));
}
}
The code of my custom filter is:
<?php
class Pcw_Filter_Hash implements Zend_Filter_interface
{
/**
* HashFilter::filter()
*
* #param string $value
* #return
*/
public function filter($value)
{
return hash('sha512', $value);
}
}
When using it this way I keep getting this message:
Message: Plugin by name 'Pcw_Filter_Hash' was not found in the registry; used paths: Zend_Filter_: Zend/Filter/
I have found documentation about setting namespaces and adding paths but I can't get anything to work...
Does anyone has a working solution for my problem? This will be highly apprectiated!
Thanks in advance!
You have to add the path to your filter in your from
<?php
class Application_Form_Auth_Login extends Zend_Form
{
public function init()
{
// add the path where own filters are located
$this->addElementPrefixPath(
'Pcw_Filter',
APPLICATION_PATH . '/../library/Pwc/Filter',
'filter'
);
$this->setMethod('post');
...
}
}
Maybe you have to change the path to fit your own layout of your application.
in you application.ini add this line autoloaderNamespaces[] = "Pcw_" next make sure the file is named Hash.php and that it lives at /application/libray/Pcw/Filter and the class name needs to remain Pcw_Filter_Hash if you do that the autoloader should find it.
I would rather rewrite you form elements like this one :
$password = new Zend_Form_Element_Password("password");
$password->setLabel("Wachtwoord")
->setRequired(true);
$password->addFilter(new Pcw_Filter_Hash() );
but I am not sure this might work :
$this->addElement('password', 'password', array(
'label' => 'Wachtwoord:',
'required' => true,
'filters' => array(new Pcw_Filter_Hash())
));
and you should double check that Pcw is defined in application.ini
I hope your problem get solved soon :)

Categories