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 .)
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]};
}
--------------------------------------------------------------------
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 .)
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]};
}
--------------------------------------------------------------------