Creating a new module in BEdita is simple like writing a CakePHP plugin. Generally a module will have a controller, one or more models, some views, at least a css that defines the module color and a special file named bedita_module_setup.php that contains some rules and definitions to start up and hook correctly the module to BEdita. Without this file the plugin will not be recognized from BEdita as a valid module. Each BEdita module has to be stay in the modules folder that you can find in bedita root folder.

You can find a sample plugin module in bedita-app/modules/sample_module : start from here, changing files and folder names and editing some other stuff

Otherwise you can build the new module from scratch using this sample module as reference. Once your module is configured properly it can be activated by the admin module web interface.

Here you can find some basic rules to build your module:

modules folder

The name of your module will be the name of your plugin folder. Pay attention that the module name isn't the module label that you can see in BEdita backend square and that you will define in bedita_module_setup.php how you can see below.

The name of your module controller file has to be the same of the plugin folder. So the structure will be:

bedita-app/modules/sample_module/controllers/sample_module_controller.php

 


config folder

This folder contains the fundamental bedita_module_setup.php and a config.php (optional) file with some global configurations. The first one will contain an array like:

$moduleSetup = array(
    "publicName" => "Sample Module",
    "author" => "Channelweb and Chialab",
    "website" => "http://www.bedita.com",
    "emailAddress" => "info@bedita.com",
    "description" => "Example of BEdita's module plugin",
    "version" => "0.1",   
    // minimum BEdita version required by this module
    "BEditaMinVersion" => "3.1",
    // maximum BEdita version supported by this module
    //"BEditaMaxVersion" => "3.1",
    // model names that are BEdita objects: i.e. extend BEAppObjectModel  
   "BEditaObjects" => array("SampleObject"),
    // extra database tables used/needed by this module
    // "tables" => array("sample_objects"),
);

publicName that is the module label you can see in the square box on BEdita that refers to your module, the author, a website of the author, a referent email, the version, the BEdita minimum compatible version, a description and an array of new BEdita objects (new model names that are BEdita objects, not just simple models) involved by module. If your module needs you can define here a BEdita maximum version supported and an array of tables used by your module.

If you needed to define some global configurations (like relations with other objects) put it in config/config.php file and it will be loaded in every BEdita page.  For example if your module defines a new free relation with document object add this:

$config["objRelationType"] = array(
   "custom_relation" => array(
      "hidden" => false,
      "left"      => array("sample_object"),
      "right"  => array("document")
   )
);

 

controller

Controller name, as we said, has to be the same as the plugin folder with _controller suffix.
Controller has to extend the ModulesController abstract class located in the app_controller.php file. If the module uses only a BEdita object the controller has to override some basic methods: index, view, save, delete, deleteSelected, forward. If your module use two or more BEdita objects then implement specific method viewBEditaObjectModel, saveBEditaObjectModel, deleteBEditaObjectModel instead of view, save and delete. ModulesController methods (view, save, delete) will take care of routing at the correct method of your controller.

A required attribute of your module controller class is:

protected $moduleName = 'sample_module';

that will be the controller name with underscores.

 

model

Your module models can be of two types. A standard cakePHP model or a BEdita object model. In the second case the model will have to follow some guidelines that you can read in BEdita object articles. However if your module uses one or more new BEdita objects, their names (class names) should be placed in bedita_module_setup.php at the key "BEditaObjects". Automatically they'll be inserted in object_types table and put in the cached configuration.

views

In modules/sample_module/views/sample_module folder reside the views of your module. Commons BEdita tabs like (text, tags, translations, etc....) are CakePHP elements so you can reuse it from your plugin calling:

{$view->element('elementName')}

In all views should be present modulesmenu.tpl element that contains the on top modules navigation. view.tpl should contain form_common_js.tpl element to load standard BEdita javascript form functions.

css

Folder /sample_module/vendors/css has to contain the module_color.css file where you can specify the module color. This file is linked in every BEdita backend page: all modules color are always available. If you need specific css for you module put it in a file named module.css that will be linked only in the module pages.

js

Like css also for specific javascript you want to use in your module put it in vendors/js/module.js file. It will be linked only in the module pages.

How to create schema.php for extra tables

When you have extra tables in your setup ($moduleSetup["tables"]), you have to create correctly the following files: config/sql/schema.php, config/sql/mysql_schema.php. In order to do that, you should use cake module script. Let see how to do that, with an example in 3 steps.

1. First of all, create your database table(s), and don't loose your sql creation script (you could use it, later).

2. Launch cake module script as follows:

./cake.sh module schema -name sample_module

The module script creates files config/sql/schema.php and config/sql/mysql_schema.sql.

3. Check config/sql/mysql_schema.sql and if it's not correct, overwrite it with sql creation script that you used at step one. Cake schema can lose some table information, like constraints, that's why you could need to overwrite it with your original sql script.

It's done! You can plug-in/plug-out your new pluginModule from BEdita Administration Plugin Module Section.