Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

DolibarrLabs/dolibase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Dolibase

Lite Version Stable Version PHP Min Dolibarr Min

Dolibase is a set of reusable code & architecture that makes coding Dolibarr modules more faster and easier.

⚠️ This project is no longer under active development, feel free to fork and use or try DAMB as an alternative.

Why to use it?

  • Module builder: Dolibase has its own module builder that you can use to speed up your developments.
  • Easy installation: Install in 1 click using the Dolibase Installer module.
  • Ready-to-use components: Several components are available like pages, pdf models, classes & functions.
  • Ensure backward compatibility: Your module(s) will work even on old Dolibarr versions (starting from version 3.8).
  • Less & clean code: Write less code in a clean way & reduce repetitive code frequency.

How it works?

Dolibase is following the main design pattern of dolibarr with some few adjustments to fit its needs.

Below a simple graph that demonstrate the directory structure differences between a basic dolibarr module & a dolibase module.

  dolibase module                                       dolibarr module
  β”œβ”€β”€ admin                                             β”œβ”€β”€ admin
  β”‚   β”œβ”€β”€ setup.php                                     β”‚   └── setup.php
+ β”‚   └── about.php                                     β”œβ”€β”€ core
  β”œβ”€β”€ core                                              β”‚   β”œβ”€β”€ modules
  β”‚   β”œβ”€β”€ modules                                       β”‚   β”‚   └── modMyModule.class.php
  β”‚   β”‚   └── modMyModule.class.php                     β”‚   β”œβ”€β”€ boxes
  β”‚   β”œβ”€β”€ boxes                                         β”‚   β”‚   └── mywidget.php
  β”‚   β”‚   └── mywidget.php                              β”‚   └── triggers
  β”‚   └── triggers                                      β”‚       └── interface_**_modMyModule_*.class.php
  β”‚       └── interface_**_modMyModule_*.class.php      β”œβ”€β”€ class
  β”œβ”€β”€ class                                             β”‚   └── *.class.php
  β”‚   └── *.class.php                                   β”œβ”€β”€ img
+ β”œβ”€β”€ dolibase                                          β”‚   └── object_mypicture.png
  β”œβ”€β”€ img                                               β”œβ”€β”€ langs
  β”‚   └── object_mypicture.png                          β”‚   β”œβ”€β”€ en_US
  β”œβ”€β”€ langs                                             β”‚   β”‚   └── mymodule.lang
  β”‚   β”œβ”€β”€ en_US                                         β”‚   └── **_**
  β”‚   β”‚   └── mymodule.lang                             β”‚       └── mymodule.lang
  β”‚   └── **_**                                         β”œβ”€β”€ sql
  β”‚       └── mymodule.lang                             β”‚   β”œβ”€β”€ *.sql
  β”œβ”€β”€ sql                                               β”‚   └── *.key.sql
  β”‚   β”œβ”€β”€ *.sql                                         β”œβ”€β”€ css
  β”‚   └── *.key.sql                                     β”‚   └── *.css
  β”œβ”€β”€ css                                               β”œβ”€β”€ js
  β”‚   └── *.css                                         β”‚   └── *.js
  β”œβ”€β”€ js                                                β”œβ”€β”€ tpl
  β”‚   └── *.js                                          β”‚   └── *.tpl.php
  β”œβ”€β”€ tpl                                               └── myfirstpage.php
  β”‚   └── *.tpl.php
  β”œβ”€β”€ myfirstpage.php
+ β”œβ”€β”€ config.php (mandatory)
+ └── autoload.php (mandatory)

