Prestashop 1.6 Create Module to Display Carrier Filter - prestashop

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.

Related

Prestashop 1.7 How to Display data from database to tpl

i'm new on Prestashop, i have a little problem on prestashop 1.7, i just bought a module for my orders, on the page of details when i launch smarty i can see the array $order_reference with some information, but when i go to the page of my history i can't access to this information, i don't know how to add this information to access it with my page history, do i have to create a function on history controller or class with dbclass or do u have any others solutions to add my details information of my order ?
The problem is my information is in a another table who call eo_orderreference and i can't access to it with my orders object. I think i need to write a function but as i see on internet it is wrong to write it on a tpl file, i have to do it on my class but i don't know how.
This is the information i want to add
SELECT 'order_reference' FROM eo_orderreference
Sorry i can't upload image because it contains sensible informations.
Thank for your time.
Smarty templates are meant to display data,
you should perform your query in the PHP file that recall your TPL and then use the Prestashop
assign()
method to return data to your TPL as smarty variables that you can show at front office.
See here for more details.
you can create the sql query within a function either within a module or a controller.
For example:
public function getAccessories($id_product)
{
// Your code
}
into a hook or initcontent, you can call the function on the query sql and assign it a template:
public function hookDiplayAccessoryExtraProduct($params)
{
$accessories = $this->getAccessories((int)$params['id_product']);
$this->context->smarty->assign(array(
'accessories_custom' => $accessories,
)
);
return $this->display(__FILE__, 'views/templates/front/accessory.tpl');
}

Prestashop How to export multiple invoice selected orders in a single pdf

How can I add an option to the "Bulk actions" button that allows exporting of all the selected orders invoice in a single pdf?
The option of exporting the invoices does not work for me, since I have to filter it by customer group and I cannot go one by one either.
Attachment capture
You need to override (or modify) AdminOrdersController, look at this file and how it's done for order state update, you have an array of bulk actions:
$this->bulk_actions = array(
'updateOrderStatus' => array('text' => $this->l('Change Order Status'), 'icon' => 'icon-refresh')
);
if you add something to this array, it will be available from this drop-down menu, a key is an action name for it, if you want to process order status change you need to use this code (for example in postProcess method), submitBulk is a standard prefix for all those actions. submitBulkYOUR_ARRAY_KEY, little snippet:
if (Tools::isSubmit('submitBulkupdateOrderStatus'.$this->table)) {
// your code
}
I hope this helps you understand how does it work. If you have more questions let me know.
If you want to understand how to generate multiple PDF at once, look at the AdminPdfController, you can look at this file from 1.6 version of PrestaShop

silverstripe 3 - How to add access control to generated data objects?

Good afternoon,
Please let me know if this question is not clear enough, I'll try my best to make as straight-forward as possible.
How can I add access control to objects that are generated by an end-user using my data object?
Example: I have a class that extends a DataObject. Someone logs in the back-end; fills out the form that's generated by the CMS for the data object. A record is then created in the database by the CMS.
I would like to add an access control to that newly created record in the database.
For a code scenario you can take a look at one of my posts: Silverstripe 3 - Unable to implement controller access security from CMS
The only other way I can think of asking this question is: How to Dynamically (or programmatically) create permissions for records that are created by a DataObject extension via the CMS?
Thanks for your assistance.
Update - Sample Code
///>snippet, note it also has a Manager class that extends ModelAdmin which manages this!
class component extends DataObject implements PermissionProvider{
public static $db = array(
'Title' => 'Varchar',
'Description' => 'Text',
'Status' => "Enum('Hidden, Published', 'Hidden')",
'Weight' => 'Int'
);
///All the regular permission checks (overrides), for the interface goes here, etc...
///That is: canView, canDelete, canEdit, canCreate, providePermissions
}
Now, from the back-end an end-user can add components using the Manager Interface that's generated by extending ModelAdmin. How can I add individual permissions to those added components by the end-user?
Thanks.
Update 2
Example: Add Process Data Object that extends ModelAdmin will give you this in the back end
Then, when you click on the generated 'Add Process' button, you'll get this:
Finally, someone fills out the form and clicks on the 'Create' button, which saves the data in the database. That looks like this:
Now, on that record thats created in MySQL I'd like to add granular permissions to that record. Meaning, for every record created I want to be able to Deny/Allow access to it via a Group/Individual, etc.
Is that even possible with the SilverStripe framework? Thanks.
Implement the functions canView, canEdit, canDelete, and/or canCreate on your DataObject.
Each function will return true or false depending on the conditions you set - any conditions, not just what is defined in the CMS.
See the example code on the tutorial site.

How to create a smarty variable in prestashop 1.5

I am working on a button that switches view with onClick. I wish to store the last/default position in a variable in order to prevent switching to the default view state on each page refresh or navigation.
I read that I can do the following in a php file:
$myVar= -1;
$smarty->assign('myVar', $myVar);
and then use $myVar in the tpl file. But it does not work for me.
The tpl file I am working on is not part of a module and has no .php file in the prestashop root folder.
Can anyone educate me a little on smarty/php and how to create variables and use them to store button's state?
Thanks
Smarty is a PHP template engine for PHP, which facilitates the separation of presentation (XHTML/CSS) from the PrestaShop's core functions/controllers.
A template file (usually with a .tpl extension in PrestaShop) is always called by a PHP controller file (it can be a Front-end core controller or a module controller).
Example: /prestashop/controllers/front/ContactController.php
$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));
$this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
We can see that this file is retrieving information from the database and assigning it to Smarty.
Then, the 'contact-form.tpl' template will display it to the visitors.
The syntax is pretty similar for modules,
example:/prestashop/modules/blocklink/blocklink.php
public function hookLeftColumn($params)
{
$this->smarty->assign('blocklink_links', $this->getLinks());
return $this->display(__FILE__, 'blocklink.tpl');
}
Also, to store values in Smarty variables, you can use the 'assign' function in two ways:
$this->context->smarty->assign('my_smarty_variable_name', $my_value);
or if you have several variables:
$this->context->smarty->assign(array('my_smarty_variable_name1' => $my_value1), ('my_smarty_variable_name2' => $my_value2));
And then in the Smarty template:
The value of my variable is {$my_smarty_variable_name|escape:'htmlall':'UTF-8'}.
The 'escape' modifier is used to avoid XSS security issues.
In order to use variables in your smarty file, you need to use for example :
$this->context->smarty->assign(
array(
'myVar' => $myvar,
'otherVar' => $otherVar
)
);
Then to use it in your tpl file you simply need to use :
<div>my var = {$myVar}</div>
To use a variable in your smarty you need to write it inside {}.

How do you check if the current page is the frontpage using YII?

Drupal has a function called "drupal_is_front_page". Does YII have something similar to deal with navigation in this way?
Unfortunately not. And while the information needed to piece this together is available, doing so is really more pain than it should be.
To begin with, the front page is defined by the CWebApplication::defaultController property, which can be configured as discussed in the definitive guide. But there's a big issue here: defaultController can in reality be any of the following:
a bare controller name, e.g. site
a module/controller pair, e.g. module/site
a controller/action pair, e.g. site/index
a module/controller/action tuple, e.g. module/site/index
If you have specified the defaultController as #4 (which is the same as #3 if your application does not include any modules) then everything is easy:
function is_home_page() {
$app = Yii::app();
return $app->controller->route == $app->defaultController;
}
The problem is that if defaultController is specified as #1 or #2 then you have to examine a lot of the runtime information to convert it to form #3 or #4 (as appropriate) so that you can then run the equality check.
Yii of course already includes code that can do this: the CWebApplication::createController method, which can accept any of the valid formats for defaultController and resolve that to a controller/action pair (where controller is dependent on the module, if applicable). But looking at the source doesn't make you smile in anticipation.
To sum it up: you can either assume that defaultController will always be fully specified and get the job done with one line of code, or borrow code from createController to determine exactly what defaultController points to (and then use the one line of code to check for equality).
I do not recommend looking into solutions based on URLs because the whole point of routes is that different URLs can point to the same content -- if you go that way, can never be sure that you have the correct result.
In my experience, there is no such function in Yii. However, you can retrieve the followings:
base url: Yii::app()->request->baseUrl
current URL : Yii::app()->request->requestUri.
current page controller with Yii::app()->getController()->getAction()->controller->id .
With these APIs, it should be possible to find out whether the current page is front page.
another simple idea:
in your action (that one you use to present your 'main front page'), you could set up a variable using a script in its view:
Yii::app()->getClientScript()->registerScript("main_screen",
"var main_front_page = true;",CClientScript::POS_BEGIN);
put that code in the "main view", (the rest view pages dont have this piece of code).
so when you need to check if a page is the "main page" you could check for it using javascript, quering for:
if(main_front_page){..do something..}.
if you need to recognize the main page in php (in server side), use the method proposed by Jon.
another solution, based on a common method for your controller:
Your controllers all of them must extend from CController, but, when you build a new fresh yii application Gii creates a base Controller on /protected/components/Controller.php so all your controllers derives from it.
So, put a main attribute on it, named:
<?php
class Controller extends CController {
public $is_main_front_page;
public function setMainFrontPage(){ $this->is_main_front_page = true; }
public function getIsMainFrontPage(){ returns $this->is_main_front_page==true; }
}
?>
well, when you render your main front page action, set up this core varible to true:
<?php
class YoursController extends Controller {
public function actionPrimaryPage(){
$this->setMainFrontPage();
$this->render('primarypage');
}
public function actionSecondaryPage(){
$this->render('secondarypage');
}
}
next, in any view, you could check for it:
<?php // views/yours/primaryview.php
echo "<h1>Main Page</h1>";
echo "is primary ? ".$this->getIsMainFrontPage(); // must say: "is primary ? true"
?>
<?php // views/yours/secondaryview.php
echo "<h1>Secondary Page</h1>";
echo "is primary ? ".$this->getIsMainFrontPage(); // must say: "is primary ? false"
?>