Create an ODT or ODS document template: Difference between revisions

Jump to navigation Jump to search
Adding ODT substitution and lines custom substitution function
(Adding ODT substitution and lines custom substitution function)
Line 91: Line 91:
This an example of what you may have in your office suite:
This an example of what you may have in your office suite:
[[File:Line for document generation.png|500px]]
[[File:Line for document generation.png|500px]]
=== Conditional substitution ===
Starting from Dolibarr v3.3, you can use conditional substitutions, which in layman terms means that you can decide to print something if a variable is true, or print something else if it's false (or nothing at all) - including text but also any more complex structures like tables and images.
Example:
<pre>
[!-- IF {my_var} --]
Print this text if {my_var} is true (can be any value but null/0/empty string)
[!-- ELSE {my_var} --]
Or print this if it's false (null/0/empty string)
[!-- ENDIF {my_var} --]
</pre>


=== Other personalized tags ===
=== Other personalized tags ===
If you want to add a field that is not predefined, this is the solution:
If you want to add a field that is not predefined, this is the solution:
* Add a file into '''htdocs/core/substitutions/functions_mymodule.lib.php''' (htdocs/includes/modules/substitutions/functions_mymodule.lib.php with version 3.1 or previous)
* Create a file into one of those locations:
'''htdocs/core/substitutions/functions_mymodule.lib.php''' with v3.2 or above
'''htdocs/mymodule/core/substitutions/functions_mymodule.lib.php''' with v3.2 or above
'''htdocs/includes/modules/substitutions/functions_mymodule.lib.php''' with version 3.1 or previous
* Inside this file, just write one function like this:
* Inside this file, just write one function like this:
<source lang="php">
<source lang="php">
Line 125: Line 141:
</source>
</source>
and module has been activated.
and module has been activated.
Note2: this only allows to define global, static ODT variables (ie: to make dynamic variables that changes for every line of product, please see the next chapter).
=== Other personalized tags for lines ===
This function is available since Dolibarr v3.3 (future development version).
Similarly to the previous chapter, you have to define a function, and it works about the same, but with a few twists:
* Create or edit the same file as before (see the previous chapter).
* Here are the twists: in the file, write another function with almost the same name but with '_lines' appended, and also add a new $lines variable:
<source lang="php">
/** Function called to complete substitution array for lines (before generating on ODT, or a personalized email)
* functions xxx_completesubstitutionarray_lines are called by make_substitutions() if file
* is inside directory htdocs/core/substitutions
*
* @param array $substitutionarray Array with substitution key=>val
* @param Translate $langs Output langs
* @param Object $object Object to use to get values
*              @param  Object          $line                  Current line being processed, use this object to get values
* @return void The entry parameter $substitutionarray is modified
*/
function mymodule_completesubstitutionarray_lines(&$substitutionarray,$langs,$object,$line) {
{
  global $conf,$db;
  $myvalue='Put here calculated value to insert';
  $substitutionarray['myowntag']=$myvalue;
}
</source>
Contrary to the previous function which was called only once, this function will be called everytime a line of product will be processed, giving you each time a new $line object that you can process.


== Store your document ==
== Store your document ==
To have your document to appear in list of available documents, put it into correct subdirectory found into
To have your document to appear in list of available documents, put it into correct subdirectory found into
directory '''documents/doctemplates'''
directory '''documents/doctemplates'''
439

edits

Navigation menu