followup : auto_truncation 3287252c0
[koha.git] / opac / opac-topissues.pl
index 1e8b195..45130c7 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-# $Id$
 
 # Copyright 2000-2002 Katipo Communications
 #
@@ -20,6 +19,8 @@
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
+use warnings;
+
 use CGI;
 use C4::Auth;
 use C4::Context;
@@ -53,31 +54,50 @@ my ($template, $borrowernumber, $cookie)
 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 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;
@@ -85,22 +105,26 @@ while (my $line= $sth->fetchrow_hashref) {
     push @results, $line;
 }
 
+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,
                 );
 
-# load the branches
-my $branches = GetBranches();
+# load the branches            ## again??
+$branches = GetBranches();
 my @branch_loop;
-for my $branch_hash ( keys %$branches ) {
+for my $branch_hash (sort keys %$branches ) {
     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
     push @branch_loop,
       {
-        value      => "branch: $branch_hash",
+        value      => "$branch_hash",
         branchname => $branches->{$branch_hash}->{'branchname'},
         selected => $selected
       };
@@ -108,17 +132,20 @@ for my $branch_hash ( keys %$branches ) {
 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
 
 #doctype
-my $itemtypes = GetItemTypes;
+$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;
 }
 
 $template->param(
                  itemtypeloop =>\@itemtypeloop,
+                 dateformat    => C4::Context->preference("dateformat"),
                 );
 output_html_with_http_headers $input, $cookie, $template->output;