4.2. API

Every module will have its own subdirectory under tutos/php. This directory must have:

4.2.1. Module description file

The module description file is typically called mconfig.pinc and could look like the next example listing:

Example 4-1. mconfig.pinc file for the CRM Groups functionality

<?
/*
 * Copyright 2002 by Gero Kohnert
 *
 * Module specific configuration
 *
 *  CVS Info:  $Id: prog-modules-api.html 12 2004-05-25 03:51:17Z aberrios $
 *  $Author: aberrios $
 */

#
# Every module must have an individual ID ( < 100 ).
# this is module 99
#
define('usegroups',-99);
$tutos[modules]['group'] = array( 
	name => "group", 
	Desc => "maintain groups of customers", 
	file => "php/group/group.pinc",
	'overview' => "group/group_overview.php",
	'perm' => usegroups
);
#
# globally enable this module
#
$tutos[usegroups] = 1; 
#
# List of objects/modules where we do something with this module
# and need to include this module
#
$tutos[modulesinc]['group'] = array(
	"company",
	"department",
	"address"
	);
?>
This file is read by the module initialization file tutos/php/modules.pinc and provides all the neccessary information to TUTOS to handle a module. It will populate two arrays, namely 'modules' and 'modulesinc'.

4.2.1.1. The 'modules' array

The 'modules' array will store the name of the module (it should be the same as the array index) a description, the path to the object class files (relative to the tutos dir), a php file which will genearte an adminitrative overview about whatever the module will do and the name of the modules permission key (see the define on top of the file !).

4.2.1.2. The 'modulesinc' array

The 'modulesinc' array is a list of all other sections in TUTOS which should import this module when doing something. In this example the "company","department" and "address" pages have something to do with the CRM module. This will automate the import of all module stuff whenever a line like the one shown in the next listing appears on top of an script.
 loadmodules("address");
This line will import all modules (the file stuff from the 'modules' array) that mention 'address'
 in their 'modulesinc' array.

4.2.2. The 'tutos_module' module

The 'module' object is a subclass of 'tutos_module' (see file: php/module_base.pinc) which itself is a subclass of 'tutos_base' (see file: php/base.pinc) which is the base of all TUTOS objects. 'module_base' will provide empty methods for all module specific tasks. Yes, it's something you might know as an interface in JAVA.

A subclass of 'tuto_module' will have the following methods:

Table 4-1. Method overview

