Bartram's Bits

Tuesday, May 13, 2008

Blogger Labels in CF Part 2

A few days ago I posted a simple solution for listing Blogger Labels in a classic template using ColdFusion. I wasn't happy with just listing the Labels though, I wanted to know how many entries were in each Label. The code that follows is my solution:

<cfscript>
// Returns number of blog entries in a Blogger Label file
function CountLabels(param) {
ct = 0;
ptr = FindNoCase('<div class="post">', param) + 1;
while (ptr gt 1) {
ct = ct + 1;
ptr = FindNoCase('<div class="post">', param, ptr) + 1;
}
return ct;
}
</cfscript>

<!--- This is the first time running, so we need to create the structures in the application scope --->
<cfif Not(StructKeyExists(application, "structLabels"))>
<cfset application.structLabels = StructNew()>
<cfset application.structEntries = StructNew()>
</cfif>

<!--- Get a directory listing of the Labels folder --->
<cfset pathLabels = "#getDirectoryFromPath(getBaseTemplatePath())#labels\">
<cfdirectory name="dirLabels" action="list" directory="#pathLabels#" sort="name asc">

<cfloop query="dirLabels">
<!--- This is the first time running, so we need to count the number of entries for this Label --->
<cfif Not(StructKeyExists(application.structLabels, dirLabels.name))>
<cffile action="read" file="#pathLabels#\#dirLabels.name#" variable="fileLabel">
<cfset ctLabel = CountLabels(fileLabel)>
<cfset StructInsert(application.structLabels, dirLabels.name, dirLabels.dateLastModified)>
<cfset StructInsert(application.structEntries, dirLabels.name, ctLabel)>
<!--- The Labels file was updated, so we need to recount the number of entries for this Label --->
<cfelseif StructFind(application.structLabels, dirLabels.name) neq dirLabels.dateLastModified>
<cffile action="read" file="#pathLabels#\#dirLabels.name#" variable="fileLabel">
<cfset ctLabel = CountLabels(fileLabel)>
<cfset StructUpdate(application.structLabels, dirLabels.name, dirLabels.dateLastModified)>
<cfset StructUpdate(application.structEntries, dirLabels.name, ctLabel)>
</cfif>
</cfloop>

<ul>
<cfloop collection="#application.structEntries#" item="key">
<!--- If there are entries in a Label file, then list it as a link with the total number of entries --->
<cfif StructFind(application.structEntries,key)>
<li><cfoutput><a href="/blog/labels/#key#">#ListFirst(key,".")#</a> (#StructFind(application.structEntries,key)#)</cfoutput></li>
</cfif>
</cfloop>
</ul>

Labels: ,

Saturday, May 10, 2008

Labels in Blogger Classic Template using ColdFusion

Blogger finally got its act together a while back and introduced Labels as a way of tagging or creating categories for blogs. However, they only half-way implemented it for Classic Templates. Each posting lists the Labels that have been assigned to it, and a folder called Labels stores a file listing all the entries for each Label exists. BUT, Blogger doesn't have a $BlogLabel$ tag to display the list of Labels in the navigation column. What's a ColdFusion developer to do? Roll his own!

<cfset pathLabels = "#getDirectoryFromPath(getBaseTemplatePath())#labels\">
<cfdirectory name="dirLabels" action="list" directory="#pathLabels#" sort="name asc">

<ul>
<cfoutput query="dirLabels">
<cffile action="read" file="#pathLabels#\#dirLabels.name#" variable="fileLabel">
<li><a href="/blog/labels/#dirLabels.name#">#ListFirst(dirLabels.name,".")#</a></li>
</cfoutput>
</ul>

First, I build the path to the labels folder using ColdFusion functions enabling the code to run in both my development and live environments. Next, the CFDIRECTORY tag returns a list of the files in the Labels folder in a query structure. Looping through the file names using CFOUTPUT tag, I assemble each list element as a link. With the ListFirst function I can treat the filename as a list with the period as the separator and return the filename without an extension giving me the name of the Label.

I store the following code in /blog-cf/labels.cfm and in my Blogger template, I use the following code to call it:

<h2 class="sidebar-title">Labels</h2>
<cfinclude template="/blog-cf/labels.cfm">

Labels: ,

Tuesday, May 29, 2007

Submit CFCALENDAR onchange

I'm building an application that needs a calendar picker tool and I decided to check out the CFCALENDAR form control. It turns out that since it is a flash component, the onchange event uses ActionScript rather than JavaScript. This proved a bit frustrating for me since I knew zero ActionScript going into this and I was initially unable to find any help online. I eventually came across a page, Passing Variables Around - Flash to HTML and Back that talked about how to emulate a METHOD="GET" submit in ActionScript. I then learned about how AS names fields and some basic date functions and came up with the following code:

<cfparam name="TestDate" default="#Now()#">
<cfoutput>#TestDate#</cfoutput>
<cfform name="TestForm" id="TestForm" action="cfcalendar.cfm">
<cfcalendar name="TestDate" selecteddate="#DateFormat(TestDate,'mm/dd/yyyy')#"
onchange="getURL('cfcalendar.cfm?TestDate=' + (TestDate.selectedDate.getMonth()+1) + '/' +
TestDate.selectedDate.getDate() + '/' + TestDate.selectedDate.getFullYear());">
</cfform>

The key was to reference the field name selectedDate rather than TestDate, otherwise you keep passing the original date. I then needed to reassemble the date into a format that ColdFusion would understand using the getMonth, getDate, and getFullYear functions. Also I needed to add one to the result of getMonth since ActionScript starts counting months at 0 instead of 1.

