Jump to content

Execute custom function in overriden controller


pl.sabotinov

Recommended Posts

Hello,

I am using prestashop 1.6.1.13 version and would like to ask the following as I am not completely sure how to do it correctly:

I am overriding the ContactController.php and exactly the functions postProcess() and initContent():

 

class ContactController extends ContactControllerCore
{
  
    /**
    * Start forms process
    * @see FrontController::postProcess()
    */
    public function postProcess()
    {
        if (Tools::isSubmit('submitMessage')) { 

        ……...

        ……..

        }

    }



	public function initContent()
    {
        parent::initContent();

    …….

    }

 
    public function MyCustomInsert ()

    {

    ………………………...

    }
}
----------------------------------------

 

 

And also have added my custom function - MyCustomInsert() after the other two functions. This is custom function , not presented in original ContactController.

But I suppose this function will not be executed, if left just so in this file in overrides,  as this function is not present or called in the original file ContactController.php or somewhere else. Or it will?

So I am wondering how to call this custom function correctly every time when data is passed to ContactController. The override controller is as usual in override/controllers/front/ContactController.php.

So what is the best way to call this function;

1. To put it in one of the another two functions so the custom function will be executed when for example the postProcess() function is called. 

2. Or to put a call in initContent() function for execution during initialization

3. Of course I can insert the code of the custom function in postProcess() and so it will execute also this action as a part of it. 

But I think all of these solutions are not good. So what would be the best way to call and execute this custom function, when this overridden controller is called.

I can not move this custom function in another file.

 

What is the best solution?

 

Thanks in advance.

 

 

 

 

 

 

 

Link to comment
Share on other sites

Hi

You are right, function will not be executed if you just add declaration on function in override. 

You do need to call it from some existing function. And that depends what you want to accomplish.

If you do want to make some action when contact form is submitted then yes you do call MyCustomInsert()

function in postProcess(). Or if it is something else you can add it do initContent() instead.

 

For example we had this in override


class ContactController extends ContactControllerCore
{
    public function postProcess()
    {
        if (Tools::isSubmit('submitMessage')) {
            $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
            $file_attachment = Tools::fileAttachment('fileUpload');
            $message = Tools::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags.

            /* New code */
			$from = trim(Tools::getValue('from'));
            if ($this->isBadDomain($from)) {
                $this->errors[] = Tools::displayError('Invalid message');
                return;
            } 
            /* end new code */

		...
	}

	public function isBadDomain($user_email)
    {
	...
	}

}	

 

Also if your override is part of module do use this 

if (Module::isEnabled('modulename')) {
	...
}

 

Link to comment
Share on other sites

Hello razaro,

and thanks for your answer. Yes i want it to do some additional action when contact form is submitted. So i will put it in the postProcess() function. As i thougth.

But i will keep it's declaration separate, out of the postProcess() function and will call it only in postProcess(). So should be the best way, am i right? 

 

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...