Last active
December 2, 2020 10:39
-
-
Save stevewithington/b3178f2de47ea55147cb to your computer and use it in GitHub Desktop.
Mura CMS : How To Import A Directory Of Files Into Mura As Content Items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// This will NOT work unless you have 'allowlocalfiles=true' in /config/settings.ini.cfm | |
// example path to files | |
try { | |
filepath = ExpandPath('./files'); | |
} catch(any e) { | |
filepath = ExpandPath('./'); | |
} | |
param name='form.directorypath' 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'; | |
validFilePath = true; | |
$ = application.serviceFactory.getBean('$').init(form.siteid); | |
if ( !$.currentUser().isSuperUser() && !$.currentUser().isInGroup('admin') ) { | |
WriteOutput('<h3>You should not be here.</h3>'); | |
abort; | |
} | |
// RSFiles | |
try { | |
rsfiles = DirectoryList(filepath,false,'query'); | |
} catch(any e) { | |
validFilePath = false; | |
} | |
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 Import A Directory Of Files Into Mura As 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 Import A Directory Of Files Into Mura As Content Items</h2> | |
<cfif validFilePath and form.isSubmitted and Len(form.directorypath)> | |
<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 ></a></p> | |
<cfabort /> | |
</cfif> | |
<cfif form.istest> | |
<h3>Test Results <a href="#CGI.script_name##CGI.query_string#">Return to form ></a></h3> | |
</cfif> | |
<cfscript> | |
WriteOutput('<h4>Files to be imported:</h4>'); | |
WriteDump(var=rsfiles); | |
for ( f in rsfiles ) { | |
thefilepath = f.Directory & '/' & f.Name; | |
content = $.getBean('content').loadBy(remoteid = thefilepath); | |
content.setTitle(ListLast(thefilepath, '/')); | |
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(thefilepath); | |
content.setNewFile(thefilepath); | |
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 ></a></h3> | |
</cfif> | |
<cfif ArrayLen(errors)> | |
<h4>ERRORS</h4> | |
<cfdump var="#errors#" label="ERRORS" /> | |
</cfif> | |
<cfelse> | |
<cfif not validFilePath> | |
<h4>Please use a valid Directory Path</h4> | |
</cfif> | |
<form name="frmTest" method="post" onsubmit="return processForm(this);"> | |
<label for="directorypath">Directory Path:</label> | |
<input type="text" name="directorypath" id="directorypath" value="#form.directorypath#" size="80" /> | |
<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 id="btnSubmit" type="submit" value="Submit"/></p> | |
</form> | |
</cfif> | |
</div> | |
<script> | |
function processForm(f) { | |
var b = document.getElementById('btnSubmit'); | |
b.disabled = true; | |
b.value = 'Please wait ...'; | |
return true; | |
} | |
</script> | |
</body> | |
</html> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment