Hi,
I am fairly new to Joomla and PHP. I do have some PHP code that is repeated in a number of forms. I was just wondering if there is an easy way to create a PHP Class and have it available to instantiate and call across multiple forms.
One application I am considering is a utility object would have a method than renders HTML select options from a database. Method arguments would include the table name and the fields within the table represent select name and value. This methods would also have an argument containing a field value(s) which indicates item(s) to mark as selected.
Any suggestions would be appreciated.
Tim
I am fairly new to Joomla and PHP. I do have some PHP code that is repeated in a number of forms. I was just wondering if there is an easy way to create a PHP Class and have it available to instantiate and call across multiple forms.
One application I am considering is a utility object would have a method than renders HTML select options from a database. Method arguments would include the table name and the fields within the table represent select name and value. This methods would also have an argument containing a field value(s) which indicates item(s) to mark as selected.
Any suggestions would be appreciated.
Tim
Hi Tim,
Two answers here.
1) Yes it's entirely possible to create a class. You can - for example - set up a file in a com_chronoforms/includes/ folder and require or include that into any form. I usually do this with any form code that is more than trivial as I dislike working in the tiny textareas.
See as an example, the administrator/components/com_chronocontact/helpers/plugin.php file. This is mainly a class used to create form elements for use in the ChronoForms plugins configuration tabs.
In the plugins themselves you end up with slightly cryptic code blocks like
2) ChronoForms has a Wizard Custom Elements link in the Forms Manager which will allow you to create custom elements for use in the Form Wizard. We don't often mention this but there is the capability to create specialist elements if you are going to need them frequently. I don't know if you can include MySQL in here though - I suspect not.
Bob
2)
Two answers here.
1) Yes it's entirely possible to create a class. You can - for example - set up a file in a com_chronoforms/includes/ folder and require or include that into any form. I usually do this with any form code that is more than trivial as I dislike working in the tiny textareas.
See as an example, the administrator/components/com_chronocontact/helpers/plugin.php file. This is mainly a class used to create form elements for use in the ChronoForms plugins configuration tabs.
In the plugins themselves you end up with slightly cryptic code blocks like
$tooltip = "Enter a message to show after redirection. if you leave this blank, redirection will be silent.";
$input = $helper->createInputTD("Redirect message", 'params[redirect_message_open_time]',
$params->get('redirect_message_open_time'), '', $attribs['input'], $tooltip);
echo $helper->wrapTR($input, array('class' => 'cf_config'));that generate a complete input with tooltip, attributes, etc. wrapped in <td> and <tr> tags.2) ChronoForms has a Wizard Custom Elements link in the Forms Manager which will allow you to create custom elements for use in the Form Wizard. We don't often mention this but there is the capability to create specialist elements if you are going to need them frequently. I don't know if you can include MySQL in here though - I suspect not.
Bob
2)
Hi,
Been messing with this for a while. I have no trouble creating the php classes and accessing them within my forms. The only problem is that any attempt to use a "require" or "include" function fails. Simply adding them cause a blank screen (no errors) to display and the changes revert back to original code.
I have experimenting with differing paths including both relative and absolute with no avail.
I currently must paste the object class definition in each form. 😟
Do you have any idea what is happening here.
Tim
Been messing with this for a while. I have no trouble creating the php classes and accessing them within my forms. The only problem is that any attempt to use a "require" or "include" function fails. Simply adding them cause a blank screen (no errors) to display and the changes revert back to original code.
I have experimenting with differing paths including both relative and absolute with no avail.
I currently must paste the object class definition in each form. 😟
Do you have any idea what is happening here.
Tim
Hi Tim,
Yes, in the backend ChronoForms has a 'feature' that causes this, it tries to evaluate the form html on save to extract the field names. This won't work if there is (some) PHP in the form html. Max is aware and will hopefully fix it in a future release.
Meanwhile the workaround is to add if ( !$mainframe->isSite() ) { return; } before using any PHP.
Bob
Yes, in the backend ChronoForms has a 'feature' that causes this, it tries to evaluate the form html on save to extract the field names. This won't work if there is (some) PHP in the form html. Max is aware and will hopefully fix it in a future release.
Meanwhile the workaround is to add if ( !$mainframe->isSite() ) { return; } before using any PHP.
Bob
Hi,
I just added the workaround and it simply moved the problem from the back end to the front end.
Here is what I have
With this the form does not render. Comment out the require-once (and any references to SelectOptions.php) and the form renders fine.
Any help would be appreciated.
Tim
I just added the workaround and it simply moved the problem from the back end to the front end.
Here is what I have
$path = JPATH_ROOT.DS.'components'.DS.'com_tp'.DS.'helpers'.DS;
echo $path;
if ( !$mainframe->isSite() ) { return; }
require-once($path.'SelectOptions.php');
With this the form does not render. Comment out the require-once (and any references to SelectOptions.php) and the form renders fine.
Any help would be appreciated.
Tim
Hi Tim,
You should use require_once(), not require-once(), as dash (-) is used for operators and thus is not a valid character for commands or variables.
/Fredrik
You should use require_once(), not require-once(), as dash (-) is used for operators and thus is not a valid character for commands or variables.
/Fredrik
Opps
I just noticed require-once instead of require_once.
I corrected this and still have problem. My guess is that we have a path problem, but the one I echo back looks find.
Apache is installed under "Program Files" - I wonder if spaces in paths could cause problems in PHP?
Is there any way I can log and view errors in PHP and the Joomla/Chronoforms environment?
Tim
I just noticed require-once instead of require_once.
I corrected this and still have problem. My guess is that we have a path problem, but the one I echo back looks find.
Apache is installed under "Program Files" - I wonder if spaces in paths could cause problems in PHP?
Is there any way I can log and view errors in PHP and the Joomla/Chronoforms environment?
Tim
Hi Tom,
Since you use the JPATH_ROOT and DS macros, I doubt any spaces in the pathname would be the cause for your issues, since the rest of your Joomla installation uses the same macros (I assume without problems).
What is most likely the cause however, is that your components/com_tp/helpers/SelectOptions.php script depends on some other class that is not yet defined. Try enabling error reporting under the site config->server settings, and set it to maximum. This should provide you with any and all warnings and/or errors generated by PHP. Just remember to turn this off once you're done debugging, if you're on a live site.
/Fredrik
Since you use the JPATH_ROOT and DS macros, I doubt any spaces in the pathname would be the cause for your issues, since the rest of your Joomla installation uses the same macros (I assume without problems).
What is most likely the cause however, is that your components/com_tp/helpers/SelectOptions.php script depends on some other class that is not yet defined. Try enabling error reporting under the site config->server settings, and set it to maximum. This should provide you with any and all warnings and/or errors generated by PHP. Just remember to turn this off once you're done debugging, if you're on a live site.
/Fredrik
This topic is locked and no more replies can be posted.
