Gossamer Forum
Home : Products : Links 2.0 : Customization :

category sort mod

Quote Reply
category sort mod
I searched for a mod to sort categories in a specific way within this forum. But all I found were posts from people looking for such a mod.
So I wrote it myself. (In fact, as I didn't know anything about Perl before, it was quite a nice thing to learn some Perl this way Smile.)
As somebody else may be interested, I decided to post it here:
I've tested it on my localhost (because my links directory is not yet online) and it seems to work fine.
If you find any bugs please tell me about and I will try to fix them.

WML

NOTE 1: If you don't use a numeric field for sorting be sure to exchange "<=>" with "cmp"!!
(Maybe someone has a good idea how we could include an automatic selection of the right operator to the mod?)

NOTE 2: It works now with "build all" and "build staggered" (which was a bit more tricky). You also have to change your db_utils.cgi. Don't forget to make a backup before you change your files!!!

NOTE 3: As a "feature" of this forum, if you leave out the space from "% category" it is changed to "Ętegory". So you have to change this back!!
************************************************************************
category sort mod:

With this mod for links 2.0 it is possible to sort the Categories and Subcategories
on all your pages by ID,name or any other field defined in category.def

(c) WML 22/6/2000

********************************************************************************

links.cfg

Add this to your links.cfg:
--------------------------------------------------------------------------
# Field number to sort categories by:
# set to 0 for sorting by ID, set to 1 for sorting by name
# or set to any other field defined in category.def
$db_sort_categories = 0;
--------------------------------------------------------------------------
(I would have prefered to add this to category.def but it is not possible
to use both links.def and category.def in your nph-build.cgi)


nph-build.cgi

in sub build_stats

delete or comment out
--------------------------------------------------------------------
foreach $cat ( keys %subcategories ) {
@{$subcategories{$cat}} = sort @{$subcategories{$cat}};
--------------------------------------------------------------------
(We will do the sorting somewhere else)


in sub build_category_pages

replace
--------------------------------------------------------------------
my ($cat, $url, $dir, @related, $relation, $page_num, $next_page,
$numlinks);
--------------------------------------------------------------------

with
--------------------------------------------------------------------
my ($cat, $url, $dir, @related, $relation, $page_num, $next_page,
$numlinks, $cat_dataset);
--------------------------------------------------------------------

and
--------------------------------------------------------------------
CATEGORY: foreach $cat (sort keys Ętegory) {
--------------------------------------------------------------------

with
--------------------------------------------------------------------
CATEGORY: foreach $cat_dataset (sort {$$a[$db_sort_categories] <=>
$$b[$db_sort_categories]} values Ętegory) {
$cat=$$cat_dataset[$db_main_category];
--------------------------------------------------------------------
(Here we do the sorting. If you don't use a numerical field for
sorting change "<=>" to "cmp"
)


in sub build_home_page

replace
--------------------------------------------------------------------
my ($subcat, @rootcat);
--------------------------------------------------------------------

with
--------------------------------------------------------------------
my ($subcat, @rootcat, $subcat_dataset);
--------------------------------------------------------------------

and
--------------------------------------------------------------------
foreach $subcat (sort keys Ętegory) {
--------------------------------------------------------------------

with
--------------------------------------------------------------------
foreach $subcat_dataset (sort {$$a[$db_sort_categories] <=>
$$b[$db_sort_categories]} values Ętegory) {
$subcat=$$subcat_dataset[$db_main_category];
--------------------------------------------------------------------
(Here again we do the sorting. If you don't use a numerical field
for sorting change "<=>" to "cmp"
)


in sub build_new_page

replace
--------------------------------------------------------------------
CATEGORY: foreach $category (sort keys %{$link_output{$date}}) {
--------------------------------------------------------------------

with
--------------------------------------------------------------------
CATEGORY: foreach $category (keys %{$link_output{$date}}) {
--------------------------------------------------------------------
(the categories are already sorted!)

in sub build_cool_page

replace
--------------------------------------------------------------------
foreach $category (sort keys %cool_links) {
--------------------------------------------------------------------

with
--------------------------------------------------------------------
foreach $category (keys %cool_links) {
--------------------------------------------------------------------
(the categories are already sorted!)



The following codes have to be changed only if you want to use staggered build:


in sub build_single_category

remove or comment out
--------------------------------------------------------------------
foreach $cat ( keys %subcategories ) {
@{$subcategories{$cat}} = sort @{$subcategories{$cat}};
}
--------------------------------------------------------------------


db_utils.cgi

in sub category_list

replace
--------------------------------------------------------------------
my (Ętegories, @fields);
--------------------------------------------------------------------

with
--------------------------------------------------------------------
my (Ętegories, @fields, @sortcat);
--------------------------------------------------------------------

and
--------------------------------------------------------------------
categories{$fields[db_sort_category]}++;
--------------------------------------------------------------------

with
--------------------------------------------------------------------
$categories{$fields[db_sort_category]} = $fields[$db_main_category];
--------------------------------------------------------------------

and
--------------------------------------------------------------------
db_category_list = sort keys Ętegories;
--------------------------------------------------------------------

with
--------------------------------------------------------------------
sortcat = sort(keys(Ętegories));
for ($i=0; $i <=$#sortcat; $i++) {
$db_category_list[$i] = $categories{$sortcat[$i]};
}
--------------------------------------------------------------------


Quote Reply
Re: category sort mod In reply to
Thanks for the contribution.

There is of course a "dynamic" solution with widgetz's category.cgi...called Category and Search Sort Mod oddly enough.

Tongue

Regards,

Eliot Lee
Quote Reply
Re: category sort mod In reply to
That's right.
But all I wanted was a static solution, so the user don't have to worry about the order of the categories.

Regards,

WML


Quote Reply
Re: category sort mod In reply to
Codes in the Category.cgi can be applied to the nph-build.cgi...point being...Mod has already been written...

But, thanks for your contribution.

Regards,

Eliot Lee
Quote Reply
Re: category sort mod In reply to
WML, thank you for your mod, but it does not work for me.
For starters, you have some typos:

foreach $cat ( keys %subcategories ) {
@{$subcategories{$cat}} = sort @{$subcategories{$cat}};
}

and

$sortcat = sort(keys(Ętegories));
for ($i=0; $i <=$#sortcat; $i ) {
$db_category_list[$i] = $categories{$sortcat[$i]};
}

I have implimented all of your code with the corrections and I cannot get it to change the order of the categories on the Home page. It does not matter if I Build All or Staggered. Any ideas?





Quote Reply
Re: category sort mod In reply to
Have you made any progress, Katana Man?

Thanks.

Quote Reply
Re: category sort mod In reply to
Unfortunately no. I've implimented this code, it does not give me any errors, but it does nothing for sorting the home page categories :(

Quote Reply
Re: category sort mod In reply to
Thanks for replying, Katana Man.
Much appreciated. Smile

Quote Reply
Re: category sort mod In reply to
looking over his mod.. it's nothing like mine.. i don't think it even does the same thing.. it sorts categories only..

Jerry Su
Quote Reply
Re: category sort mod In reply to
This doesn't work!

Did anyone get this to work?
Did the person who wrote this even test it?
Quote Reply
Re: [dimva] category sort mod In reply to
Did anyone ever get the sort on the home page to work? Placng an * or coming up with creative ways to get it to look the way you want is a PITA.



Perhaps it coul dbe implimented in the upgrade!


Sandra Roussel
Chonsa Group Design - Fresh Start Housing