sgsML tutorial: Part 3
Examples
blank.xml:
<table modulename="{t}Blank{/t}" default_view="display" default_sql="no_select">
<view name="display" displayname="{t}Display{/t}" />
<field name="id" simple_type="id" displayname="{t}Id{/t}" />
</table>
This module has the name blank. It will be displayed as {t}Blank{/t} where "{t}Blank{/t}" will be translated using the language files. As we defined one view (display), the default view is also display. The view "display" will be displayed as the translation of {t}Display{/t}. In this example we have one field called "id" with the simple_type of "id" meaning this field will be used as a primary key for the table. Also there is a default_sql statement: This overrides normal (automatic) SQL select statements. When using no_select, there will be no select statement being done. In this example we want the SQL statement to be done automatically, so we strip the default_sql attribute.
Now, let's make a second field called "Last name". Since every person should have a last name we want it to be required every time a new person is created or edited. Last name is a string and therefore we use simple_type "text" which is used for all strings that don't require line breaks. Last name should be named "lastname" in the program and displayed as the translation of {t}Last name{/t}.
blank.xml:
<table modulename="{t}Blank{/t}" default_view="display">
<view name="display" displayname="{t}Display{/t}" />
<field name="id" simple_type="id" displayname="{t}Id{/t}" />
<field name="lastname" displayname="{t}Last name{/t}" simple_type="text" required="true" />
</table>
Adding "first name" is done the same way:
<field name="firstname" displayname="{t}First name{/t}" simple_type="text" required="true" />
To help you finding your entries we want to sort them by lastname (default is Id). Therefore we expand the table statement with two attributes:
orderby="lastname" order="asc"
Orderby defines the field we want to sort by and asc means that the entries should be sorted ascending (instead of desc for descending). Having more than 100 entries makes the list very long, so we want to distribute the entries to several pages. To do this, only set a limit to the table:
limit="20"
To help you to remember the relationships to these assets we add a new field called description:
<field name="description" displayname="{t}Description{/t}" simple_type="textarea" simple_size="4" />
The field description will be displayed as the translation of {t}Description{/t}. The type used here is "textarea" which is similar to the "textarea" component used in HTML. Thus we have a text which can be longer than one line. Here simple_size indicates that textarea has a height of 4 rows.
Now we have:
blank.xml:
<table modulename="{t}Blank{/t}" default_view="display" orderby="lastname" order="asc" limit="20">
<view name="display" displayname="{t}Display{/t}" />
<field name="id" simple_type="id" displayname="{t}Id{/t}" />
<field name="lastname" displayname="{t}Last name{/t}" simple_type="text" required="true" />
<field name="firstname" displayname="{t}First name{/t}" simple_type="text" required="true" />
<field name="description" displayname="{t}Description{/t}" simple_type="textarea" simple_size="4" />
</table>
To rename our module from blank to "My Addresses", use ...
myaddresses.xml:
<table modulename="{t}My Addresses{/t}" default_view="display" orderby="lastname" order="asc" limit="20">
<view name="display" displayname="{t}Display{/t}" />
<field name="id" simple_type="id" displayname="{t}Id{/t}" />
<field name="lastname" displayname="{t}Last name{/t}" simple_type="text" required="true" />
<field name="firstname" displayname="{t}First name{/t}" simple_type="text" required="true" />
<field name="description" displayname="{t}Description{/t}" simple_type="textarea" simple_size="4" />
</table>
... and rename the file from "src/modules/schema/blank.xml" to "src/modules/schema/myaddresses.xml" if you installed Simple Groupware with language "Developer". If you installed it with English or German, rename the file from "bin/modules/schema/blank.xml" to "bin/modules/schema/myaddresses.xml".
Now we only have a simple view that can display entries. To enable you to create new entries, add the following attribute to the table tag:
enable_new="true"
This automatically adds the "New" view with the rights attributes.
To allow you to edit and delete existing entries, add these attributes to the table tag:
enable_edit="true" enable_delete="true"
This automatically adds the "Edit" view and the "Delete" buttons.
With Simple Groupware you assign one module to every folder. Therefore you may want to be able to delete all entries in the folder. To do this we use the "empty" attribute:
enable_empty="true"
This automatically adds the "Empty" button.
myaddresses.xml:
<table modulename="{t}My Addresses{/t}" default_view="display" orderby="lastname" order="asc" limit="20" enable_new="true" enable_edit="true" enable_delete="true" enable_empty="true">
<view name="display" displayname="{t}Display{/t}" />
<field name="id" simple_type="id" displayname="{t}Id{/t}" />
<field name="lastname" displayname="{t}Last name{/t}" simple_type="text" required="true" />
<field name="firstname" displayname="{t}First name{/t}" simple_type="text" required="true" />
<field name="description" displayname="{t}Description{/t}" simple_type="textarea" simple_size="4" />
</table>
To deploy your new web application, make sure that the module is stored in "src/modules/schema/myaddresses.xml" (Developer language) or "bin/modules/schema/myaddresses.xml" (English, German, etc. language).
Inside Simple Groupware, take a look at the tree. Go to a folder where you want to place the new application, e.g. click "Workspace". Below the tree, click "Options", enter a new folder name to the "New folder" form, choose "myaddresses" in the list and click Ok. This is all: The new module is running now!
Note: The modules in the list contain the filenames without the extension (.xml), not the modulename-attribute. Therefore you see "myaddresses" in the list instead of "{t}My Addresses{/t}".
To improve "My Addresses" we add some more fields:
<field name="street" displayname="{t}Street{/t}" simple_type="text" />
<field name="zipcode" displayname="{t}Zipcode{/t}" db_size="6" simple_type="text">
<validate function="numeric" />
</field>
<field name="city" displayname="{t}City{/t}" simple_type="text" />
All three are of type "text", but the zip code should be validated by some constraints: The first validation is done with db_size=6 which means that the field may not be longer than 6 characters. Since zip codes are numeric (in Germany) we add the validator "numeric" (when creating or editing a new asset the function "validate_numeric" is called). If your zip codes are not numeric, leave this step out.
But the "Display" quickly gets overloaded with fields. Thus we create a new view called "Details".
<view name="details" displayname="{t}Details{/t}" showinsingleview="true" tfield_1="firstname" tfield_2="lastname" />
We decide that Id and description are less important than the other fields and should be displayed in the second view "Details". Also the fields "lastname" and "firstname" should be displayed as a caption in the first line of the details view (indicated with the attributes "tfield_1" and "tfield_2").
This means we add new limitations to the fields:
<notin views="display" />
Notin tells the program not to display the field in the view "display". Notin uses "views" as attribute which means that you can add several views here using views="view1|view2" separated with "|".
Summarizing our activities
myaddresses.xml:
<table modulename="{t}My Addresses{/t}" default_view="display" orderby="lastname" order="asc" limit="20" enable_new="true" enable_edit="true" enable_delete="true" enable_empty="true">
<view name="display" displayname="{t}Display{/t}" />
<view name="details" displayname="{t}Details{/t}" showinsingleview="true" tfield_1="firstname" tfield_2="lastname" />
<field name="id" simple_type="id" displayname="{t}Id{/t}">
<notin views="display" />
</field>
<field name="lastname" displayname="{t}Last name{/t}" simple_type="text" required="true" />
<field name="firstname" displayname="{t}First name{/t}" simple_type="text" required="true" />
<field name="description" displayname="{t}Description{/t}" simple_type="textarea" simple_size="4">
<notin views="display" />
</field>
<field name="street" displayname="{t}Street{/t}" simple_type="text" />
<field name="zipcode" displayname="{t}Zipcode{/t}" db_size="6" simple_type="text">
<validate function="numeric"/>
</field>
<field name="city" displayname="{t}City{/t}" simple_type="text" />
</table>
When saving changes to the xml file you only need to hit the "Refresh" button in your browser to make Simple Groupware apply your changes.
sgsML: Part 1 - sgsML: Part 2 - sgsML: Part 3 - sgsML: Part 4