Calculation element

Aug 14, 2017
Calculation element
  • upload_2016-2-27_20-58-4.png

    Options:
    • Hidden - Is the field hidden
    • Width - The width of the field
    • Height - The height of the field
    More:
    • Calculation - The calculation PHP expression that returns the value.
    • It can use placeholders.

    Simple examples(top)

    Calculate:
    PHP:

    $myCalc = (int)'{table_name___elementname}' * 5;
    return $myCalc;
    Concatenate two fields:
    PHP:
    return '{table_name___element1}' . '{table_name___element2}';

    Accessing data(top)

    For elements such as radiobuttons, checkboxes and dropdowns you can use {table_name___elementname_raw} to access the value as opposed to the label.

    E.g.
    PHP:
    return (int)'{table_name___elementname_raw}' * 5;
    All posted data is stored in the array $data, this is useful if you wish to retrieve repeat group data. To get a debug output of this data you can use this php:
    PHP:

    echo '<pre>';
    print_r($data);
    echo '</pre>';
    exit;
     
    • Format string - Applies PHP's sprintf function to the data, using this fields value as the format field.
    • Only Calc on Save
      • If set to yes then the calculation is performed only when the form is saved (or if using Ajax calculations when the form's values change)
    • If set to no then the calculation is additionally applied to the data when viewing the table. This option produces an additional overhead when rendering your table's data, but is useful if you add a calculation to an existing large data set.
    • Ajax calculation-
      • If set to no then the calculation is run when the form is saved
      • If set to yes then the calculation is run whenever any element whose placeholder is referenced in the 'Calculation' field is updated. The calculation is run via an Ajax call and the element's value is updated with the new calculated value
    • Observe Fields - In addition to the element placeholders listed in the 'Calculation' field, you can supply a comma separated list of elements placeholders that you want to observe. Changing these element's values will trigger the Ajax calculation

    When are calculations run?(top)

    • The calculation is always run on form submission (in fact, it may run several times).
    • If "Calc on save only" is set to No, the calc is run every time the element is rendered, ie. in list and details views, in addition to being calculated on submission.
    • If Ajax calculation is set to Yes then the calculation runs each time an observed field's value changes.

    Retrieving data from complex elements(top)

    Json decode helper

    Using Fabrik 3.2, let's say you want to retrieve the value of DBjoin element which is in a joined repeated group (n-m relationship). As you may have multiple values, the result is a Json encoded string.

    The following syntax will return an array of values with the correct encoding structure (accent characters) whatever the number of repeated joined records you have :
    Code (Text):
    $foo = FabrikWorker::JSONtoData($data['tablename___elementname'], true);
    In order to return a string with the list of the joined values just do :
    Code (Text):
    return implode(',', $foo);

    IMPORTANT NOTES(top)


    • You should almost always put quotes around placeholders in your code, to avoid syntax errors if the element has an empty value. Remember that placeholders are replaced by the values by Fabrik before handing your code to PHP to evaluate. So if your code is "if ({yourtable___foo} > 1) {", and 'foo' is empty, the code handed to PHP is "if (> 1) {", which is a syntax error. Also, where possible, "cast" your quoted string to the appropriate type, like "if ((int)'{yourtable___foo}' > 1) {". If foo is empty in that situation, the resulting code will be "if ((int)'' > 1) {", which is syntactically correct, as (int)'' is 0.
    • The calc element should only be used to return a value. It should not be used to "do things", like update database tables. If you need to do "stuff" other than just return a value for the element, you should use a PHP form submission plugin.
    • You cannot use the values of other calc elements in your code. This creates a chicken and egg situation, as there is no guarantee that another calc element's code has been run yet. The order of execution of calc elements is "arbitrary" - we make no guarantees about what order they run in.
    • If your calc is in a repeat group, for other elements in the same group you will only be able to access data for the same repeat instance. So for a calc my_repeat___calc, if you use {my_repeat___foo}, each repeat instance of the calc will see the value of 'foo' for it's own repeat instance.
  • Loading...
Bauer, ae2c2, tagger and 1 other person like this.