Last active
November 16, 2020 21:52
-
-
Save pauldenato/705800b435607edaf12f to your computer and use it in GitHub Desktop.
Load Mura Media Player Content via CSV
This file contains 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
<!--- | |
Modified from Steve Withington's Import users via CSV: | |
https://gist.github.com/stevewithington/5051646 | |
---> | |
<cfscript> | |
param name='form.csvUrl' default=''; | |
param name='form.isSubmitted' default='false'; | |
param name='form.isTest' default='true'; | |
param name='form.siteid' default='default'; | |
param name='bulkUploadErrorEmail' default='{[email protected]}'; | |
$ = application.serviceFactory.getBean('$').init(form.siteid); | |
//This folder will live in the site folder -- this will check to see if it's an admin accessing the page | |
if ( !$.currentUser().isSuperUser() && !$.currentUser().isInGroup('admin') ) { | |
WriteOutput('<h3>You should not be here.</h3>'); | |
abort; | |
}; | |
errors = []; | |
//Get list of sites to populate dropdown menu | |
rsSites = $.getBean('settingsManager').getList(); | |
// Begin count to track updated content | |
invalidContent = 0; | |
</cfscript> | |
<!---get directory for looping to get the CSV files---> | |
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath())> | |
<!---create query to populate dropdown below with CSV files in the directory ---> | |
<cfdirectory directory="#currentDirectory#" name="dirQuery" action="LIST"> | |
<cfset dirsArray=arraynew(1)> | |
<cfset i=1> | |
<!---Get path for File Exists---> | |
<cfset thisPath = ExpandPath("*.*")> | |
<cfset thisDirectory = GetDirectoryFromPath(thisPath)> | |
<!---Create Directory Name of Folder housing Media files---> | |
<cfset mediaDirName = "media" /> | |
<!---Set Path for Media Directory You'll need to create this directory in the folder---> | |
<cfset mediaDir = "#thisDirectory#\#mediaDirName#\" /> | |
<!---if the form is submitted begin import process---> | |
<cfif form.isSubmitted and IsValid('url', form.csvUrl) > | |
<cftry> | |
<cfhttp name="rsContent" method="get" url="#form.csvUrl#" /> | |
<cfcatch> | |
<!---Something is wrong with the file---> | |
<cfset ArrayAppend(errors, 'The .CSV file either does not exist at the specified URL, or the file itself is not a valid .CSV file.')> | |
<cflog file="bulk-content-import" text="#cfcatch.message# - #cfcatch.detail#"> | |
<cfsavecontent variable="errortext"> | |
<cfoutput>An error occurred: http://#cgi.server_name##cgi.script_name#/#CGI.PATH_INFO#?#cgi.query_string#<br /> | |
Time:#dateFormat(now(), "short")##timeFormat(now(), "short")#<br /> | |
<cfdump var="#cfcatch#" label="CATCH"> | |
</cfoutput> | |
</cfsavecontent> | |
<!---Email Error to yourself---> | |
<cfmail to="#bulkUploadErrorEmail#" from="[email protected]" subject="Bulk Content Add" type="html"> | |
#errortext# | |
</cfmail> | |
</cfcatch> | |
</cftry> | |
</cfif> | |
<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>Import Content Into Mura CMS Via CSV</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; | |
} | |
.error { | |
border: 2px solid red; | |
padding: 1em; | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="wrap"> | |
<h2>Import Content Into Mura CMS Via CSV</h2> | |
<cfif form.isSubmitted and IsDefined('rsContent')> | |
<cfset tickStart = getTickCount()> | |
<!---Try to start the import---> | |
<cftry> | |
<!--Loop the CSV Query--> | |
<cfloop query="rsContent"> | |
<!---Parent Portal or Grandparent---> | |
<cfset mediaDataGrandParent = $.getBean('content').loadBy(title="{Create this content in Mura}")/> | |
<!---Media Content or Lesson Parent---> | |
<cfset mediaDataParent = $.getBean('content').loadBy(title="#rsContent.lesson#") /> | |
<!---Load empty content bean for Media (created remote id for updating purposes)---> | |
<cfset mediaData = $.getBean('content') /> | |
<!---Attempt to load by remote id---> | |
<cfset mediaDataLoad = mediaData.loadBy(remoteID="#rsContent.remoteID#") /> | |
<!---Load Parent Section of Training---> | |
<cfset mediaDataParent.setValue('siteID',form.siteID)/> | |
<cfset mediaDataParent.setValue('parentID',mediaDataGrandParent.getValue('contentID'))/> | |
<cfset mediaDataParent.setValue('contentID',mediaDataParent.getValue('contentID'))/> | |
<cfset mediaDataParent.setValue('title',rsContent.lesson)/> | |
<cfset mediaDataParent.setValue('summary','')/> | |
<cfset mediaDataParent.setValue('metakeywords',rsContent.lessonkeywords)/> | |
<cfset mediaDataParent.setValue('tags',rsContent.lessonkeywords)/> | |
<cfset mediaDataParent.setValue('approved',1) /> | |
<cfset mediaDataParent.setValue('type',"Portal") /> | |
<cfset mediaDataParent.setValue('display',1) /> | |
<cfset mediaData.setValue('orderNo',rsContent.lessonSort) /> | |
<cfset mediaDataParent.setValue('isNav',1) /> | |
<cfset mediaDataParent.setValue('inheritObjects',"inherit") /> | |
<!---Only save if not a test---> | |
<cfif not form.isTest> | |
<cfset mediaDataParent.save()> | |
</cfif> | |
<!---Load Course Section of Training---> | |
<cfset mediaData.setValue('siteID',form.siteID)/> | |
<cfset mediaData.setValue('parentID',mediaDataParent.getValue('contentID'))/> | |
<cfset mediaData.setValue('contentID',mediaDataLoad.getValue('contentID'))/> | |
<cfset mediaData.setValue('title',rsContent.topic)/> | |
<cfset mediaData.setValue('summary','<p>' &rsContent.description & '</p>')/> | |
<cfset mediaData.setValue('metakeywords',rsContent.keywords)/> | |
<cfset mediaData.setValue('tags',rsContent.tags)/> | |
<cfset mediaData.setValue('approved',1) /> | |
<cfset mediaData.setValue('type',"Page") /> | |
<!---Define Mura Mediaplayer subtype---> | |
<cfset mediaData.setValue('subType',"MuraMediaPlayer") /> | |
<cfset mediaData.setValue('display',1) /> | |
<cfset mediaData.setValue('orderNo',rsContent.topicSort) /> | |
<cfset mediaData.setValue('isNav',1) /> | |
<cfset mediaData.setValue('inheritObjects',"inherit") /> | |
<cfset mediaData.setValue('remoteID',rsContent.remoteID) /> | |
<!---MediaPlayer Requirements---> | |
<!---Only load media if it's new content - If a video updates note in the CSV doc and it will get updated---> | |
<cfif (mediaDataLoad.getIsNew() and fileExists("#mediaDir##rsContent.Mediafile#")) or rsContent.updateVideo EQ '1'> | |
<cfset mediaData.setValue('mediafile',"#getPageContext().getRequest().getScheme()#://#cgi.HTTP_HOST##getDirectoryFromPath(getPageContext().getRequest().getRequestURI())##mediaDirName#/" & rsContent.Mediafile)/> | |
</cfif> | |
<cfset mediaData.setValue('allowfullscreen',true) /> | |
<cfset mediaData.setValue('controlbar','bottom') /> | |
<cfset mediaData.setValue('playerskin','vimeo') /> | |
<cfset mediaData.setValue('videodimensions','550x408') /> | |
<cfset mediaData.setValue('stretching','uniform') /> | |
<!---Only save if not a test---> | |
<cfif not form.isTest> | |
<cfset mediaData.save()> | |
</cfif> | |
<cfif not mediaDataLoad.getIsNew()> | |
<!---Create count to subtract from records for items not loaded but only updated.---> | |
<cfset invalidContent = invalidContent+1 /> | |
</cfif> | |
</cfloop> | |
<cfcatch> | |
<cfset ArrayAppend(errors, "Theres been an error, no content was loaded. #mediaDir#") /> | |
<cflog file="bulk-content-import" text="#cfcatch.message# - #cfcatch.detail#"> | |
<cfsavecontent variable="errortext"> | |
<cfoutput> An error occurred: http://#cgi.server_name##cgi.script_name#/#CGI.PATH_INFO#?#cgi.query_string#<br /> | |
Time: #dateFormat(now(), "short")# #timeFormat(now(), "short")#<br /> | |
<cfdump var="#cfcatch#" label="CATCH"> | |
</cfoutput> | |
</cfsavecontent> | |
<!---Email error to yourself---> | |
<cfmail to="#bulkUploadErrorEmail#" from="[email protected]" subject="Bulk Content Add" type="html"> | |
#errortext# | |
</cfmail> | |
</cfcatch> | |
</cftry> | |
<cfif form.isTest> | |
<cfset textFile = fileExists('#mediaDir##rsContent.Mediafile#')/> | |
<h3>Test Results <a href="#CGI.script_name##CGI.query_string#">Return to form ></a></h3> | |
<cfdump var="#rsContent#" label="rsContent"> | |
<h4>#rsContent.recordcount-invalidContent# Content Imported and #invalidContent# Content Updated</h4> | |
<cfelse> | |
<h3>Completed! <a href="#CGI.script_name##CGI.query_string#">Return to form ></a></h3> | |
<h4>#rsContent.recordcount-invalidContent# Content Imported and #invalidContent# Content Updated</h4> | |
</cfif> | |
<cfset tickEnd = getTickCount()> | |
<p><em>Processed in #tickEnd-tickStart# milliseconds</em></p> | |
<cfelse> | |
<form name="frmUser" method="post"> | |
<label for="csvurl">CSV URL:</label> | |
<select name="csvurl" id="csvurl" > | |
<cfloop query="dirQuery"> | |
<cfif dirQuery.type IS "file" and dirQuery.name contains ".csv"> | |
<cfset dirsArray[i]=dirQuery.name> | |
<cfset i = i + 1> | |
<option value="#getPageContext().getRequest().getScheme()#://#cgi.HTTP_HOST##getDirectoryFromPath(getPageContext().getRequest().getRequestURI())##dirQuery.name#">#dirQuery.name#</option> | |
</cfif> | |
</cfloop> | |
<cfif i LTE 1> | |
<option value="#getPageContext().getRequest().getScheme()#://#cgi.HTTP_HOST##getDirectoryFromPath(getPageContext().getRequest().getRequestURI())#nocsv.csv" selected="selected">No CSV file present</option> | |
</cfif> | |
</select> | |
<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="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> | |
<!--- ERROR OUTPUT ---> | |
<cfif ArrayLen(errors)> | |
<div class="alert error"> | |
<h4>Error | |
<cfif ArrayLen(errors) gt 1> | |
s | |
</cfif> | |
</h4> | |
<ul> | |
<cfloop array="#errors#" index="error"> | |
<li> | |
<cfif IsSimpleValue(error)> | |
#error# | |
<cfelse> | |
<cfdump var="#error#" /> | |
</cfif> | |
</li> | |
</cfloop> | |
</ul> | |
</div> | |
</cfif> | |
</div> | |
</body> | |
</html> | |
</cfoutput> |
This file contains 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
Lesson | LessonSort | LessonKeywords | Topic | TopicSort | Description | Keywords | Tags | Mediafile | remoteID | updateVideo | |
---|---|---|---|---|---|---|---|---|---|---|---|
Getting Started | 1 | fringilla, est nec, molestie, laoreet, Mauris, convallis congue, ex, sed, aliquam ,tempus, ac, at, nisl | Lorem Ipsom | 2 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam fringilla est nec molestie laoreet. | fringilla, est nec, molestie, laoreet | fringilla, est nec, molestie, laoreet | Media_1-1.mp4 | MediaP1-1.1 | 0 | |
Getting Started | 1 | fringilla, est nec, molestie, laoreet, Mauris, convallis congue, ex, sed, aliquam ,tempus, ac, at, nisl | Donec Lobortis | 3 | Donec lobortis eget elit ut porttitor. Mauris convallis congue ex sed aliquam. | Mauris, convallis congue, ex, sed, aliquam | Mauris, convallis congue, ex, sed, aliquam | Media_1-2.mp4 | MediaP1-1.2 | 0 | |
Getting Started | 1 | fringilla, est nec, molestie, laoreet, Mauris, convallis congue, ex, sed, aliquam ,tempus, ac, at, nisl | Vestibulum Ut | 4 | Vestibulum ut dolor sed massa venenatis tempus ac at nisl? Nulla suscipit maximus neque ut pharetra. | tempus, ac, at, nisl | tempus, ac, at, nisl | Media_1-3.mp4 | MediaP1-1.3 | 0 | |
Next Steps | 2 | adipiscing, elit, sed, aliquam, neque, ut, pharetra | Consectetur Adipiscing | 2 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam fringilla est nec molestie laoreet. | adipiscing, elit | adipiscing, elit | Media_2-1.mp4 | MediaP1-2.1 | 0 | |
Next Steps | 2 | adipiscing, elit, sed, aliquam, neque, ut, pharetra | Mauris Convallis | 3 | Donec lobortis eget elit ut porttitor. Mauris convallis congue ex sed aliquam. | sed, aliquam | sed, aliquam | Media_2-2.mp4 | MediaP1-2.2 | 0 | |
Next Steps | 2 | adipiscing, elit, sed, aliquam, neque, ut, pharetra | Nulla Suscipit | 4 | Vestibulum ut dolor sed massa venenatis tempus ac at nisl? Nulla suscipit maximus neque ut pharetra. | neque, ut, pharetra | neque, ut, pharetra | Media_2-3.mp4 | MediaP1-2.3 | 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment