Modules location & Name
All modules should be added in the modules folder in your root directory where Our CRM is installed and each module must have unique folder name and init file with the same name as your module folder name.
Creating your first module
Before start to develop a module, make sure that you set development mode in order to see any errors and functions/hooks deprecation warnings.
To get started and create your first module, follow the steps below.
· Navigate to Our CRM installation and open the modules directory
· Create a new directory and name it after your module (e.q. sample_module)
· Create a new PHP file with the same name as the module directory, in this case, sample_module.php, this file will act as “init file” for this module.
In this case, the module is already created and registered but it does not do anything yet nor have any name or descriptions added, we will add the module metadata like name, description, URL, author via a PHP block comment which will act as module headers.
The module headers should be added only in the init module file.
Now open the sample_module.php file we created before and add the following comment at the top of the file:
<?php
/**
* Ensures that the module init file can't be accessed directly, only within the application.
*/
defined('BASEPATH') or exit('No direct script access allowed');
/*
Module Name: Sample Our CRM Module
Description: Sample module description.
Version: 2.3.0
Requires at least: 2.3.*
*/
After you save the file, you can navigate in your Our CRM admin area to Setup->Modules and you will be able to see your module listed.
The comment will act as a file header for the module, you should check the file headers guide if you want to get more familiar.
Make sure that you always add the Module Name header
It’s very important to make sure that your module folder name and the .php file name are the same, otherwise, your module won’t be shown in the modules list
Basic Hooks
You will need to implement few basic hooks when creating new module, like register_activation_hook(), register_deactivation_hook() and register_uninstall_hook()
If you are familiar with WordPress, you will probably know the work that these hooks do.
Adding Hooks
You can add hooks in your module .php files with:
hooks()->add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1);
hooks()->add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1);
hooks()->do_action($tag, $arg = '');
hooks()->apply_filters($tag, $value, $additionalParams);
You can add your own filters and actions so you can use them in your module implementation also, adding actions and filters will help other modules to interact with your module and extend the module functionalities.
Prefix Custom Functions
You should always prefix any custom functions and classes to prevent any conflicts with Our CRM default functions or classes or with any other modules.
Using Codeigniter Instance in module files
Probably you will want to use the Codeigniter framework instance in your module init file or any other modules files that are not extending the framework base classes.
$this, only works within your controllers, your models, or your views.
You can achieve this anywhere in your files with the following code:
First, assign the CodeIgniter object to a variable:
$CI = &get_instance();
Once you’ve assigned the object to a variable, you’ll use that variable instead of $this
$CI =&get_instance();
$CI->load->helper('module_name/helper_name');
$CI->load->library('module_name/library_name');
Database Prefix
From version 2.3.0 Our CRM have option to define custom database tables prefix.
The default table prefix in Our CRM is tbl
You should always use our custom function db_prefix() to get the database prefix when querying the database, this will ensure that in case the user changed the database prefix, your module will still work with the new user prefix.
Here is an example from the Goals Tracking module install.php file.
<?php
defined('BASEPATH') or exit('No direct script access allowed');
if (!$CI->db->table_exists(db_prefix() . 'goals')) {
$CI->db->query('CREATE TABLE `' . db_prefix() . "goals` (
`id` int(11) NOT NULL,
`subject` varchar(191) NOT NULL,
`description` text NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`goal_type` int(11) NOT NULL,
`contract_type` int(11) NOT NULL DEFAULT '0',
`achievement` int(11) NOT NULL,
`notify_when_fail` tinyint(1) NOT NULL DEFAULT '1',
`notify_when_achieve` tinyint(1) NOT NULL DEFAULT '1',
`notified` int(11) NOT NULL DEFAULT '0',
`staff_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');
Create Module Options
Our CRM has table in database options for storing various settings for internal usage and settings that are used in features. We have developed custom PHP functions that will perform queries to fetch options from database.
Option names are strings, and they must be unique so that they do not conflict with either Our CRM or other Modules.
add_option($name, $value, $autoload)
$name
Required (string). The name of the option to be added, make
sure it’s unique and prefixed with E.q. your module name.
$value
The option value (string)
$autoload
(integer) 1 or 0
Whether this option should be autoloaded with all other options, if you are
using the option too much time in the view, the best is to autoload it to
prevent multiple queries in order to get the option. Defaults to 1
Keep in mind that add_option function won’t create the option if the option name already exists in the options table.
get_option($option_name);
Retrieve an option from database, $option_name (string) your option name.
update_option($option_name, $new_value);
The update_option function will update the option value, since version 2.3.3, when you call update_option if the option not exists, Our CRM will create this option.