Ability to 'include' or 'require' code within a calc?

chris.paschen

Chris Paschen
I know I'm probably pushing the limits with fabrik with this - but I thought I'd ask.

I have a form with several similar items that all have the same calc element calculation in them - a rather lengthy bit of code that is a bit of a pain to maintain - because it's in several different calc elements - requiring me to update each of those elements every time I need to make a change to the calculation during development.

I had this brainstorm idea that it would be easier if I could just 'include' (or 'require') an external library file that had a function in it that the calc could call - that would allow me to just maintain the logic in one library file, rather than in several different calc elements. In addition, it would allow me to create a 'calc library' of various functions that I might use across different forms and even sites.

HOWEVER, when I try to use the 'include' or 'require' line like this:
PHP:
require JPATH_LIBRARIES.'/pc/fabrik/functions.php';
at the head of the calc area, the resulting form (on the admin-side at least) just gives a 'white screen'.

I'm guessing that the calculation code is not just plain PHP, but is in somehow parsed and is not able to handle include/require. Or that there's some sort of processing order problem in the way the calc is implemented.

Has anyone else very tried something like this?

If it worked it would open up a huge possibility of extending the functionality of fabrik elements (esp. calc elements) without needing to modify the core of fabrik.
 
OK ... for all of you 'experienced' fabrik users out there (that are sitting there saying ... why doesn't he just use the form php plugin! :-o) ... sorry for missing this.

For everyone else ...
Did you know that you can already do what I asked?
Just use the Form Plugin PHP, then process all your stuff there.

I'm still trying to get the hang of exactly WHEN to process the script (and some functions need to be processed at different times).

But this should resolve the issue and allow LOTS of cool custom processing.

I would also think that it's probably at least a LITTLE more efficient to process things at the form level, rather than on each individual element. I know it will make managing the process MUCH easier.

Now off to create cool functionality on my forms!!!!
 
If you are going to include a file more than once, and it has function definitions in it, you'll need to use require_once, rather than require:

http://php.net/require_once

... otherwise PHP will blow up with an error about duplicate function names, as you are trying to re-define the same function.

NOTE also that the calc element wasn't really designed to do hugely (or even relatively) complex stuff in, and (in some cases) it may well run more than once during any given phase of processing (during form or list load, validation, submission, etc), when you might not expect it to. Like it may run during (well, just prior to) validation, then again during form processing to db, then again if you send an email. It's really design more for simple, lightweight, one or two line type code snippets, like concat'ing a first and last name in to a fullname, or summing a couple of other element values, whatever.

That said, you can of course use static variables to keep track of state so you only do your "thing" once, but on the whole ... if you need to do heavy weight coding, especially if you need to know for sure at what point during processing your code is running, it's definitely better to do it in form plugins.

Also note that the PHP form plugin is specifically designed to let you require files. I think the tooltip explains it, but you can use both the "File" and "Eval'ed" code choices, such that you use the "File" to incude your function library, then kick off a function or two in the eval'ed box, typically passing $formModel in to your func so it can get at "stuff".

-- hugh
 
Thanks Hugh for the clarification ... I wish I had learned that BEFORE I created all sorts of complex calc fields :(
But it was a good learning process.

Just one point of clarification ... you mentioned that you can use 'both' the file and evaled code; however the WIKI says:
http://fabrikar.com/forums/index.php?wiki/php-form-plugin/#accessing-form-data

PHP code to execute on from submission - If no script is selected then the code entered here will be run instead.

Does that just mean that the wiki needs to be updated?

If so, that's a great new feature and will make it MUCH easier to understand (and code).
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top