Note
The example goes from version 17.1 A module consists of at least one PHP file .php for the code that should run in the background and a template file .tpl which provides for the representation of HTML. The PHP file is described first, at the bottom are the template files.
First, the class for the module is created, like the example module here. Static function TableSearch is used to define all live tables for the module. for the module are defined. Inside TableSearch() all live tables are enumerated with a switch. Live tables can be named arbitrarily. In the variable $heading is an array with the column names which are displayed in the live table. With the variable $width the individual column widths are specified in an array. The variable $findcols contains an array with the database columns that are sortable in the live table. $searchsql specifies in the array the database columns that can be searched in the search window. In the variable $menue, if needed, the menu items for the entries of the live table, like in this example edit and delete. In the corresponding link the respective ActionHandler can be seen, which will be executed when clicked on . The variable $sql contains the SQL query of which the result can be seen in the live table. Note that the WHERE of the query must be entered in the variable $where. variable. The same applies to GROUP BY, which is in the variable $groupby. $count takes care of the representation of the number of entries that are displayed below the live table.
?php //WAWIFILEONLYON VERSION=ALL
class example module { var $app;}
static function TableSearch(&$app, $name, $allowedvars) { // in this switch all local tables (those live tables with search etc.) for this module switch($name) { case "examplemodule_list": $allowed['examplemodule'] = array('list');
$heading = array('Adresse', 'Datum', 'Beschreibung', 'Menü'); $width = array('40%', '30%', '20%', '10%'); $findcols = array('b.id', 'b.adresse', 'b.datum', 'b.beschreibung'); $searchsql = array('b.id', 'b.adresse', 'b.datum', 'b.beschreibung'); $defaultorder = 1; $defaultorderdesc = 0; $jahr = date("Y"); $menu = "<a href=\"index.php?module=beispielmodul&action=edit&id=%value%\"><img src=\"./themes/{$app->Conf->WFconf['defaulttheme']}/images/edit.png\" border=\"0\"></a>"; $sql = "SELECT SQL_CALC_FOUND_ROWS b.id, b.adresse, b.datum, b.beschreibung, b.id FROM beispielmodul b"; $where = " YEAR(b.datum) < '$jahr'"; $groupby = "GROUP BY b.datum"; $count = "SELECT count(DISTINCT b.id) FROM beispielmodul b WHERE $where"; break; } $erg = false; foreach($erlaubtevars as $k => $v) { if(isset($$v))$erg[$v] = $$v; } return $erg;
This is followed by the function __construct() which is the constructor and defines all the ActionHandler. The ActionHandler defines in the first parameter which action is meant and in the second parameter the corresponding function that is to be respective action to be executed. The ActionHandlers can also be named arbitrarily. In addition, the function Install() is called.
function __construct($app, $intern = false) { $this->app=&$app; if($intern)return; $this->app->ActionHandlerInit($this); //Define here all ActionHandler that the module has $this->app->ActionHandler("list", "ExampleModuleList"); $this->app->ActionHandler("create", "ExampleModuleCreate"); $this->app->ActionHandler("edit", "ExampleModuleEdit"); $this->app->ActionHandler("delete", "ExampleModuleDelete");
$this->app->ActionHandlerListen($app); $this->Install(); }
In order to create new tables and columns in the database, a new function named Install() is created. Within this function the functions CheckTable() and CheckColumn() are used. CheckTable() checks if the table already exists in the database and creates it if not. If this is not the case, CheckColumn() checks if the columns in the given table already exist and creates them if they do not.
function Install() { $this->app->erp->CheckTable("examplemodule"); $this->app->erp->CheckColumn("id", "int(11)", "examplemodule", "NOT NULL AUTO_INCREMENT"); $this->app->erp->CheckColumn("address", "int(11)", "examplemodule", "NOT NULL"); $this->app->erp->CheckColumn("date", "DATE", "examplemodule", "NOT NULL"); $this->app->erp->CheckColumn("description", "VARCHAR(255)", "examplemodule", "NOT NULL"); }
Next come the individual functions that are called by the ActionHandler. ExamplemodulList() makes sure that everything is displayed that the user should see when calling the module. The functions MenuEntry adds new menu items to the navigation of the module. TableSearch specifies which live table should be used to display the data, in this case the live table sample_module_list. Without specifying a template file with the function Parse, no content can be displayed.
function examplemoduleList() { $this->app->erp->MenuItem("index.php?module=examplemodule&action=create", "New Entry"); $this->app->erp->MenuItem("index.php?module=examplemodule&action=list", "Overview");
$this->app->YUI->TableSearch('TAB1','example_module_list', "show","","",basename(__FILE__), __CLASS__);
$this->app->Tpl->Parse("PAGE", "example_module_list.tpl");
Inside the function ExampleModuleEdit() the code is defined, which will be executed when the edit symbol of an entry in the live table is defined. First, MenuEntry is used to add new menu items to the module. One that links to the current page and one that links back to the live table. is added. With GET the ID of the order to be processed is transferred into the variable $id. With POST the value of the save button is gotten. In addition an array is created that gets the values from the input fields via GetInput (more below). the input fields. If the ID is set and a number and the save button was pressed, it is checked if all mandatory fields were filled. If this is not the variable $error receives an error message. If the variable $error is not empty, that means not all mandatory fields have been filled, an error message is displayed in the template field area MESSAGE. Otherwise the entry is updated with UPDATE and a success message is displayed. Furthermore, the current date stored for this entry is fetched from the database. The same applies to the description. With the help of Set these values into the input fields, so that the person, if he wants to edit the entries entries, they can see exactly which values they are editing. Parse specifies which template file is to be used.
function samplemoduleEdit() { $this->app->erp->MenuEntry("index.php?module=samplemodule&action=edit&id=$id", "Details"); $this->app->erp->MenuEntry("index.php?module=example module&action=list", "Back to overview");
$id = (int)$this->app->Secure->GetGET('id');
$store = $this->app->Secure->GetPOST('store');
$input = array();
$input = $this->GetInput();
if(is_numeric($id) && $store!=''){
$error = '';
if($input['date']=='') $error .= 'Please enter a date.<br>';
if($input['description'] == '') $error .= 'Please enter a description.<br>';
if($error!=''){
$this->app->Tpl->Set('MESSAGE',"<div class=\"error\">$error</div>");
}else{
if($error == ""){
$this->app->DB->Update("UPDATE example module SET date ='{$input['date']}', description='{$input['description']}' WHERE id = '$id' LIMIT 1");
$this->app->Tpl->Set('MESSAGE', "<div class=\"success\">The settings were successfully übaccepted.</div>");
}
}
}
$date = $this->app->DB->Select("SELECT date FROM example module WHERE id = '$id'");
$description = $this->app->DB->Select("SELECT description FROM example module WHERE id = '$id'");
$this->app->Tpl->Set('DATE', $date);
$this->app->Tpl->Set('DESCRIPTION', $description);
$this->app->Tpl->Parse('PAGE', "example_module_edit.tpl");
}
Example moduleDelete() deletes the selected entry from the live table when clicking on the Delete icon. By means of the variable $_SERVER['HTTP_REFERER'] it is possible to determine from which page the user came, in this case the current page with the live table. With GetGET the ID of the entry to be deleted is fetched and with a MySQL statement the entry will be deleted. Finally the user is redirected to the previous page.
function examplemoduleDelete() { $ref = $_SERVER['HTTP_REFERER']; $id = $this->app->Secure->GetGET("id"); $this->app->DB->DELETE("DELETE FROM example module WHERE id = '$id' LIMIT 1");
header("Location: $ref");
exit;
The function ExampleModuleCreate() creates a new entry in the database table examplemodule. First a new menu item is added to the module with MenuEntry, which points back to the live table. With the function GetInput the values from the input fields. The explanation of this function is below. GetPOST gets the value of the Save button. If this value is not empty, i.e. it was pressed, it will be checked whether the mandatory fields have been filled in or not. If they were not filled in, the variable $error receives an error message. If the variable $error contains a value, the error message will be displayed in the template field area MESSAGE. Otherwise the new entry will be added and the message of success is displayed. If the Save button has not been pressed, the input fields are the input fields are filled with the values entered so far using the SetInput function (explained below). Example moduleCreate() refers at the end to another template, because the interface is different from ExampleModuleList(), but the same template can be used as in ExampleModuleEdit(), because the surfaces are the same for both.
function sampleModuleCreate() { $this->app->erp->MenuEntry("index.php?module=sampleModule&action=list", "Back to overview");
$input = $this->GetInput(); $speichern = $this->app->Secure->GetPOST("speichern"); if($speichern != ""){ $error = ''; if($input['datum']=='') $error .= 'Geben Sie bitte ein Datum ein.<br>'; if($input['beschreibung']=='') $error .= 'Geben Sie bitte eine Beschreibung ein.<br>'; if($error!=''){ $this->app->Tpl->Set('MESSAGE', "<div class=\"error\">$error</div>"); }else{ $this->app->DB->Insert("INSERT INTO beispielmodul (datum, beschreibung) VALUES ('{$input['datum']}', '{$input['beschreibung']}')"); $newid = $this->app->DB->GetInsertID(); $msg = $this->app->erp->base64_url_encode("<div class=\"success\">Der Eintrag wurde erfolgreich angelegt.</div>"); header("Location: index.php?module=beispielmodul&action=edit&id=$newid&msg=$msg"); exit; } } $this->SetInput($input); $this->app->Tpl->Parse('PAGE', "beispielmodul_edit.tpl");
The GetInput function will create a variable called $input, which will contain an array. At the index datum of the array the value will be fetched by POST from the variable. The same is done for the index description. At the end the array is returned at return.
function GetInput(){ $input = array(); $input['date'] = $this->app->Secure->GetPOST('date'); $input['description'] = $this->app->Secure->GetPOST('description');
return $input;
The SetInput function receives an array as a parameter. By means of the function Set the text fields for date and description are filled at the position DATE and DESCRIPTION.
function SetInput($input){ $this->app->Tpl->Set('DATE', $input['date']); $this->app->Tpl->Set('DESCRIPTION', $input['description']); }
<div id="tabs-1"> [MESSAGE] <form action="" method="post"> <fieldset> <legend>Land</legend> <table width="100%" border="0" class="mkTableFormular"> <tr><td>Datum: </td><td><input type="text" name="datum" value="[DATUM]" size="40"></td></tr> <tr><td>Beschreibung: </td><td><input type="text" name="beschreibung" value="[BESCHREIBUNG]" size="40"></td></tr> </table> </fieldset> <input type="submit" name="speichern" value="Speichern" style="float:right"/> </form> </div>