Skip to content
tenzap edited this page Dec 14, 2024 · 5 revisions

Kalkun plugins use combination of HMVC (Hierarchical model–view–controller) and hook pattern.

Every plugin has it's own controller, model, view, library. In addition it can use Kalkun's core resources.

Path

All plugins must be located in application/plugins/.

A plugin is represented by a folder named as plugin_name.

A plugin must have a core file, also named: plugin_name.php.

For example, if you have a plugin called my_plugin then it must be placed in application/plugins/my_plugin/ and the core file is application/plugins/my_plugin/my_plugin.php.

Core file

The core file must contain :

You may also add a hook.

Header

Every plugin must have a header containing information about the plugin. This information is inserted into database.

/**
* Plugin Name: Simple Autoreply
* Plugin URI: http://azhari.harahap.us
* Version: 0.1
* Description: Simple a.k.a stupid autoreply, reply to all incoming message
* Author: Azhari Harahap
* Author URI: http://azhari.harahap.us
*/

Utility function (for Kalkun < 0.8.3)

The core file must include these utility function.

Utility function name must be prefixed with the plugin name followed by an underscore.

  • Install. This function is called when the plugin is first installed into the database
function simple_autoreply_install()
{
	return true;
}
  • Activate. This function is called when the plugin is first activated
function simple_autoreply_activate()
{
	return true;
}
  • Deactivate. This function is called when the plugin is deactivated
function simple_autoreply_deactivate()
{
	return true;
}

Plugin class (for Kalkun >= 0.8.3)

class Simple_autoreply_plugin extends CI3_plugin_system {

	use plugin_trait;

	public function __construct()
	{
		parent::__construct();
		// Add hook for incoming message below if required
	}

	// ------------------------------------------------------------------------

	/**
	* Install Plugin
	*
	* Anything that needs to happen when this plugin gets installed
	*
	* @access public
	* @since   0.1.0
	* @return bool    TRUE by default
	*/
	public static function install($data = NULL)
	{
		return TRUE;
	}

	// ------------------------------------------------------------------------

	/**
	* Activate Plugin
	*
	* Anything that needs to happen when this plugin gets activate
	*
	* @access public
	* @since   0.1.0
	* @return bool    TRUE by default
	*/
	public function activate($data = NULL)
	{
		return TRUE;
	}

	// ------------------------------------------------------------------------

	/**
	* Deactivate Plugin
	*
	* Anything that needs to happen when this plugin gets deactivate
	*
	* @access public
	* @since   0.1.0
	* @return bool    TRUE by default
	*/
	public function deactivate($data = NULL)
	{
		return TRUE;
	}
}

Adding a hook

For Kalkun < 0.8.3

  1. First you need to register the hook. Do it this way:

    add_action(HOOK_NAME, FUNCTION_NAME, PRIORITY);

    For example:

    add_action("message.incoming.before", "simple_autoreply", 11);
  2. Create the function you just referenced in add_action. For example: Function simple_autoreply:

    function simple_autoreply($sms)
    {
    	$config = simple_autoreply_initialize();
    	$CI =& get_instance();
    	$CI->load->model('Message_model');
    	$data['coding'] = 'default';
    	$data['class'] = '1';
    	$data['dest'] = $sms->SenderNumber;
    	$data['date'] = date('Y-m-d H:i:s');
    	$data['message'] = $config['message'];
    	$data['delivery_report'] = 'default';
    	$data['uid'] = $config['uid'];
    	$CI->Message_model->send_messages($data);
    }

For Kalkun >= 0.8.3

It is possible to register the hook in two ways: action or filter. An action will just fire off a function when the tag is called, a filter will parse data and return the modified data. This behavior is slightly different than in Kalkun < 0.8.3 which only had action and behaved like filter in kalkun >= 0.8.3

  • add_action() work differently than the add_action() in kalkun < 0.8.3
  • add_filter() works the same way as add_action() in kalkun < 0.8.3
  1. Registering the hook has to be done inside the constructor.

    	public function __construct()
    	{
    		parent::__construct();
    		// Add hook for incoming message
    		add_action(HOOK_NAME, FUNCTION_NAME, PRIORITY);
    	}

    FUNCTION_NAME should be:

    • either the name of a function
    • or of an array with object or class, function name.

    For example:

    	public function __construct()
    	{
    		parent::__construct();
    		// Add hook for incoming message
    		add_filter('message.incoming.before', array($this, 'simple_autoreply'), 11);
    	}
  2. Create the function you just referenced in add_filter. Put it inside the class.

    	function simple_autoreply($sms)
    	{
    		$config = Plugin_helper::get_plugin_config('simple_autoreply');
    		$CI = &get_instance();
    		$CI->load->model('Message_model');
    		$data['class'] = '1';
    		$data['dest'] = $sms->SenderNumber;
    		$data['date'] = date('Y-m-d H:i:s');
    		$data['message'] = $config['message'];
    		$data['delivery_report'] = 'default';
    		$data['uid'] = $config['uid'];
    		$CI->Message_model->send_messages($data);
    	}

Available hooks

  • message.incoming.before
  • message.incoming.after
  • message.outgoing
  • message.outgoing_all
  • message.outgoing_dest_data
  • phonebook.contact.get
  • phonebook.contact.menu

Access Kalkun core resource

If you need to access the kalkun core resources (which you usually do with $this when in a controller, model...), this can't be done in the core file. For that you need to get it as below instead.

$CI =& get_instance();
$CI->load->model('Message_model');

Helper functions

If you want to load the configuration of the plugin, or the language files to have the translated labels, there are 2 helper functions in class Plugin_helper:

  • Plugin_helper::get_plugin_config('plugin_name');
  • Plugin_helper::load_lang('plugin_name');
Clone this wiki locally