Extension Manager

What is Extension

To understand what Extension Manager is, you first need to understand what is an extension. In the BlogEngine, any class marked with [Extension] attribute is an extension. What this attribute does is actually pretty simple - it insures that instance of this class will be created and loaded in the application process on application start. This let you hook to application events in the constructor and handle these events in the extension code. Suppose, you want to check for web URL in the comments and, if found any, automatically replace it with hyper link. To do this, you would first register event listener in the extension constructor:

[Extension("My extension", "1.0", "Me")]
public class MyExtension
{
    public MyExtension()
    {
        Comment.Serving += new EventHandler<ServingEventArgs>(Post_CommentServing);
    }
    private void Post_CommentServing(object sender, ServingEventArgs e)
    {
        string body = e.Body;
        //TODO: search for URL in the comment and replace it with link
    }
}

Now you just search and replace to implement your function and you done. This is how most extensions operate and there are tons of events to subscribe to in the BlogEngine.

What is Extension Manager

Extension Manager is a component in the BlogEngine that:

  1. Provides graphical interface to view and configure extensions.
  2. Enables input and management of extensions data (settings) through customizable form.
  3. Let extensions save and retrieve data (settings) to and from data storage through API calls.

Some of the functionality provided out of the box – your extension will be listed in the admin console, source code will be accessible by clicking “view” link and extension can be disabled or enabled with a click of a button. Other functions require extension author to write code.

Why do you need Extension Manager

You might not need it at all. Again, your extension will pop up in the Admin under Extensions tab and blogger will be able to turn it on/off, see information (that you supply in the attribute tag) and look at the code. That Manager will do automatically. But there are two scenarios when you might really need it: you want to collect user input and you want to be able save and get back some data. Sure, you can implement this on your own, but it would probably be more intensive than writing extension itself. With extension manager, most tasks will take few lines of code.

Collecting user input

You want to write an extension that will block "bad" words in the comment. Obviously, you want blogger to decide words to be blocked, so you have to provide user input form to enter these words and then save them for use in your extension. The code below, as simple as it is, does it all.

using System;
using System.Data;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
 
[Extension("Bad words", "1.0", "<a href=\"http://me.net\">Me</a>")]
public class BadWords
{
    public BadWords()
    {
        Comment.Serving += new EventHandler<ServingEventArgs>(Post_CommentServing);
        ExtensionSettings settings = new ExtensionSettings("BadWords");
        settings.AddParameter("Word");
        settings.AddValue("Word", "frack");
        ExtensionManager.ImportSettings(settings);
    }
 
    private void Post_CommentServing(object sender, ServingEventArgs e)
    {
        string body = e.Body;
        DataTable table = ExtensionManager.GetSettings("BadWords").GetDataTable();
        foreach (DataRow row in table.Rows)
        {
            body = body.Replace((string)row["Word"], "***");
        }
        e.Body = body;
    }
}

If you copy this code in the class named BadWords.cs and drop this class into ~/app_code/extensions folder, when run application, Extension Manager will generate input form for your extension. This form will be available by clicking "settings" link in the extension list in the admin/extensions section. You tell Manager that you need input form by creating settings object and importing it into manager. In the example Extension Manager knows that you want text box for parameter named "Word" and by default it assumes that you want lots of words, so it will put "Add" button and build updatable grid so user can enter, update and delete words. When someone making comment on the blog, extension will run on the comment serving event, get all saved by blogger words and make sure they will be replaced in the comment with asterisks.

In this case, settings object is basically a data table. You can add columns and rows as you wish. For example, if you need more than one parameter (column), you would add another one:

settings.AddParameter("Word");
settings.AddParameter("Replacement");

Manager will add another input box for second parameter. To add couple of values (rows):

settings.AddValues(new string[] { "nasty word", "***" });
settings.AddValues(new string[] { "another one", "###" });

Number of values in the string array has to match number of parameters (columns). There are number of overloads in the AddParameter method:

public void AddParameter(string name, string label, int maxLength, bool required, bool keyfield)
name - name of the parameter
label - text on the label next to input box (default same as name)
maxLength - maximum input value length (100)
required - if true, parameter is required and EM won't let to save null for this parameter (false)
keyfield - EM will insure that all values for this parameter are unique (false)
[work in progress]
 
<<  May 2008  >>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Enhanced with Snapshots

Subscribe to Rtur.net