Jump to content

Basic Communication Between Classes When Building A Module


Recommended Posts

Hello,

 

I am starting with PS ; I have knowledge of OOP.

 

I have begun my module as a trial to dive into PS functionning.

 

What is not clear for me is the link between my module class and controllers.

 

Basically I would like to take out some logic from my module class to put it into an adminController that will display a form (the configuration one) on the backoffice and handle post results.

 

But I don't get when and how I can let my adminController takes over from my module class. My feeling is that it should be in the getContent method. I found some leads :

- AdminController::getControllerName

- or Tools::redirectAdmin()

 

but the usage of those ones is unclear.

 

Could someone give some help please ? To answer : [email protected]

 

Thanks!

 

Alexandre

Link to comment
Share on other sites

Hello again,

 

I am replying to myself in case someone could just confirm. It may probably help someone else reading on this forum.

 

A controller corresponds to a route so I basically have to call it through a URL request. Then my module in its configuration page shall offer a button triggering my controller and its template.

 

Let's hope I am on the right way. I will give a try anyway.

 

Thanks.

 

Alex

Link to comment
Share on other sites

You have your module installation with some basic things like install in your module tree you should have :

 

"MY_MODULE/controllers/admin/CONTROLLER_NAMEController.php" (you must add Controller if you are under 1.5 under 1.6 the name are exactly the same as the file name.

 

Then in installation to install a new tab' into your controller you should use these functions :

/**
     * UK/US/EN : Installation of tabs.
     * FR : Installation des tabs.
     * @return mixed
     */
    private function installTab($tabClass, $tabName, $idTabParent) {
        $tab = new Tab();
        foreach (Language::getLanguages(false) as $language)
            $tab->name[$language['id_lang']] = $tabName;
        $tab->class_name = $tabClass;
        $tab->module = $this->name;
        $tab->id_parent = $idTabParent;
        return $tab->save();
    }

    /**
     * UK/US/EN : Uninstallation of tabs.
     * FR : Désinstallation des tabs.
     * @return bool
     */
    private function uninstallModuleTab($tabClass) {
        $idTab = Tab::getIdFromClassName($tabClass);
        if ($idTab != 0) {
            $tab = new Tab($idTab);
            $tab->delete();
            return true;
        }
        return false;
    }

And use them like that :

$this->installTab('CONTROLLER_NAME', $this->l('TAB\'S NAME'), Tab::getIdFromClassName('AdminPriceRule'));
$this->uninstallModuleTab('CONTROLLER_NAME');

You should take a look at the module canvas for more help :

https://github.com/PrestaEdit/Canvas-Module-Prestashop-15

Edited by Diurne (see edit history)
Link to comment
Share on other sites

Hello Diurne,

 

I thank you very much for the time you took to replay to my trivial question.

 

I will dig into information you provided and I may come back with some other questions.

 

However 2 questions :

 

1) I wonder what you mean by a tab. For now I am doing fine with my very simple module (I let aside for now the question of moving some logics in a controller) and I am concentrating on getContent method to display a form in the "Configure' option you get in the button right after the module's name. I split the getContent into others (postValidation, postProcess) to make it more readable. So is a tab an option you can add in the button/list with 'Configure', 'Uninstall', 'Deactivate' and so on ?

 

2) If I wish to display customized message to my user above the form triggered by getContent -> DisplayForm, how can I simply do that ? I tried in a function located in mymodule to add to my html output the content of a template : return parent::display(__FILE__, 'infos.tpl') and added my tpl in /mymodule/views/template/admin; it doesn't work because it seems parent::display function is looking for a php file and not a tpl one... Can you clarify that ?

 

Thanks again. Hope I am not annoying you with my questions...

 

Alex

Link to comment
Share on other sites

1) For your first question a tab' is each of these buttons under 1.6, and each of this buttons are linked to a controller :

 

rtqEgjY.png

 

2) To display errors you have a lot of choices the best practice is to use a private class variable like :

private $displayConfirmation = null;

And then use these functions to display errors or confirmations : (works for 1.5 and 1.6).

$this->configurationSave .= $this->displayConfirmation($this->l('Updated !'));
$this->configurationSave .= $this->displayError($this->l('Not updated error !'));

And when you're displaying content in getContent the return should look like :

return $this->configurationSave . $this->display(basename(__FILE__), '/views/templates/config/configurations.tpl');
Link to comment
Share on other sites

Thank you so much Diurne :D ! You helped a lot ! I am getting slowly familiar with PS architecture and basics routines. I have still a long way to go but I am doing fine !

Just as a curiosity I didn't find configurationSave method in module parent class (perhaps your recommendation was to create one to store all messages in one shot...).
But anyway I tried <return parent::display(basename(__FILE__), '/views/templates/admin/infos.tpl');> as the return of my extra info function and it worked well !

I have almost ended my first module.

Terrifically efficient as a tool to build upon.

Thanks Diurne for your help !

Alex

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...