Hi all,
I have designed quite a few forms for a complex "module" that I am authoring with ChronoForms, and I have quite a few more to go.
BUT, many of the features for my program are "shared" functions. Rather than duplicate the code in the forms, over and over again, I would like to put these shared functins in an include file.
Please give me an idea of where to put this file and how to link it. Do I link it in the basic Chronoforms code files or in each one of my forms?
A quick example would be helpful.
Thanks, in advance for your help.
The_fitz
I have designed quite a few forms for a complex "module" that I am authoring with ChronoForms, and I have quite a few more to go.
BUT, many of the features for my program are "shared" functions. Rather than duplicate the code in the forms, over and over again, I would like to put these shared functins in an include file.
Please give me an idea of where to put this file and how to link it. Do I link it in the basic Chronoforms code files or in each one of my forms?
A quick example would be helpful.
Thanks, in advance for your help.
The_fitz
Hi the_fitz,
Absolutely use include files for shared functionality or shared data.
I create a components/com_chronocontact/includes folder on pretty much all the sites I work with and put a sub-folder there for each form. I'll put formhtml.php, onsubmit_before.php and javascript.php files in the the form folder and include in the code for the ChronoForms boxes using
If it's code shared between many forms then I may put it into the include folder directly or create a new sub-folder.
As an example I recently created a 20(ish) step multi-page form for a client. Many of the steps were very similar so I built a helper.php file with most of the code, that let me build short succinct formhtml.php files for each of the step forms.
Here's one of the step forms
With constant data files (for example the names of regions or states) that are used in more than one place I'll put the data in PHP arrays and include the array file in wherever it is needed.
Here's an example for a French site with lists of regions and departments.
Hope that helps
Bob
Absolutely use include files for shared functionality or shared data.
I create a components/com_chronocontact/includes folder on pretty much all the sites I work with and put a sub-folder there for each form. I'll put formhtml.php, onsubmit_before.php and javascript.php files in the the form folder and include in the code for the ChronoForms boxes using
<?php
if ( !$mainframe->isSite() ) { return; }
$formname =& JRequest::getString('chronoformname', '', 'get');
include (JPATH_SITE.DS.'components'.DS.'com_chronocontact'.DS.'includes'.DS.$formname.DS.'formhtml.php');
?>
(I even have a macro for that snippet).If it's code shared between many forms then I may put it into the include folder directly or create a new sub-folder.
As an example I recently created a 20(ish) step multi-page form for a client. Many of the steps were very similar so I built a helper.php file with most of the code, that let me build short succinct formhtml.php files for each of the step forms.
Here's one of the step forms
<?php
/* ensure that this file is called by another file */
defined('_JEXEC') or die('Restricted access');
include(JPATH_SITE.DS.'components'.DS.'com_chronocontact'.DS.'includes'.DS.'helper.php');
$title = 'Bathroom 2';
$input_name = 'bath2';
echo "<fieldset><legend>$title assessment</legend>";
createOverallCondition($title, $input_name);
?>
<fieldset>
<?php
checkRepair();
checkStateRadio('Cabinets - '.$title, $input_name.'_cabs');
checkStateRadio('Tub - '.$title, $input_name.'_apps');
?>
</fieldset>
<fieldset>
<?php
checkDamage();
createScaleRadio('Paint - '.$title, $input_name.'_paint');
createScaleRadio('Flooring - '.$title, $input_name.'_floor');
?>
</fieldset>
<?php
createComments($title, $input_name);
echo '</fieldset>';
createSubmit('Next page');
?>
and here's the helper.php file that makes it work<?php
/* ensure that this file is called by another file */
defined('_JEXEC') or die('Restricted access');
function createScaleRadio($title, $input_name, $checkbox=true)
{
$c = '';
if ( $checkbox) {
$c = "<input value='1' title='' class='checkbox' id='$input_name' name='$input_name' type='checkbox' /> ";
}
?>
<div class="form_item">
<div class="form_element cf_radiobutton">
<label class="cf_label" style="width: 150px;"><?php echo $c.$title; ?></label>
<div class="float_left">
<input value="N/A" title="" class="radio" id="<?php echo $input_name; ?>_damage0" name="<?php echo $input_name; ?>_damage" type="radio" />
<label for="<?php echo $input_name; ?>_damage0" class="radio_label">N/A</label>
<input value="25%" title="" class="radio" id="<?php echo $input_name; ?>_damage1" name="<?php echo $input_name; ?>_damage" type="radio" />
<label for="<?php echo $input_name; ?>_damage1" class="radio_label">25%</label>
<input value="50%" title="" class="radio" id="<?php echo $input_name; ?>_damage2" name="<?php echo $input_name; ?>_damage" type="radio" />
<label for="<?php echo $input_name; ?>_damage2" class="radio_label">50%</label>
<input value="75%" title="" class="radio" id="<?php echo $input_name; ?>_damage3" name="<?php echo $input_name; ?>_damage" type="radio" />
<label for="<?php echo $input_name; ?>_damage3" class="radio_label">75%</label>
<input value="100%" title="" class="radio" id="<?php echo $input_name; ?>_damage4" name="<?php echo $input_name; ?>_damage" type="radio" />
<label for="<?php echo $input_name; ?>_damage4" class="radio_label">100%</label>
</div>
</div>
<div class="cfclear"> </div>
</div>
<?php
}
function createOverallCondition($title, $input_name)
{
?>
<fieldset>
<div class="form_item">
<div class="form_element cf_radiobutton">
<label class="cf_label" style="width: 150px;">Overall condition of <?php echo $title; ?></label>
<div class="float_left">
<input value="Poor" title="" class="radio" id="<?php echo $input_name; ?>_oc0" name="<?php echo $input_name; ?>_oc" type="radio" />
<label for="<?php echo $input_name; ?>_oc0" class="radio_label">Poor</label>
<input value="Good" title="" class="radio" id="<?php echo $input_name; ?>_oc1" name="<?php echo $input_name; ?>_oc" type="radio" />
<label for="<?php echo $input_name; ?>_oc1" class="radio_label">Good</label>
<input value="Excellent" title="" class="radio" id="<?php echo $input_name; ?>_oc2" name="<?php echo $input_name; ?>_oc" type="radio" />
<label for="<?php echo $input_name; ?>_oc2" class="radio_label">Excellent</label>
</div>
</div>
<div class="cfclear"> </div>
</div>
</fieldset>
<?php
}
function checkStateRadio($title, $input_name)
{
?>
<div class="form_item">
<div class="form_element cf_radiobutton">
<label class="cf_label" style="width: 150px;"><?php echo $title; ?></label>
<div class="float_left">
<input value="OK" title="" class="radio" id="<?php echo $input_name; ?>_rr0" name="<?php echo $input_name; ?>_rr" type="radio" />
<label for="<?php echo $input_name; ?>_rr0" class="radio_label">OK</label>
<input value="Repair" title="" class="radio" id="<?php echo $input_name; ?>_rr1" name="<?php echo $input_name; ?>_rr" type="radio" />
<label for="<?php echo $input_name; ?>_rr1" class="radio_label">Repair</label>
<input value="Replace" title="" class="radio" id="<?php echo $input_name; ?>_rr2" name="<?php echo $input_name; ?>_rr" type="radio" />
<label for="<?php echo $input_name; ?>_rr2" class="radio_label">Replace</label>
</div>
</div>
<div class="cfclear"> </div>
</div>
<?php
}
function checkRepair()
{
?>
<div class="form_item">
<div class="form_element cf_text">If damaged, please indicate if it can be repaired or needs to be replaced:</div>
</div>
<?php
}
function checkDamage()
{
?>
<div class="form_item">
<div class="form_element cf_text">Please check any that are present and, if damaged, select the percentage that applies:</div>
</div>
<?php
}
function createComments($title, $input_name)
{
?>
<fieldset>
<div class="form_item">
<div class="form_element cf_textarea">
<label class="cf_label" style="width: 150px;">Comments - <?php echo $title; ?></label>
<textarea class="cf_inputbox" rows="3" id="<?php echo $input_name; ?>_comments" title="" cols="30" name="<?php echo $input_name; ?>_comments"></textarea>
</div>
<div class="cfclear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_fileupload">
<label class="cf_label" style="width: 150px;">Upload <?php echo $title; ?> Photos</label>
<input class="cf_fileinput cf_inputbox" title="" size="20" id="<?php echo $input_name; ?>_photos" name="<?php echo $input_name; ?>_photos" type="file" />
</div>
<div class="cfclear"> </div>
</div>
</fieldset>
<?php
}
function createSubmit($value, $name='')
{
?>
<div class="form_item">
<div class="form_element cf_button">
<input value="<?php echo $value; ?>" name="submit" type="submit" />
</div>
<div class="cfclear"> </div>
</div>
<input type='hidden' name='step_name' id='step_name' value='<?php echo $name; ?>' />
<?php
$cf_id =& JRequest::getString('cf_id', '', 'post');
if ( !$cf_id ) {
$last_step_name =& JRequest::getString('step_name', '', 'post');
if ( $last_step_name ) {
$formData =& CFChronoForm::getInstance($last_step_name);
//echo'<div>$formData: '.print_r($formData, true).'</div>';
$data =& $formData->tablerow['jos_assessments'];
$cf_id = $data->cf_id;
}
}
echo "<input type='hidden' name='cf_id' id='cf_id' value='".$cf_id."' />";
}
?>
With constant data files (for example the names of regions or states) that are used in more than one place I'll put the data in PHP arrays and include the array file in wherever it is needed.
Here's an example for a French site with lists of regions and departments.
<?php
defined('_JEXEC') or die('Restricted access');
$cat_array = array(
1 => 'Locale',
2 => 'Locale / régionale',
3 => 'Régionale',
4 => 'Inter-régionale',
5 => 'Régionale / nationale',
6 => 'Nationale / internationale',
9 => 'Bureau locale'
);
$reg_array = array(
'A' => 'Alsace',
'B' => 'Aquitaine',
'C' => 'Auvergne',
'P' => 'Basse-Normandie',
'D' => 'Bourgogne',
'E' => 'Bretagne',
'F' => 'Centre',
'G' => 'Champagne-Ardenne',
'H' => 'Corse',
'I' => 'Franche-Comté',
'Q' => 'Haute-Normandie',
'J' => 'Île-de-France',
'K' => 'Languedoc-Roussillon',
'L' => 'Limousin',
'M' => 'Lorraine',
'N' => 'Midi-Pyrénées',
'O' => 'Nord-Pas-de-Calais',
'R' => 'Pays de la Loire',
'S' => 'Picardie',
'T' => 'Poitou-Charentes',
'U' => 'Provence-Alpes-Côte d’Azur',
'V' => 'Rhône-Alpes',
'Z' => 'DOM-TOM'
);
$dep_array = array (
'01' => 'Ain',
'02' => 'Aisne',
'03' => 'Allier',
'04' => 'Alpes-de-Haute-Provence',
'06' => 'Alpes-Maritimes',
'07' => 'Ardèche',
'08' => 'Ardennes',
'09' => 'Ariège',
'10' => 'Aube',
'11' => 'Aude',
'12' => 'Aveyron',
'67' => 'Bas-Rhin',
'13' => 'Bouches-du-Rhône',
'14' => 'Calvados',
'15' => 'Cantal',
'16' => 'Charente',
'17' => 'Charente-Maritime',
'18' => 'Cher',
'19' => 'Corrèze',
'2A' => 'Corse-du-Sud',
'21' => 'Côte-d’Or',
'22' => 'Côtes-d’Armor',
'23' => 'Creuse',
'79' => 'Deux-Sèvres',
'24' => 'Dordogne',
'25' => 'Doubs',
'26' => 'Drôme',
'91' => 'Essonne',
'27' => 'Eure',
'28' => 'Eure-et-Loir',
'29' => 'Finistère',
'30' => 'Gard',
'32' => 'Gers',
'33' => 'Gironde',
'2B' => 'Haute-Corse',
'31' => 'Haute-Garonne',
'43' => 'Haute-Loire',
'52' => 'Haute-Marne',
'05' => 'Hautes-Alpes',
'70' => 'Haute-Saône',
'74' => 'Haute-Savoie',
'65' => 'Hautes-Pyrénées',
'87' => 'Haute-Vienne',
'68' => 'Haut-Rhin',
'92' => 'Hauts-de-Seine',
'34' => 'Hérault',
'35' => 'Ille-et-Vilaine',
'36' => 'Indre',
'37' => 'Indre-et-Loire',
'38' => 'Isère',
'39' => 'Jura',
'40' => 'Landes',
'42' => 'Loire',
'44' => 'Loire-Atlantique',
'45' => 'Loiret',
'41' => 'Loir-et-Cher',
'46' => 'Lot',
'47' => 'Lot-et-Garonne',
'48' => 'Lozère',
'49' => 'Maine-et-Loire',
'50' => 'Manche',
'51' => 'Marne',
'53' => 'Mayenne',
'54' => 'Meurthe-et-Moselle',
'55' => 'Meuse',
'56' => 'Morbihan',
'57' => 'Moselle',
'58' => 'Nièvre',
'59' => 'Nord',
'60' => 'Oise',
'61' => 'Orne',
'75' => 'Paris',
'62' => 'Pas-de-Calais',
'63' => 'Puy-de-Dôme',
'64' => 'Pyrénées-Atlantiques',
'66' => 'Pyrénées-Orientales',
'69' => 'Rhône',
'71' => 'Saône-et-Loire',
'72' => 'Sarthe',
'73' => 'Savoie',
'77' => 'Seine-et-Marne',
'76' => 'Seine-Maritime',
'93' => 'Seine-Saint-Denis',
'80' => 'Somme',
'81' => 'Tarn',
'82' => 'Tarn-et-Garonne',
'90' => 'Territoire de Belfort',
'94' => 'Val-de-Marne',
'95' => 'Val-d’Oise',
'83' => 'Var',
'84' => 'Vaucluse',
'85' => 'Vendée',
'86' => 'Vienne',
'88' => 'Vosges',
'89' => 'Yonne',
'78' => 'Yvelines',
'MQ' => 'Martinique',
'RE' => 'La Réunion'
);
?>
Hope that helps
Bob
Hi Bob,
Brilliant, as usual!!!!
quick question. If I am doing js includes or css includes do I do them in the main form html or in the appropriate js and css sections? I will experiment....
Thanks, again for the Best in support!!!
The_fitz
Brilliant, as usual!!!!
quick question. If I am doing js includes or css includes do I do them in the main form html or in the appropriate js and css sections? I will experiment....
Thanks, again for the Best in support!!!
The_fitz
Hi the_fitz,
Adding script and CSS.
If it's a 'static' CSS or JavaScript snippet (that is it's always the same) then you can add it in the Form CSS or Form JavaScript boxes.
If it's a dynamic snippet (that is there is some PHP variable involved in creating it) then I use this code structure in the Form HTML box. It's overkill for a four line form but works wonders with long forms:
To include CSS or HTML files I use this in the Form HTML
Bob
Adding script and CSS.
If it's a 'static' CSS or JavaScript snippet (that is it's always the same) then you can add it in the Form CSS or Form JavaScript boxes.
If it's a dynamic snippet (that is there is some PHP variable involved in creating it) then I use this code structure in the Form HTML box. It's overkill for a four line form but works wonders with long forms:
<?php
if ( !$mainframe->isSite() ) { return; }
$script = $style = "";
$doc =& JFactory::getDocument();
. . . // some other code here
$script .= "
//some script snippet
";
. . . // some other code here
$style .= "
//some style snippet
";
. . . // some other code here
// repeat as required until the end of the document
if ( $script ) {
$script = "window.addEvent('domready', function() { $script });";
$doc->addScriptDeclaration($script);
}
if ( $style ) {
$doc->addStyleDeclaration($style);
}
?>
To include CSS or HTML files I use this in the Form HTML
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base().'path/to/file.js');
$doc->addStyleSheet(JURI::base().'path/to/file.css');
?>
Bob
Thanks Bob, That did it for me!!!🤣
The_fitz
To include CSS or HTML files I use this in the Form HTML
Bob
The_fitz
Hi the_fitz,
Adding script and CSS.
If it's a 'static' CSS or JavaScript snippet (that is it's always the same) then you can add it in the Form CSS or Form JavaScript boxes.
If it's a dynamic snippet (that is there is some PHP variable involved in creating it) then I use this code structure in the Form HTML box. It's overkill for a four line form but works wonders with long forms:
<?php
if ( !$mainframe->isSite() ) { return; }
$script = $style = "";
$doc =& JFactory::getDocument();
. . . // some other code here
$script .= "
//some script snippet
";
. . . // some other code here
$style .= "
//some style snippet
";
. . . // some other code here
// repeat as required until the end of the document
if ( $script ) {
$script = "window.addEvent('domready', function() { $script });";
$doc->addScriptDeclaration($script);
}
if ( $style ) {
$doc->addStyleDeclaration($style);
}
?>
To include CSS or HTML files I use this in the Form HTML
<?php
if ( !$mainframe->isSite() ) { return; }
$doc =& JFactory::getDocument();
$doc->addScript(JURI::base().'path/to/file.js');
$doc->addStyleSheet(JURI::base().'path/to/file.css');
?>
Bob
This topic is locked and no more replies can be posted.