# Suite 330, Boston, MA 02111-1307 USA
use strict;
+use warnings;
+
use CGI;
use C4::Auth;
use C4::Context;
my $dbh = C4::Context->dbh;
# Displaying results
my $limit = $input->param('limit') || 10;
-my $branch = $input->param('branch');
-my $itemtype = $input->param('itemtype');
+my $branch = $input->param('branch') || '';
+my $itemtype = $input->param('itemtype') || '';
my $timeLimit = $input->param('timeLimit') || 3;
-my $whereclause;
-$whereclause .= 'items.homebranch='.$dbh->quote($branch)." AND " if ($branch);
-$whereclause .= 'biblioitems.itemtype='.$dbh->quote($itemtype)." AND " if $itemtype;
-$whereclause .= ' TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30).' AND ' if $timeLimit < 999;
+my $whereclause='';
+$whereclause .= ' AND items.homebranch='.$dbh->quote($branch) if ($branch);
+$whereclause .= ' AND TO_DAYS(NOW()) - TO_DAYS(biblio.datecreated) <= '.($timeLimit*30) if $timeLimit < 999;
$whereclause =~ s/ AND $//;
-$whereclause = " WHERE ".$whereclause if $whereclause;
-
-my $query = "SELECT datecreated, biblio.biblionumber, title,
- author, sum( items.issues ) AS tot, biblioitems.itemtype,
- biblioitems.publishercode,biblioitems.publicationyear,
- itemtypes.description
- FROM biblio
- LEFT JOIN items USING (biblionumber)
- LEFT JOIN biblioitems USING (biblionumber)
- LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
- $whereclause
- GROUP BY biblio.biblionumber
- HAVING tot >0
- ORDER BY tot DESC
- LIMIT $limit
- ";
-
+my $query;
+if(C4::Context->preference('AdvancedSearchTypes') eq 'ccode'){
+ $whereclause .= ' AND authorised_values.authorised_value='.$dbh->quote($itemtype) if $itemtype;
+ $query = "SELECT datecreated, biblio.biblionumber, title,
+ author, sum( items.issues ) AS tot, biblioitems.itemtype,
+ biblioitems.publishercode,biblioitems.publicationyear,
+ authorised_values.lib as description
+ FROM biblio
+ LEFT JOIN items USING (biblionumber)
+ LEFT JOIN biblioitems USING (biblionumber)
+ LEFT JOIN authorised_values ON items.ccode = authorised_values.authorised_value
+ WHERE 1
+ $whereclause
+ AND authorised_values.category = 'ccode'
+ GROUP BY biblio.biblionumber
+ HAVING tot >0
+ ORDER BY tot DESC
+ LIMIT $limit
+ ";
+}else{
+ $whereclause .= ' AND biblioitems.itemtype='.$dbh->quote($itemtype) if $itemtype;
+ $query = "SELECT datecreated, biblio.biblionumber, title,
+ author, sum( items.issues ) AS tot, biblioitems.itemtype,
+ biblioitems.publishercode,biblioitems.publicationyear,
+ itemtypes.description
+ FROM biblio
+ LEFT JOIN items USING (biblionumber)
+ LEFT JOIN biblioitems USING (biblionumber)
+ LEFT JOIN itemtypes ON itemtypes.itemtype = biblioitems.itemtype
+ WHERE 1
+ $whereclause
+ GROUP BY biblio.biblionumber
+ HAVING tot >0
+ ORDER BY tot DESC
+ LIMIT $limit
+ ";
+}
my $sth = $dbh->prepare($query);
$sth->execute();
my @results;
push @results, $line;
}
-if($timeLimit eq 999){ $timeLimit = 0 };
+my $timeLimitFinite = $timeLimit;
+if($timeLimit eq 999){ $timeLimitFinite = 0 };
$template->param(do_it => 1,
limit => $limit,
- branch => $branches->{$branch}->{branchname},
- itemtype => $itemtypes->{$itemtype}->{description},
+ branch => $branches->{$branch}->{branchname} || 'all locations',
+ itemtype => $itemtypes->{$itemtype}->{description} || 'item types',
timeLimit => $timeLimit,
timeLimitFinite => $timeLimit,
results_loop => \@results,
#doctype
$itemtypes = GetItemTypes;
my @itemtypeloop;
-foreach my $thisitemtype (keys %$itemtypes) {
+foreach my $thisitemtype (sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
+ my $selected = 1 if $thisitemtype eq $itemtype;
my %row =(value => $thisitemtype,
description => $itemtypes->{$thisitemtype}->{'description'},
- );
+ selected => $selected,
+ );
push @itemtypeloop, \%row;
}