Knowledgebase: Support
How To Create A New Module
Posted by Lena Kosyakova on 11 March 2013 03:03 PM

The module name must begin with the prefix “custom_”, e.g. “custom_sample”, otherwise the module will require a license to work.

Each module class is a PHP file, their names must match and be unique, i.e. the name of a class and its PHP file must not be met in other modules. The only exception is the file Module.php which must exist in the folder of new module.

A module consists of the following components:

  1. the folder with the module name (custom_ sample), which must be located in /modules
  2. the file Module.php, located in the root of module folder (/modules/custom_sample/Module.php)
  3. the file db.sql, located in the root of module folder (/modules/custom_sample/db.sql). This file is not obligatory, but necessary if the module installation requires changes in the database
  4. executable scripts, which are, in fact, the functions of the module. Administrator’s functions must be located in /apps/AdminPanel/scripts, while user’s scripts should be in /apps/FrontEnd/scripts (e.g. /modules/custom_sample/apps/FrontEnd/scripts/ReceiveTime.php)
  5. library scripts, which are used by module functions, located in /apps/lib/ (e.g. /modules/custom_sample/apps/lib/TimeManager.php)
  6. template files, which displays information, located in /apps/AdminPanel/templates for the Admin Panel side and in /apps/FrontEnd/templates for the Front-End (e.g. /modules/custom_sample/apps/FrontEnd/templates/time.tpl)


Below is the example of the Module.php contents:
<?php

// the path to module folder == module name
namespace modules\custom_sample;

class Module extends \core\Module  // all modules are inherited of \core\Module
{
    protected $name = 'custom_sample'; // a unique module name (module id)
    protected $caption = 'Custom Sample'; // module caption, which will be displayed in the Module Manager
    protected $version = '6.1.3-1'; // module version, which should be equal to the current version of the product
    protected $dependencies = array // module dependencies of other modules, required for the correct work of a new module
    (
        'site_pages',
        'static_content',
    );
}


The example of the module function, ReceiveTime.php:
<?php

namespace modules\custom_sample\apps\FrontEnd\scripts; // the path to the script

// the scripts, which are used for information display, must be inherited of \apps\FrontEnd\ContentHandlerBase and must have an obligatory method respond()
class ReceiveTime extends \apps\FrontEnd\ContentHandlerBase
{
    protected $moduleName = 'custom_sample'; // module name
    protected $functionName = 'receive_time'; // function name

    // the method, called during function call
    public function respond()
    {
        $timeManager = new \modules\custom_sample\lib\TimeManager();

        // $templateProcessor is a class, responsible for information display on the website
        $templateProcessor = \App()->getTemplateProcessor();

        // passing parameters to a template
        $templateProcessor->assign('time', $timeManager->get_time());

        // the display of desired template; the template is taken from the folder /modules/custom_sample/apps/FrontEnd/templates/
        $templateProcessor->display('time.tpl');
    } 
}

The example of TimeManager.php, a library script which are used by the module function ReceiveTime.php:
<?php

namespace modules\custom_sample\lib;

class TimeManager
{
    public function get_time()
    {
        return time();
    }
}


The example of the template, time.tpl:
{* the display of the variable, passed to the template; in templates you can use all Smarty functions *}
<span>{$time|date_format:"%H:%M:%S"}</span>


You can call a module function by two ways:
1.    create a new Site Page specifying a new module and its function;
2.    insert the code like {module name="custom_sample" function="receive_time"} to an existing template which is already displayed on the website.

When the module is created, you need to add it to the list of available modules by clicking the button “Refresh Module List” in the Admin Panel -> Module Manager, and afterwards you can install it.
In case you added more functions to the module after it was installed, you need to refresh the module list again. If the module or its new functions did not appear after the refresh, then you need to clear cache in the Admin Panel -> System settings -> Clear Cache.  After that you’ll be able to see module errors on the website, if any, and fix them.

(12 votes)
This article was helpful
This article was not helpful

Comments (0)
Post a new comment 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below. This is required to prevent automated registrations and form submissions.

Help Desk Software by Kayako Fusion