Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active April 27, 2018 19:23
Show Gist options
  • Save stevewithington/5963610 to your computer and use it in GitHub Desktop.
Save stevewithington/5963610 to your computer and use it in GitHub Desktop.
Mura CMS : Iterating over form results. Mura CMS form structures can change, which means fields may be added and/or deleted by Content Managers and thus the form data is stored as XML packets (WDDX, to be exact).
<cfscript>
formName = 'Information Request';
rsData = QueryNew('');
dcm = $.getBean('dataCollectionManager');
</cfscript>
<cfoutput>
<cfif !Len($.event('responseid'))>
<!--- All Form Submission Results --->
<cfscript>
formBean = $.getBean('content').loadBy(title=formName);
if ( !formBean.getIsNew() ) {
currentFieldList = dcm.getCurrentFieldList(formBean.getContentID());
data = {
sortby = 'entered'
,sortdirection = 'desc'
,keywords = ''
,siteid = $.event('siteid')
,contentid = formBean.getContentID()
};
rsData = dcm.getData(data);
}
</cfscript>
<cfif !rsData.recordcount>
<h3>Sorry, either the form does not exist, or no records have been submitted yet.</h3>
<cfelse>
<table cellspacing="5" cellpadding="5" border="1">
<!--- FieldNames --->
<thead>
<tr>
<th>&nbsp;</th>
<th>Date/Time Entered</th>
<cfloop list="#currentFieldList#" index="fieldName">
<th>#esapiEncode('html', fieldName)#</th>
</cfloop>
</tr>
</thead>
<!--- Actual Output --->
<tbody>
<cfloop query="rsData">
<tr>
<!--- Edit --->
<td>
<a href="./?responseid=#responseid#">Edit</a>
</td>
<!--- The Date/Time Stamp --->
<td>
#entered#
</td>
<!--- The Data --->
<!--- Forms are stored as WDDX files ... so we need to unpack them --->
<cfwddx action="wddx2cfml" input="#data#" output="record" />
<cfloop list="#currentFieldList#" index="fieldName">
<td>
<cfif StructKeyExists(record, fieldName)>
#record[fieldName]#
<cfelse>
&nbsp;
</cfif>
</td>
</cfloop>
</tr>
</cfloop>
</tbody>
</table>
</cfif>
<cfelse>
<!--- Form Details --->
<cfscript>
if ( $.event('isSubmitted') eq 'true' ) {
try {
dcm.update(form);
$.event('updated', true);
} catch (any e) {
$.event('updated', false);
}
}
rsData= dcm.read($.event('responseid'));
</cfscript>
<cfif !rsData.recordcount>
<h3>Sorry, the selected form does not appear to exist.</h3>
<cfelse>
<!--- Notices --->
<cfif $.event('updated') eq 'true'>
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<strong>Yes!</strong> Update worked just fine.
</div>
<cfelseif $.event('updated') eq 'false'>
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<strong>Sorry...</strong> something went wrong with that update.
</div>
</cfif>
<!--- need to parse the WDDX to get to the actual data --->
<cfwddx action="wddx2cfml" input="#rsData.data#" output="record">
<cfset currentFieldList = dcm.getCurrentFieldList(rsData.formID)>
<form class="form-horizontal" method="post">
<fieldset>
<legend>Edit Form</legend>
<!--- output the fields --->
<cfloop list="#currentFieldList#" index="fieldName">
<cfset fieldValue = ''>
<cfif StructKeyExists(record, fieldName)>
<cfset fieldValue = record[fieldName]>
</cfif>
<!--- if it's a file, then we don't want to allow the ability to edit --->
<cfif FindNoCase('attachment', fieldName) or FindNoCase('cid', fieldName)>
<input type="hidden" name="#fieldName#" value="#fieldValue#">
<cfelse>
<div class="control-group">
<label class="control-label" for="#fieldName#">#esapiEncode('html_attr', fieldName)#</label>
<div class="controls">
<cfif Len(fieldValue) gt 100>
<textarea name="#fieldName#" rows="3">#esapiEncode('html_attr', fieldValue)#</textarea>
<cfelse>
<input type="text" name="#fieldName#" value="#esapiEncode('html_attr', fieldValue)#">
</cfif>
</div>
</div>
</cfif>
</cfloop>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</fieldset>
<input type="hidden" name="responseid" value="#rsData.responseID#">
<input type="hidden" name="siteid" value="#rsData.siteID#">
<input type="hidden" name="formid" value="#rsData.formID#">
<input type="hidden" name="contentid" value="#rsData.formID#">
<input type="hidden" name="entered" value="#rsData.entered#">
<input type="hidden" name="fieldlist" value="#currentFieldList#">
<input type="hidden" name="isSubmitted" value="true">
</form>
</cfif>
</cfif>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment