Module CustomFields Dev: Difference between revisions

Jump to navigation Jump to search
→‎Input forms for custom fields: Added AJAX-PHP callback
(→‎Modifying extra options: Changed extra from object to associative array)
(→‎Input forms for custom fields: Added AJAX-PHP callback)
Line 366: Line 366:


<source lang="php">
<source lang="php">
print $customfields->showInputField($field, $currentvalue=null, $moreparam='');
print $customfields->showInputField($field, $currentvalue=null, $moreparam='', $ajax_php_callback='');
</source>
</source>


showInputField() takes 3 parameters:
showInputField() takes 4 parameters:


* $field is a CustomFields structure object (eg: returned by fetchFieldStruct()) and is necessary to detect the right datatype and the possible values.
* $field is a CustomFields structure object (eg: returned by fetchFieldStruct()) and is necessary to detect the right datatype and the possible values.
* $currentvalue is the current value if you want to preselect a value (for example if the user already defined a value, you can reload it here). This is optional, in case it is not defined, either a null/empty value will be chosen, or either the default value defined when creating the custom field.
* $currentvalue is the current value if you want to preselect a value (for example if the user already defined a value, you can reload it here). This is optional, in case it is not defined, either a null/empty value will be chosen, or either the default value defined when creating the custom field.
* $moreparam is used to add more HTML attributes, if you want for example to add some css styling or whatever you want.
* $moreparam is used to add more HTML attributes, if you want for example to add some css styling or whatever you want.
* $ajax_php_callback is optional and allows to specify the relative url to the php callback script. If specified, an AJAX script will be automatically attached to this field (see AJAX-PHP callback below).


showInputField() only prints ONE HTML input field, thus you have to enclose it/them by your own HTML form.
showInputField() only prints ONE HTML input field, thus you have to enclose it/them by your own HTML form.
Line 405: Line 406:
print $customfields->showInputForm($id, $field, $currentvalue=null, $idvar='id', $page=null, $moreparam='');
print $customfields->showInputForm($id, $field, $currentvalue=null, $idvar='id', $page=null, $moreparam='');
</source>
</source>
=== AJAX-PHP callback ===
'''showInputFieldAjax()''' will output an AJAX script (using jQuery) to send and receive data to and from a specified php script. The AJAX script will automatically manage HTML form input's updating as long as the received data follows some convention.
An AJAX script can be attached to any field (including non-CustomFields) by using the method:
<source lang="php">
print $customfields->showInputFieldAjax($id, $php_callback_url, $on_func="change", $request_type="post");
</source>
Where:
* $id is the HTML id of the field you want to attach the AJAX to.
* $phpcallback the relative path from Dolibarr's htdocs root folder to the php script that will receive and send back data.
* $on_func is the Javascript or jQuery event that should trigger the AJAX script (by default on change).
* $request_type is the HTML query type that will be used to send the data to the PHP callback (either "get" or "post").
The AJAX script will automatically manage sending/receiving of data:
* automatically send all form's inputs values, as well as the current calling field's name and value. The sending happens on change by default, but another event can be set using the $on_event argument. Data will be sent using standard GET or POST.
* Upon reception of data from the php script, the AJAX script will automatically parse the data and update the HTML fields.
The PHP callback script must send back to AJAX the data in JSON encoded format, and with with a specific format:
* the data must be an associative array, where each entry's key is the name (not ID!) of an HTML field to update.
* each entry's value is an associative array, where each sub-entry's key is the action to do. Available actions: 'options', 'value', 'html', 'alert'.
* each sub-entry's value is the value for this action and HTML field.
Thus, this approach allows to generically support a wide range of updating actions, managed automatically.
You, the implementer, have just to edit the PHP callback script and return an associative array defining what action you want for each field you want to update. This is exactly what has been implemented in the Custom AJAX Functions library, which simplifies even more your job by managing automatically the step described here (generating AJAX and formatting data), so that you can focus on '''making the data'''.


== Printing custom fields in documents ==
== Printing custom fields in documents ==
439

edits

Navigation menu