Labels:

Wednesday, May 16, 2007

Development Directory Structure

While implementing version control, I changed several things in my development environment. One of those things is the directory structure. I did some reading online, especially ColdFusion MX Coding Guidelines - Structure: Application, Component, Tag etc (scroll down to the Directory Structure For Applications section). Most of what everybody else talked about referred back to this guide. The big question that I had as a developer was; how to handle multiple sites? The apparent consensus is to place each application in wwwroot whose parent is cfmxroot. Each application has its own webroot where the .cfm files go and other non-web folders. The overall development environment then has folders for supporting files within the cfmxroot. The following is the expanded structure including some folders which I probably won't use, but are there if I need them:
C:\cfmxroot
config
extensions
components
customtags
includes
wwwroot
appname
db
extensions
components
customtags
includes
webroot
images
shared
css
documents
images
js
services

Labels:

Thursday, May 10, 2007

New in CFMX 6&7: What did you miss?

At lunch today I attended Charlie Arehart's CFUnited preview presentation, New in CFMX 6&7: What did you miss? online a la the ColdFusion Meetup Group. It looks like I'm really going to miss out by not attending this conference! Charlie has provided a loooooong list of both major features of CFMX 6 & 7 as well as not so major enhancements in CFMX 6 (no list for 7). Being only 30 minutes he was not able to go into detail on any of these items, but he sure wetted my appetite and gave me a lot to research. He started with an example of an enhancement that came out way back in ColdFusion 4.01 that is not widely known: ListQualify(), for example, can convert a list such as "red,blue,green" to 'red','blue','green' for use with a SQL clause WHERE IN(...)

ColdFusion MX 6 Major Features
ColdFusion Components (CFCs)
Web Services
Flash Remoting
XML

ColdFusion MX 7 Major Features
Great User Experience
- Printable Web Content
- Structured Business Reports
- Flash Forms and Controls
- XML Forms
- New CFCHART engine
- Ready-to-use chart styles
- Improved text searching capabilities
Rapid Development
- Dreamweaver extensions
- Improved form validation
- Parameter validation
- Strong encryption
- Integrated NT Domain authentication
- Improved XML functionality
- In-memory Query Metadata support
- Debugging improvements
- Cross-site Scripting attack prevention
- Application event traps
- Enhanced Web Services support
Flexible Deployment
- Enterprise Manager
- EAR/WAR deployment
- Sourceless deployment
- Administrator API
- License Usage Scanner
New Classes of Application Development
- SMS Gateway
- Lotus Sametime Gateway
- XMPP Gateway
- File System Gateway
- CFML Aynchronous Gateway
- Extensible Gateway Architecture

More than just the "major" features: Other big CFMX 6 enhancements
Build tag-based UDFs with CFFUNCTION
Enable user authentication and autorization with CFLOGIN
Implement Developer Resource Controls with Sandbox/Resource Security
Dynamic charting with CFCHART
Improved debugging and tracing with CFTRACE
Internationalization of your applications
Several Regular Expression (RE) support changes
CFTHROW can now throw an object
CFLOG now always writes thread, date, and time
CFHTTP reading text file in as query, new FirstRowAsHeaders attribute
Verity K2 Server Improvements
New commenting flexibility
Still more CFMX 6 enhancements
- Dreamweaver MX
- Database connectivity
- Query of Query enhancements
- Several CFLDAP changes
- CFMAIL spooling option changes
- Sort order in ListSort, ArraySort
- Date processing functions have short, medium, long formats
- Localization functions now follow Java standard rules
Some CFMX 6 hidden gems
- CFLOCATION fixed to properly set cookies
- Compilation/PreCompilation of CF Templates
- Extensibility of Built-in Web Server
- CFIMPORT of CF Custom Tags
- Sever-side Redirect forwarding
- CFMX Tag Updater for CF Studio AKA HomeSite Plus
- CFSETTING RequestTimeOut
- Optional UUID for CFTOKEN
- UrlSessionFormat() Function
- Exception Handling in CFSCRIPT
- Evaluation within expressions
- New variable scopes available as structures
- Appending arrays to one another

Resources for learning more

ColdFusion MX 7 Upgrade Guide
CF7 Migration Guide
CF 7 Evaluator's Guide
Edition Comparison Matrix (5,6,7)

Labels:

ColdFusion 8 Preview

Ben Forta came to the cccfug on May 7 as part of his Scorpio User Group Tour to present a sneak peak of ColdFusion 8. The gathering was quite crowded and very enthusiastic. Ben spoke for 2.5 hours on the following key items grouped into 3 categories:

Developers Section
CFIMAGE
AJAX
FckEditor
Yahoo Calendar Control
CFWINDOW
CFFORM binds
low level CFFILE handling
CFTHREAD
CFDOCUMENT enhancements
Reporting enhancements
CFC Interfaces
Argument collections - use a structure to pass default settings to any cf tag
Implicit Array & Structure creation
Javascript operators in CFSCRIPT
Eclipse Debugger

Integration Section
CFOBJECT invokes .NET Assemblies within the CLR
* ColdFusion can talk to ColdFusion, Java, and .NET Objects all at once
MS Echange - full client control
CFPRESENTATION creates Adobe Connect Breezos
Flash Media Server Gateway
CFFEED - Atom and RSS
CFPDF - PDF manipulation
CFPDFFORM - populate and extract data in PDF forms

Management and Administration
Server Monitoring API and Flex front-end
User-based Admin & RDS
Per application settings

Labels: