Universal Search Filter Syntax
The Universal Search Filter (also mentioned as USF) is the technical syntax, provided to developers, to define a filter in Dolibarr ERP CRM application.
When to use the UFS
It is used by developers to:
- build rules to filter some combo list when the define an object dataset (into the property ->fields of an object)
- build condition for some properties of extrafields.
- to provide filters to some functions and methods into Dolibarr internal code.
- to provide filters to some Ajax components.
- to define filters on some requests on APIs.
- to propagate securely filters in some tools, for example in the customreport.php tools.
Why to use the UFS
The UFS syntax has been introduced for two main reason:
- To provide a secured way to propagate a filter from a component to another one, with no risk of injections.
- To provide a generic solution to define a filter that follows structured rules making possible to convert it easily and programmaticaly into the expected language, whatever is this language (most often in SQL but also in PHP or Javascript).
Syntax of UFS
A simple test in UFS syntax is forged this way: Open parenthesis, then the first operand that is the field, then :, then the operator, then :, then the second operand that is the value, then the closing parenthesis.
- The first operator allows only the following characters: a to z, 0 to 9, _, - and .
- The operator can be: =, <, >, <=, >=, !=, in, notin, like, notlike, isnull, isnotnull and nothing else.
- If the value is a string, you must add simple quotes. If value is a date, it must use the international format (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS) in the timezone of the server and be quotes too.
When using the in or notin operator, the character must be used to separate each possible value, the char can be used inside the value.
Then, you can combine any simple test using parenthesis and OR or AND to forge combined tests.
Example of simple tests:
(myfield:=:'mystringvalue')
(myfield:>=:123.45)
(t.mydatefield:>:'2024-01-01 10:00:00')
(myfield:like:'asubstring')
(myintfield:in:1,2,3,4,5)
(mystringfield:in:'1','2','3','4','5')
Example of combined tests:
((myfield:=:'mystringvalue') OR ((myfield:>=:myintorfloatvalue) AND (myfield:>:'2024-01-01 10:00:00')))