Module Definition

Build 2023-11-30 11:00

Module Definition

How to build a module definition for your application.

Module Definition

A Module Definition describes business objects (and sometimes complex business processes) for your application.

Setting "driver"

string, default module_name

Which driver implements the main logic. If no one is given, then the module name is tried as the driver.

Regularly this will be “objects” or “files”.

Setting "collection"

string, default module_name

Which collection in the key value store is used. If no one is given, then the module name is tried as the collection.

Setting "needs_person"

string, default true

Normally all objects in the system do have a connection to the originally creating user. If this is not wanted or not needed simply set this to false.

Section "parent"

object, optional

Sets the parent object for a new created object in this module.

refstring The collection the parent is stored in.
ref_idstring If given, the ID of the parent.
ref_lookupstring If given, the field name whose value is used to lookup the parent ID.
selfboolfalseIf set it becomes a direct connection between current object and parent.
The available parent settings
{
		"parent": {
			"ref": "devices",
			"ref_lookup": "serial_no",
			"self": true
		}
}

Section "default_object"

object, optional

Sets the default data for a new created object in this module.

Within the “default_object” setting all fields could be set to static values. Additionally using the prefix “?” field values out of a current form could be set into the “default_object”.

{
		"default_object": {
			"data": {
				"field1": "This is Field 1",
				"field2": "?field2"
			}
		}
}

Setting "default_mode"

string, enum ("front", “cards”, “list”), default “cards”

The default mode to view the index of this module in. This not only changes the called view template but also the way the retrieved data is given to the appropriate view.

Section "default_access"

object[], optional

Sets the default access for a new created object in this module.

^user_idstring Gets replaced with the current user's ID.
^groupstring Gets replaced with the current user's primary group.
The available special tags
{
		"default_access": [
			{
				"scope" : [
					"*"
				],
				"allow" : [
					{
						"user":"^user_id"
					}
				]
			}
		]
}

See concept “Authorization”.

Section "default_sharing"

string[], optional

Sets the default sharing for a new created object in this module.

^string Gets replaced with the current “Site Tag”.
The available special tags
{
		"default_sharing": ["site1", "^"]
}

See concept “Sharing”.

Setting "details_action"

string, optional

Normally clicking an object in the index of this module would navigate to details action. With this setting you could change that behavior e.g. having a click in the index directly go into edit action.

Setting "disable_add"

string, default false

Normally the system offers the add action where applicable (and permitted). With this setting this behavior could be disabled.

Setting "disable_edit"

string, default false

Normally the system offers the edit action where applicable (and permitted). With this setting this behavior could be disabled.

Setting "disable_map"

string, default false

Normally the system offers the map action where applicable (and permitted). With this setting this behavior could be disabled.

Setting "disable_tools"

string, default false

Normally the system offers the defined tools where applicable (and permitted). With this setting this behavior could be disabled.

Section "links"

map(string => object), optional

Link Definitions describe which object( collection)s this object( collection) is linked to and how. First the current request's "Action" and "Mode" are used to find the correct Link Definition.

“Action”_"Mode"index-cardsFor current “Action” and “Mode”
“Action”indexFor current “Action”
__The default
Finding the right Link Definition

Additionally the "args" section could be used to adapt the found Link Definition.

A Link Definition is an array of strings each describing a link to a different object( collection) and has the following syntax:

[~] ~The Link is a direct Link
[~] ~The Link should return the data directly
[<]"Collection"[."Link Field"] organizationsThe collection to link to. If given use the “Link Field” instead of current class name.
[;+"Select Field"]… ;+phoneAdd the “Select Field” to the returned data. If used all needed fields must be given.
[;-"Unselect Field"]… ;-descriptionDo not add the “Unselect Field” to the returned data.
[;"Field""Operator""Value"]… ;country=CAThe Link should only return data with “Field” compared using “Operator” to “Value” evaluates to true.
[^[+-]"Sort Field"] +cityThe Link should sort the returned data using “Sort Field”.
[:"Limit"] :10The Link should only return the first “Limit” records.
The syntax of a Link Definition Item
{
	"links": {
		"_": [
			"projects",
			"~~organizations;-description;country=CA^city:10"
		]
	}
}

Section "lookup"

string[], optional

A Lookup is used for so called translation tables (e.g. list boxes in forms) where a list of human readable strings in relation to their appropriate ID are given to the user for selection.

The collections “categories” and “entities” are formed as a tree while all others are linear lists.

