Gossamer Forum
Home : Products : Gossamer Links : Discussions :

need to be able to plugin a callback routine

Quote Reply
need to be able to plugin a callback routine
Hi there,

Could we please have a links callback place we can plugin to in Search.pm routines?


something like this perhaps

INSIDE OF Search::query

change:
$args->{callback} = \&_search_subcat if ($args->{catid});
to:
$args->{callback} = \&search_subcat;

THEN CREATE
sub search_subcat {
# -------------------------------------------------------------------
# First argument is the query/table object, second argument is the current
# result set (note: can be quite large). Must return a new result set.
#
my ($query, $results) = @_;
$PLG->dispatch('search_callback', sub { return _search_subcat(@_) },$query, $results);

}


AND IN
sub _search_subcat {
my ($query, $results) = @_;
$IN->param('catid') or return $results; #I guess this would be needed?


HISTORY:
I'ven been struggling with this problem since 2003 and have only found hacks that don't solve the problem.

In short, I want to be able to create my own links callback routine when using search.cgi.
We're only able to plugin before or after Search::query.

my first hack was to create a subroutine in search.pm then replace the GT callback with mine like this.
# $args->{callback} = \&_search_subcat if ($args->{catid});
$args->{callback} = \&_search_products;
This basically searches my products database for the keywords and returns the corresponding Links.
Often, my products will have keywords not found in the Link itself and I want the Link returned in the results where it belongs.


at the bottom of &_search_products are these lines so I continue what GT does.
# do the subcategory callback if necessary;
$results = _search_subcat($query, $results) if $IN->param('catid');
return $results;

So that worked really well until an upgrade takes place and I have to re-do what the upgrade overwrote. So that was a pain, so I did a plugin which ran a modified version of Search.pm and that worked really well until Search.pm changed and the templates changed - so I'm back to square 1 on many sites.

So now, I'm plugging in after Search::query and working with the results that GT provides and adding my results to the found results. The big problem comes in with doing this because it's very very difficult to merge the two results when taking into consideration any of the following parameters.
paging,build_search_gb,build_sort_order_search,next and all the other stuff without redoing what's already been done. In situations where there are no results, I endup having to rewrite all the GT code when I get additional results.

thanks for your consideration.

peace.
Quote Reply
Re: [klangan] need to be able to plugin a callback routine In reply to
I added two hooks, but they're a little different from the regular plugin hooks. Instead, the hooks will set the callback. The code is now:
Code:
$args->{callback} = $PLG->dispatch('search_set_cat_callback', sub { return \&_cat_search_subcat if @_ }, $args->{catid});
...
$args->{callback} = $PLG->dispatch('search_set_link_callback', sub { return \&_search_subcat if @_ }, $args->{catid});
Your hooks should return your callback function and call $PLG->action(STOP) to prevent the default code from running (if you want it to run, then call it yourself from your callback). Thus, only one thing can 'hook' into it.

Here's an example plugin (extraneous code removed):

Code:
sub return_link_callback {
# -----------------------------------------------------------------------------
# This subroutine will be called whenever the hook 'search_set_link_callback' is run. You
# should call $PLG->action(STOP) if you don't want the regular
# 'search_set_link_callback' code to run, otherwise the code will continue as normal.
#
my (@args) = @_;
$PLG->action(STOP);
return \&link_callback;
}

sub link_callback {
my ($query, $results) = @_;

# Do stuff with $results here

return $results;
}
This will be available in the next release.

Adrian