Hi,
I am using a REST API to communicate with an external data source in about 20 different CF5 forms, with more to come.
It has got a bit tedious and repetitive to insert authentication and other regularly used CURL code in every form, as well as meaning that a change could easily mean I have to change every form, sometimes in several places.
So I would like to create global functions for these operations, including the possibility of global variables as well. These would not need to be website-wide, as long as they are available within the Chronoforms and Chronoconnectivity forms and connections.
I'm guessing that I can add some function code into a php file somewhere, or even a separate php file and an include statement. But I am not sure where or how to ensure that the functions have the necessary scope. I am not a skilled php programmer, though I am OK when working within forms. It is the wider scope that I do not yet understand.
Most of my work is in CF5 with a small amount of CC5. I am exploring CF6 and CC6 and will probably start to develop new forms and connections using the new versions.
Cheers
Tim
I am using a REST API to communicate with an external data source in about 20 different CF5 forms, with more to come.
It has got a bit tedious and repetitive to insert authentication and other regularly used CURL code in every form, as well as meaning that a change could easily mean I have to change every form, sometimes in several places.
So I would like to create global functions for these operations, including the possibility of global variables as well. These would not need to be website-wide, as long as they are available within the Chronoforms and Chronoconnectivity forms and connections.
I'm guessing that I can add some function code into a php file somewhere, or even a separate php file and an include statement. But I am not sure where or how to ensure that the functions have the necessary scope. I am not a skilled php programmer, though I am OK when working within forms. It is the wider scope that I do not yet understand.
Most of my work is in CF5 with a small amount of CC5. I am exploring CF6 and CC6 and will probably start to develop new forms and connections using the new versions.
Cheers
Tim
Thanks Bob,
That's very helpful, sorry I didn't see the FAQ was there.
A couple of supplementary questions - the FAQ has sample code:
but I'm guessing this should read (for a php file):
Also: I need to authenticate to the API in each event of a form, so does that mean I need the include statement at the top of each event, or is there a place I can include it in the form so that it has scope throughout the form?
I am sure these are naive questions - I am not experienced with the scope rules of php.
Cheers
Tim
That's very helpful, sorry I didn't see the FAQ was there.
A couple of supplementary questions - the FAQ has sample code:
<?phpinclude
(JPATH_SITE.'/components/com_chronoforms5/includes/'.$form->form['Form']['title'].'/load_js.js');
?>
but I'm guessing this should read (for a php file):
<?php
include (JPATH_SITE.'/components/com_chronoforms5/includes/'.$form->form['Form']['title'].'/mycode.php');
?>
Also: I need to authenticate to the API in each event of a form, so does that mean I need the include statement at the top of each event, or is there a place I can include it in the form so that it has scope throughout the form?
I am sure these are naive questions - I am not experienced with the scope rules of php.
Cheers
Tim
Hi Bob,
A bit more testing:
My authentication code works when included inline and placed in a Custom Code box in the Load event.:
So I put it into a file which I placed in the .../includes/ folder. The file has permissions 644 and the folder has permissions 755.
This gives no error if the function execution is commented out, but an error 500 if it is executed.
Probably I have some error in the include statement but I can't see it.
Cheers
Tim
A bit more testing:
My authentication code works when included inline and placed in a Custom Code box in the Load event.:
<?php
function authSF() {
$sfdata = array(
'grant_type' => 'password',
'response_type' => 'code',
'username' => 'myemail@mysite.com',
'password' => 'mypass',
'client_id' => 'myid',
'client_secret' => 'mysecret'
);
// $form->data['sfdata'] = $sfdata;
$sfdata_param = http_build_query ($sfdata, '&');
$url = "https://ap4.salesforce.com/services/oauth2/token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$sfdata_param);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sfauth1 = curl_exec($ch);
curl_close($ch);
// $form->data['sfauth1'] = $sfauth1;
$sf = json_decode($sfauth1, true);
// $form->data['sfaccess'] = $sf['access_token'];
//
// find latest API version
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ap4.salesforce.com/services/data/");
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sfvers1 = curl_exec($ch);
curl_close($ch);
$version = json_decode($sfvers1, true);
// $form->data['latestversion'] = end($version);
return ($sf['access_token']);
}
?>
So I put it into a file which I placed in the .../includes/ folder. The file has permissions 644 and the folder has permissions 755.
<?php
include (JPATH_SITE.'/components/com_chronoforms5/includes/func_authenticateAPIandgetversion.php');
$form->data['sfaccess'] = authSF();
?>
This gives no error if the function execution is commented out, but an error 500 if it is executed.
Probably I have some error in the include statement but I can't see it.
Cheers
Tim
Dear Bob,
Apologies!! Just after I posted, I saw an error in the path.
It works now. The earlier question remains though - do I have to run the include at the top of every event, or is there a way I can load it once for the entire form?
Tim
Apologies!! Just after I posted, I saw an error in the path.
It works now. The earlier question remains though - do I have to run the include at the top of every event, or is there a way I can load it once for the entire form?
Tim
Hi Tim,
In v6 there is a way to define this as a function in 1 form and call it using a shortcode from any other form, and you can also pass parameters (but that would need a patch), but this is in v6 only.
You will call something like this:
Then you can use $this->get("param") in your main code.
Best regards,
Max
In v6 there is a way to define this as a function in 1 form and call it using a shortcode from any other form, and you can also pass parameters (but that would need a patch), but this is in v6 only.
You will call something like this:
{chronoform.function:form_name/function_name$param=value}
Then you can use $this->get("param") in your main code.
Best regards,
Max
Hi Tim,
I fixed the missing line-break in the FAQ - sorry about that.
What do you actually need from this code? If it just the access token and that can be used in each event, then you could run the code once and save the token in the Joomla! User session from where you can read it in later form events.
Often though you only need the API access after the form data is submitted in which case you can run the code just in the On Submit event.
Bob
I fixed the missing line-break in the FAQ - sorry about that.
What do you actually need from this code? If it just the access token and that can be used in each event, then you could run the code once and save the token in the Joomla! User session from where you can read it in later form events.
Often though you only need the API access after the form data is submitted in which case you can run the code just in the On Submit event.
Bob
Hi Bob,
Not only the authentication.
I have created a series of functions to make it easier and less repetitious to use the API: createRecord, readRecord, queryTable, updateRecord and so on. I include these in the Load event to pre-populate forms, and in the Submit and other events to perform create and update operations.
So I just put the include at the top of each event and then the functions have scope throughout the event.
It isn't really a problem to do this, I was just asking if there was a way to have a form-wide or component-wide scope for these functions.
Cheers
Tim
Not only the authentication.
I have created a series of functions to make it easier and less repetitious to use the API: createRecord, readRecord, queryTable, updateRecord and so on. I include these in the Load event to pre-populate forms, and in the Submit and other events to perform create and update operations.
So I just put the include at the top of each event and then the functions have scope throughout the event.
It isn't really a problem to do this, I was just asking if there was a way to have a form-wide or component-wide scope for these functions.
Cheers
Tim
This topic is locked and no more replies can be posted.