obj_delete Whenever an object will be deleted this function is called. And the module should make everything that is neccessary to handle this event. For example: If your module handle something like notes you should delete all notes that where attached to the object.
  function obj_delete (&$user,&$obj) { 
  
 
obj_save Whenever a object is saved this callback is activated and allows the module to do whatever it want. For example: If you maintain some summaries you might want to recalculate because the calling object might have been modified.
 
   function obj_save (&$user,&$obj) { 
  
 
parseform This callback is activated in the insert scripts and could look at the POST and GET variables of PHP. It is the counterpart of the addform function below. For example: The CRM group module will insert a selection list into the "new" forms of other objects and read the results via parseform.
 
   function parseform (&$user,&$obj,&$gotourl) { 
  
 
addform This callback is activated in the new scripts and could provide some form elements that can be evaluated with the parseform function above.
 
   function addform (&$user,&$obj,$cols) {
  
 
infolist This callback is activated when obj is displayed (detail view). If your module will tell the world something about its relation to obj whis is the place to do so.
   function infolist (&$user,&$obj,$cols) { 
  
 
small_infolist This is the little brother of the above function and up to know called in the calendar scripts.
 
  function small_infolist (&$user,&$obj,$cols) {
  
 
getNewLink, getSelectLink, getOverviewLink These functions should provide urls (the result of makelink) to the approbiate locations that must be called to run the desired scripts.
 
  function getNewLink (&$user,$text = "") {
  function getSelectLink (&$user,$text = "") {
  function getOverviewLink (&$user, $text = "") {
 
getAddLink Should return a url (makelink result) that allows one to add something to the given object.
 
  function getAddLink (&$user, &$obj , $text = "") { 
  
 
getOverviewHeader Overview scripts should call this function when construction their header line. For every data line the getOverviewData function is called.
 
  function getOverviewHeader (&$user,$objtype) { 
  
 
getOverviewData Overview scripts should call this function for every displayed line.
 
   function getOverviewData (&$user,&$obj) { 
  
 
checkservice This function is called on every run of check.php (but only if you register your module for "check")
  function checkservice (&$user) {
  
 

4.2.3. The 'mtable.pinc' file

This file in your module directory is the little brother of the 'table.pinc' file in the main TUTOS directory. It uses the same format and has the same functionality.

If a user calls update.php or sheme.php TUTOS will first check all main tables an then go through all known modules and do the same with the information in every 'mtable.pinc' file.

The main administration page will give you totals about all tables mentioned in your mtable.pinc file.

Example 4-2. Invoice mtable.pinc

<?
/*
 * Copyright 2002 by Gero Kohnert
 *
 * Information about the tables and indices neccessary for this module
 *
 *  CVS Info:  $Id: prog-modules-api.html 12 2004-05-25 03:51:17Z aberrios $
 *  $Author: aberrios $
 */

$table['invoice'] = array(
  name => "invoice",
  Desc => "TUTOS invoices",
  "id"         => array(type => "ID_TYPE",     constraints => "PK",  Desc => "Object ID"),
  "state"      => array(type => "ID_TYPE",     constraints => "",    Desc => "invoice state"),
  "link_id"    => array(type => "ID_TYPE",     constraints => "NN",  Desc => "id of billed object"),
  "name"       => array(type => "VARCHAR", size => 60, constraints => "",    Desc => "invoice name"),
  "d_sent0"    => array(type => "TS_TYPE",     constraints => "",    Desc => "invoice sent"),
  "d_due0"     => array(type => "TS_TYPE",     constraints => "",    Desc => "invoice due"),
  "d_sent1"    => array(type => "TS_TYPE",     constraints => "",    Desc => "first reminder sent"),
  "d_due1"     => array(type => "TS_TYPE",     constraints => "",    Desc => "first reminder due"),
  "d_sent2"    => array(type => "TS_TYPE",     constraints => "",    Desc => "second reminder sent"),
  "d_due2"     => array(type => "TS_TYPE",     constraints => "",    Desc => "second reminder due"),
  "d_finish"   => array(type => "TS_TYPE",     constraints => "",    Desc => "payed or cancelled"),
  "customer"   => array(type => "ID_TYPE",     constraints => "NN",  Desc => "customer ID"),
  "accountant" => array(type => "ID_TYPE",     constraints => "",    Desc => "location/address/company ID"),
  "creator"    => array(type => "ID_TYPE",     constraints => "NN",  Desc => "creator ID"),
  "creation"   => array(type => "TS_TYPE",     constraints => "NN",  Desc => "creation date")
);

$tableidx['invoice1'] = array(
  name  => "invoice_idx1",
  table => "invoice",
  column => array("customer"),
  Desc  => "invoices customers",
);
$tableidx['invoice2'] = array(
  name  => "invoice_idx2",
  table => "invoice",
  column => array("link_id"),
  Desc  => "invoices links",
);

$table['invpos'] = array(
  name => "invoice_pos",
  Desc => "TUTOS invoice positions",
  "id"          => array(type => "ID_TYPE",     constraints => "PK",  Desc => "Object ID"),
  "pos"         => array(type => "ID_TYPE",     constraints => "",    Desc => "position in bill"),
  "ref_id"      => array(type => "ID_TYPE",     constraints => "",    Desc => "possible reference"),
  "description" => array(type => "VARCHAR", size => 60, constraints => "",    Desc => "description"),
  "items"       => array(type => "float",       constraints => "",    Desc => "number of items"),
  "ityp"        => array(type => "VARCHAR", size => 30, constraints => "",    Desc => "type of items"),
  "sum_single"  => array(type => "float",       constraints => "",    Desc => "sum of a single item"),
  "currency"    => array(type => "VARCHAR", size => 4, constraints => "",    Desc => "currency for this position"),
  "tax"         => array(type => "float",       constraints => "",    Desc => "percentage of tax"),
  "inv_id"      => array(type => "ID_TYPE",     constraints => "NN",  Desc => "link to invoice")
);
$tableidx['invpos1'] = array(
  name  => "invpos_idx1",
  table => "invoice_pos",
  column => array("inv_id"),
  Desc  => "invoice positions invoice",
);
$tableidx['invpos2'] = array(
  name  => "invpos_idx2",
  table => "invoice_pos",
  column => array("ref_id"),
  Desc  => "invoice positions reference",
);

$sequence['invname'] = array(
  name  => "invname",
  'start' => 1,
  Desc  => "TUTOS invoice sequencer",
);

?>?>
This file will trigger the generation of two tables (invoice and invoice_pos) with their indices and a sequencer called invname.