Category order hack

Download plugins, language packs and stuff.

Category order hack

Postby FI-DD » Fri Jan 19, 2007 12:56 pm

This hack lets you change the order of your categories. In the category manager next to each category there will be a "Up" and "Down" link. When clicking it you can move the category one position up or down.



Installation:

1. Open inc/functions.inc.php and replace this:

Code: Select all
function category_get_tree($prefix = '', $tpl = '{name}', $no_prefix = true, $id = 0, $level = 0){

global $sql, $PHP_SELF;

static $johnny_left_teat;



   $level++;



   foreach ($sql->select(array('table' => 'categories', 'where' => array("parent = $id"), 'orderby' => array('id', 'ASC'))) as $row){

      $find = array('/{id}/i', '/{name}/i', '/{url}/i', '/{icon}/i', '/{template}/i', '/{prefix}/i', '/\[php\](.*?)\[\/php\]/ie');

      $repl = array($row['id'], $row['name'], $row['url'], ($row['icon'] ? '<img src="'.$row['icon'].'" alt="'.$row['icon'].'" border="0" align="absmiddle">' : ''), $row['template'], (($row['parent'] or !$no_prefix) ? $prefix : ''), '\\1');

      $johnny_left_teat .= ($no_prefix ? preg_replace('/('.$prefix.'{1})$/i', '', str_repeat($prefix, $level)) : str_repeat($prefix, $level));

      $johnny_left_teat .= preg_replace($find, $repl, $tpl);

      category_get_tree($prefix, $tpl, $no_prefix, $row['id'], $level);

   }



return $johnny_left_teat;

}




with this:

Code: Select all
function category_get_tree($prefix = '', $tpl = '{name}', $no_prefix = true, $id = 0, $level = 0){

global $sql, $PHP_SELF;

static $johnny_left_teat;



   $level++;



   foreach ($sql->select(array('table' => 'categories', 'where' => array("parent = $id"), 'orderby' => array('ord', 'ASC'))) as $row){

      $find = array('/{id}/i', '/{name}/i', '/{url}/i', '/{icon}/i', '/{template}/i', '/{prefix}/i', '/\[php\](.*?)\[\/php\]/ie', '/{parent}/i', '/{ord}/i');

      $repl = array($row['id'], $row['name'], $row['url'], ($row['icon'] ? '<img src="'.$row['icon'].'" alt="'.$row['icon'].'" border="0" align="absmiddle">' : ''), $row['template'], (($row['parent'] or !$no_prefix) ? $prefix : ''), '\\1', $row['parent'], $row['ord']);

      $johnny_left_teat .= ($no_prefix ? preg_replace('/('.$prefix.'{1})$/i', '', str_repeat($prefix, $level)) : str_repeat($prefix, $level));

      $johnny_left_teat .= preg_replace($find, $repl, $tpl);

      category_get_tree($prefix, $tpl, $no_prefix, $row['id'], $level);

   }



return $johnny_left_teat;

}




2. Replace your old inc/mod/categories.mdu with the one from the download.



3. Go to Options -> Manage categories to "activate" it.



4. If you don't want to replace your old categories.mdu you can manually apply the changes. Please check the readme.txt coming with the download for further instructions.
Attachments
category_order.zip
Category order hack
(4.31 KiB) Downloaded 464 times
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Fri Jan 19, 2007 1:46 pm

FI-DD, I can't believe you did this. I owe you big time for this. Thanks soooooooo much! :D :D :shock: :D :D
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby maqs » Sun Jan 21, 2007 1:07 pm

And you know it, I am as happy as scottdallas, there should be a holiday named after FI-DD. :D Thanks man. :)
User avatar
maqs
 
Posts: 112
Joined: Sun Aug 20, 2006 7:26 am
Location: Lund, Sweden

Postby maqs » Sun Jan 21, 2007 1:22 pm

A little question. Does this hack only effect the order in the admin-section, and not on the actual webpage? Or am I doing something wrong? I have cleared the cache.
User avatar
maqs
 
Posts: 112
Joined: Sun Aug 20, 2006 7:26 am
Location: Lund, Sweden

Postby FI-DD » Mon Jan 22, 2007 8:23 pm

Where exactly it's not working?



It should work everywhere the function category_get_tree() is used, for example add news, edit news, manage categories and cn_category().
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby maqs » Mon Jan 22, 2007 10:13 pm

It effects everything exept cn_category(), that still turns up in numreric order. Could it be beacuse I have outruled one category? But that shouldn't matter i think.
User avatar
maqs
 
Posts: 112
Joined: Sun Aug 20, 2006 7:26 am
Location: Lund, Sweden

Postby FI-DD » Mon Jan 22, 2007 11:50 pm

I just tested it again, it's perfectly working for me in cn_category().



What's your code for cn_category() (it's in plugins/etc.php)?
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby maqs » Tue Jan 23, 2007 12:46 am

plugins/etc.php :

Code: Select all
function cn_category($prefix = '&nbsp;', $tpl = '<a href="[php]cute_get_link($row, category)[/php]">{name} ([php]count_category_entry({id})[/php])</a><br />', $no_prefix = true, $level = 0){



global $PHP_SELF, $cache_file;



static $uniqid;







   if (!$cache = cute_cache('category', $uniqid++)){



      $cache = category_get_tree_modified($prefix, $tpl, $no_prefix, $level);



      file_write($cache_file, $cache);



   }







return $cache;



}




And in functions.inc.php :

Code: Select all
////////////////////////////////////////////////////////



// Function:   category_get_tree_modified



// Description: what it sounds like



function category_get_tree_modified($prefix = '', $tpl = '{name}', $no_prefix = true, $id = 0, $level = 0){



global $sql, $PHP_SELF;



static $johnny_left_teat;







   $level++;







   foreach ($sql->select(array('table' => 'categories', 'where' => array("parent = $id", 'and', "id != 12"), 'orderby' => array('id', 'ASC'))) as $row){



      $find = array('/{id}/i', '/{name}/i', '/{url}/i', '/{icon}/i', '/{template}/i', '/{prefix}/i', '/\[php\](.*?)\[\/php\]/ie');



      $repl = array($row['id'], $row['name'], $row['url'], ($row['icon'] ? '<img src="'.$row['icon'].'" alt="'.$row['icon'].'" border="0" align="absmiddle">' : ''), $row['template'], (($row['parent'] or !$no_prefix) ? $prefix : ''), '\\1');



      $johnny_left_teat .= ($no_prefix ? preg_replace('/('.$prefix.'{1})$/i', '', str_repeat($prefix, $level)) : str_repeat($prefix, $level));



      $johnny_left_teat .= preg_replace($find, $repl, $tpl);



      category_get_tree_modified($prefix, $tpl, $no_prefix, $row['id'], $level);



   }







return $johnny_left_teat;



}
User avatar
maqs
 
Posts: 112
Joined: Sun Aug 20, 2006 7:26 am
Location: Lund, Sweden

Postby FI-DD » Tue Jan 23, 2007 12:58 am

In function category_get_tree_modified() change this:

Code: Select all
'orderby' => array('id', 'ASC')))




to this:

Code: Select all
'orderby' => array('ord', 'ASC')))
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Tue Jan 23, 2007 1:30 am

Are ASC and DESC PHP terms? If so that will provide me much flexibility in many things.
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby maqs » Tue Jan 23, 2007 1:34 am

Thanks man. :)
User avatar
maqs
 
Posts: 112
Joined: Sun Aug 20, 2006 7:26 am
Location: Lund, Sweden

Postby FI-DD » Tue Jan 23, 2007 1:39 am

scottdallas wrote:Are ASC and DESC PHP terms? If so that will provide me much flexibility in many things.


No, they are MySQL terms.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby scottdallas » Tue Jan 23, 2007 5:27 pm

Finally applied this to one of my admin panels to see it in action and use it. Works exactly like I hoped for. I don't know how your brain does it. Thanks again.
Everyday I'm tumblin http://scodal.tumblr.com
User avatar
scottdallas
 
Posts: 2209
Joined: Thu May 04, 2006 6:46 pm
Location: US

Postby Mystic2010 » Fri Feb 22, 2008 11:54 pm

Let's kick this post up :lol:
I love the hack as I quite often want to change category order listing because of a different order I had in mind.
On to why I replied. I think I found a bug.
The bug occurs when you have no categories set. After you implement the hack (including uploading the new categories.mdu), everything is fine. Adding categories is no problem, and moving the categories up and down are working perfectly.

However, if you decide to delete all categories, you receive the following error:
Code: Select all
txtSQL Warning: Column ord already exists in /home/whatsup/domains/test.com/public_html/test/cnr/index.php on line 144


This only occurs when you delete all categories. Any solutions to this?

Oh and I'm running CNR 2.5.4
Mystic2010
 
Posts: 51
Joined: Wed Jan 10, 2007 6:36 pm

Postby FI-DD » Wed Feb 27, 2008 7:33 pm

Try this:

Open categories.mdu and find this:
Code: Select all
// ********************************************************************************
// Add new column to db
// ********************************************************************************


and add below:
Code: Select all
$sql->strict();
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Next

Return to Additional Downloads



Who is online

Users browsing this forum: No registered users and 0 guests

cron