Prestashop 1.6: how to load a different template after an admin form submission? - prestashop

I'm new to Prestashop and currently working on a custom admin module in 1.6.
My scenario is, users can load orders to this module/admin controller and from the list they can select which ever order they like using 'Select All' checkbox selector.
And then these selected order ids are submitted back to admin controller to be displayed on a different template with full information in a form.
And then again this form will be submitted to a third party API for further processing.
Now my problem is I can get the order listing to my admin controller and get them submitted back to the same controller. But I do not know how to switch to a different template to display in a form.
And also how to call a different method to process and export once second form substitution is done.
Any advice would be greatly appreciated.
Thanks in advance
Roshan

In your AdminController extend the method initContent with a condition that is you need to check if a certain button was pressed. Something like
public function initContent()
{
if (Tools::isSubmit('the_button')) {
'do what you want to do'
}
parent::initContent();
}
if you want to redirect to a different controller use Tools::redirectAdmin() and set the path of redirection like Context::getContext()->link->getAdminLink('your_another_controller', true), also you can send all necessary data with the third parameter of the method(array)
public function initContent()
{
if (Tools::isSubmit('the_button')) {
Tools::redirectAdmin(
Context::getContext()->link->getAdminLink('your_another_controller', true, $orders)
);
}
parent::initContent();
}
You can use as many conditions, inside the method, as you need so I hope it will help you in all your cases.

Related

How to make required fields using JS

I have a SharePoint Form. How can I make a required field( people picker and date) using Java Scripts ? Thanks in advance
Check the OOTB options first. Obviously a list field marked required will not submit until it's filled in.
You can also setup validation in list setting under validation settings. Enter a formula that can evaluate to TRUE to pass validation (Title = "TitleXYZ"). Custom error text can also be added.
There are endless options using JavaScript. The basic SharePoint forms model (assuming your validating on the submit event) uses the PreSaveItem function:
function PreSaveItem()
{
if ("function"==typeof(PreSaveAction))
{
return PreSaveAction();
}
return true;
}
PreSaveAction function also allows override behavior for the Save button. For example:
function PreSaveAction() {
return ifValidSSN();
}
You can find a good example of using this function
here
If you want more of a framework there are multiple jQuery validation plugins you can try. I recommend the jquery Validation plugin
If you want some examples of using this plugin let me know

TYPO3 Plugin - One action public access and one private

I'm developing an extension that have both, a list of records (action show) and a form to send a new record (action new).
The list must be for public access, but the form must require a login form (I'm using the login form content type that comes with TYPO3).
I have tried using the Access Tab for the plugin selecting Show at any Login but it applies to the entire plugin not for each action.
Currently, this is how the page looks like:
How could I get to display the login form only when someone tries to create a new record?
Note: The extension is based on Extbase and Fluid. The target version is TYPO3 6.2.
The easiest would be to split the actions in different "views" with switchableControllerActions in your flexform. Then you would need to place separate plugins on two different pages, that way you can have different access configuration for the plugins. If you don't know how to adjust the flexform, you can post the content of it here.
The other way would be to make a check inside the controller, but i would only use it if you have a lot of different roles you need to check.
if ($this->loginUser === null && $GLOBALS['TSFE']->loginUser && !empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
// the user is logged in
} else {
// return '' as action content
return '';
}

Prestashop Login First

I use latest prestashop 1.6.1.4, I want to every customer firstly login on site and then they have access all over pages of website, without login user can't do anything on site.
So, I want know that there is any configuration at backoffice side and if Yes then how can do that?
There is no built in function to do this, but you can easily override the FrontController to achieve this behavior.
Put this code inside /override/classes/controller/FrontController.php:
<?php
class FrontController extends FrontControllerCore
{
public function init()
{
parent::init();
if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password')
{
Tools::redirect('index.php?controller=authentication?back=index');
}
}
}
If the user is not on an authentication page or a forgot password page he will be redirected to the authentication page. After log in he will be redirected to the home.
EDIT:
If the file FrontController.php doesn't exist, you will have to create it and delete the file /cache/class_index.php. class_index caches every classes path on your website. So if you create a new Class File, you need to delete it to let Prestashop search for newly created ones.
There is no such configuration for this in back office. You can only achieve this by developing a custom module for your store, that uses a hook that is called on all the pages (hookDisplayHeader or hookDisplayTop etc.)
If you don't want to create a module for this then you can also achieve the same by overriding FrontController.php as it is called on each and every page of PrestaShop.

Prestashop how to know when a new user makes register in the store

I am using prestashop 1.6. I wanted to know how can I get to know when some user makes register in the store? Is there any kind of hook or something like that? Any help and suggestions will be really appreciable.
Thanks
there's an hook and it's called right after a customer has been succesfully registered to the website.
The hook name is actionCustomerAccountAdd, that you can call in your custom module by the function hookActionCustomerAccountAdd($params){ ... }, and registering it by $this->registerHook('actionCustomerAccountAdd')
the code of the hook is found inside the AuthController :
Hook::exec('actionCustomerAccountAdd', array(
'_POST' => $_POST,
'newCustomer' => $customer
));
as you can see you have access to the full $_POST of the registration form + the new customer object created with it.
If you need an example on how to use the hook you can see the code of the blocknewsletter module, it uses an actionCustomerAccountAdd hook, right as you need it.
You will be informed in admin panel. Left top corner. User icon -> when new customer will be registred this icon will have number of registred user 1, 2, 120. Click to user icon to clean counter

A better way of passing variables from controller to view in symfony

Hey.
I've got a login form with post as method. The action goes to 'auth/login' and will check the database if the user exists. If the user exists, I call the $this->getUser->setAuthenticated(true);. After this I want to redirect to a welcome page if success.
If the login failed, I would want to tell the user so in the view of course. But settings variables in the controller only if login failed, and check in the view if each of those variables are set, is a lot of work?
This means I have to check almost all variables I want to use in the view set from the controller. If it should happen that it is not set, and I just go ahead and echo it, I get an error from symfony, and production stage-mode-ish don't show anything but an 500 internal server error .
Thanks
EDIT:
This is my current, new and better solution. Still looking for feeback.
in /templates/loginSuccess
if ($sf_params->has('bad_login')) {
echo "Wrong username or password";
}
And in my controller:
$this->redirect('auth/login?bad_login=');
Take a look at how sfDoctrineGuardPlugin (the de-facto standard for authentication) does it: they created sfGuardValidatorUser and use it as a post validator in the signin form.
Advantage of this method: the form takes care of the username/password validation, you do not need to put that code in your action. It simplifies that to a simple $form->isValid() { $this->redirect("#homepage"); }.
It seems like you could use symfony's form to take care of the validation. Since the forms show errors built in, you could put this into the form validation and then your controller looks something like:
$form = new LoginForm;
if ($request->isMethod('post'))
{
if ($form->isValid())
{
$this->redirect('account');
}
else
{
// this would show the form, and since you put the login in the form validation it will show errors. You could have the username show the error
}
}
To do what you are doing though, I'd recommend this. That way you aren't accessing any parameters in the view as well.
Controller:
$this->bad_login = $this->getParameter('bad_login',false);
View:
if ($bad_login) { echo 'bad login'; }
Use forward()
Put all the logic required for the view population into separate method of a controller, and call it in both places.
Use cgratigny's solution - put login form and processing code in a single action, and redirect to welcome page if isMethod('post') && $login_success