Create a module
Make sure you have the following tools:
- PHP command line interface (php-cli)
- subversion
- A text editor
- Database client (PhpMyAdmin for example)
Checkout latest Subversion repository:
$ svn checkout https://anonymous@group- \
office.svn.sourceforge.net/svnroot/group-office/branches/ \
groupoffice-com-2.17
office.svn.sourceforge.net/svnroot/group-office/branches/ \
groupoffice-com-2.17
Design your database
We're going to use a shipping module as example. It contains two tables. One with the destinations and one with the jobs to be processed for a destination. CREATE TABLE `sh_destinations` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `sh_jobs` (
`id` int(11) NOT NULL,
`destination_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `destination_id` (`destination_id`)
) ;
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `sh_jobs` (
`id` int(11) NOT NULL,
`destination_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `destination_id` (`destination_id`)
) ;
Create module directory structure and interface
Execute the lib/scripts/createmodule.php script in the command line shell:
$ php createmodule.php
The script will ask you some questions:
What's the ID of the module?
shipping
The module ID must be a unique string containing only textual or numeric characters and no spaces.
What's the short name of the module (used for table prefixing)?
sh
Enter a prefix for table and language variables here
Enter table name (or leave empty to finish):
sh_destinations
Enter the name of the first table we created.
Enter the code variable name referring to an item in the table (No spaces and no strange characters please eg. 'item'):
destination
Enter the code variable name referring to multiple items in the table (No spaces and no strange characters please eg. 'items'):
destinations
Is there a dropdown field in the table that should get values from another table? (y/N):
n
We don't do this yet but we use this on the sh_jobs table.
Enter table name (or leave empty to finish):
sh_jobs
Enter the name of the first table we created.
Enter the code variable name referring to an item in the table (No spaces and no strange characters please eg. 'item'):
job
Enter the code variable name referring to multiple items in the table (No spaces and no strange characters please eg. 'items'):
jobs
Is there a dropdown field in the table that should get values from another table? (y/N):
y
We want to link the destination_id field to the sh_destinations table here. So the script will generate a dropdown select box in the interface with the destinations instead of showing an input box for the destination_id field.
Enter field name:
destination_id
Enter the class name that has the function to get the dropbox values:
shipping
The script will generate a class with the same name as the module ID.
Enter the function name with variables eg. get_items('some_value');:
get_destinations()
The script will generate a function called get_destinations() because we entered that value to address multiple items as destinations
Enter the name of the record field that holds the dropbox value:
id
This is the sh_destinations.id field.
Enter the name of the record field that holds the dropbox text:
name
This is the sh_destinations.name field.
Is there a dropdown field in the table that should get values from another table? (y/N):
Enter table name (or leave empty to finish):
Module structure generated
Processing tables
Finished
This will create a very default shipping module. Login to Group-Office now and install the module.
The module files explained
The createmodule.php script created directories and files now. But what are their functions?| File/Directory | Function |
| modules/shipping/ | The module directory. This contains all the module files. You shouldn't modify files outside this directory to make your module work because things will break when you update to a later version of Group-Office. |
| modules/shipping/sql/ | Contains SQL queries to install, uninstall and update the module |
| modules/shipping/classes/ | Contains the classes that handle all the module functions. |
| modules/shipping/language/ | Contains the language files |
| modules/shipping/index.php | The index page of the module. The first page loaded when the module is accessed. |
| modules/shipping/themes/ | Theme directory where all the styles and images specific to this module should be. A default image is put there. The *.inc files and other php files in the modules/shipping/ directory. These are generated based on the SQL table structure. They belong to the Graphical user interface of the module to add, edit, delete items to the database. |
Installing the module
You can install the module in Group-Office now. Log in as an administrator and go to:Administrator menu -> modules
Look for the shipping module at the left and click at 'Install'.
Taking a look at the generated code
Open the modules/shipping/index.php to see what the script created.
I think the code is very easy to read and there are some comments in there to help you. The index file presents a tab window with two tabs. Jobs and destinations.
<?php
/**
* @copyright Intermesh 2006
* @author Merijn Schering <mschering@intermesh.nl>
* @version $Revision: 1.00 $ $Date: 2006/12/05 11:37:30 $
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
//Initialize Group-Office framework
require_once('../../Group-Office.php');
/**
* @copyright Intermesh 2006
* @author Merijn Schering <mschering@intermesh.nl>
* @version $Revision: 1.00 $ $Date: 2006/12/05 11:37:30 $
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
//Initialize Group-Office framework
require_once('../../Group-Office.php');
the Group-Office.php file initializes all the Group-Office functtions. It creates a number of objects:
$GO_CONFIG - configuration settings - see GroupOffice.php
$GO_SECURITY - security class - see classes/base/*.security.class.inc
$GO_USERS - user management class see classes/base/*.users.class.inc
$GO_GROUPS - user group management class see classes/base/*.groups.class.inc
$GO_LANGUAGE - language class - Handles the multi lingual functions
$GO_THEME - theme class - Loads themes
It also initializes the session variables. Group-Office has some commonly used information about the logged in user stored in the session.
These values are always available from the $_SESSION['GO_SESSION'] var:
["theme"]=>
string(7) "Default"
["decimal_seperator"]=>
string(1) ","
["thousands_seperator"]=>
string(1) "."
["date_seperator"]=>
string(1) "-"
["date_format"]=>
string(5) "d-m-Y"
["time_format"]=>
string(3) "G:i"
["currency"]=>
string(3) "€"
["timezone"]=>
string(1) "1"
["DST"]=>
string(1) "1"
["user_id"]=>
string(1) "2"
["username"]=>
string(22) "mschering@intermesh.nl"
["authcode"]=>
string(8) "ngekv8ja"
["name"]=>
string(15) "Merijn Schering"
["first_name"]=>
string(6) "Merijn"
["middle_name"]=>
string(0) ""
["last_name"]=>
string(8) "Schering"
["country_id"]=>
string(3) "150"
["email"]=>
string(22) "mschering@intermesh.nl"
["lastlogin"]=>
string(10) "1170055655"
["max_rows_list"]=>
string(2) "30"
["start_module"]=>
string(7) "summary"
["first_weekday"]=>
string(1) "1"
["sort_name"]=>
string(9) "last_name"
["use_checkbox_select"]=>
string(1) "0"
//Load commonly used controls
load_basic_controls();
//Authenticate the user for the framework
$GO_SECURITY->authenticate();
//Authenticate the user for the module
$GO_MODULES->authenticate('shipping');
//Get the language variables
require_once($GO_LANGUAGE->get_language_file('shipping'));
//Require the module class
require_once($GO_MODULES->class_path.'shipping.class.inc');
$shipping = new shipping();
//Declare variables
$task = isset($_REQUEST['task']) ? $_REQUEST['task'] : '';
$link_back=$_SERVER['PHP_SELF'];
$form = new form('shipping_form');
$form->add_html_element(new input('hidden','task','',false));
//$form->add_html_element(new html_element('h1', $lang_modules['shipping']));
//Create tabstrip control
$tabstrip = new tabstrip('shipping_tabstrip', $lang_modules['shipping']);
$tabstrip->set_attribute('style','width:100%');
$tabstrip->add_tab('destinations.inc', $sh_destinations);
$tabstrip->add_tab('jobs.inc', $sh_jobs);
//create menu. The buttons are added in the include files.
$menu = new button_menu();
require($tabstrip->get_active_tab_id());
$form->add_html_element($menu);
$form->add_html_element($tabstrip);
require($GO_THEME->theme_path.'header.inc');
echo $form->get_html();
require($GO_THEME->theme_path.'footer.inc');
