azahraoui / dolicore
DoliCore based on dolibase plugin of Dolibarr
Requires
- php: >=8.1
This package is auto-updated.
Last update: 2024-09-19 03:12:31 UTC
README
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
andadmin/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 theconfig.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 theobject_
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/BaseModule.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.
To add a message to debug bar simply call the dolibase_debug
function:
dolibase_debug('your message');
- Access to dolibarr configuration variables & constants.
- Get all the executed queries ~ even after a page redirection.
To save queries on page redirection use the dolibase_redirect
function:
dolibase_redirect('page.php');
- Optimise load & response time of your modules.
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).
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.