Updated to Graphene theme

I’ve been a co-developer for the WordPress Graphene Theme since February 2011, and it’s about time a starting using this great theme on my own blog ;)

You can follow our development at Google Code – http://code.google.com/p/graphene/, here you can also report bugs and request new features.

The original developer and designer of the theme is Syahir Hakim, on his blog you can read a bit more about the theme.

WordPress pages into nested tree array

I was looking for a way to get a nested array (or tree array) from the pages or posts in WordPress, for a theme I’m currently working on.

If you just want to display the links you could use the wp_list_pages() function.

But this wont get you a nested array, then there are the functions get_pages() and get_posts(). These return a one-dimensional array of pages or posts.

Using this array I’ve written a simple recursive function which creates a nested array.

/**
 * Build a nested tree from an array of posts/pages.
 * @see WordPress::get_pages()
 * @see WordPress::get_posts()
 *
 * @param array $items The array of posts/pages
 * @param int $parentid The parent id to start with
 * @param array $tree
 * @return array The nested pages tree
 */
function thumnet_build_nested_tree($items, $parentid = 0, &$tree = array()) {
    foreach ($items as $key => $item) {
        // we are only interested in the pages that have the current parent
        if ($item->post_parent == $parentid){
            // add the item to the parent node
            $tree[$item->ID] = array('self' => $item, 'children' => array());
            // remove the page from the pages array, so it's not iterated in the recursive call
            unset($items[$key]);
            // call the function recursively, but with the children node as the tree
            thumnet_build_nested_tree($items, $item->ID, $tree[$item->ID]['children']);
        }
        // if the parent page has no children remove the node
        if (count($tree[$item->ID]['children']) == 0){
            unset($tree[$item->ID]['children']);
        }
    }   

    return $tree;
}

And not to forget a quick sample on how to use this

$pages = get_pages('sort_column=menu_order');
$tree = thumnet_build_nested_tree($pages);
thumnet_display_nested_tree($tree);

function thumnet_display_nested_tree($items, $indent = 0){
    foreach ($items as $item){
        echo str_repeat('&nbsp;', $indent*3) . (count($item['children']) ? '+' : '-') . ' ' . $item['self']->post_title . '<br />';
        if (count($item['children'])) { thumnet_display_nested_tree($item['children'], $indent+1); }
    }
}

Sample output:

+ Welcome
   + Subpage with children
      - Child 1
      - Child two
   - Subpage without children
- Top level page without children
+ About
   - Who am I

Install SABnzbd as a service on Windows 2008

Until version 0.6.0 is released of SABnzbd it is needed to take manual steps to install SABnzbd as a service. For Windows 2003 this could be achieved using this tutorial.

But for Windows 2008 this is not as easy anymore, therefore I had a look around and came with the following solution:

  1. Download the NSSM (the None Sucking Service Manager) here
  2. Put this file in a Windows PATH location (for example %windir%\system32, or extend the path)
  3. Run the commandline: nssm install sabnzbd (“sabnzbd” is the name for the service)
  4. A UI will popup asking for the location of the sabnzbd.exen file and optional startup parameters (I supplied -b0 for background mode)
  5. For me it was also necessary to setup the service (services.msc) to use the same account I once configured it with, otherwise you’ll end up with the wizard

Afterwards you are able to use net start sabnzbd to start the service and net stop sabnzbd to stop it. To uninstall use nssm remove sabnzbd confirm

That’s it for installing sabnzbd as a service under Windows 2008 (should also work on Windows 7!)

Automatically connect WebParts on a SharePoint page

For a recent project we had several WebParts (Provider and Consumer webparts) on a page which had to be connected to each other when the page was provisioned by our solution.

Unfortunately this cannot be done within the Module.xml :(

So I created a method which automatically connects the consumer WebParts to their correct provider WebParts.

To do this I basically used the SPLimitedWebPartManager, ProviderConnectionPoint, ConsumerConnectionPoint and some other nice classes.

The method (warning long block of code)

/// <summary>
/// Connects the web parts on page.
/// </summary>
/// <param name="web">The web.</param>
/// <param name="pageUrl">The page URL.</param>
/// <param name="publish">if set to <c>true</c> [publish].</param>
public static void ConnectWebPartsOnPage(SPWeb web, string pageUrl, bool publish)
{
	// get the page from the url
	SPFile page = web.GetFile(pageUrl);

	// make sure the page exists
	if (page.Exists)
	{
		// if the page is checked out, check it in because then the user can do an rollback
		if (page.Level == SPFileLevel.Checkout)
		{
			page.CheckIn("Page was checked out, needs to be checked in before connecting webparts", SPCheckinType.MinorCheckIn);
		}

		// check out the page because we are going to do changes on it
		page.CheckOut();

		// get the webpartmanager for the page
		using (Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
		{
			try
			{
				// try to connect the webparts on the page
				if (ConnectWebParts(mgr))
				{
					string checkinComment = "Auto connected WebParts on page";

					// webparts are connected so update the page and check it in
					page.Update();
					page.CheckIn(checkinComment);

					if (publish)
					{
						page.Publish(checkinComment);
						page.Approve(checkinComment);
					}
				}
			}
			catch (Exception ex)
			{
				page.UndoCheckOut();
				throw ex;
			}
		}
	}
}

/// <summary>
/// Checks if a WebPart connection exists.
/// </summary>
/// <param name="connections">The connections.</param>
/// <param name="provider">The provider.</param>
/// <param name="consumer">The consumer.</param>
/// <returns></returns>
private static bool WebPartConnectionExist(Microsoft.SharePoint.WebPartPages.SPWebPartConnectionCollection connections,
	System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.WebPart consumer)
{
	foreach (Microsoft.SharePoint.WebPartPages.SPWebPartConnection conn in connections)
	{
		if (conn.Provider == provider && conn.Consumer == consumer)
		{
			return true;
		}
	}

	return false;
}

/// <summary>
/// Connects the web parts.
/// </summary>
/// <param name="manager">The manager.</param>
/// <returns></returns>
private static bool ConnectWebParts(Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager manager)
{
	bool connectionMade = false;

	// get the webparts on the page
	Microsoft.SharePoint.WebPartPages.SPLimitedWebPartCollection webparts = manager.WebParts;

	// only continu if there are any webparts
	if (webparts.Count > 0)
	{
		ProviderConnectionPointCollection provColl;
		ConsumerConnectionPointCollection consColl;
		// walk through all the webparts on the page to find provider webparts
		foreach (System.Web.UI.WebControls.WebParts.WebPart providerPart in webparts)
		{

			// check if the webpart is a Provider webpart
			provColl = manager.GetProviderConnectionPoints(providerPart);
			if (provColl != null && provColl.Default != null)
			{

				// walk through all the webparts on the page again now to find the consumer webparts
				foreach (System.Web.UI.WebControls.WebParts.WebPart consumerPart in webparts)
				{

					// first make sure the webpart isn't the provider webpart
					if (consumerPart != providerPart)
					{
						// check if the webpart is a Consumer webpart,
						// then make sure the consumer webpart isn't already connected to the provider webpart
						// then check if the interface of the consumer webpart is assignable from the interface of the provider webpart
						consColl = manager.GetConsumerConnectionPoints(consumerPart);
						if (consColl != null && consColl.Default != null
							&& !WebPartConnectionExist(manager.SPWebPartConnections, providerPart, consumerPart)
							&& consColl.Default.InterfaceType.IsAssignableFrom(provColl.Default.InterfaceType))
						{
							// connect the webparts
							manager.SPConnectWebParts(providerPart, provColl.Default, consumerPart, consColl.Default);
							connectionMade = true;
						}
					}
				}
			}
		}
	}
	return connectionMade;
}

Sample usage (console application)

try
{
	using (SPSite site = new SPSite("http://localhost"))
	using (SPWeb web = site.OpenWeb())
	{
		ConnectWebPartsOnPage(web, "pages/default.aspx", false);
	}
}
catch (Exception ex)
{
	Console.WriteLine();
	Console.WriteLine("Message:\n{0}\n\nStack:\n{1}\n\n", ex.Message, ex.StackTrace);
}

First NEN 2082 certified project with SharePoint 2010


Together with 3 colleagues from QNH Business Integration I am currently finishing implementing a already NEN 2082 certified project based on SharePoint 2010.
When SharePoint 2010 hits RTM there will be another audit for this version of SharePoint, because currently the pre release version has been certified.

NEN 2082 is a Dutch standard which is similar the DoD5015 and MoReq standards.

This project was done for the Gemeente Nieuwegein, which is a Dutch local Government.

Gemeente Nieuwegein

For this project we have also been nominated for the “Innovation of the year” award:

LRG Innovator Nomination 2010

More information (Dutch)

My colleagues at QNH:

Older posts «