Search Only This Category Mod
--------------------------------------
This mod was written with the guidance, input and help of the following
people; AnthroRules, pugdog, and especially Alex404.
The mod is only useful if you have a search field/box on your category
pages and you make the necessary changes to the category template.
Note: I have made modifications to my own files, so some of the code
may appear slightly different in your implementation of links, but you
should be able to make the necessary changes as outlined here.
Backup
--------------------
Make a backup copy of the following files:
/search.cgi
/admin/Links/Search.pm
/admin/Links/DB_Utils.pm
/admin/templates/category.html
search.cgi
--------------------
Add the following variables to the top at the 'my' statement:
# ---------------------------------------------------
# Performs the actual search.
#
my ($in, $dynamic) = @_;
my ($mh, $bool, $nh, $ww, $order, $query, $ignored, %seen, $next, $catid, $sub_list, $cat_name,
$catdb, $cat_hits, $category_results, $cat_count, $cat_errors, $linkdb,
$link_hits, $link_results, $link_count, $link_errors, $banner_html);
my %in = %{&cgi_to_hash($in)};After ...
# Get/Set the search options.
($in->param('mh') =~ /^(10|25|50|100)$/) ? ($mh = $1) : ($mh = 25);
($in->param('bool') =~ /^(and|or)$/i) ? ($bool = uc $1) : ($bool = 'OR');
($in->param('nh') =~ /^(\d+)$/) ? ($nh = $1) : ($nh = 1);
($in->param('substring')) ? ($ww = 1) : ($ww = 0);
($in->param('order') =~ /^(score|category)$/i) ? ($order = uc $1) : ($order = 'CATEGORY');
... add the following ...
$catid = $in->param('catid');
# Get a list of catgeory ID's for 'only this cat' type of search
if ($catid eq ""){$cat_name=""} else {
# Get Category Name
$cat_name = &get_category_name ($catid);
# Get a list of category IDs where the name starts with $cat_name
$sub_list = &get_category_id_list ($cat_name);
chop($sub_list);
$in->param ( 'CategoryID' => $sub_list );
}
At the top of '# Search the category listings' add the following:
# Search the category listings
my %catfilter = ();
%catfilter->{'ID'}=$sub_list;
... and change $cat_hits ... to ...
$cat_hits = $catdb->query ( { query => $query, mh => $mh, nh => $nh, ww => $ww, filter => \%catfilter } );
Search.pm
--------------------
Go to sub query { and look for # ... setup the filter if it was asked for
Change it to ...
# ... setup the filter if it was asked for
my $filterref = $self->{filter};
my $filter = undef;
my $where ;
if ( ($filterref) and (@query_results)) {
$filter = " and (";
foreach my $flt(keys %$filterref){
if ($filterref->{$flt} !~ m/,/){
$filter.= "(" . $flt . " like '%" . $filterref->{$flt} . "%') and ";
} else {
$filter.= "(" .$flt . " in (" . $filterref->{$flt} . ")) and ";
}
}
chop($filter);chop($filter);chop($filter);chop($filter);
$filter .= ")";
Now go a little further down to # ... since there was a change in the number of results AND there ...
and change the following section from ...
foreach $row ( $sth->fetchrow_hashref () ) {
delete $needed { $$row { $id_col } };
}... to ...
while ( $row = $sth->fetchrow_hashref () ) {
delete $needed { $$row { $id_col } };
}
DB_Utils.pm
--------------------
At the top of the file a few lines after '@EXPORT = ' find this line ...
&build_category_row &build_category_row_val &build_categoryd_row
... and change it to this ...
&build_category_row &build_category_row_val &build_categoryd_row &get_category_id_list
Next we add the new routine after ...
sub get_category_list {
# --------------------------------------------------------
# Builds a <select> list of category name to category id.
#
...
$sth->finish;
$CATEGORY_LIST{$value,$fname} = $output;
}
return $output;
}
... add this new routine ...
sub get_category_id_list {
# --------------------------------------------------------
# Get a list of category IDs based on match with start of category name.
#
my $value = shift;
my ($query, $sth, $id, $name, $output);
$output = $CATEGORY_LIST{$value};
if (!$output) {
if (! $CATDB) {
$CATDB = new Links::DBSQL $LINKS{admin_root_path} . "/defs/Category.def";
}
$query = qq!
SELECT ID
FROM Category
WHERE Name LIKE '$value%'
!;
$sth = $CATDB->prepare ($query);
$sth->execute() or die "Can't Execute: $DBI::errstr";
while (($id) = $sth->fetchrow_array) {
($output .= "$id,");
}
$sth->finish;
$CATEGORY_LIST{$value} = $output;
}
return $output;
}
That's all the routines and variables we'll need to adjust and
modify, so now we have to put the CategoryID variable into the
category page so we can tell links which category the search
has come from.
Template category.html
--------------------
In your category template file you will have the 'FORM' details
for the search.
We now need to add the radio buttons to allow our visitor to
search through 'All categories' or 'Only this category'.
Look for the HTML that displays the SEARCH button ...
<input type=submit value="Search">
Directly after this add the following ...
<INPUT NAME="catid" TYPE="radio" VALUE="" CHECKED>All categories
<INPUT NAME="catid" TYPE="radio" VALUE="<%category_id%>">Only this category
This will put two 'radio' type buttons on your category page next
to the search box, with the options for 'All categories'
highlighted by default.
Re-build
--------------------
Upload the modified files and make sure all the file permissions are
correct, then re-build your directory to effect the changes.
Go to one of your category pages and make sure the search is
working correctly.
If you have any problems use your backup files to put your site back
into working order then double-check the changes you made to
trouble-shoot the problem or problems.
Thats it - hope you enjoy
All the best
Shaun
--------------------------------------
This mod was written with the guidance, input and help of the following
people; AnthroRules, pugdog, and especially Alex404.
The mod is only useful if you have a search field/box on your category
pages and you make the necessary changes to the category template.
Note: I have made modifications to my own files, so some of the code
may appear slightly different in your implementation of links, but you
should be able to make the necessary changes as outlined here.
Backup
--------------------
Make a backup copy of the following files:
/search.cgi
/admin/Links/Search.pm
/admin/Links/DB_Utils.pm
/admin/templates/category.html
search.cgi
--------------------
Add the following variables to the top at the 'my' statement:
Code:
sub search { # ---------------------------------------------------
# Performs the actual search.
#
my ($in, $dynamic) = @_;
my ($mh, $bool, $nh, $ww, $order, $query, $ignored, %seen, $next, $catid, $sub_list, $cat_name,
$catdb, $cat_hits, $category_results, $cat_count, $cat_errors, $linkdb,
$link_hits, $link_results, $link_count, $link_errors, $banner_html);
my %in = %{&cgi_to_hash($in)};
# Get/Set the search options.
($in->param('mh') =~ /^(10|25|50|100)$/) ? ($mh = $1) : ($mh = 25);
($in->param('bool') =~ /^(and|or)$/i) ? ($bool = uc $1) : ($bool = 'OR');
($in->param('nh') =~ /^(\d+)$/) ? ($nh = $1) : ($nh = 1);
($in->param('substring')) ? ($ww = 1) : ($ww = 0);
($in->param('order') =~ /^(score|category)$/i) ? ($order = uc $1) : ($order = 'CATEGORY');
... add the following ...
Code:
$catid = $in->param('catid');
# Get a list of catgeory ID's for 'only this cat' type of search
if ($catid eq ""){$cat_name=""} else {
# Get Category Name
$cat_name = &get_category_name ($catid);
# Get a list of category IDs where the name starts with $cat_name
$sub_list = &get_category_id_list ($cat_name);
chop($sub_list);
$in->param ( 'CategoryID' => $sub_list );
}
# Search the category listings
my %catfilter = ();
%catfilter->{'ID'}=$sub_list;
... and change $cat_hits ... to ...
$cat_hits = $catdb->query ( { query => $query, mh => $mh, nh => $nh, ww => $ww, filter => \%catfilter } );
Search.pm
--------------------
Go to sub query { and look for # ... setup the filter if it was asked for
Change it to ...
Code:
# ... setup the filter if it was asked for
my $filterref = $self->{filter};
my $filter = undef;
my $where ;
if ( ($filterref) and (@query_results)) {
$filter = " and (";
foreach my $flt(keys %$filterref){
if ($filterref->{$flt} !~ m/,/){
$filter.= "(" . $flt . " like '%" . $filterref->{$flt} . "%') and ";
} else {
$filter.= "(" .$flt . " in (" . $filterref->{$flt} . ")) and ";
}
}
chop($filter);chop($filter);chop($filter);chop($filter);
$filter .= ")";
and change the following section from ...
Code:
my %needed = map { $$_[0] => 1 } @query_results; foreach $row ( $sth->fetchrow_hashref () ) {
delete $needed { $$row { $id_col } };
}
Code:
my %needed = map { $$_[0] => 1 } @query_results; while ( $row = $sth->fetchrow_hashref () ) {
delete $needed { $$row { $id_col } };
}
--------------------
At the top of the file a few lines after '@EXPORT = ' find this line ...
&build_category_row &build_category_row_val &build_categoryd_row
... and change it to this ...
&build_category_row &build_category_row_val &build_categoryd_row &get_category_id_list
Next we add the new routine after ...
sub get_category_list {
# --------------------------------------------------------
# Builds a <select> list of category name to category id.
#
...
$sth->finish;
$CATEGORY_LIST{$value,$fname} = $output;
}
return $output;
}
... add this new routine ...
Code:
sub get_category_id_list {
# --------------------------------------------------------
# Get a list of category IDs based on match with start of category name.
#
my $value = shift;
my ($query, $sth, $id, $name, $output);
$output = $CATEGORY_LIST{$value};
if (!$output) {
if (! $CATDB) {
$CATDB = new Links::DBSQL $LINKS{admin_root_path} . "/defs/Category.def";
}
$query = qq!
SELECT ID
FROM Category
WHERE Name LIKE '$value%'
!;
$sth = $CATDB->prepare ($query);
$sth->execute() or die "Can't Execute: $DBI::errstr";
while (($id) = $sth->fetchrow_array) {
($output .= "$id,");
}
$sth->finish;
$CATEGORY_LIST{$value} = $output;
}
return $output;
}
modify, so now we have to put the CategoryID variable into the
category page so we can tell links which category the search
has come from.
Template category.html
--------------------
In your category template file you will have the 'FORM' details
for the search.
We now need to add the radio buttons to allow our visitor to
search through 'All categories' or 'Only this category'.
Look for the HTML that displays the SEARCH button ...
<input type=submit value="Search">
Directly after this add the following ...
<INPUT NAME="catid" TYPE="radio" VALUE="" CHECKED>All categories
<INPUT NAME="catid" TYPE="radio" VALUE="<%category_id%>">Only this category
This will put two 'radio' type buttons on your category page next
to the search box, with the options for 'All categories'
highlighted by default.
Re-build
--------------------
Upload the modified files and make sure all the file permissions are
correct, then re-build your directory to effect the changes.
Go to one of your category pages and make sure the search is
working correctly.
If you have any problems use your backup files to put your site back
into working order then double-check the changes you made to
trouble-shoot the problem or problems.
Thats it - hope you enjoy
All the best
Shaun