Child pages
  • Diving into PrestaShop Core development

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
/**
* Example from the CMS model (CMSCore) 
*/
public static $definition = array(
  'table' => 'cms',
  'primary' => 'id_cms',
  'multilang' => true,
  'fields' => array(
    'id_cms_category'  => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
    'position'         => array('type' => self::TYPE_INT),
    'active'           => array('type' => self::TYPE_BOOL),
    // Lang fields
    'meta_description' => 
        array('type' =>self> self::TYPE_STRING, 'lang' =>true> true, 'validate' => 'isGenericName', 'size' =>255> 255),
    'meta_keywords'    => 
        array('type' =>self> self::TYPE_STRING, 'lang' =>true> true, 'validate' => 'isGenericName', 'size' =>255> 255),
    'meta_title'       => 
        array('type' =>self> self::TYPE_STRING, 'lang' =>true> true, 'validate' => 'isGenericName', 'required' =>true> true, 'size' =>128> 128),
    'link_rewrite'     =>
        array('type' => self::TYPE_STRING, 'lang' =>true> true, 'validate' => 'isLinkRewrite', 'required' =>true> true, 'size' =>128> 128),
    'content'          => 
        array('type' => self::TYPE_HTML,   'lang' => true, 'validate' => 'isString', 'size' => 3999999999999),
  ),
);

...

Method name and parameters

Description

getValidationRules($className = _CLASS_)

Return object validation rules (fields validity).

getFields()

Prepare fields for ObjectModel class (add, update).

__construct($id = NULL, $id_lang = NULL)

Build object.

save($nullValues = false, $autodate = true)

Save current object to database (add or update).

add($autodate = true, $nullValues = false)

Save current object to database (add or update).

add($autodate = true, $nullValues = false)

Add current object to database.

update($nullValues = false)

Update current object to database.

delete()

Delete current object from database.

deleteSelection($selection)

Delete several objects from database.

toggleStatus()

Toggle object's status in database.

validateFields($die = true, $errorReturn = false)

Check for fields validity before database interaction.

The DBQuery class

The DBQuery class is a query builder, which helps you creation SQL queries. For instance:

...

All of PrestaShop's controllers actually override the Controller class through another inheriting class, such as AdminController, ModuleAdminController, FrontController or ModuleFrontController.

The FrontController class

...

  1. __contruct(): Sets all the controller's member variables.
  2. init(): Initializes the controller.
  3. setMedia() or setMobileMedia(): Adds all JavaScript and CSS specifics to the page so that they can be combined, compressed and cached (see PrestaShop's CCC tool, in the back-office "Performance" page, under the "Advanced preferences" menu).
  4. postProcess(): Handles ajaxProcess.
  5. initHeader(): Called before initContent().
  6. initContent(): Initialize Initializes the content.
  7. initFooter(): Called after initContent().
  8. display() or displayAjax(): Displays the content.

...

PrestaShop's controllers are all stored in the /controllers folder, and use the "Core" suffix.

For instance, when working with the category controller:

...

In order to override a controller, you must first create a new class without the "Core" suffix, and place its file in the /override/controllers folder.

...

TokenDescription
date_addThe date and time the cookie was created (in YYYY-MM-DD HH:MM:SS format).
id_langThe ID of the selected language.
id_currencyThe ID of the selected currency.
last_visited_categoryThe ID of the last visited category of product listings.
ajax_blockcart_displayWhether the cart block is "expanded" or "collapsed".
viewedThe IDs of recently viewed products as a comma-separated list.
id_wishlistThe ID of the current wishlist displayed in the wishlist block.
checkedTOSWhether the "Terms of service" checkbox has been ticked (1 if it has and 0 if it hasn't)
id_guestThe guest ID of the visitor when not logged in.
id_connectionsThe connection ID of the visitor's current session.
id_customerThe customer ID of the visitor when logged in.
customer_lastnameThe last name of the customer.
customer_firstnameThe first name of the customer.
loggedWhether the customer is logged in.
passwdThe MD5 hash of the _COOKIE_KEY_ in config/settings.inc.php and the password the customer used to log in.
emailThe email address that the customer used to log in.
id_cartThe ID of the current cart displayed in the cart block.
checksumThe Blowfish checksum used to determine whether the cookie has been modified by a third party.
The customer will be logged out and the cookie deleted if the checksum doesn't match.

...

TokenDescription
date_addThe date and time the cookie was created (in YYYY-MM-DD HH:MM:SS format).
id_langThe ID of the selected language.
id_employeeThe ID of the employee.
lastnameThe last name of the employee.
firstnameThe first name of the employee.
emailThe email address the employee used to log in.
profileThe ID of the profile that determines which tabs the employee can access.
passwdThe MD5 hash of the _COOKIE_KEY_ in config/settings.inc.php and the password the employee used to log in.
checksumThe Blowfish checksum used to determine whether the cookie has been modified by a third party.
The customer will be logged out and the cookie deleted if the checksum doesn't match.

...

Code Block
public function install()
{
    return parent::install() && $this->registerHook('NameOfHook');
}

Existing hooks: front-office

...

Hook nameDescription
displayHeaderCalled between within the HEAD HTML <head> tags. Ideal location for adding JavaScript and CSS files.
displayTopCalled in the page's header.
displayLeftColumnCalled when loading the left column.
displayRightColumnCalled when loading the right column.
displayFooterCalled in the page's footer.
displayHomeCalled at the center of the homepage.

...

Hook nameDescription
displayLeftColumnProductCalled right before the "Print" link, under the picture.
displayRightColumnProductCalled right after the block for the "Add to Cart" button.
displayProductButtonsCalled inside the block for the "Add to Cart" button, right after that button.
actionProductOutOfStockCalled inside the block for the "Add to Cart" button, right after the "Availability" information.
displayFooterProductCalled right before the tabs.
displayProductTabCalled in tabs list, such as "More info", "Data sheet", "Accessories", etc...
displayProductTabContentCalled when a tab is clicked.

...