The modules documentation is valid starting from version 2.3.2
SudoWorkspace modules use the Codeigniter PHP framework code base like controllers, models, language files, libraries, views etc… if you are already familiar with the Codeigniter framework, you will be able to easily get started, if not we recommend you to visit their official website and documentation .
Additionally, we added a bunch of helper functions, features, action hooks and filters that will make the process of creating and integrating modules easier.
All modules should be added in the modules folder in your root directory where SudoWorkspace is installed and each module must have a unique folder name and init file with the same name as your module folder name .
To get started and create your first module, follow the steps below.
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 SudoWorkspace Module
Description: Sample module description.
Version: 2.3.0
Requires at least: 2.3.*
*/
<?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 SudoWorkspace Module
Description: Sample module description.
Version: 2.3.0
Requires at least: 2.3.*
*/
<?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 SudoWorkspace Module
Description: Sample module description.
Version: 2.3.0
Requires at least: 2.3.*
*/
After you save the file, you can navigate in your SudoWorkspace 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.
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.
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);
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);
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.
You should always prefix any custom functions and classes to prevent any conflicts with SudoWorkspace default functions or classes or with any other modules.
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();
$CI = &get_instance();
$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');
$CI =&get_instance();
$CI->load->helper('module_name/helper_name');
$CI->load->library('module_name/library_name');
$CI =&get_instance();
$CI->load->helper('module_name/helper_name');
$CI->load->library('module_name/library_name');
From version 2.3.0 SudoWorkspace have option to define custom database tables prefix.
The default table prefix in SudoWorkspace 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 . ';');
<?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 . ';');
<?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
SudoWorkspace 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 SudoWorkspace or other Modules. add_option($name, $value, $autoload)
add_option($name, $value, $autoload)
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);
get_option($option_name);
get_option($option_name);
Retrieve an option from database, $option_name (string) your option name.
update_option($option_name, $new_value);
update_option($option_name, $new_value);
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, SudoWorkspace will create this option.