Page 1 of 1

Retrieve Calendar Variables and pass them to FormBuilder

Posted: Wed Jan 21, 2009 1:33 am
by mw
I commisioned calguy's talents to implement this feature.
To retrieve a list of event titles and pass them on to formbuilder so the name will be both displayed on the form as "selected" and also to be emailed as "selected".
You will need FormBuilder 0.5.11 and CGSimpleSmarty 1.4


save the below and create a UDT called : "fb_form_test"

Code: Select all

$form =& $params['FORM'];
if( !is_object($form) ) return;
$fid = $form->HasFieldNamed('Selected Courses');
if( $fid < 0 ) return;  
$field =& $form->GetFieldById($fid);
if( $field === false ) return;

$opt1 = $field->GetOption('destination_displayname');
$opt2 = $field->GetOption('destination_value');
$opt3 = $field->GetOption('destination_filename');

if( isset($_SESSION['sel_courses']) )
{
   $filenames = array();
   foreach( $_SESSION['sel_courses'] as $one )
   {
      /* build a filename here, from the selected courses */
      $filenames[] = $one.'.txt';
   }
   $field->SetOption('destination_displayname',$_SESSION['sel_courses']);
   $field->SetOption('destination_value',$_SESSION['sel_courses']);
   $field->SetOption('destination_filename',$filenames);
   $field->ResetValue();
   $field->SetValue($_SESSION['sel_courses']);
}

save the below as the content of the test form page

Code: Select all

{if !isset($smarty.post.testsubmit)}
  <h3>Manually Created Form - Imagine this came from Calendar</h3>
  <form name="test" method="post">
    <input type="checkbox" name="opts[]" value="Course 1"/>Course 1<br/>
    <input type="checkbox" name="opts[]" value="Course 2"/>Course 2<br/>
    <input type="checkbox" name="opts[]" value="Course 3"/>Course 3<br/>
    <input type='submit' name='testsubmit' value='Submit'/>
  </form>
{else}
  <h3>Formbuilder test form</h3>
  {session_put var="sel_courses"  value=$smarty.post.opts}
  {cms_module module='FormBuilder' form='test'} 
{/if}

