I'm trying to figure out the best way to load a class into PrestaShop that I can use on overriding controllers.
I started off by creating a module that has an override of the Controller class with my files required at the top. Is there a better way to do this?
If you do not want to put class to override folder you can change class_index.php
Who add minus it is his problem but I'm right. If you do not want to use override folder you can set module folder with class
'SomeClass' =>
array (
'path' => 'modules/yourmodulename/class/class_name.php',
It is not normal but you can override without using override folder in particular circumstance.
And if you will read with carfull you will see: If you do not want to put class to override folder
EDITED 2015/10/19
It is craizy to add minus for what you do not know.
1) First look to PrestaShopAutoload class
2) all your class paths in array in the public $index in PrestaShopAutoload
3) what you need to do? just change this array
4) how to change this array? in your module you can get this instance using this PrestaShopAutoload::getInstance()
5) after you got instance you have full controll of your class paths!!! and you can add eny path what you need
6) what we need now? just make our module work maximum befour other class used. How we can do? Just hook in dispacher.
Related
I implemented a Prestashop module for Payment
beside of my main Module I generated a ModuleFrontController , which work fine and I have access on it by using URL like mydomain.com/module/controllers/mymethod
The problem is I would like to execute this method from my main module class / a simple php file
But I can not find a solution for that yet.
Note :
I Can not use CURL / open_socket
I can not call this URL from an Ajax
Simply I want to make an instance of my FrontController and call a method
Any Idea?
Thx
Include that controller file where you want to use and create instance of class and call desired method on that
FrontController define like this :
class ModulenameControllernameModuleFrontController extends ModuleFrontController
{
...
}
and make didn't accept like normal instance to call $obj = new Classname()
,... or when some how make an instance , wasn't work well. was missing some parameters in $this->context->XXX
Any way my problem solve some have different without need to make an instance .
But I think should be another way like how Cron execute
Ex. for Cron can send parameter like :
$_GET['fc'] = 'module';
$_GET['module'] = 'modulename';
$_GET['controller'] = 'controllername';
,....
Some how to call FrontController , independently
I am using Yii2 and want to create a attribute and access this attribute in every where in my project for exp:
Yii::$app->name
Above is an example of yii2 default name.
Is there any way to declare my custom attribute as following :
Yii::$app->supportname
You can extend Application class, add required properties, and use it on bootstrap.
Create class for web:
class MyApplication extends \yii\web\Application {
public $supportname;
}
And use it in index.php:
(new MyApplication($config))->run();
You need to do the same for \yii\console\Application and yii script.
But probably the best way is to use Application::$params and access value by Yii::$app->params['supportname'].
My Prestashop-based site is currently having an override for AdminOrdersController.php, I have placed it in override folder.
From the link provided below, it is perfectly working fine to add a Carrier filter which is not available in Prestashop 1.6 now. I have tried the solution and it is working perfectly.
Reference: Adding carrier filter in Orders page.
Unfortunately, for production site, I have no access to core files and unable to implement as such. Thus, I will need to create a custom module. Do take note that I already have an override in place for AdminOrdersController.php. I would like to tap on this override and insert the filter.
I have managed to create a module and tried placing an override (with the code provided in the URL) in mymodule/override/controller/admin/AdminOrdersController.php with the carrier filter feature.
There has been no changes/effect, I am baffled. Do I need to generate or copy any .tpl file?
Any guidance is greatly appreciated.
Thank you.
While the answer in the linked question works fine the same thing can be achieved with a module alone (no overrides needed).
Admin controllers have a hook for list fields modifications. There are two with the same name however they have different data in their params array.
actionControllernameListingFieldsModifier executes before a filter is applied to list.
actionControllernameListingFieldsModifier executes before data is pulled from DB and list is rendered.
So you can add fields to existing controller list definition like this in your module file:
public function hookActionAdminOrdersListingFieldsModifier($params) {
if (isset($params['select'])) {
$params['select'] .= ', cr.name';
$params['join'] .= ' LEFT JOIN `'._DB_PREFIX_.'carrier` cr ON (cr.`id_carrier` = a.`id_carrier`)';
}
$params['fields']['carrier'] = array(
'title' => $this->l('Carrier'),
'align' => 'text-center',
'filter_key' => 'cr!name'
);
}
Because array data is being passed into $params array by reference you can modify them in your hook and changes persist back to controller. This will append carrier column at the end of list.
It is prestashop best practice to try and solve problems through module hooks and only if there is really no way to do it with hooks, then do it with overrides.
Did you delete /cache/class_index.php ? You have to if you want your override to take effect.
If it still does not work, maybe you can process with the hook called in the AdminOrderControllers method with your new module.
I am developing a social networking website using Yii. While frequently using the following things I am having great data manageability issue.
- User ID
- Current user ID (the user which profile is the owner viewing)
- Is owner???
where can I define these things.
I would something like
if(Yii::app()->owner==ME){
//do something
}
// and similarly
if($this->isMyFreind(<Current user ID>){
}
// $this(CanIView()){
}
I want these functions to be public for any page? But how?
In other words
Where can I put my library which contains my own favorite functions like text shortening, image cropping, date time format etc etc??
In Yii, you can do achieve this by making a class (under protected/compoents) which inherits
CApplicationComponent
class. And then you call any property of this class globally as a component.
class GlobalDef extends CApplicationComponent {
public $aglobalvar;
}
Define this class in main config under components as:
'globaldef' => array('class' => 'application.components.GlobalDef '),
And you can call like this:
echo Yii::app()->globaldef->aglobalvar;
Hope that can help.
According to the MVC model, things like image cropping or date time formats would go in models. You would simply create models for that.
I usually use a globals.php file with all my common functions
In the index.php (yip the one in the root):
$globals='protected/globals.php';
require_once($globals);
I can then call my global functions anywhere. For example, I shorten certain Yii functions like:
function bu($url=null){
static $baseUrl;
if ($baseUrl===null)
$baseUrl=Yii::app()->request->baseUrl;
return $url===null ? $baseUrl : $baseUrl.'/'.ltrim($url,'/');
}
So I can then call the Yii::app()->request->baseUrl by simply calling bu()
I noticed that the latest version of slick-grid has a 'headerCssClass' attribute on the column, which would work great, except I am restricted to using slick-grid version 1.4.3.
I am extending slick-grid so as to add multiple column sort functionality and will need to set the class of each sort header accordingly. So, to re-iterate the question, does anyone know of a way to dynamically change the class of any given header in slick-grid 1.4.3?
I don't really love this solution, as it is not preferred to use JQuery to modify the slick-grid, but here goes:
var headers = $('.slick-header-columns').get(0).children;
remove any non-default class information from all the headers(i.e., $(headers).removeClass('slick-header-column-sorted'))
iterate through the columns of the slick-grid and use JQuery to add a class at the wanted column index
var header = headers[wantedColIdx];
$(header).addClass('yourClass');
I think you can do this with jQuery.
Add a blank css class with some name.
Then you can call addClass and removeClass to change the class dynamically.
http://api.jquery.com/addClass/
http://api.jquery.com/removeClass/
i.e.
jQuery('.my-class').addClass('new-header-class');