ZnetDK 4 Mobile

Modules

Sharing the capabilities of ZnetDK for Mobile

What is a module?

A module provides a particular functionality which is intended to be shared between developers of ZnetDK applications.
Another term that could be used to designate a module is plug-in.

Here are some examples of modules that could be developed:

  • Sending emails,
  • Generation of documents in PDF format,
  • Data export in Excel format,
  • Display of charts,
  • Customization of the colors and font of the application,
  • etc.

A module can be made up of views, controllers, models, labels translated into several languages, classes of PHP objects or services, JavaScript libraries, style sheets, pictures, etc.

Where are the modules installed?

The modules are installed into the INSTALL_DIR/engine/modules/ folder.

A module is a subdirectory of the modules/ folder that has a name that must be unique, for example my_module.

The typical folder tree of a module is as below:

  •  INSTALL_DIR/engine/modules/
    •  my_module/
      •  mod/Here are installed the private PHP scripts.
        •  controller/
          •  mymodulectrl.php
        •  lang/
          •  locale_en.php
          •  locale_es.php
          •  locale_fr.php
        •  model/
          •  mydao.php
        •  validator/
          •  myvalidator.php
        •  view/
          •  mymoduleview.php
        •  .htaccessProhibits access to the contents of the folder.
        •  config.php
        •  myclass.php
      •  public/Here are installed the public resources.
        •  css/
          •  minified/
            •  mystyles_min.css
        •  js/
          •  minified/
            •  mylibrary_min.js

You will notice that the module folder tree is the same as the Starter App's, except that the app folder is renamed to mod.

Namespace of the PHP classes in a module

The PHP classes of a module must be declared with a namespace prefixed by my_module\mod where my_module is to replace to the name of the module.

Here are some examples of namespace according to the folder where the PHP script is located:

mymodulectrl.php script (controller)

<?php
namespace my_module\mod\controller;
class 
MyModuleCtrl extends \AppController {

    static protected function 
action_get() {
        
/* Code of the controller's action */
    
}

}

mydao.php script (model)

<?php
namespace my_module\mod\model;
class 
MyDao extends \DAO {

    protected function 
initDaoProperties() {
        
/* Code of the DAO */
    
}

}

myvalidator.php script (validator)

<?php
namespace my_module\mod\validator;
class 
MyValidator extends \Validator
{
    protected function 
initVariables() {
        return array(
'my_first_value''my_second_value');
    }

    protected function 
check_my_first_value($value) {        
        
/* Code for validating data */
    
}
    protected function 
check_my_second_value($value) {
        
/* Code for validating data */
    
}
    
}

myclass.php script

<?php
namespace my_module\mod;
class 
MyClass
{
    
/* Code of the class */
}

Naming the controllers and the views of a module

The name given to the views and the controllers of a module must be chosen wisely.

Indeed, a view is designated in the menu.php script of the Starter App from its name and also can be loaded in JavaScript with znetdkMobile.ajax.loadView(), again from its name.
if a view in a module is named for example myview.php and if a view exists in the Starter App with the same name, then the view finally displayed will be the view of the Starter App which has priority over the views of any module.

Regarding the controllers, the principle is exactly the same. When a controller is invoked from a view from its name, for example when the znetdkMobile.ajax.request() method is called, the controller finally called as a priority is the one implemented for the Starter App in the case where a controller with the same name also has been implemented by a module.

CSS and JS libraries are loaded automatically

The CSS and JavaScript libraries installed into the css/minified/ and js/minified/ folders of a module are automatically loaded in the page of the Starter Application.

Thus, there is no need to explicitly request their loading in the config.php of the Starter Application through the CFG_APPLICATION_CSS and CFG_APP_JS PHP constants.