Module programming
Fundanemt gives developers the possibility to add new functionality to the system by creating modules and installing them into the system. In this article I will go through the process of creating a module in the hope of making it easier for outsiders to create modules.
Note: This article has been written with UNIX systems in mind, but it should be easy to follow the steps on a Windows system as well.
Example module functionality
In this article I will create a pizza guide module with the following functionality:
- Keep a list of different pizza resturants with
- a picture of the resturant
- name
- phone number
- address
- List pizzas for each resturant with
- price
- number
- ingredients
- Add new pizzas to a resturant
Telling Fundanemt about your module
First step is to create the folder where your module should be located.
$ cd fundanemt/modules
$ mkdir PizzaGuide
it would be possible to test the module by placing scripts in fundanemt/modules/PizzaGuide and entering http://domain.tld/fundanemt/modules/PizzaGuide/ in your browser, but for the user to access the module from the Fundanemt main page, you will need to place a configuration file in the module folder to inform Fundanemt about the modules name, description, author and version, the size of the modules main dialog.
<?php
// Meta tags for module
$name = "PizzaGuide";
$version = "1.0";
$author = "Brian Jørgensen";
$authormail = "qte [at] fundanemt.com";
$title = gt("Pizza Guide");
$description = gt("Administrate list of pizza places and pizzas.");
$icon = "pizza.png";
// Setup of the initial module dialog
$startupdialog = "";
$startupdialogHeight = "500";
$startupdialogWidth = "770";
// Placement on the frontpage
$group = gt("Modules");
?>
save the above code in fundanemt/modules/PizzaGuide/config.php
When reloading the main Fundanemt page the Pizza guide module will appear in the module list
Data design
The second step is to design what data the module needs to function and where it should be stored. In our example the module both needs store images on disk (pictures of the resturants) and the rest of the data in database tables.
Table: resturant list
| Field name | Field type |
| id | integer |
| name | text |
| phone number | text |
| address | text |
| picture | text |
Table: pizza list
| Field name | Field type |
| resturant | integer |
| number | integer |
| name | text |
| ingredients | text |
for the simplicity of this article the ingredients are written in normal english just like they would be on a real life pizza-list.
Transforming into database tables
When the table structure has been designed, the tables needs to be created by the module. This is done by transforming the table structure into an array and let the Fundanemt database object create the table.
<?php
define("PIZZA_PLACES", "pizza_places");
define("PIZZA_LIST", "pizza_list");
$GLOBALS["D"]->dbSetTable(PIZZA_PLACES);
if (!$GLOBALS["D"]->dbExists()) {
$data = array(
"id" => "integer",
"name" => "text",
"phonenumber" => "text",
"address" => "text",
"picture" => "text"
);
$GLOBALS["D"]->dbCreateTable($data);
}
$GLOBALS["D"]->dbSetTable(PIZZA_LIST);
if (!$GLOBALS["D"]->dbExists()) {
$data = array(
"resturant" => "integer"
"number" => "integer",
"name" => "text",
"ingredients" => "text"
);
$GLOBALS["D"]->dbCreateTable($data);
}
?>
Main module file - index.php
In the configuration file option $startupdialog it is possible to specify a path within the module folder that the main module dialog should open with.
If you have not specified a filename path youl will have to create a index.php file in the module folder. The next step is to create this main file so we can get on with programming the module.
<?php
// Include fundanemt setup file
include "../../core/fundaGUI.php";
// Include library and database structure
include "includes/datastructure.inc";
include "include/library.inc";
$arrStatus = array(
gt("Kategori") => isset($_SESSION[$GLOBALS["module"]["name"]]["id"]) ? $arrayCats[$_SESSION[$GLOBALS["module"]["name"]]["id"]] : gt("None selected"),
gt("Spillere i alt:") => $totalcount
);
printMainDialog(gt("Pizza Guide"), $arrStatus, MODULE_URL."PizzaGuide/includes/");
?>
Resturant dialog - Add/edit
Pizza dialog - Add/edit
Main window
General notes
Database table field names should be uppercase to work on both MySQL and PostgreSQL
TODO
- Add usage of DB_PREFIX constants in datastructure.inc