BlogCFC Tweaks: View Entries By Category

With BlogCFC, there isn't an easy way to browse your entries by category. The closest you can get is to go to Entries put the category into the search. This actually wasn't too hard, once I figured out some of the more cryptic bits of some of the custom tags in BlogCFC.

Warning, this code is down and dirty. It works for me, and that is good enough for now.

First things first, I had to create the basic page to view the information. I achived this by hacking together a copy of category.cfm and some code from the main index page.


<cfsetting enablecfoutputonly="true">
<cfprocessingdirective pageencoding="utf-8">
<!---
    Name : viewByCategory.cfm
    Author : Jon Hartmann
    Created : Dec 3, 2008
    Last Updated :
    History :
--->


<cftry>
    <cfif url.id neq 0>
        <cfset cat = application.blog.getCategory(url.id)>
        <cfparam name="form.name" default="#cat.categoryname#">
        <cfparam name="form.alias" default="#cat.categoryalias#">
    <cfelse>
        <cfparam name="form.name" default="">
        <cfparam name="form.alias" default="">
    </cfif>
    <cfcatch>
        <cflocation url="categories.cfm" addToken="false">
    </cfcatch>
</cftry>

<cfset params["bycat"] = url.id />
<cfset entries = application.blog.getEntries(params) />

<cfmodule template="../tags/adminlayout.cfm" title="View Entries By Category">

    <cfoutput>
    <p>
    Your blog currently has #entries.recordcount# "#cat.categoryName#" <cfif entries.recordCount eq 1>entry<cfelse>entries</cfif>.
    </p>
    
    </cfoutput>

    <cfmodule template="../tags/datatable.cfm" data="#entries#" editlink="entry.cfm" label="Entries"
             linkcol="title" defaultsort="posted" defaultdir="desc" querystring="id=#url.id#">

        <cfmodule template="../tags/datacol.cfm" colname="title" label="Title" />
        <cfmodule template="../tags/datacol.cfm" colname="released" label="Released" format="yesno"/>
        <cfmodule template="../tags/datacol.cfm" colname="posted" label="Posted" format="datetime" />
        <cfmodule template="../tags/datacol.cfm" colname="views" label="Views" format="number" />
        <cfmodule template="../tags/datacol.cfm" label="View" data="<a href=""#application.rooturl#/index.cfm?mode=entry&entry=$id$"">View</a>" sort="false"/>
    </cfmodule>
</cfmodule>

<cfsetting enablecfoutputonly="false">

Now I need code to reach that page. Primarily I wanted to be able to get to it from the categories page, so I added in another column there:


...
<cfmodule template="../tags/datatable.cfm" data="#categories#" editlink="category.cfm" label="Categories"
         linkcol="categoryname" linkval="categoryid">

    <cfmodule template="../tags/datacol.cfm" colname="categoryname" label="Category" />
    <cfmodule template="../tags/datacol.cfm" colname="entrycount" label="Entries" />
    <cfmodule template="../tags/datacol.cfm" label="View Entries" data="<a href=""#application.rooturl#/admin/viewByCategory.cfm?id=$categoryid$"">View</a>" sort="false"/>
</cfmodule>
...

In order to get this to work though, I had to hack up the datatable.cfm custom tag. Starting on line 180 you'll find a list of substitutions to work with the $something$ notation seen above. There is a note from Ray Camden saying it needs to be dynamic, but since it isn't yet, I added the following code the list of conditionals:


<cfif findNoCase("$categoryid$", value)>
    <cfset value = replace(value, "$categoryid$", categoryid, "all")>
</cfif>

That got me up and running. Lastly, I wanted to add a link from the category.cfm page as well, since I thought that that might be useful. I added a link right in the middle of the form like so:



<form action="category.cfm?id=#url.id#" method="post">
<table>
    <tr>
        <td align="right">name:</td>
        <td><input type="text" name="name" value="#form.name#" class="txtField" maxlength="50"></td>
    </tr>
    <tr>
        <td align="right">alias:</td>
        <td><input type="text" name="alias" value="#form.alias#" class="txtField" maxlength="50"></td>
    </tr>
    <tr>
        <td> </td>
        <td><a href="viewByCategory.cfm?id=#url.id#">View Entries in this Category</a></td>
    </tr>
    <tr>
        <td> </td>
        <td><input type="submit" name="save" value="Save"> <input type="submit" name="cancel" value="Cancel"></td>
    </tr>
</table>
</form>

 

Related Blog Entries

Comments

Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me.

Post a job. Find one. authenticjobs.com

Interested in becoming a sponsor? Contact me.