["Field"<] ~The field containing the ID of the lookup
[=] ~Set if it is a direct link
"Collection"[."Link Field"] organizationsThe collection to lookup from. If given use the “Link Field” instead of “title”.
The syntax of a Link Definition Item
{
	"lookup": [
		"projects",
		"customer<organizations.title"
	]
}

Section "args"

map(string => object), optional

The arguments are used to inject query string variables or form fields into Links and Queries. First the current request's "Action" and "Mode" are used to find the correct Arguments Definition.

“Action”_"Mode"index-cardsFor current “Action” and “Mode”
“Action”indexFor current “Action”
__The default
Finding the right Arguments Definition

A Arguments Definition is an array of strings each describing an argument and has the following syntax:

[.] .If set the field is a system field
[(timestamprange)] (timestamprange)The argument contains 2 values seperated by ‘..’ evaluating to a timestamp range
[(timestamp)] (timestamp)The argument is evaluating to a timestamp
“Field Name” serial_noThe field name
[!] !Normally arguments will only taken into account if an appropriate field exists within the current request. Setting this means that even empty values should be included.
The syntax of a Link Definition Item

Of course all resulting relations between query string or form field relations and Links and Queries are always ment to be “equal” operator based.

{
	"args": [
		"index": [
			"serial_no!",
			".(timestamp)created_at"
		]
	]
}

Section "filter"

object, optional

The Filter Definition object defines special filter and sorting operations to be performed on every data query. This filter is applied directly onto the appropriate query, so working with the data fields require the prefix “data.”.

“Filter Field”any“data.city”: “Vancouver”Return only data where “Filter Field” is set to the given value.
+string[]“+”: ["data.city", “data.country”]Add the fields to the returned data. If used all needed fields must be given.
-string[]“-”: ["data.description"]Do not add the fields to the returned data.
^"Sort Field"integer (-1, 1)“^data.city”: 1The Link should sort the returned data using “Sort Field”.
:integer“:”: 10The Link should only return the first “Limit” records.
The syntax variants of a Filter Definition Item

Using the special operator “||” as a prefix for a field name makes that condition into the “OR” path of that whole Filter Definition. Additionally the following special rules apply.

user_idstring“user_id”: “^”If “Filter Field” is set to “user_id” and value is set to “^” it is replaced with the user ID of the current authenticated user.
“Filter Field”array“data.city”: {"like": “vancouver”}If the value of the filter is an object, then the key of an element is taken to be an operator while the value becomes the filter value for that operator.
The available special rules

Remember, the Filter Definition is applied to all data queries within the appropriate Module Definition.

{
	"filter": [
		"^created": 1,
		"^user_id": "^",
		"data.city": "Vancouver",
		"||data.city": {"like": "vancouver"},
		"-": "data.description",
		":": 10
	]
}

Section "actions"

map(string => object), optional

Actions are used to implement additional methods within modules.

methodstring 

The method the custom action is based on.

This must be one of the following: 'index', 'details', 'add', 'edit', 'connect', 'process'.

titlestring, optional The title of the action. If none is given, the name is used.
queryobject, optional A fully formed Mongo DB query which then will be used instead of the built query out of Links, Parent, Filter.
hintstring, optional If Mongo DB uses the wrong index on the query, here the right index to use could be given.
dataobject[], optional 

An array of additional data sources to provide the action with additional data.

See the following table.

argsobject, optional 

An object containing field names and values to for injection into the query and data queries.

Within the appropriate Mongo DB query you could use “$args.” followed by the argument name to have that replaced with a query string in the request.

alternative_navigationbool, default false Switch to alternate navigation for this action if the template offers one.
alternative_user_navigationbool, default false Switch to alternate navigation for this action if the template offers one and there is an authenticated user.
The Action Definition

The following table shows the properties of an item of the data array in an Action Definition.

collectionstring The collection to use for that data entry. If none is given, then the collection of the module is taken.
aliasstring, optional Normally the results are stored in the result sets using the name of the collection. Using alias a different name could be given.
queryobject, optional 

A fully formed Mongo DB query which then will be used instead of the built query out of Links, Parent, Filter.

If no query is given, then a lookup table is generated based on the collection.

hintstring, optional If Mongo DB uses the wrong index on the query, here the right index to use could be given.
selectstring, optional If in lookup table mode, the field to build the human readable column of the lookup table is speciefied here. If none is given the field title is used.
is_refbool, default false If in lookup table mode, this field sets if the lookup table searches only for entries where the appropriate class is equal to the collection.
The Action Definition Data Item

Remember that you must pay attention to the used template if injecting additional data having the template use the right names.

{
	"actions": [
		"locations": {
			"method": "index",
			"title": "Locations"
		}
	]
}

See "Action Definition".

