User-Defined Function (UDF) Using CFSwitch and CFIF to Dynamically Generate a Document Icon
Have you ever needed (or wanted) to dynamically generate an icon that represents the document type in your document management system? Maybe you just didn't have the time (or want to take the time) to write a user-defined function (UDF) that would accomplish this. Well, have no fear because I've already started the process for you.
In order to use the UDF, you will need a couple of items:
- A database with a table named "tblDocuments"
- The table should have at least two columns:
- DocID (the primary key)
- DocFilename (contains full name of the document including extension)
- Document icons stored in the "/assets/icons/" directory
- The UDF listed below
- Update <cfquery name="getDocType" datasource="#APPLICATION.dsn#"> with your own datasource
<!------------------------------------------------------------------------------------------
Document: /extensions/includes/getIconFromExt.cfm
Author: steve@stephenwithington.com (sjw)
Creation Date: May 7, 2008
Copyright: (c) 2008 Stephen J. Withington, Jr. | http://www.stephenwithington.com
Purpose: User-Defined Function (UDF) which queries tblDocuments to obtain
file extension to generate icon to use.
METHOD(s)/VAR: 1) getIconResult | returnIcon
Example Usage: <img src="/assets/icons/#getIconResult(DocID)#" border="0" />
ASSUMPTIONS:
a) icons listed in the CFSwitch below are stored in
the directory '/assets/icons/'
b) database with a table named tblDocuments
c) at least two (2) columns:
1) DocID (primary key)
2) DocFilename (contains full name of the document including extension)
NOTES: This UDF does NOT include all available document types. If another
document type is needed, simply add the icon to the "icons" folder and
create a new CFCase. If the three (3) right-most characters of the extension
are already in use, then simply nest a CFIF statement similar to DOTM and POTM.
Revision Log:
MM/DD/YYYY - sjw - comments.
-------------------------------------------------------------------------------------------->
<cffunction name="getIconResult" access="public" returntype="string" output="no">
<!---
DocID is required
--->
<cfargument name="DocID" type="string" required="yes" />
<!---
Scope variable
--->
<cfset returnIcon = "" />
<!---
perform the query
--->
<cfquery name="getDocType" datasource="#APPLICATION.dsn#">
SELECT DocID, DocFilename
FROM dbo.tblDocuments
WHERE DocID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ARGUMENTS.DocID#" />
</cfquery>
<!---
Check the file extension and assign an icon and ALT tag
--->
<cfswitch expression="#right(getDocType.DocFilename, 3)#">
<!--- DOC --->
<cfcase value="doc">
<cfset returnIcon = "doc.gif" & """ alt=""DOC" />
</cfcase>
<!--- DOCM --->
<cfcase value="ocm">
<cfset returnIcon = "docm.gif" & """ alt=""DOCM" />
</cfcase>
<!--- DOCX --->
<cfcase value="ocx">
<cfset returnIcon = "docx.gif" & """ alt=""DOCX" />
</cfcase>
<!--- DOTM and POTM --->
<!--- warning: this is the same as POTM (powerpoint template master for office 2007) --->
<cfcase value="otm">
<cfif right(getDocType.DocFilename, 4) EQ "dotm">
<cfset returnIcon = "dotm.gif" & """ alt=""DOTM" />
<cfelse>
<cfset returnIcon = "potm.gif" & """ alt=""DOTM" />
</cfif>
</cfcase>
<!--- DOTX and POTX --->
<!--- warning: this is the same as POTX (powerpoint template for office 2007) --->
<cfcase value="otx">
<cfif right(getDocType.DocFilename, 4) EQ "dotx">
<cfset returnIcon = "dotx.gif" & """ alt=""DOTX" />
<cfelse>
<cfset returnIcon = "potx.gif" & """ alt=""POTX" />
</cfif>
</cfcase>
<!--- HTM --->
<cfcase value="htm">
<cfset returnIcon = "htm.gif" & """ alt=""HTM" />
</cfcase>
<!--- HTM --->
<cfcase value="tml">
<cfset returnIcon = "htm.gif" & """ alt=""HTML" />
</cfcase>
<!--- JPG--->
<cfcase value="jpg">
<cfset returnIcon = "jpg.gif" & """ alt=""JPG" />
</cfcase>
<!--- JPEG --->
<cfcase value="peg">
<cfset returnIcon = "jpg.gif" & """ alt=""JPEG" />
</cfcase>
<!--- MDB --->
<cfcase value="mdb">
<cfset returnIcon = "mdb.gif" & """ alt=""MDB" />
</cfcase>
<!--- MOV --->
<cfcase value="mov">
<cfset returnIcon = "mov.gif" & """ alt=""MOV" />
</cfcase>
<!--- PDF --->
<cfcase value="pdf">
<cfset returnIcon = "pdf.gif" & """ alt=""PDF" />
</cfcase>
<!--- PPAM --->
<cfcase value="pam">
<cfset returnIcon = "ppam.gif" & """ alt=""PPAM" />
</cfcase>
<!--- PPS --->
<cfcase value="pps">
<cfset returnIcon = "pps.gif" & """ alt=""PPS" />
</cfcase>
<!--- PPSM --->
<cfcase value="psm">
<cfset returnIcon = "ppsm.gif" & """ alt=""PPSM" />
</cfcase>
<!--- PPSX --->
<cfcase value="psx">
<cfset returnIcon = "ppsx.gif" & """ alt=""PPSX" />
</cfcase>
<!--- PPT --->
<cfcase value="ppt">
<cfset returnIcon = "ppt.gif" & """ alt=""PPT" />
</cfcase>
<!--- PPTM --->
<cfcase value="ptm">
<cfset returnIcon = "pptm.gif" & """ alt=""PPTM" />
</cfcase>
<!--- PPTX --->
<cfcase value="ptx">
<cfset returnIcon = "pptx.gif" & """ alt=""PPTX" />
</cfcase>
<!--- RTF --->
<cfcase value="rtf">
<cfset returnIcon = "rtf.gif" & """ alt=""RTF" />
</cfcase>
<!--- TXT --->
<cfcase value="txt">
<cfset returnIcon = "rtf.gif" & """ alt=""RTF" />
</cfcase>
<!--- WMV --->
<cfcase value="wmv">
<cfset returnIcon = "wmv.gif" & """ alt=""WMV" />
</cfcase>
<!--- XLAM --->
<cfcase value="lam">
<cfset returnIcon = "xlam.gif" & """ alt=""XLAM" />
</cfcase>
<!--- XLS --->
<cfcase value="xls">
<cfset returnIcon = "xls.gif" & """ alt=""XLS" />
</cfcase>
<!--- XLSB --->
<cfcase value="lsb">
<cfset returnIcon = "xlsb.gif" & """ alt=""XLSB" />
</cfcase>
<!--- XLSM --->
<cfcase value="lsm">
<cfset returnIcon = "xlsm.gif" & """ alt=""XLSM" />
</cfcase>
<!--- XLTM --->
<cfcase value="ltm">
<cfset returnIcon = "xltm.gif" & """ alt=""XLTM" />
</cfcase>
<!--- XLSX --->
<cfcase value="lsx">
<cfset returnIcon = "xlsx.gif" & """ alt=""XLSX" />
</cfcase>
<!--- ZIP--->
<cfcase value="zip">
<cfset returnIcon = "zip.gif" & """ alt=""ZIP" />
</cfcase>
<!--- DEFAULT --->
<cfdefaultcase>
<cfset returnIcon = "rtf.gif" & """ alt=""" />
</cfdefaultcase>
</cfswitch>
<cfreturn returnIcon />
</cffunction>
You can either add missing document icons or even remove unnecessary document icons to/from the UDF if needed.
In order to use the UDF, simply include the file using <cfinclude template="/extensions/includes/getIconFromExt.cfm" /> (update the template path to where you place the UDF in your site) in the page where you want to use this. Then in your query output, place <img src="/assets/icons/#getIconResult(DocID)#" border="0" /> next to the document title, etc.
Did this help you? Please let me know.



There are no comments for this entry.
[Add Comment]