Skip to content

Instantly share code, notes, and snippets.

@jsieber
Forked from stevewithington/muraSaveImage.cfm
Last active August 29, 2015 14:08
Show Gist options
  • Save jsieber/ae3091ab28347a2ab6aa to your computer and use it in GitHub Desktop.
Save jsieber/ae3091ab28347a2ab6aa to your computer and use it in GitHub Desktop.
<cfscript>
// This will NOT work unless you have 'allowlocalfiles=true' in /config/settings.ini.cfm
// read image info on local file for default value
try {
ir = ImageRead('steve-withington.jpg');
img = ir.source;
} catch(any e) {
img = '';
}
param name='form.newimg' default=img;
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 Image 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 Image To Content Items</h2>
<cfif form.isSubmitted and Len(form.newimg)>
<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(remoteid = form.newimg);
content.setTitle(ListLast(form.newimg, '/'));
content.setMenuTitle('');
content.setURLTitle('');
content.setHTMLTitle('');
content.setApproved(1);
content.setIsNav(0);
content.setParentID(parentid);
content.setRemoteID(form.newimg);
// This is the important field!
content.setNewFile(form.newimg);
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="newimg">Image Path:</label>
<input type="text" name="newimg" id="newimg" value="#form.newimg#" size="80" />
<small><em>Could also be a full URL to an image</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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment