Transferring and Creating My Blog..
OK, I'm not comfortable with the idea of having to copy and paste HTML into each of the weekly folders. I want to experiment with the information design of the blog as well as the CSS. So, I'm gonna move this blog over to mattwaterworth.com. I registered it in second year, let it expire and it was subsequently captured by one of those domain parking spammer bastards. Thankfully they let it expire, probably realising it was as much use to them as a condom made out of rice paper.
My inital plan was to use Wordpress for the blog but I think it'll be more of a challenge and ultimately more useful to try to build my own blog with CakePHP. I want the blog to be as simple as possible to start with. In fact it'll only have 2 controllers, one for each week and one for each post. I'll eventually need to add in a users controller to develop an administration panel, but this isn't really important now.
The first step involves creating all of the meta redirects for each week. Before doing this I'll need to decide where all of my week entries will appear on the site. I was thinking of something like...
http://www.mattwaterworth.com/des511j1/weeks/view/3
The CakePHP install will go inside the actual directory des511ji. Weeks and view will be sent to the front controller (index.php) as parameters using mod_rewrite (which CakePHP uses to route all requests to index.php). In this case 'weeks' will be the controller, 'view' an action within that controller and 3 the week parameter which is passed to the view method.
Here's the code I used to automatically generate all of the meta redirects.

Now that the redirects are in place it's time to think about my requirements for the blog. I know I should have done this BEFORE creating the redirects, but it's easy enough to change them with the script if the URL's happen to change.
Requirements
- When the end-user visits 'http://www.mattwaterworth.com/des511j1/weeks/view/3' they must be shown a list of the posts that have been created within that week. There's two ways to do this. The easiest method involves placing a week attribute inside the posts table, which could be entered manually by me into a web form when I'm updating my blog. The second way, would involve detecting what week it is by calling gmtmktime() and running a MYSQL query to check which posts fall within that date range. This would be a bit harder to implement,
- Each post's title must link to a seperate page where it's again displayed. This will make my blog more accessible. I'll need to include a 'slug' attribute to store the key part of the permalink in.
- Without an administration panel I will have to use CakePHP's scaffolding facility which automatically generates a rudimentary admin facility by reading in the structure of the database table and its relationships. Note : this is seriously insecure. Anyone can access the admin panel without a username/password. Scaffolding is turned on in CakePHP by declaring var $scaffold; inside of the necessary controller. I'll need to ensure I delete this variable once I'm finished to disable this feature.
- Since I want to occasionally display source code in my blog, I'll need to use a code highlighting tool that'll do all of the hard work for me. According to this article there are two options, the highlight_string() native PHP function or PEAR's Text Highlighter package. Either way, I will have to store the code samples in a seperate database table, which I can then fetch through a CakePHP component.
- Finally, the week bar at the top of the blog must be controlled dynamically.
Listing source code
This is going to be a lot more difficult than I'd initally anticipated. Whenever I try to post PHP source code into my blog it's executed automatically and therefore doesn't display right. I'll have to include another controller in the system called code, which will then help to store, retrieve and then format the code using either the highlight_string or the PEAR Text_Highlighter package.

This is the 'codes' table in my database and it's reasonably self explanatory. The 'type' attribute is there in case I use Text_Highlighter, which can apply source formatting to a range of development languages.
My biggest problem was finding a way to display the source code in the appropriate place. I initally planned to list the code at the very bottom of the post, linking to it through an anchor, but quickly realised this would be inconvienent to the reader. I instead chose to print out the listings inside a hidden div, placing another DIV where the source code should be listed. I then used Javascript to copy the contents of innerHTML of the hidden div into the correct div.
For this to work I had to devise a strict naming convention which could then be used to match the hidden div up with the source code div. Let's say that the code's slug is 'refresh'..
The hidden div would be called
code_refresh_test
The display div would be called
code_refresh
The code starts by searching for all DIV tags within the DOM. Then, each id is read in and split by the underscore delimiter. Then it tests to see if the first element of the resulting array is equal to "code", if this is true the DIV's id is stored in a results array with the slug acting as the array key. I then created another array inside the array to store the id's of the test and display divs. If both of these elements are present then the DIV's contents are substituted and the code appears in the correct place.
In the end I opted for highlight_string() over the Text_Highlighter package, which I couldn't get working as my webhost doesn't support PEAR. However after checking the source code that highlight_string produces I feel like I'm back to square one. Have a look for yourself...

Yuck! I don't have enough time to spend toying about with source formatting forever. I'll have another look at this next week so for the meantime I'll be displaying source code with screenshots.
Dynamic Week Bar
Listed below is the source code for the Weekbar helper that I created for my blog. In CakePHP, helpers are small chunks of code that help with marking up presentation logic in your layout.

The weekbar works by calculating the current week and then displaying it along with the preceding weeks as links. This means that on October 22nd at 12:00, Week 5 will automatically become available for selection. I think I'll refine this in the coming weeks to only display weeks that have posts in them.
How does the system know if I've made a post this week or not? Well it's quite simple, I placed the following SQL statement in the week controller.
SELECT * FROM `posts` WHERE UNIX_TIMESTAMP(created) BETWEEN ($this->weekOneStart + (($this->selectedWeek - 1) * 604800) + (6 * 3600)) AND ($this->weekOneStart + ($this->selectedWeek * 604800 - 1) + (6 * 3600))
If no rows are returned then there are no posts for that week.
Now if you look up to the last line of the WeekbarHelper source code you'll notice that I've made the following call.
$this->Html->guiListTree($weeks, array('id' => 'weeks')).
This command automatically generates an unordered list by taking the contents of the weeks array enclosing each element in














