Help Tips « back
Is it possible to use variables in the forms/reports XML file?


Both forms and reports XML specification accepts variables, inside the CDATA sections of some elements or inside some special tag attributes. There is a subset of attributes and elements that have this feature enabled...

When you create a form using the forms PHP2Go API, all the fields are evaluated to perform automatic population of the field values. However, the framework respects an order to search for the field value (you can see this process in action in FormField.class.php, in the _parseField method):

1) check if the "VALUE" attribute is defined. This attribute may be used to statically define the value of the field. Here, variables are not accepted;
2) check for the field name in the global scope, respecting the order "EPROSGC";
3) check if the "DEFAULT" attribute is defined. This attribute can be used to define a "DEFAULT" value if the field value can't be retrieved from the request. Besides, you can use the pattern ~foo_bar~ to tell the framework that the value must be evaluated as a variable. The pattern accepts simple string variables (~logged_user~) or array entries (~SERVER['REMOTE_ADDR']~, ~arr[1]~). Once more, the value of this variable is searched in the global scope, using the same variable order: "EPROSGC".

So, wtf is "EPROSGC"? This string is a "changed" version of the "variables_order" entry that can be found in php.ini, and also can be set by the developer. From left to right: E is environment, P is post, S is session, G is get and C is cookie. However, the other 2 entries are not in PHP official specification: R and O.

» *R* represents the Registry, a global data registry that is used by PHP2Go to store values that can be retrieved by any component of the framework and by any component of the user's application. As shown in the framework example scripts, you can call Registry::get to get a variable and Registry::set to define a new entry or replace an existing one. To use the registry in forms and reports, here's an example:

// in your PHP code, before creating the Form or Report instance
Registry::set('USER_TYPE', 1);
// in the form XML file
<hiddenfield name="USER_TYPE"/>
// or in the report XML file
<clause>USER.USER_TYPE = ~USER_TYPE~</clause>    

» *O* represents the instances of SessionObject that are persisted in PHP session scope. To get a value from these objects, you can write something like this:

// in your PHP code, you must instantiate a SessionObject an register it
$sess =& new SessionObject('MY_SESSION');
$sess->createProperty('USER_ID', 1);
$sess->register();
// in the form XML file
<hiddenfield name="USER_ID" default="~MY_SESSION:USER_ID~"/>
// or in the report XML file
<clause>where PROJECT.OWNER_ID = ~MY_SESSION:USER_ID~</clause>    
    

The list of attributes and elements of the forms and reports XML specification are:

» DEFAULT in all field elements;
» DISPLAYFIELD, LOOKUPTABLE, CLAUSE, GROUPBY and ORDERBY inside any DATASOURCE element of the forms;
» PROCEDURE, FIELDS, TABLES, CLAUSE, GROUPBY and ORDERBY, inside the report DATASOURCE element.

For more details and explanations about form population and about the use of variables in the XML files, please consult the examples that use the form API (formbasic.example.php, formtemplate.example.php), included in the framework example scripts.