Jump to content

Prestashop Classes


DariusClay

Recommended Posts

I'm creating a new module for my company, and it uses its own database table. I want to create a model class for the database to make it easier to link to rows, add new rows, etc much like the built-in Order, OrderDetail, or Product classes.

 

What is the best way to create the class so that its relatively easy when updating Prestashop in the future? I want to make this module as self-contained as possible. We use Prestashop version 1.4.4.

 

Is there any way to define this class inside module's directory? such as /modules/mymodule/classes/myclass.php

 

or can it only be done by placing the class inside the /classes/ folder?

 

Thanks,

DC

Link to comment
Share on other sites

Ok, checking the /config/autoload.php file, I see that classes are by default loaded only from the /classes/ or /override/classes/ folders.

 

So if I wanted to keep this module self-contained, I could basically keep the class in the modules folder, and do an explicit include() on that file from any page that would need to use it. Otherwise it seems I should put it into the /override/classes/ folder, and whenever we update move this file to the new installation.

Link to comment
Share on other sites

Alright, I think I know what I need to do. Inside the module folder will be a classes folder containing all the custom classes related to it.

 

Upon installation of the module, it copies these files from /modules/mymodule/classes/ into the /override/classes/ folder. If uninstalled it will unlink these files in the /override/classes/ folder. All functionality is contained within the actual module class, and future updates can be done with relative ease.

Same idea will be used on any BO Tabs that might need to be created as well.

 

Sorry for talking to myself here in the forums but I had to express my thoughts somewhere :)

Link to comment
Share on other sites

You can also place your class inside the module directory and include it when needed.

 

As an example, have a look to the referralprogram module.

 

For the admin tab, you can also keep the AdminXXX.php file inside your module directory

When you declare this tab in the DB, you indicate the corresponding AdminXXX.php file and the module where it is located.

  • Like 1
Link to comment
Share on other sites

Here's a basic example of how I did it : Both to keep things organized, and to make it easy (so you don't have to remember to include the class wherever you need it)...

 

on the module's install function -- note I have changed names of files/classes to keep it generic :

 

public function install()
{
   	/* Install and register on hook */
   	.... code here ....

   	/* Set database */
   	... add new db ....

   	/* Set configuration */
   	... add configuration values, whatever...

   	/* copy files to prestashop */
   	copy(dirname(__FILE__).'/override/classes/mymoduleclass.php', dirname(__FILE__).'/../../override/classes/mymoduleclass.php');

   	/* register AdminMyModuleClass tab */
   	if (Tab::getIdFromClassName('AdminMyModuleClass') === false) {
       	$tab = new Tab();
       	$tab->class_name = "AdminMyModuleClass"; // class name -- inside module directory
       	$tab->module = $this->name; // name of this module
       	$tab->id_parent = 3; // AdminOrders
       	foreach (Language::getLanguages() AS $language)
       	{
           	$tab->name[$language['id_lang']] = 'My Module';
       	}
       	$tab->save(true);
   	}
	return true;
}

 

and then for the uninstall function:

 

public function uninstall()
{
	if (!Configuration::deleteByName('WHATEVER_CONFIG_VALUES') 
       	OR !parent::uninstall())
		return false;

   	/* unregister AdminMyModuleClass tab */
   	if (($tab = Tab::getIdFromClassName('AdminMyModuleClass')) !== false) {
       	$tab = new Tab($tab);
       	$tab->delete();
   	}

   	/* remove files */
   	unlink(dirname(__FILE__).'/../../override/classes/mymoduleclass.php');

   	........ any other uninstall codes needed ......

	return true;
}

 

For "AdminMyModuleClass" ... I extend a new class from a current tab class (make sure you include the files for the tab at the top).

 

ex:

include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
include(PS_ADMIN_DIR.'/tabs/AdminOrders.php');

class AdminMyModuleClass extends AdminOrders
{ 
..... class definition ...
}

 

Place it in the modules directory. Also place a gif image there with the same name as the "AdminMyModuleClass" file name, eg AdminMyModuleClass.gif... if you want this new tab to have an icon.

 

For the custom class the module uses.. you can create a new instance of it from any page you wish (in the admin tab... in the module files themselves, etc) ... and the __autoload function will include it automatically. For an example here, I use this class inside one of the hook functions for the module. It extends the basic ObjectModel class.

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