Forums

including php files in Chronoforms

Jean-Claude 15 Jul, 2013
Hi,
This is my first time that I submitted a question to the forum. I'm just beginning to use Chronoforms and it's looking very good on my site.
I have a few questions about self written code.
I find myself writting the same code (CSS, PHP, Javascript) over and over again in the custom elements or js events. Now, in a webproject, for instance in a PHP project, you can write the code, save it to an include file and include it in another PHP file. The same applies for CSS and Javascript files.

Question: How can I do the same in Chronoforms? Do I have to make a file system (and if so, where is the best place to make these?) (I'm using Joomla 2.5.11 stable version) 🀨
GreyHead 15 Jul, 2013
Hi Jean-Claude,

I thought I'd written a FAQ about this but apparently not.

Later: I've modified this post a little and added it to this FAQ.

It's easy to include files into ChronoForms and I do it with almost any code that is more than about twenty lines. Then I can edit it with an FTP enabled editor that has a built in linter for highlighting code errors (I use Sublime Text 2).

You can include files into textarea boxes in many ChronoForms elements and actions provided that they evaluate PHP when they are run. (The exceptions I can think of at the moment are options boxes of Radio Button and Checkbox groups* and the Thank You Message action**).

I have a macro set up that inserts this code into a textarea:
<?php
include (JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'includes'.DS.$form->form_details->name.DS.'file_name.php');
?>
Note .DS. is deprecated in Joomla! 3; a simple / should work OK.

This will automatically add the form name to the path; replace file_name with the name of your file: I typically use on_load_a.php, on_load_b.php, on_submit_a.php, . . .

For code that is specific to a form I use:
[list]
  • Custom Code actions to insert PHP that is pre-processing data;

  • Load JS actions to insert JavaScript files load_js.js;

  • Custom Element elements to insert HTML with PHP that is directly related to the HTML

  • Load CSS files to add CSS (though more often I type this directly in the action)
  • [/list]

    For code that is related to several forms I'll probably put it directly in the 'includes' folder. For example, as a helper file with functions that are used in several files.

    For third-party code, like API libraries, I typically create a components/com_chronoforms/extras/ folder*** and add libraries in sub_folders there.

    See the screenshot below for the structure of my J3 test site - it's easier to see than to describe.

    For completeness you can use Joomla! code to load CSS and JavaScript files (or snippets) into the page header.
    <?php
    $doc =& JFactory::getDocument();
    $doc->addScript('url_of_js_file.js');
    $doc->addScriptDeclaration('some js snippet');
    $doc->addStyleSheet('url_of_css_file.css');
    $doc->addStyleDeclaration('some css snippet');
    
    // code to force the MooTools library to load
    // this is sometimes needed to handle the load sequence
    JHTML::_load('behaviour.mootools'); // Joomla! 2.5 and earlier
    JHTML::_('behavior.framework'); // Joomla! 3 MooTools core only
    JHTML::_('behavior.framework', true); // Joomla! 3 MooTools core + more
    
    // code load the JQuery library to load (Joomla! 3 only)
    JHtml::_('jquery.framework'); // load jQuery in no conflict mode
    
    // code to load the Bootstrap library (Joomla! 3 only)
    JHtml::_('bootstrap.framework');
    ?>

    Please see the Joomla docs for more info on these methods in Joomla! 3 and their limitations.

    There are more things that you can do but this should be enough to get you started.

    Bob

    * Use the Dynamic Data options with PHP to pre-process the data if needed.

    ** Use a Custom Code action in place of the Thank You page action.

    *** I keep the included files and folders inside the components/com_chronoforms folder for convenience but you can have them anywhere that is accessible.
    GreyHead 15 Jul, 2013
    Hi Jean-Claude,

    There's a second part to this answer; if you find yourself repeating code elements on the same site and the forms are similar then it is worth thinking about merging the forms into one and using Code to show the different variations as they are needed. This can be more complicated to build; but is usually much simpler to maintain.

    As an example, I have a client who wanted four very similar voting forms. I was able to merge those into one form using a parameter to show and hide the elements; and to show a fifth 'What do you want to vote for?' form if the parameter wasn't set.

    There are various ways to structure this; in this case I put each of the voting radio button groups into a Container element and used PHP in the Container Start Code box to show or hide the element.

    Bob
    Jean-Claude 15 Jul, 2013
    Hi Bob

    Thanks for this (very swift) reply. It's much apreciated.πŸ˜€

    For the last part of your reply: I'm developing a system for our website (Tendoryu BelgiΓ« - an Aikido site) so that people can enroll into the summerschool and other events organised by Tendoryu Belgium. I'm the superuser, but on a day to day basis, I let others do the maintenance of the site (like create events, articles, etc.). They do their job very well, but they're not that IT-savy. So I'm creating a system to create countries, events, enrollments with the KISS principle in mind (Also for the update and deleting of this records. - hope this brief description gives you an idea why I do not merge cerain forms into 1 big form)

    For the first part of your reply: When you say "components/com_chronoforms/extras/" folder, I think you mean that folder and not the one in the administrator folder. Am I correct?
    In joomla, as you know, if you want to override a component's functionality, you should do that in the html folder of the template. This is done to avoid beeing overwritten if there is an update.

    Will the creation of this folder stay intact, and of course the contents of this folder, when Chronoforms has an update?

    And again, thx for your prompt response. It was very fast indeed.
    GreyHead 16 Jul, 2013
    Hi Jean-Claude,

    Yes I do use the components/com_chronoforms folder and not the administrator/components/com_chronoforms. Either will work but I prefer to keep the code related to the forms in the front-end folders.

    Neither folder (includes or extras) exists in ChronoForms by default so, unless Max adds them in some future version, they will not be changed in an upgrade. (There is no direct equivalent of a template over-ride for ChronoForms.)

    As I said in the FAQ, you can use any folder that you like that is accessible to Joomla!

    Bob
    Jean-Claude 16 Jul, 2013
    Thanks Bob for yet another quick response.

    I also thought that it was the front-end folder.πŸ˜€
    I have another question though:
    Is it possible to make a form with Chronoconnectivity for admin tasks? I'll explaine:

    What I want to do is to make a form (or a couple of them) where I can see the records in a gridview and update or delete them (it's still the same enrollement system I was talking off before)? This will, I guess, only be possible from the front-end view (after logging in)?

    Any tips are greatly appreciated
    Jean-Claude
    GreyHead 16 Jul, 2013
    Hi Jean-Claude,

    ChronoConnectivity will let you do this in an Admin view (though I've never used it that way). Usually my preference is to keep as many users as possible out of the site admin so I create front-end forms with user-group limited access.

    Bob
    Jean-Claude 17 Jul, 2013
    Hi Bob
    That's my method of working too. That's why I made the chronoforms (for the non-IT'ers). The Chronoconnectivity forms are meant for me πŸ˜€

    Thanks a lot for your advise and prompt responses
    Jean-Claude
    zingeonaut 31 Jul, 2013
    Hello, I'm also trying to use the tutorial Grayhead provided, but i've found that the form is not loading the php file.

    I'm using

    
    include (JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'libraries'.DS.'includes'.DS.'cfobb'.DS.'class.formAw.php';)
    


    on a Custom Code action, but it is not loading it. I'am currently trying this:

    
    $form = new formAward;
    
    $form->test(); //-> this calls var_dump(get_object_vars($this));
    


    thanks
    GreyHead 31 Jul, 2013
    Hi zingeonaut,

    You'll need <?php ?> tags as well, and theπŸ˜‰ at the end needs to be );

    Bob
    zingeonaut 31 Jul, 2013
    Well i didn't put here the php braces but... this is the whole code i'm using right now

    
    <?php
    include (JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'libraries'.DS.'includes'.DS.'cfobb'.DS.'class.formAw.php');
    
    /*
    $pageContentInc = JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'libraries'.DS.'includes'.DS.'cfobb'.DS.'class.formAw.php';
    
        if(file_exists($pageContentInc)){
            require_once $pageContentInc;
        }else{
            $pageContentInc = "common/content_404.inc";
            require_once $pageContentInc;
        }
    */
    
    $form = new formAward;
    
    //dump the contents of the form object
    $form->test();
    
    ?>
    


    i left a part commented because i don't know if CF supports the require_once
    GreyHead 02 Aug, 2013
    Hi zingeonaut,

    There's no problem with require_once (ChronoForms supports any valid PHP).

    There is a problem with using $form !!!

    ChronoForms stores all of the data for your form in $form; if you redefine that you will delete it all. Please try:
    <?php
    require_once(JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'libraries'.DS.'includes'.DS.'cfobb'.DS.'class.formAw.php');
    
    $formAward = new formAward;
    //dump the contents of the form object
    $formAward->test();
    ?>

    Bob
    oneira 04 Aug, 2013
    Thank you so much. By the way, is there any list of reserved names i can't use for variables?
    GreyHead 04 Aug, 2013
    Hi oniera,

    There is no official list - but basically avoid anything you might see in a Joomla! URL as otherwise you may overwrite the value and get unexpected results. Here's a short list off the top of my head but it's not complete: event, task, action, view, option, tmpl, itemid, . . .

    Bob
    oneira 04 Aug, 2013
    Thanks. You've been very helpful!! πŸ˜€
    This topic is locked and no more replies can be posted.