save the below as "test_form.xml" (and import into formbuilder)

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form id="5"
	alias="test">
		<form_name><![CDATA[test]]></form_name>
		<attribute key="next_button_text"><![CDATA[Continue...]]></attribute>
		<attribute key="prev_button_text"><![CDATA[Back...]]></attribute>
		<attribute key="css_class"><![CDATA[formbuilderform]]></attribute>
		<attribute key="unspecified"><![CDATA[[unspecified]]]></attribute>
		<attribute key="submit_action"><![CDATA[text]]></attribute>
		<attribute key="list_delimiter"><![CDATA[,]]></attribute>
		<attribute key="submit_response"><![CDATA[]]></attribute>
		<attribute key="inline"><![CDATA[0]]></attribute>
		<attribute key="required_field_symbol"><![CDATA[*]]></attribute>
		<attribute key="form_template"><![CDATA[{* DEFAULT FORM LAYOUT / pure CSS *}
{$fb_form_header}
{if $fb_form_done == 1}
	{* This first section is for displaying submission errors *}
	{if isset($fb_submission_error) && $fb_submission_error}
		<div class="error_message">{$fb_submission_error}</div>
		{if isset($fb_show_submission_errors) && $fb_show_submission_errors}
			<div class="error">
			<ul>
			{foreach from=$fb_submission_error_list item=thisErr}
				<li>{$thisErr}</li>
			{/foreach}
			</ul>
		</div>
		{/if}
	{/if}
{else}
	{* this section is for displaying the form *}
	{* we start with validation errors *}
	{if isset($fb_form_has_validation_errors) && $fb_form_has_validation_errors}
		<div class="error_message">
		<ul>
		{foreach from=$fb_form_validation_errors item=thisErr}
			<li>{$thisErr}</li>
		{/foreach}
		</ul>
		</div>
	{/if}
	{if isset($captcha_error) && $captcha_error}
		<div class="error_message">{$captcha_error}</div>
	{/if}

	{* and now the form itself *}
	{$fb_form_start}
	<div>{$fb_hidden}</div>
	<div{if $css_class != ''} class="{$css_class}"{/if}>
	{if $total_pages gt 1}<span>{$title_page_x_of_y}</span>{/if}
	{foreach from=$fields item=entry}
		{if $entry->display == 1}
        	{strip}
         	{if $entry->needs_div == 1}
            	<div
            	{if $entry->required == 1 || $entry->css_class != '' || $entry->valid == 0} class="
              		{if $entry->required == 1}required{/if}
              		{if $entry->css_class != ''} {$entry->css_class}{/if}
                    {if $entry->valid == 0} fb_invalid{/if}
              		"
            	{/if}
            	>
         	{/if}
         	{if $entry->hide_name == 0}
            	<label{if $entry->multiple_parts != 1} for="{$entry->input_id}"{/if}>{$entry->name}
            	{if $entry->required_symbol != ''}
               		{$entry->required_symbol}
            	{/if}
            	</label>
         	{/if}
         	{if $entry->multiple_parts == 1}
            	{section name=numloop loop=$entry->input}
               		{if $entry->label_parts == 1}
               			<div>{$entry->input[numloop]->input} {$entry->input[numloop]->name}</div>
               		{else}
               			{$entry->input[numloop]->input}
               		{/if}
               		{if isset($entry->input[numloop]->op) && $entry->input[numloop]->op}{$entry->input[numloop]->op}{/if}
            	{/section}
         	{else}
            	{if $entry->smarty_eval == '1'}{eval var=$entry->input}{else}{$entry->input}{/if}
         	{/if}
         	{if $entry->valid == 0} <--- {$entry->error}{/if}
         	{if $entry->needs_div == 1}
            	</div>
         	{/if}
         	{/strip}
     	{/if}
	{/foreach}
	{if $has_captcha == 1}
		<div class="captcha">{$graphic_captcha}{$title_captcha}<br />{$input_captcha}</div>
	{/if}
	<div class="submit">{$prev}{$submit}</div>
	</div>
	{$fb_form_end}
{/if}
{$fb_form_footer}]]></attribute>
		<attribute key="input_button_safety"><![CDATA[0]]></attribute>
		<attribute key="submit_javascript"><![CDATA[]]></attribute>
		<attribute key="redirect_page"><![CDATA[-1]]></attribute>
		<attribute key="submit_button_text"><![CDATA[Submit Form]]></attribute>
		<attribute key="predisplay_udt"><![CDATA[fb_form_test]]></attribute>
		<attribute key="display_udt"><![CDATA[fb_form_test]]></attribute>
	<field id="107"
		type="DispositionUserTag"
		validation_type=""
		order_by="1"
		required="0"
		hide_label="0"
		alias="">
			<field_name><![CDATA[Call UDT]]></field_name>
			<options>
			<option name="udtname"><![CDATA[fb_form_test]]></option>
			<option name="field_alias"><![CDATA[]]></option>
			<value></value>
			</options>
</field>
	<field id="71"
		type="TextField"
		validation_type="none"
		order_by="2"
		required="1"
		hide_label="0"
		alias="">
			<field_name><![CDATA[Your Name]]></field_name>
			<options>
			<option name="length"><![CDATA[80]]></option>
			<option name="field_alias"><![CDATA[]]></option>
			<option name="css_class"><![CDATA[]]></option>
			<option name="javascript"><![CDATA[]]></option>
			<option name="regex"><![CDATA[]]></option>
			<option name="default"><![CDATA[]]></option>
			<option name="clear_default"><![CDATA[0]]></option>
			<value></value>
			</options>
</field>
	<field id="72"
		type="PulldownField"
		validation_type=""
		order_by="3"
		required="1"
		hide_label="0"
		alias="">
			<field_name><![CDATA[Gender]]></field_name>
			<options>
			<option name="select_one"><![CDATA[Select One]]></option>
			<option name="sort"><![CDATA[0]]></option>
			<option name="option_name"><![CDATA[Male]]></option>
			<option name="option_name"><![CDATA[Female]]></option>
			<option name="option_value"><![CDATA[m]]></option>
			<option name="option_value"><![CDATA[f]]></option>
			<option name="field_alias"><![CDATA[]]></option>
			<option name="css_class"><![CDATA[]]></option>
			<option name="javascript"><![CDATA[]]></option>
			<value></value>
			</options>
