The blog saga continues, we still don’t have any fancy WordPress style filtering of the content. You know, creating these nice looking quotes and filtering potentially nasty html and stuff. Sure enough, TinyMCE has some function for allowing only certain tags and discarding others but I haven’t had time to look at that yet, I will though… Fancy filtering is not in the requirements for this project either however. But changing and deleting blog posts are necessary actions though, so is some kind of search for articles. Let’s begin by looking at what’s been added to the desktop menu:

The getArticles function is basically just an alias with some probable default values set. In this case it will retrieve all articles of the logged in user. Next we append all connected categories to each article, the number of round trips to the database could become huge here which is not optimal of course. However, focus is on speed at the moment, we have to get finished sometime and we don’t want to get bogged down by creating some massive optimized SQL statement that will fetch the right stuff from the beginning. Besides, SQL is not my strong side, I can hack most stuff but I’m not lightning fast, going with this possibly temporary PHP solution will do for now and is much faster. Furthermore, when the service is released it won’t get a bazillion users straight away either, performance can always be fine tuned later if need be.

Apparently updateArticle will launch the blog_write.tpl with a flag called ‘update’ set to ‘yes’. Of course when saving there the good old saveArticle function will be called. In that function we now do one extra thing, in addition to the user id we also keep track of the user name in each article. Why we do this will become apparent soon enough.

The delete connection function is just an alias. Why don’t I use the $_referenceMap with a cascade delete like it says in the ZF manual, or InnoDB with this set in the schema?

1.) I tried my ass off with the reference map but I couldn’t get it to work, I must have missed something, beats me what I missed though because this isn’t supposed to be complicated at all. Let me know if you’ve made it work and how you did it.

2.) InnoDB has wreaked havoc with my data on several occasions by crashing miserably, I don’t know why. It might have been my fault or something but it doesn’t really matter because I’ve never had any problems with MyISAM, that’s why I’m prepared to go through the extra trouble of explicitly deleting dependencies in this way.

You might notice something strange in assignCategories, it seems that the rowset has got a new function called toArrayWithKey. It has and it is my doing, instead of making yet another static function I opted to put the functionality in an extension of the rowset class this time because it’s somewhat prettier:

ToArrayWithKey will convert the rowset to a 2d array and assign each sub array a key that matches the id of each entry. The whole point of this is to be able to easily keep track of checked / not checked categories in the template. Take a look at assignCategories again and you will understand what I mean. This functionality is only needed if we allow updating of articles, which we currently do. One more thing, we have to use the new ExtRowset class in ExtModel like this at the top: protected $_rowsetClass = ‘ExtRowset’; Don’t forget to include the new rowset script in the bootstrap file either.

Let’s retrace our steps to the beginning and the new menu item that links to getSearchForm():

Yeah, I know, it’s a biggie but it has it’s reasons, in probably 90% of all cases it would be smaller and easier. So we begin by creating an array that we will use with the necessary information like “count” which is how many results we want to display per page, “where” which is yet another array with what we want to search for, “obj” is the object we want to use in the search, leaving this empty will default to $this->obj, last but not least the “offset” which will control where we are in the pagination.

First of all, let’s start with $this->session->conf->cat_count. Since the last part I’ve mustered the strength to actually start putting stuff in a configuration table which we will later be able to change in the future admin interface:

Here we use the other function we have extended rowset with to fetch the config info as an array that has the field conf_var as it’s keys and conf_val as values. Notice the explicit type conversion from array to object, it’s easier to write ->var instead of [‘var’], at least in my book.

It’s basically just an alias which I’ve created to avoid a fetchAll function with an ugly list of long strings as parameters. We need the params array in more places and this is also a good way of encapsulating the dangerous but powerful extract function. I know, it’s a borderline example of trying to minimize code size at the expense of readability but we go with it for now. Let’s look at sumCategories:

What’s happening here is that there might exist categories with the same headline created and attached to different users. When doing the search by category we want the number of articles in each category irrespective of which user has written something in that category. In this case we basically treat the categories as tags by their headlines even though there is a connection to a unique user for each category. Yes it’s complicated and convoluted, but if we had had a tag system then the complexity would have shown it’s ugly face when retrieving “categories” in the write/update template instead. Having a twofold solution like we maybe should have, would’ve added extra code and complexity in it’s own way. It’s always easy to create complexity but sometimes it’s impossible to avoid it. Note also that we are doing a looping retrieve from the database again, not exactly optimized.

The if(empty($params[‘offset’])) conditional is necessary because we need to set $this->session->category->tot_count to a proper value when the search is executed, but only once.

Nothing special here, it’s just creating the array we need to draw the pagination controls. As you can see there is no grouping of pages being done at this stage. That will be added later if needed. In this case we have 2 results per page, a 1000 item result will actually draw 500 page links numbered from 1 to 500. We will see what happens, we will probably have to implement some grouping eventually but at the moment we need something to show asap and this works. Whatever the managers are calling it, “proof of concept” or a “testable prototype”, we all know what they mean, a dirty first iteration that they can play around with.

I think we are finally ready to take a look at blog_cat_search_result.tpl:

Here the new username field in com_blog makes sense, if it wasn’t already there we would have had to do yet another table join to retrieve it. Moreover, since articles will not be able to change authors this will not prove to be a problem. For once we have a change that makes sense from both an optimization and dev-speed point of view!

There is one more change of importance, during the process leading up to the above logic I thought it would be nice to integrate the gallery into the blog by enabling the gallery thumbs in blog_write.tpl. The goal was to have the thumbs show if the user wanted and if clicked inserted into TinyMCE. Let’s take a look:

As you can see we can toggle the thumbs on and off, when a thumb gets clicked it will be inserted at the end of the content currently in TinyMCE. Yes I tried like a maniac to get it to work by being inserted at the place where the marker currently is in the editor but I couldn’t make it work. Probably because it loses focus when you click somewhere outside it and then it becomes impossible to read the marker position but I’m not 100% on this one. If you’ve discovered a solution then please please let me know. Of course it might be possible to create some kind of plugin to TinyMCE but with the state of my basic Javascript skills and the time pressures I’m under… Not likely at the moment.