Click here to Skip to main content
Licence CPOL
First Posted 9 Jun 2010
Views 6,941
Bookmarked 0 times

N2CMS Forum Addon: Fixing the Theme

By Martin Jarvis | 9 Jun 2010 | Technical Blog
I've recently been working with the excellent n2cms project. However, I've noticed a bug with the Forum Addon. The first page would be correctly themed but any subsequent pages were not when running against a build of the latest N2CMS.

1

2

3

4
1 vote, 100.0%
5
5.00/5 - 1 vote
μ 5.00, σa 5.00 [?]
A Technical Blog article. View original blog here.[^]

I've recently been working with the excellent n2cms project. However, I've noticed a bug with the Forum Addon. The first page would be correctly themed but any subsequent pages were not when running against a build of the latest N2CMS.

On investigation, it seems that the Forum Pages CurrentItem was resolving to null which prevented the ThemeConcern for assigning the correct theme:

 namespace N2.Templates.Web
 {
     /// <summary>
     /// Sets the theme of the page template.
     /// </summary>
     [Service(typeof(TemplateConcern))]
     public class ThemeConcern : TemplateConcern
     {
         public override void OnPreInit(ITemplatePage template)
         {
             var item = template.CurrentItem;
             if (item == null)
                 return; // <=== D'oh!  This is why the forum has no theme!
             /* Implementation doesn't matter here as it will never run! */
         }
     }
 }

Looking into the N2.Templates.dll that comes the Forum Addon, it looks like things were handled differently back in the day, with the N2.Templates.Web.ThemeModifier just using the theme for that start page:

 public void Modify<T>(TemplatePage<T> page) where T: AbstractPage
 {
     string theme = GenericFind<ContentItem, StartPage>.StartPage.Theme;
     if ((!this.themeVerified && (theme != null)) && 
	Directory.Exists(page.Server.MapPath("~/App_Themes/" + theme)))
     {
         this.themeVerified = true;
     }
     if (this.themeVerified)
     {
         page.Theme = theme;
     }
 }
 /* Taken from Reflector
 public void Modify<T>(TemplatePage<T> page) where T: AbstractPage;
 Declaring Type: N2.Templates.Web.ThemeModifier 
 Assembly: N2.Templates, Version=1.0.403.25743 
 */

So, the fix is to make the forum page take the theme from the start page, without affecting other page types… tricky whilst maintaining separation of concerns… hmm.

The Fix

The fix has to belong with the forum addon so that the change in behaviour doesn’t knacker the standard n2cms features. N2CMS populates the CurrentItem property based on the current URL and as the Forum Plugin is generating links directly to its core template (/Forum/UI/Views/Forum.aspx), this is causing a problem.

Fortunately, the YAF Framework provides an extension point we can use to fix the problem yaf.IUrlBuilder. This is a simple interface which takes the query string parameters and returns the appropriate link.

using System.Web;
namespace N2.Templates.Forum
{
    /// <summary>
    /// Build links from the current page
    /// </summary>
    public class N2UrlLinkBuilder : yaf.IUrlBuilder
    {
        #region IUrlBuilder Members
        /// <summary>
        /// Builds the URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public string BuildUrl(string url)
        {
            return string.Format("{0}?{1}", 
		HttpContext.Current.Request.RawUrl.Split('?')[0], url);
        }
        #endregion
    }
}

Unfortunately, YAF doesn’t allow you to set the link builder via configuration – it has to be done programmatically. So you’ll need to amend ‘/Forum/UI/Views/Forum.aspx’ to ensure it is set:

namespace N2.Templates.Forum.UI.Views
{
    public partial class Forum : N2.Templates.Web.UI.TemplatePage<Items.Forum>
    {
        protected override void OnPreInit(EventArgs e)
        {
            if (!(yaf.Config.IsRainbow || yaf.Config.IsDotNetNuke || 
		yaf.Config.IsPortal || 
		yaf.Config.EnableURLRewriting == "true")) 	// based on logic 
							// found in yaf.Config
                HttpContext.Current.Application["yaf_UrlBuilder"] = 
						new N2UrlLinkBuilder();
            base.OnPreInit(e);
        }
    }
}

Another possible workaround is to investigate the UrlRewriting… but this looks too involved for the time available.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Martin Jarvis

Software Developer (Senior)
Freestyle Interactive Ltd
United Kingdom United Kingdom

Member

Follow on Twitter Follow on Twitter
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.
 
I've been developing .Net applications (both Windows and Web) since 2002.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks.. This helps PinmemberJonTampa10:25 16 Jun '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 9 Jun 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid