Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active August 29, 2015 14:06
Show Gist options
  • Save stevewithington/7a35ed48fb3716a7b479 to your computer and use it in GitHub Desktop.
Save stevewithington/7a35ed48fb3716a7b479 to your computer and use it in GitHub Desktop.
Mura CMS : How to Dynamically Add Files As Content Items.
<cfscript>
// Will NOT work unless you have 'allowlocalfiles=true' in /config/settings.ini.cfm
try {
filepath = ExpandPath('temp.pdf');
} catch(any e) {
filepath = '';
}
param name='form.thefile' default=filepath;
param name='form.parentfilename' default='blog';
param name='form.isSubmitted' default='false';
param name='form.istest' default='true';
param name='form.siteid' default='default';
$ = application.serviceFactory.getBean('$').init(form.siteid);
if ( !$.currentUser().isSuperUser() && !$.currentUser().isInGroup('admin') ) {
WriteOutput('<h3>You should not be here.</h3>');
abort;
}
utility = $.getBean('contentUtility');
parent = $.getBean('content').loadBy(filename=form.parentfilename);
parentid = parent.getContentID();
rsSites = $.getBean('settingsManager').getList();
</cfscript>
<cfoutput>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<cfheader name="expires" value="#dateformat(now(), 'ddd, dd mmm yyyy')# #timeformat(now(), 'HH:mm:ss tt')#">
<cfheader name="pragma" value="no-cache">
<cfheader name="cache-control" value="no-cache, no-store">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Expires" content="#dateformat(now(), 'ddd, dd mmm yyyy')# #timeformat(now(), 'HH:mm:ss tt')#">
<title>Mura CMS: How To Dynamically Add A File To Content Items</title>
<style type="text/css">
.wrap {
clear:both;
display:block;
padding:1em;
margin:1em;
border:1px dashed grey;
font-family:Arial, Helvetica, sans-serif;
font-size:0.8em;
}
.wrap label, .wrap input {
clear:both;
display:block;
}
.wrap label {
padding:1em 0 0 0;
}
</style>
</head>
<body>
<div class="wrap">
<h2>How To Dynamically Add A File To Content Items</h2>
<cfif form.isSubmitted and Len(form.thefile)>
<cfscript>
errors = [];
</cfscript>
<cfif parent.getIsNew()>
<h3>ERROR! Parent filename does not exist.</h3>
<p><a href="#CGI.script_name##CGI.query_string#">Return to form &gt;</a></p>
<cfabort />
</cfif>
<cfif form.istest>
<h3>Test Results <a href="#CGI.script_name##CGI.query_string#">Return to form &gt;</a></h3>
</cfif>
<cfscript>
// content = $.getBean('content').loadBy(filename = '#form.parentfilename#/' & utility.formatFilename(i.title));
content = $.getBean('content').loadBy(remoteid = form.thefile);
content.setTitle(ListLast(form.thefile, '/'));
content.setMenuTitle('');
content.setURLTitle('');
content.setHTMLTitle('');
content.setType('File'); // assume this is a file
content.setApproved(1);
content.setIsNav(0);
content.setParentID(parentid);
content.setRemoteID(form.thefile);
// This is the important field!
content.setNewFile(form.thefile);
if ( !form.istest ) {
content.save();
if ( !StructIsEmpty(content.getErrors()) ) {
ArrayAppend(errors, content.getErrors());
};
};
</cfscript>
<cfif !form.istest>
<h3>Completed! <a href="#CGI.script_name##CGI.query_string#">Return to form &gt;</a></h3>
</cfif>
<cfdump var="#content.getAllValues()#" />
<cfif ArrayLen(errors)>
<h4>ERRORS</h4>
<cfdump var="#errors#" label="ERRORS" />
</cfif>
<cfelse>
<form name="frmTest" method="post">
<label for="thefile">File Path:</label>
<input type="text" name="thefile" id="thefile" value="#form.thefile#" size="80" />
<small><em>Could also be a full URL to a file</em></small>
<label for="siteid">Site ID:</label>
<select name="siteid">
<cfloop query="rsSites">
<option value="#siteid#"<cfif form.siteid eq siteid> selected="selected"</cfif>>#HTMLEditFormat(site)#</option>
</cfloop>
</select>
<label for="parentfilename">Parent Filename:</label>
<input type="text" name="parentfilename" id="parentfilename" value="#form.parentfilename#" size="80" />
<label for="istest">Test?</label>
<select name="istest">
<option value="true"<cfif form.istest> selected="selected"</cfif>>Yes</option>
<option value="false"<cfif !form.istest> selected="selected"</cfif>>No</option>
</select>
<input type="hidden" name="isSubmitted" value="true" />
<p><input type="submit" value="Submit" /></p>
</form>
</cfif>
</div>
</body>
</html>
</cfoutput>
@stevewithington
Copy link
Author

You may also be interested in How To Dynamically Add Image To Content Items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment