views:

72

answers:

2

I wrote a simple CMS for one of my clients which does specifically what he needs (without the bloat of mainstream content management systems he would never use).

The CMS enables him to create and manage two types of content: pages and news (short messages that show in the sidebar).

The pages are displayed in the navigation menu of the website sorted alphabetically. My client would like to be able to sort the pages himself in a custom order.

What is the best way to do this programatically? I'm thinking of adding a column called "priority" to the pages table. It will be a number and the pages will be sorted based on this number either ascendingly or descendingly. And the client could edit this number for every page?

Just curious what are your thoughts about this? Is there a better way to do this? Some design pattern for this purpose?

+7  A: 

I personally use the jQuery ui sortable plugin. The client can drag and drop the items from a list, changing the order.

Next I use jQuery again to loop through the list of items, compile CSV list of id's and send it to the server.

var imageList;

function init() {
 imageList = $("#imagelist");

 if(imageList.length > 0) {
  imageList.sortable({stop:onStopSorting});
 }
}

function onStopSorting() {
 var datas = imageList.find("span.data");
 var result = new Array();
 for(var i = 0; i < datas.length; i++) {
  result.push(datas.eq(i).html());
 }
 $("#imageSort").val(result.join(","));
}

and on the server:

if(isset($_REQUEST["imageSort"]) && $_REQUEST["imageSort"] != "") {
    $imageIdList = explode(",", $_REQUEST["imageSort"]);

    for($i = 0; $i < count($imageIdList); $i++) {
     $this->setImagePriority($imageIdList[$i], $i+1);
    }
}

It might be more "quick 'n dirty" than a nice Design Pattern, but it works.

Les
I used the jquery ui sortable plugin even though I implemented it in a little different way. Thanks :)
Richard Knop
+3  A: 

If I was on a budget, I'd give the user a field to edit or otherwise control. The easy method is an integer field where the values can be duplicated - you let them pick the number and type it manually into a field. Obviously, this is a quick and dirty fix - if the users sets up a big sequence of pages, then wants to add something to the middle in a specific slot, that's a problem - the priority value is equal, so then you have to sort on something else.

Using float values is another possibility. You don't let the user see the number, in this case. If the user wants to put item C between items A and B, you set the priority of C=(A+B)/2 knowing that the decimal precision will let them do this kind of operation many (many) times over before you run out of precision. You can always make a maintenance script to get the data in order and assign new values - this could be run once a month or even once a year.

It's not fancy, but it's cheap to implement.

Frank DeRosa
I used the jquery sortable plugin, so the user can just drag and drop pages in the admin panel, and I used AJAX to save the new pages order to the database every time the user drags and drops a page with the mouse. It was easier than I expected.
Richard Knop
Oh, good! I'm pretty weak on AJAX, so I tend to assume that it's hard. You got the best of both worlds - good *and* cheap. That's how you want to do it, Good for you.
Frank DeRosa