</field>
	<field id="106"
		type="StaticTextField"
		validation_type=""
		order_by="4"
		required="0"
		hide_label="0"
		alias="static">
			<field_name><![CDATA[Static Text]]></field_name>
			<options>
			<option name="text"><![CDATA[{foreach from=$smarty.post.opts item='one'}
Selected Course: {$one}<br/>
{/foreach}]]></option>
			<option name="field_alias"><![CDATA[static]]></option>
			<option name="css_class"><![CDATA[]]></option>
			<option name="smarty_eval"><![CDATA[1]]></option>
			<value></value>
			</options>
</field>
	<field id="73"
		type="DispositionMultiselectFileDirector"
		validation_type=""
		order_by="5"
		required="1"
		hide_label="0"
		alias="saveit">
			<field_name><![CDATA[Selected Courses]]></field_name>
			<options>
			<option name="select_one"><![CDATA[Select One]]></option>
			<option name="destination_displayname"><![CDATA[Junk]]></option>
			<option name="destination_value"><![CDATA[Junk]]></option>
			<option name="destination_filename"><![CDATA[junk.txt]]></option>
			<option name="field_alias"><![CDATA[saveit]]></option>
			<option name="css_class"><![CDATA[hidden]]></option>
			<option name="javascript"><![CDATA[]]></option>
			<option name="file_path"><![CDATA[/disk1/html/cms_test/svn/uploads]]></option>
			<option name="file_template"><![CDATA[]]></option>
			<option name="file_header"><![CDATA[]]></option>
			<value></value>
			</options>
</field>
	<field id="74"
		type="DispositionEmail"
		validation_type=""
		order_by="6"
		required="0"
		hide_label="0"
		alias="">
			<field_name><![CDATA[Send It]]></field_name>
			<options>
			<option name="email_subject"><![CDATA[Test formbuilder submit]]></option>
			<option name="email_from_name"><![CDATA[Form Builder]]></option>
			<option name="email_from_address"><![CDATA[info@yourdomain.com]]></option>
			<option name="destination_address"><![CDATA[someone@somewhere.org]]></option>
			<option name="destination_address"><![CDATA[hughjass@yehaw.com]]></option>
			<option name="field_alias"><![CDATA[]]></option>
			<option name="html_email"><![CDATA[1]]></option>
			<option name="email_template"><![CDATA[<h1>FormBuilder Submission</h1>
<strong>Form Name</strong>: {$sub_form_name}<br />
<strong>Date of Submission</strong>: {$sub_date}<br />
<strong>Your server</strong>: {$sub_host}<br />
<strong>IP address of person using form</strong>: {$sub_source_ip}<br />
<strong>URL of page containing form</strong>: {$sub_url}<br />
<strong>FormBuilder version</strong>: {$fb_version}<br />
<strong>Tab Character</strong>: {$TAB}<br />

<hr />
<strong>Your Name</strong>: {$fld_71}<br />
<strong>Gender</strong>: {$fld_72}<br />
<strong>Selected Courses</strong>: {$saveit}<br />
]]></option>
			<option name="email_encoding"><![CDATA[utf-8]]></option>
			<value></value>
			</options>
</field>
</form>

add the following css so the formbuilder's hidden fields do not display:

Code: Select all

.hidden { display: none; }
In the Formbuilder form: in the "Save Results to File(s) Based on Multiple Selections" the tab "Advanced" settings, note "hidden"
and also the writable path to save to.

you can replace the hard coded reference to "course 1, course 2, etc in the form page with:

Code: Select all

{capture assign='tmp'}
 {cms_module module="Calendar" display='list"}
 {/capture}
 {foreach from=$events key=key item=event}
 <input name="opts[]" value="{$event.event_title}" type="checkbox" />{$event.event_title} <br />
 {/foreach}
and it will list all your event titles and allow the end user to select then hit submit and be taken to te actual formbuilder form where they can fill out thier details.

hopefull someone finds this as useful as I have :)

Re: Retrieve Calendar Variables and pass them to FormBuilder

Posted: Wed Jan 21, 2009 5:12 pm
by Coldman
Thankyou for sharing!

This will be useful for my next project.

/Coldman

Re: Retrieve Calendar Variables and pass them to FormBuilder

Posted: Wed Jan 21, 2009 5:31 pm
by nhaack
Very useful indeed. Thanks for sharing.

Best
Nils