Explanation:

  • admin/setup.php and admin/about.php contains the module settings & the author informations (they can only be consulted by an administrator).
  • core/modules/modMyModule.class.php is the module main configuration file or class, it contains all the informations about the module: name, description, menus, user permissions, etc.. In dolibase, this is a bit different, the module main configuration is set in the config.php file in a way to allow reusing it in other parts of the module.
  • core/boxes/mywidget.php is a module widget that can be displayed in the dashboard of Dolibarr.
  • core/triggers contains trigger files that allows you to execute personalized code after a dolibarr event.
  • class folder may contain your objects class & functions, sql queries, etc.. It's a kind of model(s) container if you're familiar with the MVC architecture.
  • dolibase folder contains all the code & logic of dolibase.
  • img is a folder for your images (note that module's picture should start with the object_ prefix).
  • langs folder contains all the translations related to your module.
  • sql folder contains the sql files to create or update the tables of your module.
  • css folder should contain your css files.
  • js folder is for your javascript files.
  • myfirstpage.php is considered as your first module page in the example above.
  • config.php is the configuration file used by dolibase, it contains the main configuration for your module @see config.default.php.
  • autoload.php is responsible of loading the module configuration, dolibarr environment & dolibase requirements. You can even add any php file you want to be auto-loaded inside it @see autoload.default.php.

Installation

Dolibase can act in 2 different ways:

  • Globally (recommended): this means that dolibase needs to be installed only once in dolibarr's root directory & then all dolibase modules will use the global version.
  • Internally: each module can have its own version of dolibase (inside the module folder), so this method doesn't require any pre-installation, but some conflits may occur between modules using an old dolibase version & those using a new one.

So, to install dolibase globally, just unzip it inside your dolibarr root directory or use the Dolibase Installer module.

Quick start

Starting from version 2.4.0, you can easily generate your modules & widgets using Dolibase Builder.

To create a new module, simply go to the dolibase builder page & follow the instructions:

http://localhost/dolibarr/htdocs/dolibase/builder

Note that localhost/dolibarr may change depending on your dolibarr installation & your domain name.

Check this demonstration video for a quick example.

Examples

  • Module class:
<?php

// Load Dolibase
dol_include_once('mymodule/autoload.php');

// Load Dolibase Module class
dolibase_include_once('core/class/module.php');

class modMyModule extends DolibaseModule
{
    public function loadSettings()
    {
        // Add constant(s)
        $this->addConstant('MY_MODULE_CONST', 'test');

        // Add widget(s)
        $this->addWidget('mywidget.php');

        // Add CSS & JS files
        $this->addCssFile('mycss.css')
             ->addJsFile('myjs.js');

        // Set user permission(s)
        $this->addPermission('read', 'Read permission', 'r');

        // Add menu(s)
        $this->addTopMenu($this->config['other']['top_menu_name'], 'MyTopMenu', '/mymodule/index.php?test=1')
             ->addLeftMenu($this->config['other']['top_menu_name'], 'myleftmenu', 'MyLeftMenu', '/mymodule/index.php?test=2')
             ->addLeftSubMenu($this->config['other']['top_menu_name'], 'myleftmenu', 'mysubleftmenu', 'MySubLeftMenu', '/mymodule/index.php?test=3');
    }
}
  • Module page:
<?php

// Load Dolibase
include_once 'autoload.php';

// Load Dolibase Page class
dolibase_include_once('core/class/page.php');

// Create Page using Dolibase
$page = new Page('My Page Title', '$user->rights->mymodule->read'); // set page title & control user access

// Print page header & dolibarr's main menus
$page->begin();

echo 'Hello world!';

// Print page footer
$page->end();
  • Widget class:
<?php

// Load Dolibase
dol_include_once('mymodule/autoload.php');

// Load Dolibase Widget class
dolibase_include_once('core/class/widget.php');

// Load Dolibase QueryBuilder class
dolibase_include_once('core/class/query_builder.php');

class MyWidget extends Widget
{
    /**
     * @var Widget Label
     */
    public $boxlabel = 'MyWidget';
    /**
     * @var Widget Picture
     */
    public $boximg = 'mywidget.png';
    /**
     * @var Widget Position
     */
    public $position = 1;
    /**
     * @var Widget is Enabled
     */
    public $enabled = 1;


    /**
     * Load data into info_box_contents array to show array later. Called by Dolibarr before displaying the box.
     *
     * @param int $max Maximum number of records to load
     * @return void
     */
    public function loadBox($max = 5)
    {
        // Set title
        $this->setTitle('Users List');

        // Get users login, firstname & lastname from database
        $qb = QueryBuilder::getInstance()
                          ->select('login, firstname, lastname')
                          ->from('user')
                          ->orderBy('rowid', 'ASC')
                          ->limit($max);

        // Show users
        foreach ($qb->result() as $row) {
            $this->addContent($row->login)
                 ->addContent($row->firstname)
                 ->addContent($row->lastname)
                 ->newLine();
        }
    }
}

πŸŽ‰ So simple isn't it ?!

Find more module examples in the test folder.

Advanced features

With dolibase & debugbar module you get a bunch of benefits:

  • Easily debug your modules.

debug

To add a message to debug bar simply call the dolibase_debug function:

dolibase_debug('your message');
  • Access to dolibarr configuration variables & constants.

config

  • Get all the executed queries ~ even after a page redirection.

database

To save queries on page redirection use the dolibase_redirect function:

dolibase_redirect('page.php');
  • Optimise load & response time of your modules.

timeline

To start measuring time call the start_time_measure function then provide a measure name & label:

start_time_measure('measure_1', 'Label');

To stop the time measuring call the stop_time_measure function with your measure name as a parameter:

stop_time_measure('measure_1');
  • Check dolibarr logs instantly (logs module should be enabled).

logs

Documentation

Before consulting the documentation you must have some knowledge about how dolibarr works and how to develop a module, otherwise you are invited to check the links below:

Changelog

See changelog.

Contributing

Follow contributing guidelines.

License

Dolibase is licensed under the MIT license.

Support me

Check my own modules collection on Dolistore.