Section "tools"

map(string => object), optional

The tools are used for the context based toolbar in a view of an action. First the current request's "Action" and "Mode" are used to find the correct tools.

“Action”_"Mode"index-cardsFor current “Action” and “Mode”
“Action”indexFor current “Action”
__The default
Finding the right Arguments Definition

...

titlestring, optional The title of the tool. If  none is given, then a string is built from “Tool” and the index of the tool.
iconstring, optional The Awesome Font icon string to use for this tool.
linkstring, optional An URL to navigate to if the tool is clicked. If given, the following 4 are not taken into account.
modulestring, optional The module to build a link for. If none is given, the current is used.
actionstring, optional The action to build a link for. If none is given, the current is used.
idstring, optional The ID to build a link for. If none is given, then current is used.
paramsstring[], optional Additional parameters to build a link for.
menuobject[], optional 

If this is given all link based properties for this item are ignored.

An array of tool items to use to build a sub menu within that tool.

A tool item
{
	"tools": {
		"index": [
			"orders": {
				"title": "Orders",
				"module": "orders",
				"action": "index"
			}
		}
	]
}

Section "hooks"

map(string => string), optional

Hooks could be used to implement additional functionality into the system e.g. to do things in other backends or services. Hooks are implemented with PHP. First the current request's "Action" and "Mode" are used to find the correct hook.

“Action”_"Mode"index-cardsFor current “Action” and “Mode”
“Action”indexFor current “Action”
__The default
Finding the right hook

A found hook always is given is a filename only which then is loaded out of the main config path with added file extension “.php”.

Each hook is run within a special closed function having the following PHP variables available:

(… args …)  Depending on the method called there are various arguments given.
$context(assoc_)array Representing the context of the current request.
$__BASEobject Providing an interface to the key value store.
$__DEBUGobject Providing an interface to the current DEBUG object.
Available PHP variables

...

{
	"hooks": [
		"index": "module_index_hook"
	]
}

...

// module_index_hook.php
// 
//	$context = [
//		'def' => $def,			// The current Module Definition
//		'driver' => $driver,	// The current driver
//		'module' => $module,	// The current Module Name
//		'method' => $method,	// The current Method Name
//		'params' => $params		// The current parameters
//	];
// 
	foreach ($page_items as $temp_i => $page_item) {
		$page_items[$temp_i]['added_column'] = 'This is from a hook.';
	}
//

Section "views"

map(string => object), optional

The views are used to select the appropriate sub template for the current request's "Action" and “Mode”.

“Action”_"Mode"index-cardsFor current “Action” and “Mode”
“Action”indexFor current “Action”
__The default
Finding the right Arguments Definition

Remember that the display of the views depends on the site template used. This system only connects queried or processed data with the appropriate template files.

“View Name”  The view to load within a standard container.
["View Name", “Container Name”]  The view to load within the given container.
["", { … }]  An object describing view and container for template which supports generic standard templates (e.g. for tables).
   If nothing is given then a standard view is selected within a standard container.
The syntax variants for view selection
{
	"views": {
		"_": "general_index",
		"details": "my_module_details"
	}
}

Section "forms"

map(string => object), optional

The arguments are used to inject query string variables or form fields into Links and Queries. First the current request's "Action" and "Mode" are used to find the correct Arguments Definition.

“Action”indexFor current “Action”
__The default
Finding the right Arguments Definition

...

["Field"<] ~The field containing the ID of the lookup
[=] ~Set if it is a direct link
"Collection"[."Link Field"] organizationsThe collection to lookup from. If given use the “Link Field” instead of “title”.
The syntax of a Link Definition Item
{
	"lookup": [
		"projects",
		"customer<organizations.title"
	]
}

Setting "pagination"

integer, optional

The amount of items per page if pagination should be used. No pagination will be used if this value is not given.

Setting "hide_action"

bool, default false

Setting this to true results in not having the current action shown in the bread crumbs.

Setting "rest_mode"

string, enum ("default", “virtual”, “post_process”, “non”), default “default”

With rest_mode the automatic provides REST API for the current module could be changed in its behavior.

default  The default REST behaviour of the system
virtual  Path segments are used as method name so incoming requests are mapped to actions.
post_process  Normally the HTTP verb “POST” is translated to system's action “add”. With this mode the used system's action is “process”.
none  REST for this module is disabled.
The syntax of a Link Definition Item

Setting "alternative_navigation"

string, optional

Switch to alternate navigation for this action if the template offers one.

Setting "alternative_user_navigation"

string, optional

Switch to alternate navigation for this action if the template offers one and there is an authenticated user.