• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Repeating groups in PHP form plug-in?

I am trying to work with the PHP form plug-in to use data from the form which may or may not have a repeating group in it to create a text file.

Here is what I currently have that works for a single order submission, but when a repeating group is added into the mix I want to break it out more instead of listing the array data.

Code:
$content = "###########################
# License File
# Order ID - {customer_orders___id}
###########################
LICENSE
customer={customer_orders___sub_account_name}
{customer_orders_10_repeat___product}  {customer_orders_10_repeat___max_version}
options={customer_orders_10_repeat___options}
issued={customer_orders_10_repeat___license_start}
{customer_orders_10_repeat___license_expiration}
";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/licenses/{customer_orders___id}_{customer_orders___ma_account}_{customer_orders___sub_account_name}.lic","wb");
fwrite($fp,$content);
fclose($fp);

I know in working with e-mail HTML templates you have to do the following:

Code:
$repeatcount = $this->data['fabrik_repeat_group'][#];
    for($i=0; $i<$repeatcount; $i++ )

But how can I incorporate that into the PHP code part?

Any help or examples would be great as I have yet to find much on this in the forums.
 
Rather than use placeholders, which are really just intended for very simple usage, use $formModel->formData[], which contains all the submitted data, keyed by element name. Like $formModel->formData['customer_orders___sub_account_name']. Elements in repeated groups are arrays, so $formModel->formData['customer_orders_10_repeat___options'][1] would be the second repeated instance for that element.

SAnd use $formModel->formData['fabrik_repeat_group'] to get the repeat counts.

-- hugh
 
Hugh,

Sorry I'm sure this is simple and all but something I am doing isn't working.

Code:
$content = "###########################
# License File
# Order ID - {customer_orders___id}
###########################
 
Changed to
 
$content = "###########################
# License File
# Order ID - $formModel->formData['customer_orders___id']
###########################

Gives me

# Order ID - Array['customer_orders___id']
 
I ended up adding this before $content

$order_id = $formModel->formData['customer_orders___id'];

And that will show up now in the below example

Code:
$content = "###########################
# Delcorss License File
# Order ID - $order_id
###########################
";

But if I also add

$customer = $formModel->formData['customer_orders___sub_account_name'];

Code:
$content = "###########################
# License File
# Order ID - $order_id
###########################
LICENSE
$customer
";

$customer does not get passed into the file I create.
 
Also what would be a good way of debugging this as I am not a programmer and it would be help to see what data was there for these element names. Since I am not making a template file or anything I'm not sure how to see this type of data like what was shown in the developer video by Rob.
 
You can't have PHP code inside your quotes. Placeholders work inside quotes because those aren't being handled by PHP - we replace the placeholders in the text of your code, before feeding that code to PHP to run.

Only way to debug it is to put this:

Code:
var_dump($formModel->formData);exit;

... as the first line in your script. That will dump the formData array to your browser. Copy and paste that in to a notepad file.

Then you can see what data is in where. You may find that some things are arrays rather than strings - like dropdowns, or join elements. In which case you need to append the array key, like ...

Code:
$mydropdown = $formModel->formData['table___mydropdown'][0];

... or whatever.

-- hugh
 
Hugh,

Thanks so far. I have a question though as this seems weird to me.

Setting this variable works - $order_id = $formModel->formData['customer_orders___id']; and shows the correct Order ID as expected.

Setting this variable does not work - $order_date = $formModel->formData['customer_orders___date_time']; Nothing is printed out from this.

If I change the variable to this - $order_date = $formModel->formData['date_time']; It works.

Now if I try to does this either way with one of the repeating group elements I just get the "Array" output.

Example - $max_version = $formModel->formData['max_version'][0]; or $max_version = $formModel->formData['customer_orders_10_repeat___max_version'][0];


I was able to pass these working variables as shown below -

Code:
$content = "###########################
# License File
# Order ID - ".$order_id."
# Order Date - ".$order_date."
###########################
 
Also is the below the correct way to run through the arrays?

Code:
$repeatcount = $formModel->formData['fabrik_repeat_group'][10];
    for($i=0; $i<$repeatcount; $i++ ){
        $product = $formModel->formData['product_raw'][0];
        $max_version = $formModel->formData['max_version'][0];
}

Do I need to use the _raw versions, does it matter which one I use?

I still just get Array for these two variables so I assume I have something wrong.

Thanks.
 
I wonder also why that only the following is shown at the bottom from the output of the var_dump

Code:
'customer_orders___id' => string '4' (length=1)
  'customer_orders___id_raw' => string '4' (length=1)

None of the other elements are listed in this manner and I think that is why the order_id works as I thought it should. Is there something that is not toggled for the other elements that I overlooked?

Thanks
 
So what did the var_dump() show you for customer_orders___date_time?

BTW, we're definitely pushing the envelope for the kind of support we can provide for custom scripting on a Standard subscription.

-- hugh
 
Oh, just saw your last msg.

What hook are you running your plugin on? Is it onBeforeProcess, onAfterProcess, etc?

-- hugh
 
onAfterProcess

If I need to bump it up let me know and I can convert it for the next month?

I don't need to have you write the scripting just trying to make my way through it with trail and error and some guidance/resources is all ;).

I appreciate the assistance thus far and think I am close to getting what I need from this if I can just figure out the Array issue.
 
If I change to onBeforeProcess then the

Code:
'customer_orders___id' => string '4' (length=1)
  'customer_orders___id_raw' => string '4' (length=1)

is no longer there.
 
Well, you probably want to run onAfterProcess, as that way you know the form has passed validation, and the form's row has been written out to the database. The main issue difference is that in onAfterProcess, we've stripped the tablename___ prefix off the data in $formModel->getFormData[]. So you'd look for 'element_name' instead of 'tablename___element_name'.

In yu question about repeat group arrays and the for9) loop ... you'd use [$i] as the index, not [0]. Otherwise you are only ever getting the first item in the array (which are indexed from 0). And you'd have to actually do something with those variables in the loop, like echo them, rather than just loop round setting them.

Code:
$repeatcount = $formModel->formData['fabrik_repeat_group'][10];
for($i=0; $i<$repeatcount; $i++ ){
        $product = $formModel->formData['product_raw'][$i];
        $max_version = $formModel->formData['max_version'][$i];
        echo "
Product: $product
Max Version: $max_version
";
}

... or append that to your $content string..

Code:
    $content .= "
blah blah
";

Also, rather than accessing formData[] directly and having to worry about whether to use tablename___ or not, you can call getElementData() instead, which works that out for you ...

Code:
$foo = $formModel->getElementData('tablename___foo');

Whether you use _raw or not kind of depends on what you are doing with the data. The difference is typically with things like joins, dropdowna, etc which have a concept of "value" and "label". the _raw will be the value.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top